Basic structure there, but not functional

This commit is contained in:
karl 2021-03-19 18:27:00 +01:00
parent 9ab7f3eed0
commit 835520edb8
14 changed files with 535 additions and 90 deletions

27
Camera.h Normal file
View File

@ -0,0 +1,27 @@
#pragma once
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include "Spatial.h"
class Camera : public Spatial {
public:
/// Create a camera with a field of view (in degrees), width and height (in any unit) and near
/// and far distances
Camera(float fov, float width, float height, float near, float far)
: projection(glm::perspective(glm::radians(fov), width / height, near, far)) {
}
glm::mat4 get_projection() {
return projection;
}
glm::mat4 get_view() {
return view;
}
private:
glm::mat4 projection;
glm::mat4 view;
};

40
Framebuffer3D.cpp Normal file
View File

@ -0,0 +1,40 @@
#include "Framebuffer3D.h"
Framebuffer3D::Framebuffer3D(int width, int height, int depth) {
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_3D, texture);
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER);
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_BORDER);
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER);
glTexImage3D(GL_TEXTURE_3D, 0, GL_R32F, width, height, depth, 0, GL_RED, GL_FLOAT, nullptr);
glGenFramebuffers(1, &buffer);
glBindFramebuffer(GL_FRAMEBUFFER, buffer);
GLenum c = GL_COLOR_ATTACHMENT0;
glFramebufferTexture3D(GL_FRAMEBUFFER, c, GL_TEXTURE_3D, texture, 0, 0);
glDrawBuffers(1, &c);
}
Framebuffer3D::~Framebuffer3D() {
glDeleteFramebuffers(1, &buffer);
glDeleteTextures(1, &texture);
}
void Framebuffer3D::bind_and_clear() {
glBindFramebuffer(GL_FRAMEBUFFER, buffer);
glClear(GL_COLOR_BUFFER_BIT);
}
void Framebuffer3D::bind_to(int unit) const {
glActiveTexture(GL_TEXTURE0 + unit);
glBindTexture(GL_TEXTURE_3D, texture);
}
void Framebuffer3D::bind_layer(int layer) {
glBindFramebuffer(GL_FRAMEBUFFER, buffer);
glFramebufferTexture3D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_3D, texture, 0, layer);
}

21
Framebuffer3D.h Normal file
View File

@ -0,0 +1,21 @@
#pragma once
// Must be the first include
#include <glad/glad.h>
// Other includes
#include <GLFW/glfw3.h>
class Framebuffer3D {
public:
Framebuffer3D(int width, int height, int depth);
~Framebuffer3D();
void bind_and_clear();
void bind_to(int unit) const;
void bind_layer(int layer);
private:
GLuint texture;
GLuint buffer;
};

49
MCRenderer.cpp Normal file
View File

@ -0,0 +1,49 @@
#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[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}};
vertex_rectangle.set_data(sizeof(rectangle_data), rectangle_data, 0);
}
void MCRenderer::render(float delta) {
// Create the noise
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);
glClearColor(0.6f, 0.9f, 0.9f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
render_shader.use();
// 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
glDrawArrays(GL_POINTS, 0, 64 * 64 * 64);
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
}

22
MCRenderer.h Normal file
View File

@ -0,0 +1,22 @@
#pragma once
#include "Camera.h"
#include "Framebuffer3D.h"
#include "Shader.h"
#include "VertexBuffer.h"
class MCRenderer {
public:
MCRenderer();
void render(float delta);
private:
Shader render_shader;
Shader noise_shader;
Framebuffer3D noise;
VertexBuffer vertex_rectangle;
Camera camera;
};

View File

