Compare commits

...

5 Commits

Author SHA1 Message Date
37d2e4aabc Lots of cleanup 2021-03-20 01:11:39 +01:00
7227fef749 It works!
Lots of minor fixes
2021-03-19 23:54:24 +01:00
641d77d2db Some fixes (still not functional) 2021-03-19 19:42:35 +01:00
835520edb8 Basic structure there, but not functional 2021-03-19 18:27:00 +01:00
9ab7f3eed0 Basic project setup
the example from https://learnopengl.com/Advanced-OpenGL/Geometry-Shader is functional; built with SCons.
2021-03-16 19:53:16 +01:00
19 changed files with 10231 additions and 4 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 <glm/gtx/dual_quaternion.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 glm::inverse(get_matrix());
}
private:
glm::mat4 projection;
};

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;
};

71
MCRenderer.cpp Normal file
View File

@ -0,0 +1,71 @@
#include "MCRenderer.h"
#include "Framebuffer3D.h"
#include "VertexBuffer.h"
#include <glm/ext/matrix_transform.hpp>
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}};
vertex_rectangle.set_data(sizeof(data), 2, 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, size_x, size_y);
noise_shader.use();
noise.bind_and_clear();
noise_shader.setFloat("height", 0);
for (int i = 0; i < size_z; i++) {
// Create one layer
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
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());
// 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
glDrawArrays(GL_POINTS, 0, size_x * size_y * size_z);
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
}

26
MCRenderer.h Normal file
View File

@ -0,0 +1,26 @@
#pragma once
#include "Camera.h"
#include "Framebuffer3D.h"
#include "Shader.h"
#include "VertexBuffer.h"
class MCRenderer {
public:
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;
Camera camera;
};

View File

@ -1,7 +1,12 @@
#!python
# Create the environment and create a Compilation Database for use in VSCodium
env = DefaultEnvironment(tools=['default', 'compilation_db'])
env = Environment(
CCFLAGS=['-std=c++17', '-Wall', '-O0', '-g'],
tools=['default', 'compilation_db'])
env.CompilationDatabase()
Program('program.out', Glob('*.cpp'))
env.Append(LIBS=['glfw', 'dl'])
env.Program('program.out', [Glob('*.cpp', 'Util/*.cpp'), 'Util/glad.c'])

170
Shader.h Normal file
View File

