Search code examples
clinuxopenglglm-math

Object not rendering after adding transformations


I'm adding transformations to my C OpenGL program. I'm using CGLM as my maths library. The program has no warnings or errors. Still however, when I compile and run the program, I simply get a window coloured my clear colour. The following is my program's main loop

// Initialize variables for framerate counting
double lastTime = glfwGetTime();
int frameCount = 0;

// Program loop
while (!glfwWindowShouldClose(window)) {
    // Calculate framerate
    double thisTime = glfwGetTime();
    frameCount++;

    // If a second has passed.
    if (thisTime - lastTime >= 1.0) {
        printf("%i FPS\n", frameCount);

        frameCount = 0;
        lastTime = thisTime;
    }

    processInput(window);

    // Clear the window
    glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
    glClear(GL_COLOR_BUFFER_BIT);
    
    // Bind textures on texture units
    glActiveTexture(GL_TEXTURE0);
    glBindTexture(GL_TEXTURE_2D, texture);
    glActiveTexture(GL_TEXTURE1);
    glBindTexture(GL_TEXTURE_2D, texture2);

    // Create transformations
    mat4 transform = {{1.0f}};
    glm_translate(transform, (vec3){0.5f, -0.5f, 0.0f});
    glm_rotate(transform, (float)glfwGetTime(), (vec3){0.0f, 0.0f, 1.0f});

    printf("%i\n", transform);

    // Get matrix's uniform location and set matrix
    shaderUse(myShaderPtr);
    GLint transformLoc = glGetUniformLocation(myShaderPtr->shaderID, "transform");
    printf("%i\n", transformLoc);
    glUniformMatrix4fv(transformLoc, 1, GL_FALSE, *transform);

    glBindVertexArray(VAO);
    glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);

    glfwSwapBuffers(window); // Swap the front and back buffers
    glfwPollEvents(); // Check for events (mouse movement, mouse click, keyboard press, keyboard release etc.)
}

The Program is up on github here if you'd like to check out the full code.

The output of this program is Image of solid blue, only showing the background

However, the intended output is a spinning box with my profile picture on it.


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 transform;
    glm_mat4_identity(transform);
    glm_rotate(transform, (float)glfwGetTime(), (vec3){0.0f, 0.0f, 1.0f});
    
    glUniformMatrix4fv(transformLoc, 1, GL_FALSE, (float*)transform);
    

    Additionally, you need to specify and add an orthographic projection matrix that compensates for the aspect ratio of the viewport:

    float aspect = (float)width / (float)height;
    mat4 projection;
    glm_ortho(-aspect, aspect, -1.0f, 1.0f, -1.0f, 1.0f, projection)
    
    mat4 transform;
    glm_rotate(transform, (float)glfwGetTime(), (vec3){0.0f, 0.0f, 1.0f});
    glm_mat4_identity(transform);
    
    mat4 mvp;
    glm_mat4_mul(projection, transform, mvp);
    
    GLint transformLoc = glGetUniformLocation(myShaderPtr->shaderID, "transform");
    glUniformMatrix4fv(transformLoc, 1, GL_FALSE, (float*)mvp);