@ -1,10 +1,12 @@
#version 330 core
out vec4 FragColor;
#version 430
in vec3 fColor;
layout (binding = 0) uniform sampler3D densities;
void main()
{
FragColor = vec4(fColor, 1.0);
in vec3 varTextureG;
out vec4 color;
void main(void) {
float f = texture(densities, varTextureG).r;
color = vec4(f, f, f, 1.0);
}

View File

@ -1,31 +1,196 @@
#version 330 core
#version 430
layout (points) in;
layout (triangle_strip, max_vertices = 5) out;
layout (triangle_strip, max_vertices = 12) out;
in VS_OUT {
vec3 color;
} gs_in[];
uniform mat4 proj;
uniform mat4 view;
out vec3 fColor;
in vec3 varTexture[];
in int varIndex[];
out vec3 varTextureG;
void build_house(vec4 position)
{
fColor = gs_in[0].color; // gs_in[0] since there's only one input vertex
gl_Position = position + vec4(-0.2, -0.2, 0.0, 0.0); // 1:bottom-left
EmitVertex();
gl_Position = position + vec4( 0.2, -0.2, 0.0, 0.0); // 2:bottom-right
EmitVertex();
gl_Position = position + vec4(-0.2, 0.2, 0.0, 0.0); // 3:top-left
EmitVertex();
gl_Position = position + vec4( 0.2, 0.2, 0.0, 0.0); // 4:top-right
EmitVertex();
gl_Position = position + vec4( 0.0, 0.4, 0.0, 0.0); // 5:top
fColor = vec3(1.0, 1.0, 1.0);
EmitVertex();
EndPrimitive();
vec3 vectors[13] = {
vec3(0.0, 0.0, 0.0), v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, 0, 0, 0, 0, 5, 6,
7, 3, 1, 2, 0, 0, 0, 0, 0, 0, 5, 6, 7, 0, 0, 0, 0, 0, 0,
0, 0, 0, 3, 5, 2, 5, 2, 4, 0, 0, 0, 0, 0, 0, 4, 1, 5, 0,
0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
};
void main(void) {
vec4 base = gl_in[0].gl_Position;
int index = varIndex[0] * 12;
for(int i = 0; i < 12; i += 3) {
for(int k = 0; k < 3; k++) {
gl_Position = proj * view * (base + vec4(vectors[table[index + i + k]], 0.0));
varTextureG = varTexture[0];
EmitVertex();
}
EndPrimitive();
}
}
void main() {
build_house(gl_in[0].gl_Position);
}

View File

@ -1,14 +1,30 @@
#version 330 core
layout (location = 0) in vec2 aPos;
layout (location = 1) in vec3 aColor;
#version 430
out VS_OUT {
vec3 color;
} vs_out;
layout (binding = 0) uniform sampler3D densities;
void main()
{
vs_out.color = aColor;
gl_Position = vec4(aPos.x, aPos.y, 0.0, 1.0);
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;
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);
varIndex = (b1 << 7) | (b2 << 6) | (b3 << 5) | (b4 << 4) |
(b5 << 3) | (b6 << 2) | (b7 << 1) | b8;
}

13
Shader/noise.fs Normal file
View File

@ -0,0 +1,13 @@
#version 430
in vec3 varPosition;
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);
}

13
Shader/noise.vs Normal file
View File

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

72
Spatial.h Normal file
View File

@ -0,0 +1,72 @@
#pragma once
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
#include "glm/gtx/string_cast.hpp"
class Spatial {
public:
Spatial() = default;
void translate(glm::vec3 offset) {
matrix = glm::translate(matrix, offset);
}
glm::vec3 get_translation() {
return glm::vec3(matrix[3]);
}
void uniform_scale(float factor) {
scale(glm::vec3(factor, factor, factor));
}
void scale(glm::vec3 factors) {
matrix = glm::scale(matrix, factors);
}
void rotate(float degrees, glm::vec3 axis) {
matrix = glm::rotate(matrix, glm::radians(degrees), axis);
}
void set_origin(glm::vec3 position) {
origin = position;
}
void add_to_origin(glm::vec3 addition) {
origin += addition;
}
void set_rotation_from_quat(glm::quat quaternion) {
// Remember translation
glm::vec4 save = matrix[3];
matrix = glm::mat4_cast(quaternion);
matrix[3] = save;
}
glm::vec3 get_origin() const {
return origin;
}
glm::mat4 get_matrix() {
return matrix;
}
glm::vec3 forward() const {
return matrix * glm::vec4(0.0, 0.0, -1.0, 0.0);
}
glm::vec3 up() const {
return matrix * glm::vec4(0.0, 1.0, 0.0, 0.0);
}
glm::vec3 right() const {
return matrix * glm::vec4(1.0, 0.0, 0.0, 0.0);
}
private:
glm::mat4 matrix = glm::mat4(1.0f); // Initialize as identity
glm::vec3 origin = glm::vec3(0.0f, 0.0f, 0.0f);
};

