Lots of cleanup

This commit is contained in:
karl 2021-03-20 00:57:18 +01:00
parent 7227fef749
commit 37d2e4aabc
8 changed files with 85 additions and 64 deletions

View File

@ -3,31 +3,33 @@
#include "VertexBuffer.h" #include "VertexBuffer.h"
#include <glm/ext/matrix_transform.hpp> #include <glm/ext/matrix_transform.hpp>
MCRenderer::MCRenderer() MCRenderer::MCRenderer(int size_x, int size_y, int size_z)
: render_shader(Shader("Shader/mc.vs", "Shader/mc.fs", "Shader/mc.gs")), : size_x(size_x), size_y(size_y), size_z(size_z),
noise_shader(Shader("Shader/noise.vs", "Shader/noise.fs")), noise(Framebuffer3D(64, 64, 64)), render_shader(Shader("Shader/mc.vs", "Shader/mc.fs", "Shader/mc.gs")),
camera(Camera(90, 1920, 1080, 0.1, 1000.0)) { noise_shader(Shader("Shader/noise.vs", "Shader/noise.fs")),
noise(Framebuffer3D(size_x, size_y, size_z)), camera(Camera(90, 1920, 1080, 0.1, 1000.0)) {
// Each noise layer needs vertices, so we define two triangles which span a rectangle
float data[6][2] = {{-1.0f, -1.0f}, {-1.0, 1.0}, {1.0, -1.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}}; {1.0f, 1.0f}, {-1.0, 1.0}, {1.0, -1.0}};
// Move the camera a bit vertex_rectangle.set_data(sizeof(data), 2, data, GL_STATIC_DRAW);
camera.translate(glm::vec3(32.0, 32.0, 32.0));
vertex_rectangle.set_data(sizeof(data), data, GL_STATIC_DRAW); // Move the camera to the center
camera.translate(glm::vec3(size_x / 2.0, size_z / 2.0, size_y / 2.0));
} }
void MCRenderer::render(float delta) { void MCRenderer::render(float delta) {
// Create the noise // Create the noise
glViewport(0, 0, 64, 64); glViewport(0, 0, size_x, size_y);
noise_shader.use(); noise_shader.use();
noise.bind_and_clear(); noise.bind_and_clear();
noise_shader.setFloat("height", 0); noise_shader.setFloat("height", 0);
for (int i = 0; i < 64; i++) { for (int i = 0; i < size_z; i++) {
// Create one layer // Create one layer
noise_shader.setFloat("layer", i * (1.0f / 63.0f)); noise_shader.setFloat("layer", i * (1.0f / (static_cast<float>(size_z - 1))));
noise.bind_layer(i); noise.bind_layer(i);
// Each layer is made up of a rectangle of vertices // Each layer is made up of a rectangle of vertices
@ -52,13 +54,18 @@ void MCRenderer::render(float delta) {
render_shader.setMat4("proj", camera.get_projection()); render_shader.setMat4("proj", camera.get_projection());
render_shader.setMat4("view", camera.get_view()); render_shader.setMat4("view", camera.get_view());
// Set other variables
render_shader.setFloat("step", 1.0 / static_cast<float>(size_z - 1));
render_shader.setFloat("threshold", 0.4);
render_shader.setInt("size_x", size_x);
render_shader.setInt("size_y", size_y);
render_shader.setInt("size_z", size_z);
// Bind the noise texture to the rendering shader // Bind the noise texture to the rendering shader
noise.bind_to(0); noise.bind_to(0);
// Draw all layers as polygons // Draw all layers as polygons
mc_points.bind(); glDrawArrays(GL_POINTS, 0, size_x * size_y * size_z);
glDrawArrays(GL_POINTS, 0, 64 * 64 * 64);
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
time_passed += delta;
} }

View File

@ -7,22 +7,20 @@
class MCRenderer { class MCRenderer {
public: public:
MCRenderer(); MCRenderer(int size_x, int size_y, int size_z);
void render(float delta); void render(float delta);
private: private:
int size_x;
int size_y;
int size_z;
Shader render_shader; Shader render_shader;
Shader noise_shader; Shader noise_shader;
Framebuffer3D noise; Framebuffer3D noise;
VertexBuffer vertex_rectangle; VertexBuffer vertex_rectangle;
VertexBuffer mc_points;
Camera camera; Camera camera;
float time_passed = 0.0;
float *rectangle_data;
}; };

View File

@ -2,29 +2,37 @@
layout (binding = 0) uniform sampler3D densities; layout (binding = 0) uniform sampler3D densities;
uniform float step;
uniform float threshold;
uniform int size_x;
uniform int size_y;
uniform int size_z;
out vec3 varTexture; out vec3 varTexture;
out int varIndex; out int varIndex;
const float step = 1.0 / 63.0;
void main(void) { void main(void) {
int id = gl_VertexID; int id = gl_VertexID;
int x = id & 0x3F;
int y = (id >> 6) & 0x3F; // Could be optimized by using bit-wise '>>' and '&', but this is more generic
int z = (id >> 12) & 0x3F; int x = id % size_x;
int y = (id / size_x) % size_y;
int z = (id / size_x / size_y) % size_z;
vec3 xyz = vec3(x, y, z); vec3 xyz = vec3(x, y, z);
gl_Position = vec4(xyz, 1.0); gl_Position = vec4(xyz, 1.0);
varTexture = xyz * step; varTexture = xyz * step;
int b1 = int(texture(densities, varTexture).r < 0.5f); int b1 = int(texture(densities, varTexture).r < threshold);
int b2 = int(texture(densities, varTexture + vec3(step, 0.0, 0.0)).r < 0.5f); int b2 = int(texture(densities, varTexture + vec3(step, 0.0, 0.0)).r < threshold);
int b3 = int(texture(densities, varTexture + vec3(step, 0.0, step)).r < 0.5f); int b3 = int(texture(densities, varTexture + vec3(step, 0.0, step)).r < threshold);
int b4 = int(texture(densities, varTexture + vec3(0.0, 0.0, step)).r < 0.5f); int b4 = int(texture(densities, varTexture + vec3(0.0, 0.0, step)).r < threshold);
int b5 = int(texture(densities, varTexture + vec3(0.0, step, 0.0)).r < 0.5f); int b5 = int(texture(densities, varTexture + vec3(0.0, step, 0.0)).r < threshold);
int b6 = int(texture(densities, varTexture + vec3(step, step, 0.0)).r < 0.5f); int b6 = int(texture(densities, varTexture + vec3(step, step, 0.0)).r < threshold);
int b7 = int(texture(densities, varTexture + vec3(step, step, step)).r < 0.5f); int b7 = int(texture(densities, varTexture + vec3(step, step, step)).r < threshold);
int b8 = int(texture(densities, varTexture + vec3(0.0, step, step)).r < 0.5f); int b8 = int(texture(densities, varTexture + vec3(0.0, step, step)).r < threshold);
varIndex = (b1 << 7) | (b2 << 6) | (b3 << 5) | (b4 << 4) | varIndex = (b1 << 7) | (b2 << 6) | (b3 << 5) | (b4 << 4) |
(b5 << 3) | (b6 << 2) | (b7 << 1) | b8; (b5 << 3) | (b6 << 2) | (b7 << 1) | b8;
} }

View File

@ -1,13 +1,14 @@
#version 430 #version 430
in vec3 varPosition; in vec3 varPosition;
out float noise; out float noise;
void main(void) { void main(void) {
float sinX = sin(varPosition.x * 5.8905); // Just to random stuff to the numbers until it looks nice
float cosY = cos(varPosition.y * 5.8905); float f1 = sin((varPosition.x + varPosition.z) * 10.0);
float cosZ = cos(varPosition.z * 5.8905); float f2 = cos((varPosition.y + varPosition.x) * 7.0);
float f3 = cos((varPosition.z + varPosition.y) * 8.0);
noise = (sinX * sinX + cosY * cosY + cosZ * cosZ) * (1.0f / 3.0f);
} noise = (f1 * f1 + f2 * f2 + f3 * f3) * (1.0 / 3.0);
}

View File

@ -1,13 +1,13 @@
#version 430 #version 430
layout (location = 0) in vec2 position; layout (location = 0) in vec2 position;
uniform float layer; uniform float layer;
uniform float height; uniform float height;
out vec3 varPosition; out vec3 varPosition;
void main(void) { void main(void) {
gl_Position = vec4(position, 0.0, 1.0); gl_Position = vec4(position, 0.0, 1.0);
varPosition = vec3(position.x, position.y + height, layer); varPosition = vec3(position.x, position.y + height, layer);
} }

View File

@ -5,27 +5,33 @@ VertexBuffer::VertexBuffer() {
glGenBuffers(1, &vertex_buffer); glGenBuffers(1, &vertex_buffer);
} }
void VertexBuffer::set_data(int size, const void *data, int flag) { void VertexBuffer::set_data(int size, int dimension, const void *data, int flag) {
// We can't just do size = sizeof(data) here because of array to pointer decay!
this->size = size; this->size = size;
glBindVertexArray(vertex_array); bind_array();
glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer); bind_buffer();
// Create the data on the GPU
glBufferData(GL_ARRAY_BUFFER, size, data, flag); glBufferData(GL_ARRAY_BUFFER, size, data, flag);
// TODO: Generalize // Specify the dimension of the vectors in the data, so that the GPU knows how much to read from
// This tells that the data consists of 2xfloat packets // it at a time. (Currently only one type of vector is supported)
glEnableVertexAttribArray(0); glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 2, GL_FLOAT, false, 2 * sizeof(float), 0); glVertexAttribPointer(0, dimension, GL_FLOAT, false, dimension * sizeof(float), 0);
glBindVertexArray(0); glBindVertexArray(0);
} }
void VertexBuffer::bind() { void VertexBuffer::bind_buffer() {
glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer);
}
void VertexBuffer::bind_array() {
glBindVertexArray(vertex_array); glBindVertexArray(vertex_array);
} }
void VertexBuffer::draw() { void VertexBuffer::draw() {
bind(); bind_array();
glDrawArrays(GL_TRIANGLES, 0, size); glDrawArrays(GL_TRIANGLES, 0, size);
} }

View File

@ -10,9 +10,10 @@ class VertexBuffer {
public: public:
VertexBuffer(); VertexBuffer();
void set_data(int size, const void *data, int flag); void set_data(int size, int dimension, const void *data, int flag);
void bind(); void bind_array();
void bind_buffer();
void draw(); void draw();

View File

@ -49,7 +49,7 @@ int main() {
glEnable(GL_DEPTH_TEST); glEnable(GL_DEPTH_TEST);
// Setup the Marching Cubes renderer // Setup the Marching Cubes renderer
MCRenderer renderer = MCRenderer(); MCRenderer renderer = MCRenderer(128, 128, 128);
// render loop // render loop
// ----------- // -----------