diff --git a/PlanetMiner b/PlanetMiner index 9c460c5..7d9ca6a 100755 Binary files a/PlanetMiner and b/PlanetMiner differ diff --git a/bin/main.o b/bin/main.o index 1a31870..5dead4c 100644 Binary files a/bin/main.o and b/bin/main.o differ diff --git a/bin/worldgen.o b/bin/worldgen.o index afdb84e..cfca151 100644 Binary files a/bin/worldgen.o and b/bin/worldgen.o differ diff --git a/src/main.c b/src/main.c index a2c5c39..3c61506 100644 --- a/src/main.c +++ b/src/main.c @@ -22,7 +22,9 @@ GLFWwindow** window; static GLfloat vertexBufferData[1000]; -void setVertexBufferData(GLfloat*, unsigned int*, unsigned int); +void updateWindow(GLFWwindow** window, GLuint vertexBuffer); +void terminateProgram(GLuint* vertexBufferPtr, GLuint* vertexArrayIDPtr, GLuint programID); +bool shouldTerminate(GLFWwindow** window); int main() { @@ -50,8 +52,6 @@ int main() vec3s direction = {0.0f, 0.0f, -5.0f}; - printf("Entering main loop\n"); - do { glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ); @@ -69,8 +69,17 @@ int main() shaderSetMat4(programID, "view", &view); shaderSetMat4(programID, "model", &model); + + updateWindow(window, vertexBuffer); + } + while(!shouldTerminate(window)); + terminateProgram(&vertexBuffer, &vertexArrayID, programID); + return 0; +} +void updateWindow(GLFWwindow** window, GLuint vertexBuffer) +{ glEnableVertexAttribArray(0); glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer); @@ -85,20 +94,24 @@ int main() glDrawArrays(GL_TRIANGLES, 0, 20*3); glDisableVertexAttribArray(0); - glfwSwapBuffers(window); glfwPollEvents(); - } - while( (glfwGetKey(window, GLFW_KEY_Q) != GLFW_PRESS - && glfwGetKey(window, GLFW_KEY_ESCAPE) != GLFW_PRESS) - && !glfwWindowShouldClose(window)); +} - glDeleteBuffers(1, &vertexBuffer); +void terminateProgram(GLuint* vertexBufferPtr, GLuint* vertexArrayIDPtr, GLuint programID) +{ + glDeleteBuffers(1, vertexBufferPtr); glDeleteProgram(programID); - glDeleteVertexArrays(1, &vertexArrayID); + glDeleteVertexArrays(1, vertexArrayIDPtr); glfwTerminate(); - - return 0; +} + +bool shouldTerminate(GLFWwindow** window) +{ + // quit on , , or when otherwise told to + return glfwGetKey(window, GLFW_KEY_Q) == GLFW_PRESS + || glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS + || glfwWindowShouldClose(window); } diff --git a/src/meshing.h b/src/meshing.h new file mode 100644 index 0000000..3a68651 --- /dev/null +++ b/src/meshing.h @@ -0,0 +1,11 @@ +#include + +typedef struct +{ + unsigned int numVertices; + unsigned int numTexCoords; + unsigned int numNormals; + vec3s* vertices; + vec2s* texCoords; + vec3s* normals; +} mesh; diff --git a/src/worldgen.c b/src/worldgen.c index 80da081..5a75b9e 100644 --- a/src/worldgen.c +++ b/src/worldgen.c @@ -12,18 +12,18 @@ #define TWICE_RSQRT5 0.8944271909999159f #define GOLDEN 1.618033988749895f -worldMesh* generateWorld(vec3s position) +mesh* generateWorld(vec3s position) { // placeholder struct for world mesh - worldMesh* mesh = (worldMesh*) malloc(sizeof(worldMesh)); - populateIcosphere(mesh, ORDER, 1.0f); - return mesh; + mesh* worldMesh = (mesh*) malloc(sizeof(mesh)); + populateIcosphere(worldMesh, ORDER, 1.0f); + return worldMesh; } -void populateIcosphere(worldMesh* mesh, unsigned char order, GLfloat radius) +void populateIcosphere(mesh* worldMesh, unsigned char order, GLfloat radius) { int T = powf(4, order); - mesh->vertexArrayCount = (10*T + 2) * 3; // Final heap array size for vertices + worldMesh->numVertices = (10*T + 2) * 3; // Final heap array size for vertices // Initial vertex array. Not used other than to initialize heap array in mesh GLfloat vertices[] = { @@ -39,23 +39,13 @@ void populateIcosphere(worldMesh* mesh, unsigned char order, GLfloat radius) 3, 9, 4, 3, 4, 2, 3, 2, 6, 3, 6, 8, 3, 8, 9, 9, 8, 1, 4, 9, 5, 2, 4, 11, 6, 2, 10, 8, 6, 7 }; - - mesh->triangleArrayCount = 20*T*3; - normalizeVertices(vertices, mesh->vertexArrayCount, radius); - - // Move vetex and triangle data into mesh struct - mesh->vertices = (GLfloat*) malloc(sizeof(GLfloat) * mesh->vertexArrayCount); - memcpy(mesh->vertices, vertices, sizeof(GLfloat) * mesh->vertexArrayCount); - - mesh->triangles = (unsigned int*) malloc(sizeof(unsigned int) * mesh->triangleArrayCount); - memcpy(mesh->triangles, triangles, sizeof(unsigned int) * 60); } -void normalizeVertices(GLfloat* vertices, unsigned int vertexArrayCount, GLfloat radius) +void normalizeVertices(GLfloat* vertices, unsigned int numVertices, GLfloat radius) { - printf("Normalizing %d vertices\n", vertexArrayCount); + printf("Normalizing %d vertices\n", numVertices); vec3s vertex; - for( int i = 0; i < vertexArrayCount; i += 3) + for( int i = 0; i < numVertices; i += 3) { vertex.x = vertices[i]; vertex.y = vertices[i + 1]; diff --git a/src/worldgen.h b/src/worldgen.h index 0a91388..34971f5 100644 --- a/src/worldgen.h +++ b/src/worldgen.h @@ -1,14 +1,7 @@ #include #include -typedef struct worldMesh -{ - unsigned int vertexArrayCount; - unsigned int triangleArrayCount; - GLfloat* vertices; - unsigned int* triangles; -} worldMesh; - +#include "meshing.h" /*! * @brief top-level world generation function @@ -16,8 +9,8 @@ typedef struct worldMesh * @param[in] pos the position of the center of the roughly spherical world * @return the world mesh as a heap-allocated list of vertices. */ -worldMesh* generateWorld(vec3s pos); -void populateIcosphere(worldMesh* mesh, unsigned char order, GLfloat radius); +mesh* generateWorld(vec3s pos); +void populateIcosphere(mesh* mesh, unsigned char order, GLfloat radius); void normalizeVertices(GLfloat* vertices, unsigned int vertexArrayCount, GLfloat radius); unsigned int reducedSymmetricPair(unsigned int, unsigned int);