18
VertexBuffer.cpp Normal file
View File

@ -0,0 +1,18 @@
#include "VertexBuffer.h"
VertexBuffer::VertexBuffer() {
glGenVertexArrays(1, &vertex_array);
glGenBuffers(1, &vertex_buffer);
}
void VertexBuffer::set_data(unsigned int size, const void *data, int flag) {
this->size = size;
glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer);
glBufferData(GL_ARRAY_BUFFER, size, data, flag);
}
void VertexBuffer::draw() {
glBindVertexArray(vertex_array);
glDrawArrays(GL_TRIANGLES, 0, size);
}

21
VertexBuffer.h Normal file
View File

@ -0,0 +1,21 @@
#pragma once
// Must be the first include
#include <glad/glad.h>
// Other includes
#include <GLFW/glfw3.h>
class VertexBuffer {
public:
VertexBuffer();
void set_data(unsigned int size, const void *data, int flag);
void draw();
private:
GLuint vertex_array;
GLuint vertex_buffer;
unsigned int size;
};

View File

@ -1,18 +1,19 @@
// Must be the first include
#include <glad/glad.h>
// Needs to be second
// Other includes
#include <GLFW/glfw3.h>
#include "Shader.h"
#include <iostream>
#include "Framebuffer3D.h"
#include "MCRenderer.h"
#include "Shader.h"
void framebuffer_size_callback(GLFWwindow *window, int width, int height);
// settings
const unsigned int SCR_WIDTH = 800;
const unsigned int SCR_HEIGHT = 600;
const unsigned int SCR_WIDTH = 1920;
const unsigned int SCR_HEIGHT = 1080;
int main() {
// glfw: initialize and configure
@ -28,7 +29,7 @@ int main() {
// glfw window creation
// --------------------
GLFWwindow *window = glfwCreateWindow(SCR_WIDTH, SCR_HEIGHT, "LearnOpenGL", NULL, NULL);
GLFWwindow *window = glfwCreateWindow(SCR_WIDTH, SCR_HEIGHT, "Marching Cubes", NULL, NULL);
if (window == NULL) {
std::cout << "Failed to create GLFW window" << std::endl;
glfwTerminate();
@ -47,54 +48,19 @@ int main() {
// -----------------------------
glEnable(GL_DEPTH_TEST);
// build and compile shaders
// -------------------------
Shader shader("Shader/mc.vs", "Shader/mc.fs", "Shader/mc.gs");
// set up vertex data (and buffer(s)) and configure vertex attributes
// ------------------------------------------------------------------
float points[] = {
-0.5f, 0.5f, 1.0f, 0.0f, 0.0f, // top-left
0.5f, 0.5f, 0.0f, 1.0f, 0.0f, // top-right
0.5f, -0.5f, 0.0f, 0.0f, 1.0f, // bottom-right
-0.5f, -0.5f, 1.0f, 1.0f, 0.0f // bottom-left
};
unsigned int VBO, VAO;
glGenBuffers(1, &VBO);
glGenVertexArrays(1, &VAO);
glBindVertexArray(VAO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(points), &points, GL_STATIC_DRAW);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(float), 0);
glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void *)(2 * sizeof(float)));
glBindVertexArray(0);
// Setup the Marching Cubes renderer
MCRenderer renderer = MCRenderer();
// render loop
// -----------
while (!glfwWindowShouldClose(window)) {
// render
// ------
glClearColor(0.1f, 0.1f, 0.1f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// draw points
shader.use();
glBindVertexArray(VAO);
glDrawArrays(GL_POINTS, 0, 4);
renderer.render(1.0); // TODO: Proper delta
// glfw: swap buffers and poll IO events (keys pressed/released, mouse moved etc.)
// -------------------------------------------------------------------------------
glfwSwapBuffers(window);
glfwPollEvents();
}
// optional: de-allocate all resources once they've outlived their purpose:
// ------------------------------------------------------------------------
glDeleteVertexArrays(1, &VAO);
glDeleteBuffers(1, &VBO);
glfwTerminate();
return 0;
}