From cde188de1eecd69b99232a1e092b653ca87ff01e Mon Sep 17 00:00:00 2001 From: JonasJan2 Date: Mon, 27 May 2024 15:51:29 +0200 Subject: [PATCH] new struct for object data and refactoring of reading object files --- src/main.c | 89 ++++++------------------------------------- src/objectHandler.c | 93 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 104 insertions(+), 78 deletions(-) create mode 100644 src/objectHandler.c diff --git a/src/main.c b/src/main.c index 8d1e640..29be5a5 100644 --- a/src/main.c +++ b/src/main.c @@ -5,7 +5,7 @@ #include "vertexShader.c" #include "fragmentShader.c" - +#include "objectHandler.c" #include "matrixMath.h" #include "transformation.h" #include "wavefrontobj.h" @@ -19,8 +19,6 @@ #include GLuint program; -GLuint vao[2]; -int numObj = 0; #define NUM_TEXTURES 5 @@ -41,7 +39,8 @@ char* textureFiles[NUM_TEXTURES] = { "../texture/earth/normal.png" }; -int numFaces[2] = {0, 0}; +ObjectData* objectData; +int numFaces[]; bool exitRequested = false; @@ -121,70 +120,8 @@ void loadTexture(char* textureFile, GLuint* texture) { stbi_image_free(image); } -void load_object(ParsedObjFile* object) { - // write faces to buffer - GLuint triangleVertexBufferObject; - glGenBuffers(1, &triangleVertexBufferObject); - glBindBuffer(GL_ARRAY_BUFFER, triangleVertexBufferObject); - glBufferData(GL_ARRAY_BUFFER, object->length * sizeof(face), object->faces, GL_STATIC_DRAW); - glBindBuffer(GL_ARRAY_BUFFER, 0); - // create vertex array object - glGenVertexArrays(1, &vao[numObj]); - glBindVertexArray(vao[numObj]); - glBindBuffer(GL_ARRAY_BUFFER, triangleVertexBufferObject); - - // vertex positions - glVertexAttribPointer( - 0, - 3, - GL_FLOAT, - GL_FALSE, - sizeof(vertex), - 0 - ); - glEnableVertexAttribArray(0); - - // vertex normals - glVertexAttribPointer( - 1, - 3, - GL_FLOAT, - GL_FALSE, - sizeof(vertex), - (void*) offsetof(vertex, normal) - ); - glEnableVertexAttribArray(1); - - // vertex texture coordinates - glVertexAttribPointer( - 2, - 2, - GL_FLOAT, - GL_FALSE, - sizeof(vertex), - (void*) offsetof(vertex, texture) - ); - glEnableVertexAttribArray(2); - - // face tangents - glVertexAttribPointer( - 3, - 3, - GL_FLOAT, - GL_FALSE, - sizeof(vertex), - (void*) offsetof(vertex, tangent) - ); - glEnableVertexAttribArray(3); - - glBindBuffer(GL_ARRAY_BUFFER, 0); - glBindVertexArray(0); - - numObj++; //Erst hier inkrementieren, weil Index sonst nicht klappt -} - void init(void) { // create and compile vertex shader const GLchar *vertexTextConst = vertexShader_glsl; @@ -256,14 +193,9 @@ void init(void) { } - // --------------- READ MODEL FILE - ParsedObjFile object = readObjFile(model); - ParsedObjFile object1 = readObjFile(defaultModel); - numFaces[0] = object.length; - numFaces[1] = object1.length; - - load_object(&object); - load_object(&object1); + // --------------- READ MODEL FILES + char** paths = {&defaultModel, &model}; + objectData = readObjFiles(paths, 2); stbi_set_flip_vertically_on_load(flipFlag); @@ -416,10 +348,11 @@ void draw(void) { // draw!!1 - glBindVertexArray(vao[0]); - glDrawArrays(GL_TRIANGLES, 0, numFaces[0] * 3); - glBindVertexArray(vao[1]); - glDrawArrays(GL_TRIANGLES, 0, numFaces[1] * 3); + for (int i = 0; i < numObj; i++) + { + glBindVertexArray(vao[i]); + glDrawArrays(GL_TRIANGLES, 0, objectData[i].numFaces * 3); + } } // change viewport size and adjust aspect ratio when changing window size diff --git a/src/objectHandler.c b/src/objectHandler.c new file mode 100644 index 0000000..3fb5cb0 --- /dev/null +++ b/src/objectHandler.c @@ -0,0 +1,93 @@ +#include "wavefrontobj.h" +#include +#include + +GLuint vao[2]; +int numObj = 0; + +typedef struct ObjectData +{ + ParsedObjFile object; + int numFaces; +} ObjectData; + +void load_object(ParsedObjFile* object) { + // write faces to buffer + GLuint triangleVertexBufferObject; + glGenBuffers(1, &triangleVertexBufferObject); + glBindBuffer(GL_ARRAY_BUFFER, triangleVertexBufferObject); + glBufferData(GL_ARRAY_BUFFER, object->length * sizeof(face), object->faces, GL_STATIC_DRAW); + glBindBuffer(GL_ARRAY_BUFFER, 0); + + + // create vertex array object + glGenVertexArrays(1, &vao[numObj]); + glBindVertexArray(vao[numObj]); + glBindBuffer(GL_ARRAY_BUFFER, triangleVertexBufferObject); + + // vertex positions + glVertexAttribPointer( + 0, + 3, + GL_FLOAT, + GL_FALSE, + sizeof(vertex), + 0 + ); + glEnableVertexAttribArray(0); + + // vertex normals + glVertexAttribPointer( + 1, + 3, + GL_FLOAT, + GL_FALSE, + sizeof(vertex), + (void*) offsetof(vertex, normal) + ); + glEnableVertexAttribArray(1); + + // vertex texture coordinates + glVertexAttribPointer( + 2, + 2, + GL_FLOAT, + GL_FALSE, + sizeof(vertex), + (void*) offsetof(vertex, texture) + ); + glEnableVertexAttribArray(2); + + // face tangents + glVertexAttribPointer( + 3, + 3, + GL_FLOAT, + GL_FALSE, + sizeof(vertex), + (void*) offsetof(vertex, tangent) + ); + glEnableVertexAttribArray(3); + + glBindBuffer(GL_ARRAY_BUFFER, 0); + glBindVertexArray(0); + + numObj++; //Erst hier inkrementieren, weil Index sonst nicht klappt +} + +ObjectData* readObjFiles(char** path, int numModels) { + ObjectData* objects = (ObjectData*) malloc(sizeof(ObjectData) * numModels); + + if (!objects) { + printf("ERROR in objectHandler: Failed to allocate memory for objects\n"); + return NULL; + } + + for (int i = 0; i < numModels; ++i) { + objects[i].object = readObjFile(path[i]); + objects[i].numFaces = objects[i].object.length; // Assuming .length gives the number of faces + load_object(&objects[i].object); + } + + return objects; +} \ No newline at end of file