generated from karl/cpp-template
Lots of cleanup
This commit is contained in:
parent
7227fef749
commit
37d2e4aabc
@ -3,31 +3,33 @@
|
||||
#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)) {
|
||||
MCRenderer::MCRenderer(int size_x, int size_y, int size_z)
|
||||
: size_x(size_x), size_y(size_y), size_z(size_z),
|
||||
render_shader(Shader("Shader/mc.vs", "Shader/mc.fs", "Shader/mc.gs")),
|
||||
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},
|
||||
{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), 2, data, GL_STATIC_DRAW);
|
||||
|
||||
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) {
|
||||
// Create the noise
|
||||
glViewport(0, 0, 64, 64);
|
||||
glViewport(0, 0, size_x, size_y);
|
||||
|
||||
noise_shader.use();
|
||||
noise.bind_and_clear();
|
||||
|
||||
noise_shader.setFloat("height", 0);
|
||||
|
||||
for (int i = 0; i < 64; i++) {
|
||||
for (int i = 0; i < size_z; i++) {
|
||||
// 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);
|
||||
|
||||
// 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("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
|
||||
noise.bind_to(0);
|
||||
|
||||
// Draw all layers as polygons
|
||||
mc_points.bind();
|
||||
glDrawArrays(GL_POINTS, 0, 64 * 64 * 64);
|
||||
glDrawArrays(GL_POINTS, 0, size_x * size_y * size_z);
|
||||
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
|
||||
|
||||
time_passed += delta;
|
||||
}
|
12
MCRenderer.h
12
MCRenderer.h
@ -7,22 +7,20 @@
|
||||
|
||||
class MCRenderer {
|
||||
public:
|
||||
MCRenderer();
|
||||
MCRenderer(int size_x, int size_y, int size_z);
|
||||
|
||||
void render(float delta);
|
||||
|
||||
private:
|
||||
int size_x;
|
||||
int size_y;
|
||||
int size_z;
|
||||
|
||||
Shader render_shader;
|
||||
Shader noise_shader;
|
||||
|
||||
Framebuffer3D noise;
|
||||
VertexBuffer vertex_rectangle;
|
||||
|
||||
VertexBuffer mc_points;
|
||||
|
||||
Camera camera;
|
||||
|
||||
float time_passed = 0.0;
|
||||
|
||||
float *rectangle_data;
|
||||
};
|
||||
|
34
Shader/mc.vs
34
Shader/mc.vs
@ -2,29 +2,37 @@
|
||||
|
||||
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 int varIndex;
|
||||
|
||||
const float step = 1.0 / 63.0;
|
||||
|
||||
void main(void) {
|
||||
int id = gl_VertexID;
|
||||
int x = id & 0x3F;
|
||||
int y = (id >> 6) & 0x3F;
|
||||
int z = (id >> 12) & 0x3F;
|
||||
|
||||
// Could be optimized by using bit-wise '>>' and '&', but this is more generic
|
||||
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);
|
||||
gl_Position = vec4(xyz, 1.0);
|
||||
varTexture = xyz * step;
|
||||
|
||||
int b1 = int(texture(densities, varTexture).r < 0.5f);
|
||||
int b2 = int(texture(densities, varTexture + vec3(step, 0.0, 0.0)).r < 0.5f);
|
||||
int b3 = int(texture(densities, varTexture + vec3(step, 0.0, step)).r < 0.5f);
|
||||
int b4 = int(texture(densities, varTexture + vec3(0.0, 0.0, step)).r < 0.5f);
|
||||
int b5 = int(texture(densities, varTexture + vec3(0.0, step, 0.0)).r < 0.5f);
|
||||
int b6 = int(texture(densities, varTexture + vec3(step, step, 0.0)).r < 0.5f);
|
||||
int b7 = int(texture(densities, varTexture + vec3(step, step, step)).r < 0.5f);
|
||||
int b8 = int(texture(densities, varTexture + vec3(0.0, step, step)).r < 0.5f);
|
||||
int b1 = int(texture(densities, varTexture).r < threshold);
|
||||
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 < threshold);
|
||||
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 < threshold);
|
||||
int b6 = int(texture(densities, varTexture + vec3(step, step, 0.0)).r < threshold);
|
||||
int b7 = int(texture(densities, varTexture + vec3(step, step, step)).r < threshold);
|
||||
int b8 = int(texture(densities, varTexture + vec3(0.0, step, step)).r < threshold);
|
||||
|
||||
varIndex = (b1 << 7) | (b2 << 6) | (b3 << 5) | (b4 << 4) |
|
||||
(b5 << 3) | (b6 << 2) | (b7 << 1) | b8;
|
||||
}
|
||||
|
@ -1,13 +1,14 @@
|
||||
#version 430
|
||||
#version 430
|
||||
|
||||
in vec3 varPosition;
|
||||
in vec3 varPosition;
|
||||
|
||||
out float noise;
|
||||
out float noise;
|
||||
|
||||
void main(void) {
|
||||
float sinX = sin(varPosition.x * 5.8905);
|
||||
float cosY = cos(varPosition.y * 5.8905);
|
||||
float cosZ = cos(varPosition.z * 5.8905);
|
||||
|
||||
noise = (sinX * sinX + cosY * cosY + cosZ * cosZ) * (1.0f / 3.0f);
|
||||
}
|
||||
void main(void) {
|
||||
// Just to random stuff to the numbers until it looks nice
|
||||
float f1 = sin((varPosition.x + varPosition.z) * 10.0);
|
||||
float f2 = cos((varPosition.y + varPosition.x) * 7.0);
|
||||
float f3 = cos((varPosition.z + varPosition.y) * 8.0);
|
||||
|
||||
noise = (f1 * f1 + f2 * f2 + f3 * f3) * (1.0 / 3.0);
|
||||
}
|
||||
|
@ -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 height;
|
||||
uniform float layer;
|
||||
uniform float height;
|
||||
|
||||
out vec3 varPosition;
|
||||
out vec3 varPosition;
|
||||
|
||||
void main(void) {
|
||||
gl_Position = vec4(position, 0.0, 1.0);
|
||||
varPosition = vec3(position.x, position.y + height, layer);
|
||||
}
|
||||
void main(void) {
|
||||
gl_Position = vec4(position, 0.0, 1.0);
|
||||
varPosition = vec3(position.x, position.y + height, layer);
|
||||
}
|
||||
|
@ -5,27 +5,33 @@ VertexBuffer::VertexBuffer() {
|
||||
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;
|
||||
|
||||
glBindVertexArray(vertex_array);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer);
|
||||
bind_array();
|
||||
bind_buffer();
|
||||
|
||||
// Create the data on the GPU
|
||||
glBufferData(GL_ARRAY_BUFFER, size, data, flag);
|
||||
|
||||
// TODO: Generalize
|
||||
// This tells that the data consists of 2xfloat packets
|
||||
// Specify the dimension of the vectors in the data, so that the GPU knows how much to read from
|
||||
// it at a time. (Currently only one type of vector is supported)
|
||||
glEnableVertexAttribArray(0);
|
||||
glVertexAttribPointer(0, 2, GL_FLOAT, false, 2 * sizeof(float), 0);
|
||||
glVertexAttribPointer(0, dimension, GL_FLOAT, false, dimension * sizeof(float), 0);
|
||||
|
||||
glBindVertexArray(0);
|
||||
}
|
||||
|
||||
void VertexBuffer::bind() {
|
||||
void VertexBuffer::bind_buffer() {
|
||||
glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer);
|
||||
}
|
||||
|
||||
void VertexBuffer::bind_array() {
|
||||
glBindVertexArray(vertex_array);
|
||||
}
|
||||
|
||||
void VertexBuffer::draw() {
|
||||
bind();
|
||||
bind_array();
|
||||
glDrawArrays(GL_TRIANGLES, 0, size);
|
||||
}
|
@ -10,9 +10,10 @@ class VertexBuffer {
|
||||
public:
|
||||
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();
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user