VAO and VBO are now stored in the corresponding ObjectData object, to allow for individual manipulation of objects in scene

This commit is contained in:
JonasJan2 2024-06-06 12:05:08 +02:00
parent 73be72585c
commit 3436a8b38a
2 changed files with 30 additions and 25 deletions

View File

@ -195,16 +195,17 @@ void init(void) {
// --------------- READ MODEL FILES // --------------- READ MODEL FILES
objectData = readObjFiles(&models, numModels);
/*
objectData = malloc(numModels * sizeof(ObjectData)); objectData = malloc(numModels * sizeof(ObjectData));
for (int i = 0; i < numModels; i++) { for (int i = 0; i < numModels; i++) {
ObjectData objData = readObjFiles(models, 1);
glGenVertexArrays(1, &objData.vao); glGenVertexArrays(1, &objData->vao);
glGenBuffers(1, &objData.vbo); glGenBuffers(1, &objData->vbo);
glBindVertexArray(objData.vao); glBindVertexArray(objData->vao);
glBindBuffer(GL_ARRAY_BUFFER, objData.vbo); glBindBuffer(GL_ARRAY_BUFFER, objData->vbo);
glBufferData(GL_ARRAY_BUFFER, objData.numFaces * 3 * sizeof(Vertex), objData.vertices, GL_STATIC_DRAW); 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); glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (GLvoid*)0);
glEnableVertexAttribArray(0); glEnableVertexAttribArray(0);
@ -217,6 +218,7 @@ void init(void) {
objectData[i] = objData; objectData[i] = objData;
} }
*/
stbi_set_flip_vertically_on_load(flipFlag); stbi_set_flip_vertically_on_load(flipFlag);
// -------------- READ TEXTURE FILES // -------------- READ TEXTURE FILES
@ -310,7 +312,8 @@ void draw(void) {
rotateX(&modelTransformation, &modelTransformation, stepi); rotateX(&modelTransformation, &modelTransformation, stepi);
} else if (i == 3) { } else if (i == 3) {
// Example transformations for the fourth object // 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; mat4 modelView;
@ -365,7 +368,7 @@ void draw(void) {
// draw each object separately // draw each object separately
glBindVertexArray(objectData[i].vao); glBindVertexArray(objectData[i].vao);
glDrawArrays(GL_TRIANGLES, 0, objectData[i].numFaces * 3); glDrawArrays(GL_TRIANGLES, 0, objectData[i].object.length * 3);
} }
} }

View File

@ -2,29 +2,25 @@
#include <GL/glew.h> #include <GL/glew.h>
#include <stdlib.h> #include <stdlib.h>
GLuint vao[2];
int numObj = 0;
typedef struct { typedef struct {
GLuint vao; GLuint vao;
GLuint vbo; GLuint vbo;
int numFaces;
ParsedObjFile object; ParsedObjFile object;
} ObjectData; } ObjectData;
void load_object(ParsedObjFile* object) { void load_object(ObjectData* objectData) {
// write faces to buffer // write faces to buffer
GLuint triangleVertexBufferObject; //GLuint triangleVertexBufferObject;
glGenBuffers(1, &triangleVertexBufferObject); glGenBuffers(1, &objectData->vbo);
glBindBuffer(GL_ARRAY_BUFFER, triangleVertexBufferObject); glBindBuffer(GL_ARRAY_BUFFER, objectData->vbo);
glBufferData(GL_ARRAY_BUFFER, object->length * sizeof(face), object->faces, GL_STATIC_DRAW); glBufferData(GL_ARRAY_BUFFER, objectData->object.length * sizeof(face), objectData->object.faces, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0); glBindBuffer(GL_ARRAY_BUFFER, 0);
// create vertex array object // create vertex array object
glGenVertexArrays(1, &vao[numObj]); glGenVertexArrays(1, &objectData->vao);
glBindVertexArray(vao[numObj]); glBindVertexArray(objectData->vao);
glBindBuffer(GL_ARRAY_BUFFER, triangleVertexBufferObject); glBindBuffer(GL_ARRAY_BUFFER, objectData->vao);
// vertex positions // vertex positions
glVertexAttribPointer( glVertexAttribPointer(
@ -72,10 +68,12 @@ void load_object(ParsedObjFile* object) {
glBindBuffer(GL_ARRAY_BUFFER, 0); glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(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* readObjFiles(char** path, int numModels) {
ObjectData* objects = (ObjectData*) malloc(sizeof(ObjectData) * 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) { for (int i = 0; i < numModels; ++i) {
objects[i].object = readObjFile(path[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]);
objects[i].vbo =
load_object(&objects[i].object);
} }
return objects; 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);
}