Search code examples
c++glm-mathrotational-matrices

GLM: rotation matrix initialisation


The basic aim of my code is just to rotate a few points around the z-axis. However, after trying to initialise a rotation matrix using glm::rotate(m4, a, v3) and trying to check its components, a quick printf outputs some 1 800 000 000.0000000... for each element. I believe this is not the correct functionality.~

Note: I am not asking about the maths behind spatial transformations. They are clear to me.

Code in essence:

int main(int argc, char **argv){
    InitGL(); // openGL, GLFW..
    CreateShaders();            // vertShader: "mat4 trans"-uniform
    GenerateTextures();

    glClearColor(0.0f, 0.0f, 0.0f, 1.0f);

    uniTrans = glGetUniformLocation(shaderProgram, "trans");
    glm::mat4 trans;

    while(!glfwWindowShouldClose(window)){
        glClear(GL_COLOR_BUFFER_BIT);

        trans = glm::rotate(trans, glm::radians(180.0f), glm::vec3(0.0f, 0.0f, 1.0f));
        glUniformMatrix4fv(uniTrans, 1, GL_FALSE, glm::value_ptr(trans));

        glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);

        glfwSwapBuffers(window);
        glfwPollEvents();
    }

    CleanUp();
    glfwDestroyWindow(window);
    glfwTerminate();
    return 0;
}

Program works as intended when the rotation matrix isn't being used. The matrix values were checked with printf("%f", m[0][0]) and so on. Any idea as to where to even start with this? No errors or even warnings are thrown.


Solution

  • Self-answer:

    The matrices have to be initialised:

    glm::mat4 trans(1.0f);
    

    results to an identity matrix.