Added a few comments and a function to load a single object multiple times.
This commit is contained in:
parent
32afa76813
commit
40af75ad94
44
src/main.c
44
src/main.c
|
@ -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) {
|
|
||||||
// Example transformations for the first object
|
|
||||||
rotateY(&modelTransformation, &modelTransformation, stepi * 2);
|
|
||||||
} else if (i == 1) {
|
|
||||||
// Example transformations for the second object
|
|
||||||
rotateZ(&modelTransformation, &modelTransformation, -23.5f / 180 * pi);
|
|
||||||
} 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 = {
|
vec3 v = {
|
||||||
objectPosition.x + cos(stepi) * 1.0f,
|
(GLfloat)i*2+objectPosition.x,
|
||||||
objectPosition.y + sin(stepi) * 1.0f,
|
0.0f+objectPosition.y,
|
||||||
objectPosition.z
|
0.0f+objectPosition.z
|
||||||
};
|
};
|
||||||
translate(&modelTransformation, &modelTransformation, &v);
|
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();
|
||||||
|
|
|
@ -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
|
||||||
|
|
Loading…
Reference in New Issue