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>
|
#include <stdbool.h>
|
||||||
|
|
||||||
GLuint program;
|
GLuint program;
|
||||||
GLuint vao;
|
GLuint vao[2];
|
||||||
|
int numObj = 0;
|
||||||
|
|
||||||
#define NUM_TEXTURES 5
|
#define NUM_TEXTURES 5
|
||||||
|
|
||||||
|
@ -40,7 +41,7 @@ char* textureFiles[NUM_TEXTURES] = {
|
||||||
"../texture/earth/normal.png"
|
"../texture/earth/normal.png"
|
||||||
};
|
};
|
||||||
|
|
||||||
int numFaces = 0;
|
int numFaces[2] = {0, 0};
|
||||||
|
|
||||||
bool exitRequested = false;
|
bool exitRequested = false;
|
||||||
|
|
||||||
|
@ -58,8 +59,8 @@ const GLfloat pi = 3.14159f;
|
||||||
|
|
||||||
vec3 cameraPosition = {0.0f, 3.0f, 5.5f};
|
vec3 cameraPosition = {0.0f, 3.0f, 5.5f};
|
||||||
|
|
||||||
char* defaultModel = "../obj/monkey.obj";
|
char* defaultModel = "../obj/plane.obj";
|
||||||
char* model;
|
char* model = "../obj/earth.obj";
|
||||||
|
|
||||||
// input handler for camera movement
|
// input handler for camera movement
|
||||||
void handleInputs(double deltaTime) {
|
void handleInputs(double deltaTime) {
|
||||||
|
@ -120,6 +121,77 @@ 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);
|
||||||
|
|
||||||
|
|
||||||
|
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) {
|
void init(void) {
|
||||||
// create and compile vertex shader
|
// create and compile vertex shader
|
||||||
const GLchar *vertexTextConst = vertexShader_glsl;
|
const GLchar *vertexTextConst = vertexShader_glsl;
|
||||||
|
@ -192,76 +264,13 @@ void init(void) {
|
||||||
|
|
||||||
|
|
||||||
// --------------- READ MODEL FILE
|
// --------------- READ MODEL FILE
|
||||||
ParsedObjFile teapot = readObjFile(model);
|
ParsedObjFile object = readObjFile(model);
|
||||||
numFaces = teapot.length;
|
ParsedObjFile object1 = readObjFile(defaultModel);
|
||||||
|
numFaces[0] = object.length;
|
||||||
// write faces to buffer
|
numFaces[1] = object1.length;
|
||||||
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);
|
|
||||||
|
|
||||||
|
load_object(&object1);
|
||||||
|
load_object(&object);
|
||||||
|
|
||||||
// ENABLE BACKFACE CULLING
|
// ENABLE BACKFACE CULLING
|
||||||
glFrontFace(GL_CCW);
|
glFrontFace(GL_CCW);
|
||||||
|
@ -298,7 +307,7 @@ void draw(void) {
|
||||||
|
|
||||||
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
||||||
glUseProgram(program);
|
glUseProgram(program);
|
||||||
glBindVertexArray(vao);
|
|
||||||
|
|
||||||
// step for rotations
|
// step for rotations
|
||||||
// counts up to 1.0 and then resets back to 0.0 forever
|
// counts up to 1.0 and then resets back to 0.0 forever
|
||||||
|
@ -407,7 +416,10 @@ void draw(void) {
|
||||||
|
|
||||||
|
|
||||||
// draw!!1
|
// 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
|
// change viewport size and adjust aspect ratio when changing window size
|
||||||
|
|
Loading…
Reference in New Issue