diff --git a/Makefile b/Makefile index 3bb3f39..21acdc7 100644 --- a/Makefile +++ b/Makefile @@ -25,7 +25,7 @@ LDLIBS = # add include paths (start with -I) INC = -# finds all your objects that corrispond to your .cpp files, system agnostic version +# finds all your objects that corrispond to your .c files, system agnostic version OBJECTS := $(patsubst $(SRC_DIR)/%.c, $(BUILD_DIR)/%.o, $(wildcard $(SRC_DIR)/*.c)) .PHONY: all diff --git a/PlanetMiner b/PlanetMiner index 51d7794..9c460c5 100755 Binary files a/PlanetMiner and b/PlanetMiner differ diff --git a/bin/initialization.o b/bin/initialization.o new file mode 100644 index 0000000..a53152b Binary files /dev/null and b/bin/initialization.o differ diff --git a/bin/main.o b/bin/main.o index a19f6b3..1a31870 100644 Binary files a/bin/main.o and b/bin/main.o differ diff --git a/bin/renderer.o b/bin/renderer.o new file mode 100644 index 0000000..7b14952 Binary files /dev/null and b/bin/renderer.o differ diff --git a/bin/shaders.o b/bin/shaders.o new file mode 100644 index 0000000..3cb7e60 Binary files /dev/null and b/bin/shaders.o differ diff --git a/bin/worldgen.o b/bin/worldgen.o index 2da4c50..afdb84e 100644 Binary files a/bin/worldgen.o and b/bin/worldgen.o differ diff --git a/src/initialization.c b/src/initialization.c new file mode 100644 index 0000000..f2958b1 --- /dev/null +++ b/src/initialization.c @@ -0,0 +1,48 @@ +#include "initialization.h" + +void initializeWindowSystem(GLFWwindow** windowPtr) +{ + glewExperimental = true; + if( !glfwInit() ) + { + fprintf( stderr, "Failed to initialize GLFW :(\n" ); + return -1; + } + glfwWindowHint(GLFW_SAMPLES, 4); + glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); + glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); + glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); + + glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); + + *windowPtr = glfwCreateWindow(WINW, WINH, "Planet Miner", NULL, NULL); + if( *windowPtr == NULL ) + { + fprintf( stderr, "Failed to create window. If you are using an Intel GPU, please note that they are not compatible with OpenGL 3.3\n" ); + glfwTerminate(); + return -1; + } + glfwMakeContextCurrent(*windowPtr); + + glewExperimental = true; + if( glewInit() != GLEW_OK ) + { + fprintf( stderr, "Failed to initialize GLEW\n" ); + getchar(); + glfwTerminate(); + return -1; + } + + glfwSetInputMode(*windowPtr, GLFW_STICKY_KEYS, GL_TRUE); + + glClearColor(0.0f, 0.0f, 0.0f, 0.0f); + + printf("Window: %d\n", (int)windowPtr); +} + +void initializeVertexBuffer(GLuint* vertexBufferPtr, GLfloat* vertexBufferData) +{ + glGenBuffers(1, vertexBufferPtr); + glBindBuffer(GL_ARRAY_BUFFER, *vertexBufferPtr); + glBufferData(GL_ARRAY_BUFFER, sizeof(*vertexBufferData), vertexBufferData, GL_STATIC_DRAW); +} diff --git a/src/initialization.h b/src/initialization.h new file mode 100644 index 0000000..dab6777 --- /dev/null +++ b/src/initialization.h @@ -0,0 +1,11 @@ +#include +#include +#include +#include + +#define WINW 800 +#define WINH 450 + +void initializeWindowSystem(GLFWwindow** windowPtr); +void initializeVertexBuffer(GLuint* vertexBufferPtr, GLfloat* vertexBufferDataPtr); +void initializeRenderer(); diff --git a/src/main.c b/src/main.c index 2dbe6ec..a2c5c39 100644 --- a/src/main.c +++ b/src/main.c @@ -5,86 +5,48 @@ #include #include - #include -GLFWwindow* window; #include #include "worldgen.h" - -#define WINW 800 -#define WINH 450 +#include "shaders.h" +#include "initialization.h" +#include "renderer.h" #define FOV 1.4f #define SHADER_BUF_SIZE 1000 -GLuint loadShaders(const char*, const char*); +GLFWwindow** window; + static GLfloat vertexBufferData[1000]; void setVertexBufferData(GLfloat*, unsigned int*, unsigned int); int main() { - glewExperimental = true; - if( !glfwInit() ) - { - fprintf( stderr, "Failed to initialize GLFW :(\n" ); - return -1; - } - glfwWindowHint(GLFW_SAMPLES, 4); - glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); - glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); - glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); - - glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); - - window = glfwCreateWindow(WINW, WINH, "Planet Miner", NULL, NULL); - if( window == NULL ) - { - fprintf( stderr, "Failed to create window. If you are using an Intel GPU, please note that they are not compatible with OpenGL 3.3\n" ); - glfwTerminate(); - return -1; - } - glfwMakeContextCurrent(window); - - glewExperimental = true; - if( glewInit() != GLEW_OK ) - { - fprintf( stderr, "Failed to initialize GLEW\n" ); - getchar(); - glfwTerminate(); - return -1; - } - - glfwSetInputMode(window, GLFW_STICKY_KEYS, GL_TRUE); - - glClearColor(0.0f, 0.0f, 0.0f, 0.0f); + // start initialization + initializeWindowSystem(&window); GLuint vertexArrayID; glGenVertexArrays(1, &vertexArrayID); glBindVertexArray(vertexArrayID); - //glEnable(GL_DEPTH_TEST); + glEnable(GL_DEPTH_TEST); GLuint programID = loadShaders( "src/shader.vert", "src/shader.frag" ); GLuint matrixID = glGetUniformLocation(programID, "MVP"); mat4s projection = glms_perspective(FOV, 16.0f / 9.0f, 0.1f, 100.0f); + shaderSetMat4(programID, "projection", &projection); vec3s eye = {0.0f, 0.0f, 5.0f}; vec3s center = {0.0f, 0.0f, 0.0f}; vec3s up = {0.0f, 1.0f, 0.0f}; - vec3s planetPos = {0.0f, 0.0f, 0.0f}; - worldMesh* mesh = generateWorld(planetPos); - setVertexBufferData(mesh->vertices, mesh->triangles, mesh->triangleArrayCount); - - GLuint vertexbuffer; - glGenBuffers(1, &vertexbuffer); - glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer); - glBufferData(GL_ARRAY_BUFFER, sizeof(vertexBufferData), vertexBufferData, GL_STATIC_DRAW); + GLuint vertexBuffer; + initializeVertexBuffer(&vertexBuffer, &vertexBufferData); vec3s direction = {0.0f, 0.0f, -5.0f}; @@ -96,14 +58,21 @@ int main() mat4s view = glms_lookat(eye, center, up); mat4s model = glms_mat4_identity(); - mat4s mvp = glms_mat4_mul(glms_mat4_mul(projection, view), model); + // start shaders glUseProgram(programID); + + shaderSet3f(programID, "objectColor", 1.0f, 0.5f, 0.31f); + shaderSet3f(programID, "lightColor", 1.0f, 1.0f, 1.0f); + shaderSet3f(programID, "lightPos", 1.0f, 1.0f, 1.0f); + shaderSetVec3(programID, "viewPos", &eye); + + shaderSetMat4(programID, "view", &view); + shaderSetMat4(programID, "model", &model); - glUniformMatrix4fv(matrixID, 1, GL_FALSE, &mvp); glEnableVertexAttribArray(0); - glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer); + glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer); glVertexAttribPointer( 0, @@ -116,6 +85,7 @@ int main() glDrawArrays(GL_TRIANGLES, 0, 20*3); glDisableVertexAttribArray(0); + glfwSwapBuffers(window); glfwPollEvents(); @@ -124,7 +94,7 @@ int main() && glfwGetKey(window, GLFW_KEY_ESCAPE) != GLFW_PRESS) && !glfwWindowShouldClose(window)); - glDeleteBuffers(1, &vertexbuffer); + glDeleteBuffers(1, &vertexBuffer); glDeleteProgram(programID); glDeleteVertexArrays(1, &vertexArrayID); @@ -132,38 +102,3 @@ int main() return 0; } - - - -void setVertexBufferData(GLfloat* vertices, unsigned int* triangles, unsigned int triangleCount) -{ - vec3s triangleVerts[3]; - int startingIndex; - for( unsigned int i = 0; i < triangleCount; i+=3 ) - { - startingIndex = i*6; - for( int j = 0; j < 3; ++j ) - { - triangleVerts[j].x = vertices[triangles[i+j]*3]; - triangleVerts[j].y = vertices[triangles[i+j]*3 + 1]; - triangleVerts[j].z = vertices[triangles[i+j]*3 + 2]; - - vertexBufferData[startingIndex + j*6] = triangleVerts[j].x; - vertexBufferData[startingIndex + j*6 + 1] = triangleVerts[j].y; - vertexBufferData[startingIndex + j*6 + 2] = triangleVerts[j].z; - } - - vec3s normal = glms_vec3_crossn(glms_vec3_sub(triangleVerts[0], triangleVerts[1]), - glms_vec3_sub(triangleVerts[0], triangleVerts[2])); - - vertexBufferData[startingIndex + 3] = normal.x; - vertexBufferData[startingIndex + 4] = normal.y; - vertexBufferData[startingIndex + 5] = normal.z; - vertexBufferData[startingIndex + 9] = normal.x; - vertexBufferData[startingIndex + 10] = normal.y; - vertexBufferData[startingIndex + 11] = normal.z; - vertexBufferData[startingIndex + 15] = normal.x; - vertexBufferData[startingIndex + 16] = normal.y; - vertexBufferData[startingIndex + 17] = normal.z; - } -} diff --git a/src/renderer.c b/src/renderer.c new file mode 100644 index 0000000..160ea72 --- /dev/null +++ b/src/renderer.c @@ -0,0 +1,36 @@ +#include "renderer.h" +#include + +void setVertexBufferData(GLfloat* vertexBufferData, GLfloat* vertices, unsigned int* triangles, unsigned int triangleCount) +{ + vec3s triangleVerts[3]; + int startingIndex; + for( unsigned int i = 0; i < triangleCount; i+=3 ) + { + startingIndex = i*6; + for( int j = 0; j < 3; ++j ) + { + triangleVerts[j].x = vertices[triangles[i+j]*3]; + triangleVerts[j].y = vertices[triangles[i+j]*3 + 1]; + triangleVerts[j].z = vertices[triangles[i+j]*3 + 2]; + + vertexBufferData[startingIndex + j*6] = triangleVerts[j].x; + vertexBufferData[startingIndex + j*6 + 1] = triangleVerts[j].y; + vertexBufferData[startingIndex + j*6 + 2] = triangleVerts[j].z; + } + + vec3s normal = glms_vec3_crossn(glms_vec3_sub(triangleVerts[0], triangleVerts[2]), + glms_vec3_sub(triangleVerts[0], triangleVerts[1])); + + vertexBufferData[startingIndex + 3] = normal.x; + vertexBufferData[startingIndex + 4] = normal.y; + vertexBufferData[startingIndex + 5] = normal.z; + vertexBufferData[startingIndex + 9] = normal.x; + vertexBufferData[startingIndex + 10] = normal.y; + vertexBufferData[startingIndex + 11] = normal.z; + vertexBufferData[startingIndex + 15] = normal.x; + vertexBufferData[startingIndex + 16] = normal.y; + vertexBufferData[startingIndex + 17] = normal.z; + + } +} diff --git a/src/renderer.h b/src/renderer.h new file mode 100644 index 0000000..e9d5c1e --- /dev/null +++ b/src/renderer.h @@ -0,0 +1,2 @@ +#include +void setVertexBufferData(GLfloat* vertexBufferData, GLfloat* vertices, unsigned int* triangles, unsigned int triangleCount); diff --git a/src/shader.vert b/src/shader.vert index 5c096e4..95e82b3 100644 --- a/src/shader.vert +++ b/src/shader.vert @@ -1,15 +1,16 @@ #version 330 core - layout(location = 0) in vec3 aPos; layout(location = 1) in vec3 aNormal; +out vec3 FragPos; out vec3 Normal; -uniform mat4 MVP; +uniform mat4 model; +uniform mat4 view; +uniform mat4 projection; void main(){ FragPos = vec3(model * vec4(aPos, 1.0)); Normal = mat3(transpose(inverse(model))) * aNormal; - - gl_Position = MVP * vec4(aPos, 1.0); + gl_Position = projection * view * vec4(aPos, 1.0); } diff --git a/src/shaders.c b/src/shaders.c index a7c9391..eff52af 100644 --- a/src/shaders.c +++ b/src/shaders.c @@ -1,3 +1,6 @@ +#include "shaders.h" +#include "textfiles.h" + GLuint loadShaders(const char* vertexShaderPath, const char* fragmentShaderPath) { char* vertexShaderCode = readTextFile( vertexShaderPath ); @@ -61,4 +64,62 @@ GLuint loadShaders(const char* vertexShaderPath, const char* fragmentShaderPath) return programID; } +void shaderSetBool(GLuint ID, char* name, bool value) +{ + glUniform1i(glGetUniformLocation(ID, name), (int) value); +} +void shaderSetInt(GLuint ID, char* name, int value) +{ + glUniform1i(glGetUniformLocation(ID, name), value); +} + +void shaderSetFloat(GLuint ID, char* name, float value) +{ + glUniform1f(glGetUniformLocation(ID, name), value); +} + +void shaderSetVec2(GLuint ID, char* name, vec2s* ref) +{ + glUniform2fv(glGetUniformLocation(ID, name), 1, ref); +} + +void shaderSet2f(GLuint ID, char* name, float x, float y) +{ + glUniform2f(glGetUniformLocation(ID, name), x, y); +} + +void shaderSetVec3(GLuint ID, char* name, vec3s* ref) +{ + glUniform3fv(glGetUniformLocation(ID, name), 1, ref); +} + +void shaderSet3f(GLuint ID, char* name, float x, float y, float z) +{ + glUniform3f(glGetUniformLocation(ID, name), x, y, z); +} + +void shaderSetVec4(GLuint ID, char* name, vec4s* ref) +{ + glUniform4fv(glGetUniformLocation(ID, name), 1, ref); +} + +void shaderSet4f(GLuint ID, char* name, float x, float y, float z, float w) +{ + glUniform4f(glGetUniformLocation(ID, name), x, y, z, w); +} + +void shaderSetMat2(GLuint ID, char* name, mat2s* ref) +{ + glUniformMatrix2fv(glGetUniformLocation(ID, name), 1, GL_FALSE, ref); +} + +void shaderSetMat3(GLuint ID, char* name, mat3s* ref) +{ + glUniformMatrix3fv(glGetUniformLocation(ID, name), 1, GL_FALSE, ref); +} + +void shaderSetMat4(GLuint ID, char* name, mat4s* ref) +{ + glUniformMatrix4fv(glGetUniformLocation(ID, name), 1, GL_FALSE, ref); +} diff --git a/src/shaders.h b/src/shaders.h index b114713..0c77378 100644 --- a/src/shaders.h +++ b/src/shaders.h @@ -4,21 +4,19 @@ GLuint loadShaders(const char* vertexShaderPath, const char* fragmentShaderPath); -void use(); +void shaderSetBool(GLuint ID, char* name, bool value); +void shaderSetInt(GLuint ID, char* name, int value); +void shaderSetFloat(GLuint ID, char* name, float value); -void setBool(char* name, bool value); -void setInt(char* name, int value); -void setFloat(char* name, float value); +void shaderSetVec2(GLuint ID, char* name, vec2s* ref); +void shaderSet2f(GLuint ID, char* name, float x, float y); -void setVec2(char* name, vec2s value); -void setVec3(char* name, float x, float y); +void shaderSetVec3(GLuint ID, char* name, vec3s* ref); +void shaderSet3f(GLuint ID, char* name, float x, float y, float z); -void setVec3(char* name, vec3s value); -void setVec3(char* name, float x, float y, float z); +void shaderSetVec4(GLuint ID, char* name, vec4s* ref); +void shaderSet4f(GLuint ID, char* name, float x, float y, float z, float w); -void setVec4(char* name, vec4s value); -void setVec4(char* name, float x, float y, float z, float w); - -void setMat2(char* name, mat2s value); -void setMat3(char* name, mat3s value); -void setMat4(char* name, mat4s value); +void shaderSetMat2(GLuint ID, char* name, mat2s* ref); +void shaderSetMat3(GLuint ID, char* name, mat3s* ref); +void shaderSetMat4(GLuint ID, char* name, mat4s* ref); diff --git a/src/worldgen.c b/src/worldgen.c index 9e44cf8..80da081 100644 --- a/src/worldgen.c +++ b/src/worldgen.c @@ -4,6 +4,9 @@ #include #include +#define MIN(i, j) (((i) < (j)) ? (i) : (j)) +#define MAX(i, j) (((i) > (j)) ? (i) : (j)) + #define ORDER 0 #define RSQRT5 0.4472135954999579f #define TWICE_RSQRT5 0.8944271909999159f @@ -65,3 +68,8 @@ void normalizeVertices(GLfloat* vertices, unsigned int vertexArrayCount, GLfloat vertices[i + 2] = vertex.z; } } + +unsigned int reducedSymmetricPair(unsigned int a, unsigned int b) +{ + return ((a+b+1)*(a+b+1) - (a+b+1)&1) >> 2 + MIN(a,b); +} diff --git a/src/worldgen.h b/src/worldgen.h index 183a720..0a91388 100644 --- a/src/worldgen.h +++ b/src/worldgen.h @@ -19,3 +19,5 @@ typedef struct worldMesh worldMesh* generateWorld(vec3s pos); void populateIcosphere(worldMesh* mesh, unsigned char order, GLfloat radius); void normalizeVertices(GLfloat* vertices, unsigned int vertexArrayCount, GLfloat radius); + +unsigned int reducedSymmetricPair(unsigned int, unsigned int);