new branch for mult obj testing
This commit is contained in:
parent
588eb327c9
commit
2e460aef60
162
src/main.c
162
src/main.c
|
@ -19,7 +19,8 @@
|
|||
#include <stdbool.h>
|
||||
|
||||
GLuint program;
|
||||
GLuint vao;
|
||||
GLuint vao[2];
|
||||
int numObj = 0;
|
||||
|
||||
#define NUM_TEXTURES 5
|
||||
|
||||
|
@ -40,7 +41,7 @@ char* textureFiles[NUM_TEXTURES] = {
|
|||
"../texture/earth/normal.png"
|
||||
};
|
||||
|
||||
int numFaces = 0;
|
||||
int numFaces[2] = {0, 0};
|
||||
|
||||
bool exitRequested = false;
|
||||
|
||||
|
@ -58,8 +59,8 @@ const GLfloat pi = 3.14159f;
|
|||
|
||||
vec3 cameraPosition = {0.0f, 3.0f, 5.5f};
|
||||
|
||||
char* defaultModel = "../obj/monkey.obj";
|
||||
char* model;
|
||||
char* defaultModel = "../obj/plane.obj";
|
||||
char* model = "../obj/earth.obj";
|
||||
|
||||
// input handler for camera movement
|
||||
void handleInputs(double deltaTime) {
|
||||
|
@ -120,6 +121,77 @@ 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);
|
||||
|
||||
|
||||
stbi_set_flip_vertically_on_load(flipFlag);
|
||||
// -------------- READ TEXTURE FILES
|
||||
for (int i = 0; i < NUM_TEXTURES; i++) {
|
||||
loadTexture(textureFiles[i], &textures[i]);
|
||||
}
|
||||
|
||||
|
||||
// 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;
|
||||
|
@ -192,76 +264,13 @@ void init(void) {
|
|||
|
||||
|
||||
// --------------- READ MODEL FILE
|
||||
ParsedObjFile teapot = readObjFile(model);
|
||||
numFaces = teapot.length;
|
||||
|
||||
// write faces to buffer
|
||||
GLuint triangleVertexBufferObject;
|
||||
glGenBuffers(1, &triangleVertexBufferObject);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, triangleVertexBufferObject);
|
||||
glBufferData(GL_ARRAY_BUFFER, teapot.length * sizeof(face), teapot.faces, GL_STATIC_DRAW);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
||||
|
||||
|
||||
stbi_set_flip_vertically_on_load(flipFlag);
|
||||
// -------------- READ TEXTURE FILES
|
||||
for (int i = 0; i < NUM_TEXTURES; i++) {
|
||||
loadTexture(textureFiles[i], &textures[i]);
|
||||
}
|
||||
|
||||
|
||||
// create vertex array object
|
||||
glGenVertexArrays(1, &vao);
|
||||
glBindVertexArray(vao);
|
||||
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);
|
||||
ParsedObjFile object = readObjFile(model);
|
||||
ParsedObjFile object1 = readObjFile(defaultModel);
|
||||
numFaces[0] = object.length;
|
||||
numFaces[1] = object1.length;
|
||||
|
||||
load_object(&object1);
|
||||
load_object(&object);
|
||||
|
||||
// ENABLE BACKFACE CULLING
|
||||
glFrontFace(GL_CCW);
|
||||
|
@ -298,7 +307,7 @@ void draw(void) {
|
|||
|
||||
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
||||
glUseProgram(program);
|
||||
glBindVertexArray(vao);
|
||||
|
||||
|
||||
// step for rotations
|
||||
// counts up to 1.0 and then resets back to 0.0 forever
|
||||
|
@ -407,7 +416,10 @@ void draw(void) {
|
|||
|
||||
|
||||
// draw!!1
|
||||
glDrawArrays(GL_TRIANGLES, 0, numFaces * 3);
|
||||
glBindVertexArray(vao[0]);
|
||||
glDrawArrays(GL_TRIANGLES, 0, numFaces[0] * 3);
|
||||
glBindVertexArray(vao[1]);
|
||||
glDrawArrays(GL_TRIANGLES, 0, numFaces[1] * 3);
|
||||
}
|
||||
|
||||
// change viewport size and adjust aspect ratio when changing window size
|
||||
|
|
Loading…
Reference in New Issue