marching-cubes-opengl/MCRenderer.cpp

62 lines
1.9 KiB
C++

#include "MCRenderer.h"
#include "Framebuffer3D.h"
#include "VertexBuffer.h"
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, 100.0)) {
float rectangle_data[12] = {-1.0f, -1.0f, //
-1.0, 1.0, //
1.0, -1.0, //
1.0f, 1.0f, //
-1.0, 1.0, //
1.0, -1.0};
vertex_rectangle.set_data(6, rectangle_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.translate(glm::vec3(-32.0, -32.0, -32.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);
}