new struct for object data and refactoring of reading object files
This commit is contained in:
parent
5d9ccd1ef1
commit
cde188de1e
89
src/main.c
89
src/main.c
|
@ -5,7 +5,7 @@
|
|||
|
||||
#include "vertexShader.c"
|
||||
#include "fragmentShader.c"
|
||||
|
||||
#include "objectHandler.c"
|
||||
#include "matrixMath.h"
|
||||
#include "transformation.h"
|
||||
#include "wavefrontobj.h"
|
||||
|
@ -19,8 +19,6 @@
|
|||
#include <stdbool.h>
|
||||
|
||||
GLuint program;
|
||||
GLuint vao[2];
|
||||
int numObj = 0;
|
||||
|
||||
#define NUM_TEXTURES 5
|
||||
|
||||
|
@ -41,7 +39,8 @@ char* textureFiles[NUM_TEXTURES] = {
|
|||
"../texture/earth/normal.png"
|
||||
};
|
||||
|
||||
int numFaces[2] = {0, 0};
|
||||
ObjectData* objectData;
|
||||
int numFaces[];
|
||||
|
||||
bool exitRequested = false;
|
||||
|
||||
|
@ -121,70 +120,8 @@ void loadTexture(char* textureFile, GLuint* texture) {
|
|||
stbi_image_free(image);
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
void init(void) {
|
||||
// create and compile vertex shader
|
||||
const GLchar *vertexTextConst = vertexShader_glsl;
|
||||
|
@ -256,14 +193,9 @@ void init(void) {
|
|||
}
|
||||
|
||||
|
||||
// --------------- READ MODEL FILE
|
||||
ParsedObjFile object = readObjFile(model);
|
||||
ParsedObjFile object1 = readObjFile(defaultModel);
|
||||
numFaces[0] = object.length;
|
||||
numFaces[1] = object1.length;
|
||||
|
||||
load_object(&object);
|
||||
load_object(&object1);
|
||||
// --------------- READ MODEL FILES
|
||||
char** paths = {&defaultModel, &model};
|
||||
objectData = readObjFiles(paths, 2);
|
||||
|
||||
|
||||
stbi_set_flip_vertically_on_load(flipFlag);
|
||||
|
@ -416,10 +348,11 @@ void draw(void) {
|
|||
|
||||
|
||||
// draw!!1
|
||||
glBindVertexArray(vao[0]);
|
||||
glDrawArrays(GL_TRIANGLES, 0, numFaces[0] * 3);
|
||||
glBindVertexArray(vao[1]);
|
||||
glDrawArrays(GL_TRIANGLES, 0, numFaces[1] * 3);
|
||||
for (int i = 0; i < numObj; i++)
|
||||
{
|
||||
glBindVertexArray(vao[i]);
|
||||
glDrawArrays(GL_TRIANGLES, 0, objectData[i].numFaces * 3);
|
||||
}
|
||||
}
|
||||
|
||||
// change viewport size and adjust aspect ratio when changing window size
|
||||
|
|
|
@ -0,0 +1,93 @@
|
|||
#include "wavefrontobj.h"
|
||||
#include <GL/glew.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
GLuint vao[2];
|
||||
int numObj = 0;
|
||||
|
||||
typedef struct ObjectData
|
||||
{
|
||||
ParsedObjFile object;
|
||||
int numFaces;
|
||||
} 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
|
||||
load_object(&objects[i].object);
|
||||
}
|
||||
|
||||
return objects;
|
||||
}
|
Loading…
Reference in New Issue