diff --git a/PlanetMiner b/PlanetMiner index 3e8c029..22ad1a1 100755 Binary files a/PlanetMiner and b/PlanetMiner differ diff --git a/bin/imagefiles.o b/bin/imagefiles.o new file mode 100644 index 0000000..7d9faa6 Binary files /dev/null and b/bin/imagefiles.o differ diff --git a/bin/main.o b/bin/main.o index f89d254..75c8d16 100644 Binary files a/bin/main.o and b/bin/main.o differ diff --git a/bin/textfiles.o b/bin/textfiles.o index 7614711..779fb7b 100644 Binary files a/bin/textfiles.o and b/bin/textfiles.o differ diff --git a/bin/worldgen.o b/bin/worldgen.o index 3e9a882..3f8f7f3 100644 Binary files a/bin/worldgen.o and b/bin/worldgen.o differ diff --git a/src/collections.h b/src/collections.h new file mode 100644 index 0000000..b702c6f --- /dev/null +++ b/src/collections.h @@ -0,0 +1,19 @@ +typedef struct intHashTableItem +{ + int key; + int value; +} + +typedef struct intHashTable +{ + intHashTableItem** items; + int size; + int count; +} + +intHashTable* createTable(int); +intHashTableItem* createIntHashTableItem(int key, int value); +void freeIntHashTableItem(intHashTableItem*); +void freeIntHashTable(intHashTable*); +void intHashTableInsert(intHashTable*, int, int); + diff --git a/src/imagefiles.c b/src/imagefiles.c index d42409d..628fef8 100644 --- a/src/imagefiles.c +++ b/src/imagefiles.c @@ -1,7 +1,7 @@ #include "imagefiles.h" #include #include - +/* bmp loadBitmapImage(char* path) { FILE* inFile; @@ -58,4 +58,4 @@ bmp loadBitmapImage(char* path) fread(data, 1, imageSize, inFile); fclose(inFile); -} +}*/ diff --git a/src/imagefiles.h b/src/imagefiles.h index 7e5c827..07a1581 100644 --- a/src/imagefiles.h +++ b/src/imagefiles.h @@ -1,4 +1,5 @@ -typedef struct bmp { +typedef struct bmp +{ unsigned char header[54]; unsigned int dataPos; unsigned int width, height; diff --git a/src/main.c b/src/main.c index fb61c1e..17d2d45 100644 --- a/src/main.c +++ b/src/main.c @@ -11,6 +11,8 @@ GLFWwindow* window; #include +#include "worldgen.h" + #define WINW 800 #define WINH 450 @@ -19,6 +21,9 @@ GLFWwindow* window; #define SHADER_BUF_SIZE 1000 GLuint loadShaders(const char*, const char*); +static GLfloat vertexBufferData[1000]; + +void setVertexBufferData(GLfloat*, unsigned int*, unsigned int); int main() { @@ -72,93 +77,14 @@ int main() vec3s center = {0.0f, 0.0f, 0.0f}; vec3s up = {0.0f, 1.0f, 0.0f}; - static const GLfloat g_vertex_buffer_data[] = { - // BAC - 1.0f, 0.0f, 2.0f, - 2.0f, 1.0f, 0.0f, - 0.0f, 2.0f, 1.0f, - // BCH - 1.0f, 0.0f, 2.0f, - 0.0f, 2.0f, 1.0f, - -1.0f, 0.0f, 2.0f, - // BHF - 1.0f, 0.0f, 2.0f, - -1.0f, 0.0f, 2.0f, - 0.0f, -2.0f, 1.0f, - // BFG - 1.0f, 0.0f, 2.0f, - 0.0f, -2.0f, 1.0f, - 2.0f, -1.0f, 0.0f, - // BGA - 1.0f, 0.0f, 2.0f, - 2.0f, -1.0f, 0.0f, - 2.0f, 1.0f, 0.0f, - // AIC - 2.0f, 1.0f, 0.0f, - 0.0f, 2.0f, -1.0f, - 0.0f, 2.0f, 1.0f, - // ICD - 0.0f, 2.0f, -1.0f, - 0.0f, 2.0f, 1.0f, - -2.0f, 1.0f, 0.0f, - // CDH - 0.0f, 2.0f, 1.0f, - -2.0f, 1.0f, 0.0f, - -1.0f, 0.0f, 2.0f, - // DHJ - -2.0f, 1.0f, 0.0f, - -1.0f, 0.0f, 2.0f, - -2.0f, -1.0f, 0.0f, - // HJF - -1.0f, 0.0f, 2.0f, - -2.0f, -1.0f, 0.0f, - 0.0f, -2.0f, 1.0f, - // JFL - -2.0f, -1.0f, 0.0f, - 0.0f, -2.0f, 1.0f, - 0.0f, -2.0f, -1.0f, - // FLG - 0.0f, -2.0f, 1.0f, - 0.0f, -2.0f, -1.0f, - 2.0f, -1.0f, 0.0f, - // LGE - 0.0f, -2.0f, -1.0f, - 2.0f, -1.0f, 0.0f, - 1.0f, 0.0f, -2.0f, - // GEA - 2.0f, -1.0f, 0.0f, - 1.0f, 0.0f, -2.0f, - 2.0f, 1.0f, 0.0f, - // EAI - 1.0f, 0.0f, -2.0f, - 2.0f, 1.0f, 0.0f, - 0.0f, 2.0f, -1.0f, - // KEI - -1.0f, 0.0f, -2.0f, - 1.0f, 0.0f, -2.0f, - 0.0f, 2.0f, -1.0f, - // KID - -1.0f, 0.0f, -2.0f, - 0.0f, 2.0f, -1.0f, - -2.0f, 1.0f, 0.0f, - // KDJ - -1.0f, 0.0f, -2.0f, - -2.0f, 1.0f, 0.0f, - -2.0f, -1.0f, 0.0f, - // KJL - -1.0f, 0.0f, -2.0f, - -2.0f, -1.0f, 0.0f, - 0.0f, -2.0f, -1.0f, - // KLE - -1.0f, 0.0f, -2.0f, - 0.0f, -2.0f, -1.0f, - 1.0f, 0.0f, -2.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(g_vertex_buffer_data), g_vertex_buffer_data, GL_STATIC_DRAW); + glBufferData(GL_ARRAY_BUFFER, sizeof(vertexBufferData), vertexBufferData, GL_STATIC_DRAW); vec3s direction = {0.0f, 0.0f, -5.0f}; @@ -197,8 +123,8 @@ int main() } // center = glms_vec3_add(eye, direction); - eye.x = radius * (float) sin((double) theta); - eye.z = radius * (float) cos((double) theta); + eye.x = radius * sinf(theta); + eye.z = radius * cosf(theta); mat4s view = glms_lookat(eye, center, up); mat4s model = glms_mat4_identity(); @@ -301,3 +227,11 @@ GLuint loadShaders(const char* vertexShaderPath, const char* fragmentShaderPath) glDeleteShader( fragmentShaderID ); return programID; } + +void setVertexBufferData(GLfloat* vertices, unsigned int* triangles, unsigned int triangleCount) +{ + printf("Loading %d triangles into buffer\n", triangleCount); + for( int i = 0; i < triangleCount; ++i ) + { + } +} diff --git a/src/textfiles.c b/src/textfiles.c index 7bd8c61..b033f36 100644 --- a/src/textfiles.c +++ b/src/textfiles.c @@ -16,7 +16,7 @@ char* readTextFile(char* fname) if( inFile == NULL ) { fprintf( stderr, "Failed open file, [%s].\n", fname ); - exit(); + exit(1); } fseek( inFile, 0, SEEK_END ); diff --git a/src/worldgen.c b/src/worldgen.c index f46472b..7f4c3e6 100644 --- a/src/worldgen.c +++ b/src/worldgen.c @@ -1,24 +1,67 @@ #include "worldgen.h" #include #include +#include +#include -#define ORDER 1 -#define RSQRT5 0.4472135954999579 -#define TWICE_RSQRT5 = 0.8944271909999159 +#define ORDER 0 +#define RSQRT5 0.4472135954999579f +#define TWICE_RSQRT5 0.8944271909999159f +#define GOLDEN 1.618033988749895f worldMesh generateWorld(vec3s position) { // placeholder struct for world mesh - worldMesh mesh; - mesh.count = 20 * ORDER * 3; // Icosahedron has 20 triangular sides - - mesh.vertices = (GLfloat*) malloc(sizeof(GLfloat) * mesh.count); - populateIcosphere(mesh, ORDER, 1); - + world return mesh; } -void populateIcosphere(worldMesh mesh, unsigned char order, float radius) +void populateIcosphere(unsigned char order, GLfloat radius) { - + int T = powf(4, order); + mesh.vertexArrayCount = (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[] = { + -1.0f, GOLDEN, 0.0f, 1.0f, GOLDEN, 0.0f, -1.0f, -GOLDEN, 0.0f, 1.0f, -GOLDEN, 0.0f, + 0.0f, -1.0f, GOLDEN, 0.0f, 1.0f, GOLDEN, 0.0f, -1.0f, -GOLDEN, 0.0f, 1.0f, -GOLDEN, + GOLDEN, 0.0f, -1.0f, GOLDEN, 0.0f, 1.0f, -GOLDEN, 0.0f, -1.0f, -GOLDEN, 0.0f, 1.0f + }; + + + unsigned int triangles[] = { + 0, 11, 5, 0, 5, 1, 0, 1, 7, 0, 7, 10, 0, 10, 11, + 11, 10, 2, 5, 11, 4, 1, 5, 9, 7, 1, 8, 10, 7, 6, + 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) +{ + printf("Normalizing %d vertices\n", vertexArrayCount); + vec3s vertex; + for( int i = 0; i < vertexArrayCount; i += 3) + { + vertex.x = vertices[i]; + vertex.y = vertices[i + 1]; + vertex.z = vertices[i + 2]; + + glms_vec3_normalize(vertex); + glms_vec3_scale(vertex, radius); + + vertices[i] = vertex.x; + vertices[i + 1] = vertex.y; + vertices[i + 2] = vertex.z; + } } diff --git a/src/worldgen.h b/src/worldgen.h index 0fafaa4..0786695 100644 --- a/src/worldgen.h +++ b/src/worldgen.h @@ -1,9 +1,12 @@ #include #include -typedef struct worldMesh { - long int count; +typedef struct worldMesh +{ + unsigned int vertexArrayCount; + unsigned int triangleArrayCount; GLfloat* vertices; + unsigned int* triangles; } worldMesh; @@ -14,3 +17,5 @@ typedef struct worldMesh { * @return the world mesh as a heap-allocated list of vertices. */ worldMesh generateWorld(vec3s pos); +worldMesh populateIcosphere(unsigned char order, GLfoat radius); + diff --git a/todo.md b/todo.md index 05c7b8b..5a96f7d 100644 --- a/todo.md +++ b/todo.md @@ -2,12 +2,15 @@ ## Priority List 1. Render stuff - 1. Render a cube - 2. Render a bunch of cubes - 3. Diffuse lighting - 4. Ray tracing? + - [x] Render a cube + - [x] Render icosahedron + - [ ] Vertices to triangle mesh + - [ ] Subdivide icosahedron 2. Procedural worlds - 1. Optimize rendering for this many cubes + - [ ] Morph vertices of the subdivided icosphere + - [ ] Terrain generation + - [ ] Planet scaling + - [ ] 3. Block game 1. World editing