Search code examples
copenglglm-math

Object not rendering after adding the third dimension and z axis rotation


I'm adding 3D and some rotation on the z axis to my program which uses OpenGL, CGLM, STB, GLFW and GLAD. The program has no warnings or errors, and still the program's output is simply my clear colour.

The following is the part of my program loop where I apply transformations.

// Create transformations
mat4 model      = {{1.0f}};
mat4 view       = {{1.0f}};
mat4 projection = {{1.0f}};

glm_rotate(model, glm_rad(-55.0f), (vec3){1.0f, 0.0f, 0.0f});

// Translating the scene in the reverse direction of where the user wants to move
glm_translate(view, (vec3){0.0f, 0.0f, -3.0f});
glm_perspective(glm_rad(45.0f), (float)WINDOW_WIDTH / (float)WINDOW_HEIGHT, 0.1f, 100.0f, projection);

// Retrieve the matrix uniform locations and pass them to the shaders
GLint modelLoc = glGetUniformLocation(myShaderPtr->shaderID, "model");
GLint viewLoc = glGetUniformLocation(myShaderPtr->shaderID, "view");
GLint projectionLoc = glGetUniformLocation(myShaderPtr->shaderID, "projection");

glUniformMatrix4fv(modelLoc, 1, GL_FALSE, &model[0][0]);
glUniformMatrix4fv(viewLoc, 1, GL_FALSE, &view[0][0]);
glUniformMatrix4fv(projectionLoc, 1, GL_FALSE, &projection[0][0]);

And this is my vertex shader:

#version 330 core

layout (location = 0) in vec3 aPos;
layout (location = 1) in vec2 aTexCoord;

out vec2 TexCoord;

uniform mat4 model;
uniform mat4 view;
uniform mat4 projection;

void main() {
    gl_Position = projection * view * model * vec4(aPos, 1.0);
    TexCoord = vec2(aTexCoord.x, aTexCoord.y);
}

The program is up on github here if more code is required.

The intended output of the code is the image below but with my profile picture instead of the smiley face.

Intended output


Solution

  • mat4 transform = {{1.0f}}; does not do what you expect. C doesn't have a constructor like C++. The C++ version's constructor initialized the matrix with the Identity matrix. You have to use glm_mat4_identity to initialize with the identity matrix:

    mat4 model, view, projection;
    glm_mat4_identity(model);
    glm_mat4_identity(view);
    glm_mat4_identity(projection);
    

    Notice that the identity matrix has ones on the main diagonal and zeros elsewhere:

    {{1.0f, 0.0f, 0.0f, 0.0f},
     {0.0f, 1.0f, 0.0f, 0.0f},
     {0.0f, 0.0f, 1.0f, 0.0f},
     {0.0f, 0.0f, 0.0f, 1.0f}}