@ -0,0 +1,170 @@
// Adapted from LearnOpenGL
#ifndef SHADER_H
#define SHADER_H
#include <glad/glad.h>
#include <glm/glm.hpp>
#include <fstream>
#include <iostream>
#include <sstream>
#include <string>
class Shader {
public:
unsigned int ID;
// constructor generates the shader on the fly
// ------------------------------------------------------------------------
Shader(const char *vertexPath, const char *fragmentPath, const char *geometryPath = nullptr) {
// 1. retrieve the vertex/fragment source code from filePath
std::string vertexCode;
std::string fragmentCode;
std::string geometryCode;
std::ifstream vShaderFile;
std::ifstream fShaderFile;
std::ifstream gShaderFile;
// ensure ifstream objects can throw exceptions:
vShaderFile.exceptions(std::ifstream::failbit | std::ifstream::badbit);
fShaderFile.exceptions(std::ifstream::failbit | std::ifstream::badbit);
gShaderFile.exceptions(std::ifstream::failbit | std::ifstream::badbit);
try {
// open files
vShaderFile.open(vertexPath);
fShaderFile.open(fragmentPath);
std::stringstream vShaderStream, fShaderStream;
// read file's buffer contents into streams
vShaderStream << vShaderFile.rdbuf();
fShaderStream << fShaderFile.rdbuf();
// close file handlers
vShaderFile.close();
fShaderFile.close();
// convert stream into string
vertexCode = vShaderStream.str();
fragmentCode = fShaderStream.str();
// if geometry shader path is present, also load a geometry shader
if (geometryPath != nullptr) {
gShaderFile.open(geometryPath);
std::stringstream gShaderStream;
gShaderStream << gShaderFile.rdbuf();
gShaderFile.close();
geometryCode = gShaderStream.str();
}
} catch (std::ifstream::failure &e) {
std::cout << "ERROR::SHADER::FILE_NOT_SUCCESFULLY_READ" << std::endl;
}
const char *vShaderCode = vertexCode.c_str();
const char *fShaderCode = fragmentCode.c_str();
// 2. compile shaders
unsigned int vertex, fragment;
// vertex shader
vertex = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertex, 1, &vShaderCode, NULL);
glCompileShader(vertex);
checkCompileErrors(vertex, "VERTEX");
// fragment Shader
fragment = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragment, 1, &fShaderCode, NULL);
glCompileShader(fragment);
checkCompileErrors(fragment, "FRAGMENT");
// if geometry shader is given, compile geometry shader
unsigned int geometry;
if (geometryPath != nullptr) {
const char *gShaderCode = geometryCode.c_str();
geometry = glCreateShader(GL_GEOMETRY_SHADER);
glShaderSource(geometry, 1, &gShaderCode, NULL);
glCompileShader(geometry);
checkCompileErrors(geometry, "GEOMETRY");
}
// shader Program
ID = glCreateProgram();
glAttachShader(ID, vertex);
glAttachShader(ID, fragment);
if (geometryPath != nullptr) glAttachShader(ID, geometry);
glLinkProgram(ID);
checkCompileErrors(ID, "PROGRAM");
// delete the shaders as they're linked into our program now and no longer necessery
glDeleteShader(vertex);
glDeleteShader(fragment);
if (geometryPath != nullptr) glDeleteShader(geometry);
}
// activate the shader
// ------------------------------------------------------------------------
void use() {
glUseProgram(ID);
}
// utility uniform functions
// ------------------------------------------------------------------------
void setBool(const std::string &name, bool value) const {
glUniform1i(glGetUniformLocation(ID, name.c_str()), (int)value);
}
// ------------------------------------------------------------------------
void setInt(const std::string &name, int value) const {
glUniform1i(glGetUniformLocation(ID, name.c_str()), value);
}
// ------------------------------------------------------------------------
void setFloat(const std::string &name, float value) const {
glUniform1f(glGetUniformLocation(ID, name.c_str()), value);
}
// ------------------------------------------------------------------------
void setVec2(const std::string &name, const glm::vec2 &value) const {
glUniform2fv(glGetUniformLocation(ID, name.c_str()), 1, &value[0]);
}
void setVec2(const std::string &name, float x, float y) const {
glUniform2f(glGetUniformLocation(ID, name.c_str()), x, y);
}
// ------------------------------------------------------------------------
void setVec3(const std::string &name, const glm::vec3 &value) const {
glUniform3fv(glGetUniformLocation(ID, name.c_str()), 1, &value[0]);
}
void setVec3(const std::string &name, float x, float y, float z) const {
glUniform3f(glGetUniformLocation(ID, name.c_str()), x, y, z);
}
// ------------------------------------------------------------------------
void setVec4(const std::string &name, const glm::vec4 &value) const {
glUniform4fv(glGetUniformLocation(ID, name.c_str()), 1, &value[0]);
}
void setVec4(const std::string &name, float x, float y, float z, float w) {
glUniform4f(glGetUniformLocation(ID, name.c_str()), x, y, z, w);
}
// ------------------------------------------------------------------------
void setMat2(const std::string &name, const glm::mat2 &mat) const {
glUniformMatrix2fv(glGetUniformLocation(ID, name.c_str()), 1, GL_FALSE, &mat[0][0]);
}
// ------------------------------------------------------------------------
void setMat3(const std::string &name, const glm::mat3 &mat) const {
glUniformMatrix3fv(glGetUniformLocation(ID, name.c_str()), 1, GL_FALSE, &mat[0][0]);
}
// ------------------------------------------------------------------------
void setMat4(const std::string &name, const glm::mat4 &mat) const {
glUniformMatrix4fv(glGetUniformLocation(ID, name.c_str()), 1, GL_FALSE, &mat[0][0]);
}
private:
// utility function for checking shader compilation/linking errors.
// ------------------------------------------------------------------------
void checkCompileErrors(GLuint shader, std::string type) {
GLint success;
GLchar infoLog[1024];
if (type != "PROGRAM") {
glGetShaderiv(shader, GL_COMPILE_STATUS, &success);
if (!success) {
glGetShaderInfoLog(shader, 1024, NULL, infoLog);
std::cout << "ERROR::SHADER_COMPILATION_ERROR of type: " << type << "\n"
<< infoLog
<< "\n -- --------------------------------------------------- -- "
<< std::endl;
}
} else {
glGetProgramiv(shader, GL_LINK_STATUS, &success);
if (!success) {
glGetProgramInfoLog(shader, 1024, NULL, infoLog);
std::cout << "ERROR::PROGRAM_LINKING_ERROR of type: " << type << "\n"
<< infoLog
<< "\n -- --------------------------------------------------- -- "
<< std::endl;
}
}
}
};
#endif

12
Shader/mc.fs Normal file
View File

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

196
Shader/mc.gs Normal file
View File

