95 lines
2.0 KiB
C
95 lines
2.0 KiB
C
#include "wavefrontobj.h"
|
|
#include <GL/glew.h>
|
|
#include <stdlib.h>
|
|
|
|
GLuint vao[2];
|
|
int numObj = 0;
|
|
|
|
typedef struct {
|
|
GLuint vao;
|
|
GLuint vbo;
|
|
int numFaces;
|
|
ParsedObjFile object;
|
|
} 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
|
|
objects[i].vbo =
|
|
load_object(&objects[i].object);
|
|
}
|
|
|
|
return objects;
|
|
} |