Modelview translation with GLM


Hope this is the right place to ask this kind of question.

I’m doing an exercise where I created a cube out of several quads. I then created a projection matrix (according to the exercise description) looking like this:

  float n = 1.0, f = 10.0;	// near and far clipping planes
  float a = (f + n) / (f - n);	
  float b = 2 * f * n / (f - n);

  GLfloat projectionMatrix[4][4] = {
	  {1.0, 0.0, 0.0, 0.0},
	  {0.0, 1.0, 0.0, 0.0},
	  {0.0, 0.0,  -a,  -b},
	  {0.0, 0.0,-1.0, 0.0}}; 

I added a modelview matrix, rotating the cube and translating it along the z-axis:

     GLfloat modelviewMatrix[4][4] = {
	  {cos(time) , 0.0, sin(time),  0.0},
	  {0.0       , 1.0, 0.0      ,  0.0},
	  {-sin(time), 0.0, cos(time), -4.0},
	  {0.0       , 0.0, 0.0      ,  1.0}}; 

I sent both the projection and the modelview matrices to the vertex shader where I multiply them with the vertice positions. This was working perfectly fine, until I added GLM. Now I’m supposed to change the modelview matrix to a glm::mat4 matrix and compute the translation and rotation using glm::translate and glm::rotate. The problem is, as soon as I even use glm::translate my cube disappears, never to be seen again.

Obviously I’m doing something wrong. I tried just translating at first:

 glm::mat4 modelviewMatrix = glm::translate(		
		glm::vec3(0.0f, 0.0f, -4.0f)); 

I guess this is not equivalent to the translation part of the previous matrix? Btw, the projection matrix has to remain as it is.

Thankful for all hints!

P.S. I changed the glUniformMatrix4fv call to use glm::value_ptr(modelviewMatrix) instead of modelviewMatrix[0]

This was working perfectly fine, until I added GLM.

That’s because you were using row-major matrices, and probably compensating in your vertex shader by multiplying them backwards or transposing their upload. GLM properly uses column-major matrices.

I’d suggest replacing your matrix code entirely with GLM, and adjusting your shaders to multiply correctly or to stop transposing your matrix uploads with glUniformMatrix.

Thank you very much, this solved my first problem.

What would be the GLM equivalent to the rotation part of the previous modelview matrix? I’ve playes around with glm::rotate but can’t seem to figure out how to get it to rotate at the same rate as before.

Nvm, I figured it out. Now I’ve got another problem. I’m implementing the Phong Lighting Model in my fragment shader:

vec3 Ca = vec3(0.8, 0.8, 0.5);  // Ambient material/reflection
vec3 La = vec3(0.2, 0.2, 0.2);  // Ambient light intensity

vec3 Cd = vec3(0.8, 0.8, 0.5);  // Diffuse material/reflection
vec3 Ld = vec3(0.8, 0.8, 0.8);  // Diffuse light intensity

vec3 Cs = vec3(1.0, 1.0, 1.0);  // Specular material/reflection
vec3 Ls = vec3(3.0, 3.0, 3.0);  // Specular light intensity
float Fs = 20.0;                // Specular light factor

void main() {

    vec4 VL = normalize(light0pos - position);  // Light source vector
    vec4 N = normalize(normal);                 // Normal vector
    vec4 Ve = normalize(position);              // Viewpoint (eye) vector
    vec4 rL = reflect(VL, N);                   // Reflection vector

    vec3 ambient = Ca * La;                     // Calculate ambient light
    float VLDotN = max(dot(VL, N), 0.0);        
    vec3 diffuse = Cd * Ld * VLDotN;            // Calculate diffuse light             
    vec3 specular = vec3(0.0);                  // Calculate specular light     
    if ( VLDotN > 0.0 )
        specular = Cs * Ls * pow( max( dot(rL, Ve), 0.0 ), Fs );

    fragmentColour = colour * vec4(ambient + diffuse + specular, 1.0);

and it seems to work just fine. I get my cube, lighting/shades etc, but still I constantly get a “GL ERROR: invalid operation” message. Any idea?

One of the OpenGL API calls is used wrong. There are two ways to find which one it is. Either you do the error check after every OpenGL call (take them away afterwards), or you use the debug extension that can give you error messages directly when they happen.

How do I use the debug extension?

I narrowed it down to the diffuse and specular vectors …

It’s pretty straightforward. You enable the debug report, you register a callback, and then when the callback is fired, you get messages. Simple.

My SDK even has a special module for piping messages to stdout or stderr. If you don’t want to use that, you can always just look directly at the source.

Sorry, but I still don’t follow. All I’ve ever used is the regular debug mode in VS. Is there a tutorial around where I can read more about this stuff?

Tried glslDevil but can’t seem to get anything other than the trace part to display anything …