Added a few comments and a function to load a single object multiple times.

This commit is contained in:
JonasJan2 2024-06-07 15:31:56 +02:00
parent 32afa76813
commit 40af75ad94
2 changed files with 54 additions and 27 deletions

View File

@ -61,10 +61,16 @@ GLfloat radius = 1.0f;
int numModels = 4; int numModels = 4;
char* models[] = { char* models[] = {
"../obj/Yblock.obj", //"../obj/Yblock.obj",
"../obj/Zblock.obj", //"../obj/Zblock.obj",
"../obj/Yblock_rotated.obj", //"../obj/Yblock_rotated.obj",
"../obj/Xblock.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 // input handler for camera movement
@ -75,10 +81,10 @@ void handleInputs(double deltaTime) {
if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS) { if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS) {
cameraPosition.z -= deltaTime * 10; cameraPosition.z -= deltaTime * 10;
} }
if (glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS) { if (glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS) {
cameraPosition.x += deltaTime * 10; cameraPosition.x += deltaTime * 10;
} }
if (glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS) { if (glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS) {
cameraPosition.x -= deltaTime * 10; cameraPosition.x -= deltaTime * 10;
} }
if (glfwGetKey(window, GLFW_KEY_SPACE) == GLFW_PRESS) { if (glfwGetKey(window, GLFW_KEY_SPACE) == GLFW_PRESS) {
@ -215,7 +221,11 @@ void init(void) {
// --------------- READ MODEL FILES // --------------- 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)); objectData = malloc(numModels * sizeof(ObjectData));
for (int i = 0; i < numModels; i++) { for (int i = 0; i < numModels; i++) {
@ -321,24 +331,13 @@ void draw(void) {
identity(&modelTransformation); identity(&modelTransformation);
// Apply object-specific transformations // Apply object-specific transformations
if (i == 0) { vec3 v = {
// Example transformations for the first object (GLfloat)i*2+objectPosition.x,
rotateY(&modelTransformation, &modelTransformation, stepi * 2); 0.0f+objectPosition.y,
} else if (i == 1) { 0.0f+objectPosition.z
// Example transformations for the second object };
rotateZ(&modelTransformation, &modelTransformation, -23.5f / 180 * pi); translate(&modelTransformation, &modelTransformation, &v);
} 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);
}
mat4 modelView; mat4 modelView;
identity(&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)); printf("OpenGL version supported by this platform (%s):\n", glGetString(GL_VERSION));
init(); init();
// exit when window should close or exit is requested (ESC) // exit when window should close or exit is requested (ESC)
while (!glfwWindowShouldClose(window) && !exitRequested) { while (!glfwWindowShouldClose(window) && !exitRequested) {
draw(); draw();

View File

@ -8,6 +8,10 @@ typedef struct {
ParsedObjFile object; ParsedObjFile object;
} ObjectData; } ObjectData;
/**
* Loads an object.
* Basically generates vbo and vao for an object and saving them in the ObjectData struct.
*/
void load_object(ObjectData* objectData) { void load_object(ObjectData* objectData) {
// write faces to buffer // write faces to buffer
//GLuint triangleVertexBufferObject; //GLuint triangleVertexBufferObject;
@ -72,7 +76,7 @@ void load_object(ObjectData* objectData) {
/** /**
* Takes a string-pointer and a number of files to read. * 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* readObjFiles(char** path, int numModels) {
ObjectData* objects = (ObjectData*) malloc(sizeof(ObjectData) * numModels); ObjectData* objects = (ObjectData*) malloc(sizeof(ObjectData) * numModels);
@ -90,6 +94,31 @@ ObjectData* readObjFiles(char** path, int numModels) {
return objects; 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) { void draw_object(ObjectData* objectData) {
glBindVertexArray(objectData->vao); glBindVertexArray(objectData->vao);
glDrawArrays(GL_TRIANGLES, 0, objectData->object.length * 3); // Annahme: Jedes face hat 3 vertices glDrawArrays(GL_TRIANGLES, 0, objectData->object.length * 3); // Annahme: Jedes face hat 3 vertices