marching-cubes-opengl/MCRenderer.cpp
karl 7227fef749 It works!
Lots of minor fixes
2021-03-19 23:54:24 +01:00

64 lines
1.8 KiB
C++

#include "MCRenderer.h"
#include "Framebuffer3D.h"
#include "VertexBuffer.h"
#include <glm/ext/matrix_transform.hpp>
MCRenderer::MCRenderer()
: render_shader(Shader("Shader/mc.vs", "Shader/mc.fs", "Shader/mc.gs")),
noise_shader(Shader("Shader/noise.vs", "Shader/noise.fs")), noise(Framebuffer3D(64, 64, 64)),
camera(Camera(90, 1920, 1080, 0.1, 1000.0)) {
float data[6][2] = {{-1.0f, -1.0f}, {-1.0, 1.0}, {1.0, -1.0},
{1.0f, 1.0f}, {-1.0, 1.0}, {1.0, -1.0}};
// Move the camera a bit
camera.translate(glm::vec3(32.0, 32.0, 32.0));
vertex_rectangle.set_data(sizeof(data), data, GL_STATIC_DRAW);
}
void MCRenderer::render(float delta) {
// Create the noise
glViewport(0, 0, 64, 64);
noise_shader.use();
noise.bind_and_clear();
noise_shader.setFloat("height", 0);
for (int i = 0; i < 64; i++) {
// Create one layer
noise_shader.setFloat("layer", i * (1.0f / 63.0f));
noise.bind_layer(i);
// Each layer is made up of a rectangle of vertices
vertex_rectangle.draw();
}
// Actual rendering
glViewport(0, 0, 1920, 1080);
render_shader.use();
// Bind the screen framebuffer
glBindFramebuffer(GL_FRAMEBUFFER, 0);
// glDrawBuffer(GL_BACK);
glClearColor(0.6f, 0.9f, 0.9f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// Set the camera position
camera.rotate(delta, glm::vec3(0.0, 1.0, 0.0));
// Bind the camera to the rendering shader
render_shader.setMat4("proj", camera.get_projection());
render_shader.setMat4("view", camera.get_view());
// Bind the noise texture to the rendering shader
noise.bind_to(0);
// Draw all layers as polygons
mc_points.bind();
glDrawArrays(GL_POINTS, 0, 64 * 64 * 64);
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
time_passed += delta;
}