From 3436a8b38a0714504a7b6780f3679381ad72fc53 Mon Sep 17 00:00:00 2001 From: JonasJan2 Date: Thu, 6 Jun 2024 12:05:08 +0200 Subject: [PATCH] VAO and VBO are now stored in the corresponding ObjectData object, to allow for individual manipulation of objects in scene --- src/main.c | 19 +++++++++++-------- src/objectHandler.c | 36 +++++++++++++++++++----------------- 2 files changed, 30 insertions(+), 25 deletions(-) diff --git a/src/main.c b/src/main.c index 415c39e..df88cae 100644 --- a/src/main.c +++ b/src/main.c @@ -195,16 +195,17 @@ void init(void) { // --------------- READ MODEL FILES + objectData = readObjFiles(&models, numModels); + /* objectData = malloc(numModels * sizeof(ObjectData)); for (int i = 0; i < numModels; i++) { - ObjectData objData = readObjFiles(models, 1); - glGenVertexArrays(1, &objData.vao); - glGenBuffers(1, &objData.vbo); + glGenVertexArrays(1, &objData->vao); + glGenBuffers(1, &objData->vbo); - glBindVertexArray(objData.vao); - glBindBuffer(GL_ARRAY_BUFFER, objData.vbo); - glBufferData(GL_ARRAY_BUFFER, objData.numFaces * 3 * sizeof(Vertex), objData.vertices, GL_STATIC_DRAW); + glBindVertexArray(objData->vao); + glBindBuffer(GL_ARRAY_BUFFER, objData->vbo); + glBufferData(GL_ARRAY_BUFFER, objData->object.numFaces * 3 * sizeof(Vertex), objData->object.vertices, GL_STATIC_DRAW); glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (GLvoid*)0); glEnableVertexAttribArray(0); @@ -217,6 +218,7 @@ void init(void) { objectData[i] = objData; } + */ stbi_set_flip_vertically_on_load(flipFlag); // -------------- READ TEXTURE FILES @@ -310,7 +312,8 @@ void draw(void) { rotateX(&modelTransformation, &modelTransformation, stepi); } else if (i == 3) { // Example transformations for the fourth object - translate(&modelTransformation, &modelTransformation, 1.0f, 0.0f, 0.0f); + vec3 v = {1.0f, 0.0f, 0.0f}; + translate(&modelTransformation, &modelTransformation, &v); } mat4 modelView; @@ -365,7 +368,7 @@ void draw(void) { // draw each object separately glBindVertexArray(objectData[i].vao); - glDrawArrays(GL_TRIANGLES, 0, objectData[i].numFaces * 3); + glDrawArrays(GL_TRIANGLES, 0, objectData[i].object.length * 3); } } diff --git a/src/objectHandler.c b/src/objectHandler.c index 5badac0..ebfc2f6 100644 --- a/src/objectHandler.c +++ b/src/objectHandler.c @@ -2,29 +2,25 @@ #include #include -GLuint vao[2]; -int numObj = 0; - typedef struct { GLuint vao; GLuint vbo; - int numFaces; ParsedObjFile object; } ObjectData; -void load_object(ParsedObjFile* object) { +void load_object(ObjectData* objectData) { // 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); + //GLuint triangleVertexBufferObject; + glGenBuffers(1, &objectData->vbo); + glBindBuffer(GL_ARRAY_BUFFER, objectData->vbo); + glBufferData(GL_ARRAY_BUFFER, objectData->object.length * sizeof(face), objectData->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); + glGenVertexArrays(1, &objectData->vao); + glBindVertexArray(objectData->vao); + glBindBuffer(GL_ARRAY_BUFFER, objectData->vao); // vertex positions glVertexAttribPointer( @@ -72,10 +68,12 @@ void load_object(ParsedObjFile* object) { glBindBuffer(GL_ARRAY_BUFFER, 0); glBindVertexArray(0); - - numObj++; //Erst hier inkrementieren, weil Index sonst nicht klappt } +/** + * Takes a string-pointer and a number of files to read. + * Returns an array of objects +*/ ObjectData* readObjFiles(char** path, int numModels) { ObjectData* objects = (ObjectData*) malloc(sizeof(ObjectData) * numModels); @@ -86,10 +84,14 @@ ObjectData* readObjFiles(char** path, int numModels) { 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 - objects[i].vbo = - load_object(&objects[i].object); + load_object(&objects[i]); } return objects; +} + +void draw_object(ObjectData* objectData) { + glBindVertexArray(objectData->vao); + glDrawArrays(GL_TRIANGLES, 0, objectData->object.length * 3); // Annahme: Jedes face hat 3 vertices + glBindVertexArray(0); } \ No newline at end of file