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 "vertexShader.c"
|
||||||
#include "fragmentShader.c"
|
#include "fragmentShader.c"
|
||||||
|
#include "objectHandler.c"
|
||||||
#include "matrixMath.h"
|
#include "matrixMath.h"
|
||||||
#include "transformation.h"
|
#include "transformation.h"
|
||||||
#include "wavefrontobj.h"
|
#include "wavefrontobj.h"
|
||||||
|
@ -19,8 +19,6 @@
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
|
|
||||||
GLuint program;
|
GLuint program;
|
||||||
GLuint vao[2];
|
|
||||||
int numObj = 0;
|
|
||||||
|
|
||||||
#define NUM_TEXTURES 5
|
#define NUM_TEXTURES 5
|
||||||
|
|
||||||
|
@ -41,7 +39,8 @@ char* textureFiles[NUM_TEXTURES] = {
|
||||||
"../texture/earth/normal.png"
|
"../texture/earth/normal.png"
|
||||||
};
|
};
|
||||||
|
|
||||||
int numFaces[2] = {0, 0};
|
ObjectData* objectData;
|
||||||
|
int numFaces[];
|
||||||
|
|
||||||
bool exitRequested = false;
|
bool exitRequested = false;
|
||||||
|
|
||||||
|
@ -121,70 +120,8 @@ void loadTexture(char* textureFile, GLuint* texture) {
|
||||||
stbi_image_free(image);
|
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) {
|
void init(void) {
|
||||||
// create and compile vertex shader
|
// create and compile vertex shader
|
||||||
const GLchar *vertexTextConst = vertexShader_glsl;
|
const GLchar *vertexTextConst = vertexShader_glsl;
|
||||||
|
@ -256,14 +193,9 @@ void init(void) {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// --------------- READ MODEL FILE
|
// --------------- READ MODEL FILES
|
||||||
ParsedObjFile object = readObjFile(model);
|
char** paths = {&defaultModel, &model};
|
||||||
ParsedObjFile object1 = readObjFile(defaultModel);
|
objectData = readObjFiles(paths, 2);
|
||||||
numFaces[0] = object.length;
|
|
||||||
numFaces[1] = object1.length;
|
|
||||||
|
|
||||||
load_object(&object);
|
|
||||||
load_object(&object1);
|
|
||||||
|
|
||||||
|
|
||||||
stbi_set_flip_vertically_on_load(flipFlag);
|
stbi_set_flip_vertically_on_load(flipFlag);
|
||||||
|
@ -416,10 +348,11 @@ void draw(void) {
|
||||||
|
|
||||||
|
|
||||||
// draw!!1
|
// draw!!1
|
||||||
glBindVertexArray(vao[0]);
|
for (int i = 0; i < numObj; i++)
|
||||||
glDrawArrays(GL_TRIANGLES, 0, numFaces[0] * 3);
|
{
|
||||||
glBindVertexArray(vao[1]);
|
glBindVertexArray(vao[i]);
|
||||||
glDrawArrays(GL_TRIANGLES, 0, numFaces[1] * 3);
|
glDrawArrays(GL_TRIANGLES, 0, objectData[i].numFaces * 3);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// change viewport size and adjust aspect ratio when changing window size
|
// 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