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);
|
||||
}
|
||||
|
||||
|
||||
|
||||
void init(void) {
|
||||
// create and compile vertex shader
|
||||
const GLchar *vertexTextConst = vertexShader_glsl;
|
||||
|
@ -148,8 +146,6 @@ void init(void) {
|
|||
|
||||
vertexTextConst = NULL;
|
||||
|
||||
|
||||
|
||||
// create and compile fragment shader
|
||||
|
||||
const GLchar *fragmentTextConst = fragmentShader_glsl;
|
||||
|
@ -199,8 +195,28 @@ void init(void) {
|
|||
|
||||
// --------------- 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);
|
||||
// -------------- READ TEXTURE FILES
|
||||
|
@ -244,7 +260,6 @@ void draw(void) {
|
|||
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
||||
glUseProgram(program);
|
||||
|
||||
|
||||
// step for rotations
|
||||
// counts up to 1.0 and then resets back to 0.0 forever
|
||||
step += deltaTime / 15;
|
||||
|
@ -254,16 +269,6 @@ void draw(void) {
|
|||
// step multiplied by pi * 2 for use in rotation and trig functions
|
||||
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 -------------------
|
||||
vec3 origin = {0.0f, 0.0f, 0.0f};
|
||||
vec3 up = {0.0f, 1.0f, 0.0f};
|
||||
|
@ -271,32 +276,48 @@ void draw(void) {
|
|||
mat4 viewingTransformation;
|
||||
lookAt(&viewingTransformation, &cameraPosition, &origin, &up);
|
||||
|
||||
|
||||
|
||||
// -------------- PROJECTION TRANSFORMATION ----------------
|
||||
mat4 projectionTransformation;
|
||||
GLfloat near = 0.1f;
|
||||
GLfloat far = 20.0f;
|
||||
perspectiveProjection(&projectionTransformation, near, far);
|
||||
|
||||
|
||||
|
||||
// -------------- NORMALISATION TRANSFORMATION -------------
|
||||
mat4 normalisationTransformation;
|
||||
GLfloat fovy = pi / 2;
|
||||
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;
|
||||
identity(&modelView);
|
||||
multiply(&modelView, &modelTransformation, &modelView);
|
||||
multiply(&modelView, &viewingTransformation, &modelView);
|
||||
|
||||
mat4 projection;
|
||||
identity(&projection);
|
||||
multiply(&projection, &projectionTransformation, &projection);
|
||||
multiply(&projection, &normalisationTransformation, &projection);
|
||||
|
||||
// calculate matrix for normals
|
||||
mat3 normalModelView;
|
||||
mat3From4(&normalModelView, &modelView);
|
||||
|
@ -308,13 +329,6 @@ void draw(void) {
|
|||
glUniformMatrix3fv(glGetUniformLocation(program, "normalModelView"), 1, GL_FALSE, (GLfloat*)&normalModelView);
|
||||
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
|
||||
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, "ambientLight"), 0.05f, 0.05f, 0.05f, 1.0f);
|
||||
|
||||
|
||||
// BIND TEXTURES
|
||||
GLuint textureLocation;
|
||||
textureLocation = glGetUniformLocation(program, "day");
|
||||
|
@ -350,11 +363,8 @@ void draw(void) {
|
|||
glActiveTexture(GL_TEXTURE4);
|
||||
glBindTexture(GL_TEXTURE_2D, textures[NORMAL]);
|
||||
|
||||
|
||||
// draw!!1
|
||||
for (int i = 0; i < numObj; i++)
|
||||
{
|
||||
glBindVertexArray(vao[i]);
|
||||
// draw each object separately
|
||||
glBindVertexArray(objectData[i].vao);
|
||||
glDrawArrays(GL_TRIANGLES, 0, objectData[i].numFaces * 3);
|
||||
}
|
||||
}
|
||||
|
@ -380,7 +390,6 @@ int main(int argc, char **argv) {
|
|||
return -1;
|
||||
}
|
||||
|
||||
|
||||
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
|
||||
glfwMakeContextCurrent(window);
|
||||
|
||||
|
|
|
@ -5,10 +5,11 @@
|
|||
GLuint vao[2];
|
||||
int numObj = 0;
|
||||
|
||||
typedef struct ObjectData
|
||||
{
|
||||
ParsedObjFile object;
|
||||
typedef struct {
|
||||
GLuint vao;
|
||||
GLuint vbo;
|
||||
int numFaces;
|
||||
ParsedObjFile object;
|
||||
} ObjectData;
|
||||
|
||||
void load_object(ParsedObjFile* object) {
|
||||
|
@ -86,6 +87,7 @@ ObjectData* readObjFiles(char** path, int numModels) {
|
|||
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
|
||||
objects[i].vbo =
|
||||
load_object(&objects[i].object);
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue