WIP objecthandler stuff
This commit is contained in:
parent
4aca5eead4
commit
73be72585c
87
src/main.c
87
src/main.c
|
@ -124,8 +124,6 @@ void loadTexture(char* textureFile, GLuint* texture) {
|
||||||
stbi_image_free(image);
|
stbi_image_free(image);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
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;
|
||||||
|
@ -148,8 +146,6 @@ void init(void) {
|
||||||
|
|
||||||
vertexTextConst = NULL;
|
vertexTextConst = NULL;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// create and compile fragment shader
|
// create and compile fragment shader
|
||||||
|
|
||||||
const GLchar *fragmentTextConst = fragmentShader_glsl;
|
const GLchar *fragmentTextConst = fragmentShader_glsl;
|
||||||
|
@ -199,8 +195,28 @@ void init(void) {
|
||||||
|
|
||||||
// --------------- READ MODEL FILES
|
// --------------- READ MODEL FILES
|
||||||
|
|
||||||
objectData = readObjFiles(models, numModels);
|
objectData = malloc(numModels * sizeof(ObjectData));
|
||||||
|
for (int i = 0; i < numModels; i++) {
|
||||||
|
ObjectData objData = readObjFiles(models, 1);
|
||||||
|
|
||||||
|
glGenVertexArrays(1, &objData.vao);
|
||||||
|
glGenBuffers(1, &objData.vbo);
|
||||||
|
|
||||||
|
glBindVertexArray(objData.vao);
|
||||||
|
glBindBuffer(GL_ARRAY_BUFFER, objData.vbo);
|
||||||
|
glBufferData(GL_ARRAY_BUFFER, objData.numFaces * 3 * sizeof(Vertex), objData.vertices, GL_STATIC_DRAW);
|
||||||
|
|
||||||
|
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (GLvoid*)0);
|
||||||
|
glEnableVertexAttribArray(0);
|
||||||
|
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (GLvoid*)(3 * sizeof(GLfloat)));
|
||||||
|
glEnableVertexAttribArray(1);
|
||||||
|
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (GLvoid*)(6 * sizeof(GLfloat)));
|
||||||
|
glEnableVertexAttribArray(2);
|
||||||
|
|
||||||
|
glBindVertexArray(0);
|
||||||
|
|
||||||
|
objectData[i] = objData;
|
||||||
|
}
|
||||||
|
|
||||||
stbi_set_flip_vertically_on_load(flipFlag);
|
stbi_set_flip_vertically_on_load(flipFlag);
|
||||||
// -------------- READ TEXTURE FILES
|
// -------------- READ TEXTURE FILES
|
||||||
|
@ -244,7 +260,6 @@ 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);
|
||||||
|
|
||||||
|
|
||||||
// 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
|
||||||
step += deltaTime / 15;
|
step += deltaTime / 15;
|
||||||
|
@ -254,16 +269,6 @@ void draw(void) {
|
||||||
// step multiplied by pi * 2 for use in rotation and trig functions
|
// step multiplied by pi * 2 for use in rotation and trig functions
|
||||||
GLfloat stepi = step * pi * 2;
|
GLfloat stepi = step * pi * 2;
|
||||||
|
|
||||||
|
|
||||||
// ------------- MODEL TRANSFORMATION ---------------------
|
|
||||||
// SCALE -> ROTATE -> TRANSLATE
|
|
||||||
|
|
||||||
mat4 modelTransformation;
|
|
||||||
identity(&modelTransformation);
|
|
||||||
|
|
||||||
rotateY(&modelTransformation, &modelTransformation, stepi * 2);
|
|
||||||
rotateZ(&modelTransformation, &modelTransformation, -23.5f / 180 * pi);
|
|
||||||
|
|
||||||
// ------------- VIEWING TRANSFORMATION -------------------
|
// ------------- VIEWING TRANSFORMATION -------------------
|
||||||
vec3 origin = {0.0f, 0.0f, 0.0f};
|
vec3 origin = {0.0f, 0.0f, 0.0f};
|
||||||
vec3 up = {0.0f, 1.0f, 0.0f};
|
vec3 up = {0.0f, 1.0f, 0.0f};
|
||||||
|
@ -271,32 +276,48 @@ void draw(void) {
|
||||||
mat4 viewingTransformation;
|
mat4 viewingTransformation;
|
||||||
lookAt(&viewingTransformation, &cameraPosition, &origin, &up);
|
lookAt(&viewingTransformation, &cameraPosition, &origin, &up);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// -------------- PROJECTION TRANSFORMATION ----------------
|
// -------------- PROJECTION TRANSFORMATION ----------------
|
||||||
mat4 projectionTransformation;
|
mat4 projectionTransformation;
|
||||||
GLfloat near = 0.1f;
|
GLfloat near = 0.1f;
|
||||||
GLfloat far = 20.0f;
|
GLfloat far = 20.0f;
|
||||||
perspectiveProjection(&projectionTransformation, near, far);
|
perspectiveProjection(&projectionTransformation, near, far);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// -------------- NORMALISATION TRANSFORMATION -------------
|
// -------------- NORMALISATION TRANSFORMATION -------------
|
||||||
mat4 normalisationTransformation;
|
mat4 normalisationTransformation;
|
||||||
GLfloat fovy = pi / 2;
|
GLfloat fovy = pi / 2;
|
||||||
normalisedDeviceCoordinatesFov(&normalisationTransformation, fovy, aspectRatio, near, far);
|
normalisedDeviceCoordinatesFov(&normalisationTransformation, fovy, aspectRatio, near, far);
|
||||||
|
|
||||||
|
mat4 projection;
|
||||||
|
identity(&projection);
|
||||||
|
multiply(&projection, &projectionTransformation, &projection);
|
||||||
|
multiply(&projection, &normalisationTransformation, &projection);
|
||||||
|
|
||||||
|
// ------------- DRAW EACH OBJECT SEPARATELY ----------------
|
||||||
|
for (int i = 0; i < numModels; i++) {
|
||||||
|
// Reset model transformation for each object
|
||||||
|
mat4 modelTransformation;
|
||||||
|
identity(&modelTransformation);
|
||||||
|
|
||||||
|
// 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
|
||||||
|
translate(&modelTransformation, &modelTransformation, 1.0f, 0.0f, 0.0f);
|
||||||
|
}
|
||||||
|
|
||||||
mat4 modelView;
|
mat4 modelView;
|
||||||
identity(&modelView);
|
identity(&modelView);
|
||||||
multiply(&modelView, &modelTransformation, &modelView);
|
multiply(&modelView, &modelTransformation, &modelView);
|
||||||
multiply(&modelView, &viewingTransformation, &modelView);
|
multiply(&modelView, &viewingTransformation, &modelView);
|
||||||
|
|
||||||
mat4 projection;
|
|
||||||
identity(&projection);
|
|
||||||
multiply(&projection, &projectionTransformation, &projection);
|
|
||||||
multiply(&projection, &normalisationTransformation, &projection);
|
|
||||||
|
|
||||||
// calculate matrix for normals
|
// calculate matrix for normals
|
||||||
mat3 normalModelView;
|
mat3 normalModelView;
|
||||||
mat3From4(&normalModelView, &modelView);
|
mat3From4(&normalModelView, &modelView);
|
||||||
|
@ -308,13 +329,6 @@ void draw(void) {
|
||||||
glUniformMatrix3fv(glGetUniformLocation(program, "normalModelView"), 1, GL_FALSE, (GLfloat*)&normalModelView);
|
glUniformMatrix3fv(glGetUniformLocation(program, "normalModelView"), 1, GL_FALSE, (GLfloat*)&normalModelView);
|
||||||
glUniformMatrix4fv(glGetUniformLocation(program, "projection"), 1, GL_FALSE, (GLfloat*)&projection);
|
glUniformMatrix4fv(glGetUniformLocation(program, "projection"), 1, GL_FALSE, (GLfloat*)&projection);
|
||||||
|
|
||||||
|
|
||||||
//vec4 lightPosition = {cos(stepi) * 1000.0f, 0.0f, sin(stepi) * 1000.0f, 1.0f};
|
|
||||||
//multiplyAny((GLfloat *)&lightPosition, (GLfloat *)&viewingTransformation, (GLfloat *)&lightPosition, 4, 4, 1);
|
|
||||||
|
|
||||||
//glUniform3f(glGetUniformLocation(program, "lightPosition"), lightPosition.x, lightPosition.y, lightPosition.z);
|
|
||||||
|
|
||||||
|
|
||||||
// SET MATERIAL DATA
|
// SET MATERIAL DATA
|
||||||
glUniform1f(glGetUniformLocation(program, "shininess"), 60.0f * 4.0f);
|
glUniform1f(glGetUniformLocation(program, "shininess"), 60.0f * 4.0f);
|
||||||
|
|
||||||
|
@ -322,7 +336,6 @@ void draw(void) {
|
||||||
glUniform4f(glGetUniformLocation(program, "lightColor"), 1.0f, 1.0f, 1.0f, 1.0f);
|
glUniform4f(glGetUniformLocation(program, "lightColor"), 1.0f, 1.0f, 1.0f, 1.0f);
|
||||||
glUniform4f(glGetUniformLocation(program, "ambientLight"), 0.05f, 0.05f, 0.05f, 1.0f);
|
glUniform4f(glGetUniformLocation(program, "ambientLight"), 0.05f, 0.05f, 0.05f, 1.0f);
|
||||||
|
|
||||||
|
|
||||||
// BIND TEXTURES
|
// BIND TEXTURES
|
||||||
GLuint textureLocation;
|
GLuint textureLocation;
|
||||||
textureLocation = glGetUniformLocation(program, "day");
|
textureLocation = glGetUniformLocation(program, "day");
|
||||||
|
@ -350,11 +363,8 @@ void draw(void) {
|
||||||
glActiveTexture(GL_TEXTURE4);
|
glActiveTexture(GL_TEXTURE4);
|
||||||
glBindTexture(GL_TEXTURE_2D, textures[NORMAL]);
|
glBindTexture(GL_TEXTURE_2D, textures[NORMAL]);
|
||||||
|
|
||||||
|
// draw each object separately
|
||||||
// draw!!1
|
glBindVertexArray(objectData[i].vao);
|
||||||
for (int i = 0; i < numObj; i++)
|
|
||||||
{
|
|
||||||
glBindVertexArray(vao[i]);
|
|
||||||
glDrawArrays(GL_TRIANGLES, 0, objectData[i].numFaces * 3);
|
glDrawArrays(GL_TRIANGLES, 0, objectData[i].numFaces * 3);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -380,7 +390,6 @@ int main(int argc, char **argv) {
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
|
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
|
||||||
glfwMakeContextCurrent(window);
|
glfwMakeContextCurrent(window);
|
||||||
|
|
||||||
|
|
|
@ -5,10 +5,11 @@
|
||||||
GLuint vao[2];
|
GLuint vao[2];
|
||||||
int numObj = 0;
|
int numObj = 0;
|
||||||
|
|
||||||
typedef struct ObjectData
|
typedef struct {
|
||||||
{
|
GLuint vao;
|
||||||
ParsedObjFile object;
|
GLuint vbo;
|
||||||
int numFaces;
|
int numFaces;
|
||||||
|
ParsedObjFile object;
|
||||||
} ObjectData;
|
} ObjectData;
|
||||||
|
|
||||||
void load_object(ParsedObjFile* object) {
|
void load_object(ParsedObjFile* object) {
|
||||||
|
@ -86,6 +87,7 @@ ObjectData* readObjFiles(char** path, int numModels) {
|
||||||
for (int i = 0; i < numModels; ++i) {
|
for (int i = 0; i < numModels; ++i) {
|
||||||
objects[i].object = readObjFile(path[i]);
|
objects[i].object = readObjFile(path[i]);
|
||||||
objects[i].numFaces = objects[i].object.length; // Assuming .length gives the number of faces
|
objects[i].numFaces = objects[i].object.length; // Assuming .length gives the number of faces
|
||||||
|
objects[i].vbo =
|
||||||
load_object(&objects[i].object);
|
load_object(&objects[i].object);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue