From 40af75ad9492f438fd0e8556dc2ccfee3f4b97af Mon Sep 17 00:00:00 2001 From: JonasJan2 Date: Fri, 7 Jun 2024 15:31:56 +0200 Subject: [PATCH] Added a few comments and a function to load a single object multiple times. --- src/main.c | 50 ++++++++++++++++++++++----------------------- src/objectHandler.c | 31 +++++++++++++++++++++++++++- 2 files changed, 54 insertions(+), 27 deletions(-) diff --git a/src/main.c b/src/main.c index 32901ce..bea53f8 100644 --- a/src/main.c +++ b/src/main.c @@ -61,10 +61,16 @@ GLfloat radius = 1.0f; int numModels = 4; char* models[] = { - "../obj/Yblock.obj", - "../obj/Zblock.obj", - "../obj/Yblock_rotated.obj", - "../obj/Xblock.obj" + //"../obj/Yblock.obj", + //"../obj/Zblock.obj", + //"../obj/Yblock_rotated.obj", + "../obj/chair_test.obj", + "../obj/chair_test.obj", + "../obj/chair_test.obj", + "../obj/chair_test.obj", + "../obj/chair_test.obj", + "../obj/chair_test.obj", + "../obj/chair_test.obj" }; // input handler for camera movement @@ -75,10 +81,10 @@ void handleInputs(double deltaTime) { if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS) { cameraPosition.z -= deltaTime * 10; } - if (glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS) { + if (glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS) { cameraPosition.x += deltaTime * 10; } - if (glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS) { + if (glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS) { cameraPosition.x -= deltaTime * 10; } if (glfwGetKey(window, GLFW_KEY_SPACE) == GLFW_PRESS) { @@ -215,7 +221,11 @@ void init(void) { // --------------- READ MODEL FILES - objectData = readObjFiles(&models, numModels); + //objectData = readObjFiles(&models, numModels); + char* c = "../obj/Yblock.obj"; + int count = 0; + objectData = readSingleObjFile(&c, 20, &count); + printf("%d", count); /* objectData = malloc(numModels * sizeof(ObjectData)); for (int i = 0; i < numModels; i++) { @@ -321,24 +331,13 @@ void draw(void) { identity(&modelTransformation); // Apply object-specific transformations - if (i == 0) { - // Example transformations for the first object - rotateY(&modelTransformation, &modelTransformation, stepi * 2); - } else if (i == 1) { - // Example transformations for the second object - rotateZ(&modelTransformation, &modelTransformation, -23.5f / 180 * pi); - } else if (i == 2) { - // Example transformations for the third object - rotateX(&modelTransformation, &modelTransformation, stepi); - } else if (i == 3) { - // Example transformations for the fourth object - vec3 v = { - objectPosition.x + cos(stepi) * 1.0f, - objectPosition.y + sin(stepi) * 1.0f, - objectPosition.z - }; - translate(&modelTransformation, &modelTransformation, &v); - } + vec3 v = { + (GLfloat)i*2+objectPosition.x, + 0.0f+objectPosition.y, + 0.0f+objectPosition.z + }; + translate(&modelTransformation, &modelTransformation, &v); + mat4 modelView; identity(&modelView); @@ -432,7 +431,6 @@ int main(int argc, char **argv) { printf("OpenGL version supported by this platform (%s):\n", glGetString(GL_VERSION)); init(); - // exit when window should close or exit is requested (ESC) while (!glfwWindowShouldClose(window) && !exitRequested) { draw(); diff --git a/src/objectHandler.c b/src/objectHandler.c index ebfc2f6..7d526ff 100644 --- a/src/objectHandler.c +++ b/src/objectHandler.c @@ -8,6 +8,10 @@ typedef struct { ParsedObjFile object; } ObjectData; +/** + * Loads an object. + * Basically generates vbo and vao for an object and saving them in the ObjectData struct. + */ void load_object(ObjectData* objectData) { // write faces to buffer //GLuint triangleVertexBufferObject; @@ -72,7 +76,7 @@ void load_object(ObjectData* objectData) { /** * Takes a string-pointer and a number of files to read. - * Returns an array of objects + * Returns an array of objects. */ ObjectData* readObjFiles(char** path, int numModels) { ObjectData* objects = (ObjectData*) malloc(sizeof(ObjectData) * numModels); @@ -90,6 +94,31 @@ ObjectData* readObjFiles(char** path, int numModels) { return objects; } +/** + * Takes a single object and reads it a certain number of times. + * Returns an array of objects. + */ +ObjectData* readSingleObjFile(char** path, int numModels, int* count) { + 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); + load_object(&objects[i]); + *count++; + } + + return objects; +} + +/** + * Draw call for a single object. + * Used to clean up the draw calls in Main(). + */ void draw_object(ObjectData* objectData) { glBindVertexArray(objectData->vao); glDrawArrays(GL_TRIANGLES, 0, objectData->object.length * 3); // Annahme: Jedes face hat 3 vertices