Search code examples
openglrotationglm-math

OpenGL Rotation with vertices not working


I am trying to make a rotation with shaders on vertices, here is the code of my shader :

"#version 150 core\n"
"in vec2 position;"
"in vec3 color;"
"out vec3 Color;"
"uniform mat4 rotation;"
"void main() {"
"   Color = color;"
"   gl_Position = rotation*vec4(position, 0.0, 2.0);"
"}";

I am using it with a quat, here is the code producing the matrice and dumping it in the shader :

glm::quat rotation(x,0.0,0.0,0.5); 
x+=0.001;
ctm = glm::mat4_cast(rotation);
    GLint matrix_loc;

// get from shader pointer to global data
    matrix_loc = glGetUniformLocation(shaderProgram, "rotation");
    if (matrix_loc == -1)
        std::cout << "pointer for rotation of shader not found" << matrix_loc <<  std::endl;
// put local data in shader :
    glUniformMatrix4fv(matrix_loc, 1, GL_FALSE, glm::value_ptr(ctm));

But when it rotates, the object gets bigger and bigger, I know i don't need to GetUniformLocation every time i iterate in my loop but this is the code for a test. GlUniformMatrix is supposed to make the rotation happen as far as I know. After these calls i simply draw my vertex array.


Solution

  • Given its still drawing, rotation in the shader is probably a valid matrix. If it were an issue with the uniform it'd probably be all zeroes and nothing would draw.

    As @genpfault says, ctm needs to be initialized:

    ctm = glm::mat4_cast(rotation);
    

    See: Converting glm quaternion to rotation matrix and using it with opengl

    Also, shouldn't the 2.0 in vec4(position, 0.0, 2.0) be a 1.0?