@ -0,0 +1,196 @@
#version 430
layout (points) in;
layout (triangle_strip, max_vertices = 12) out;
uniform mat4 proj;
uniform mat4 view;
in vec3 varTexture[];
in int varIndex[];
out vec3 varTextureG;
vec3 vectors[13] = {
vec3(0.0, 0.0, 0.0), vec3(0.5, 1.0, 1.0), vec3(0.0, 0.5, 1.0),
vec3(0.0, 1.0, 0.5), vec3(1.0, 0.5, 1.0), vec3(1.0, 1.0, 0.5),
vec3(0.5, 1.0, 0.0), vec3(1.0, 0.5, 0.0), vec3(0.0, 0.5, 0.0),
vec3(0.0, 0.0, 0.5), vec3(0.5, 0.0, 1.0), vec3(1.0, 0.0, 0.5),
vec3(0.5, 0.0, 0.0)};
int table[3072] = {
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 0, 0, 0, 0,
0, 0, 0, 0, 0, 4, 1, 5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 5,
2, 5, 2, 4, 0, 0, 0, 0, 0, 0, 5, 6, 7, 0, 0, 0, 0, 0, 0,
0, 0, 0, 5, 6, 7, 3, 1, 2, 0, 0, 0, 0, 0, 0, 7, 4, 6, 4,
6, 1, 0, 0, 0, 0, 0, 0, 3, 6, 2, 7, 6, 2, 7, 2, 4, 0, 0,
0, 3, 8, 6, 0, 0, 0, 0, 0, 0, 0, 0, 0, 6, 1, 8, 1, 8, 2,
0, 0, 0, 0, 0, 0, 6, 3, 8, 1, 5, 4, 0, 0, 0, 0, 0, 0, 6,
5, 8, 4, 5, 8, 4, 8, 2, 0, 0, 0, 5, 3, 7, 3, 7, 8, 0, 0,
0, 0, 0, 0, 5, 1, 7, 2, 1, 7, 2, 7, 8, 0, 0, 0, 1, 3, 4,
8, 3, 4, 8, 4, 7, 0, 0, 0, 8, 7, 2, 4, 7, 2, 0, 0, 0, 0,
0, 0, 9, 2, 10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 9, 3, 10, 3, 10,
1, 0, 0, 0, 0, 0, 0, 4, 1, 5,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, 0, 0, 6, 1, 8, 1, 8, 2, 0, 0, 0, 0, 0, 0,
3, 8, 6, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 6, 2, 7, 6, 2, 7,
2, 4, 0, 0, 0, 7, 4, 6, 4, 6, 1, 0, 0, 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();
}
}

38
Shader/mc.vs Normal file
View File

@ -0,0 +1,38 @@
#version 430
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;
void main(void) {
int id = gl_VertexID;
// 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 < 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;
}

14
Shader/noise.fs Normal file
View File

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

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);
};

1833
Util/glad.c Normal file

File diff suppressed because it is too large Load Diff

7559
Util/stb_image.h Normal file

File diff suppressed because it is too large Load Diff

2
Util/stb_setup.cpp Normal file
View File

@ -0,0 +1,2 @@
#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"

37
VertexBuffer.cpp Normal file
View File

@ -0,0 +1,37 @@
#include "VertexBuffer.h"
VertexBuffer::VertexBuffer() {
glGenVertexArrays(1, &vertex_array);
glGenBuffers(1, &vertex_buffer);
}
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;
bind_array();
bind_buffer();
// Create the data on the GPU
glBufferData(GL_ARRAY_BUFFER, size, data, flag);
// 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, dimension, GL_FLOAT, false, dimension * sizeof(float), 0);
glBindVertexArray(0);
}
void VertexBuffer::bind_buffer() {
glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer);
}
void VertexBuffer::bind_array() {
glBindVertexArray(vertex_array);
}
void VertexBuffer::draw() {
bind_array();
glDrawArrays(GL_TRIANGLES, 0, size);
}

24
VertexBuffer.h Normal file
View File

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

View File

@ -1,7 +1,74 @@
// Must be the first include
#include <glad/glad.h>
// Other includes
#include <GLFW/glfw3.h>
#include <iostream>
int main() {
std::cout << "Hello World!" << std::endl;
#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 = 1920;
const unsigned int SCR_HEIGHT = 1080;
int main() {
// glfw: initialize and configure
// ------------------------------
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
#ifdef __APPLE__
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
#endif
// glfw window creation
// --------------------
GLFWwindow *window = glfwCreateWindow(SCR_WIDTH, SCR_HEIGHT, "Marching Cubes", NULL, NULL);
if (window == NULL) {
std::cout << "Failed to create GLFW window" << std::endl;
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window);
// glad: load all OpenGL function pointers
// ---------------------------------------
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) {
std::cout << "Failed to initialize GLAD" << std::endl;
return -1;
}
// configure global opengl state
// -----------------------------
glEnable(GL_DEPTH_TEST);
// Setup the Marching Cubes renderer
MCRenderer renderer = MCRenderer(128, 128, 128);
// render loop
// -----------
while (!glfwWindowShouldClose(window)) {
renderer.render(0.1); // TODO: Proper delta
// glfw: swap buffers and poll IO events (keys pressed/released, mouse moved etc.)
glfwSwapBuffers(window);
glfwPollEvents();
}
glfwTerminate();
return 0;
}
// glfw: whenever the window size changed (by OS or user resize) this callback function executes
// ---------------------------------------------------------------------------------------------
void framebuffer_size_callback(GLFWwindow *window, int width, int height) {
// make sure the viewport matches the new window dimensions; note that width and
// height will be significantly larger than specified on retina displays.
glViewport(0, 0, width, height);
}