Applying Look At matrix

Started by
2 comments, last by alejandro 12 years, 3 months ago
I'm developing a camera class on OpenGL in C++, I want to see the position and rotation that the camera has when using gluLookAt(), for this I'm using glm::lookAt() function, it retrieves a matrix 4x4 and then I multiply the current stack matrix with the one that was retrieve, and then I translate the camera to the position.

So my code looks like this


void camera::drawDebug()
{
glm::mat4 res = glm::lookAt(eye, target, up);
glMultMatrixf(&res[0][0]);
glTranslatef(-eye.x, -eye.y, -eye.z);
glutWireCube(1.5);
}


with this code I get the following image, considering that the red cube is at the center(0, 0, 0), and the wire cube is the camera, eye is (0, 5, 5), target is (0, 0, 0), up is (0, 1, 0)

quickimage.gif

As you can see it appears that the rotation is applied correctly but the translation is not correct. I'm doing -eye translation because of what it says on the documentation of glulookat.


...gluLookAt is equivalent to glMultMatrixf(M); glTranslated (-eyex, -eyey, -eyez);
[/quote]

Any ideas on how to fix the translation?
Advertisement
I'm not 100% sure, but it looks like you're applying the eye transform twice. glm::lookAt() should already include the eye transform.
I try it, still not there, it looks similar to previous result.
Solved, if I inverse the matrix I get the results that I expected, at the end the code is like the following


void camera::drawDebug()
{
glm::mat4 res = glm::lookAt(eye, target, up);
res = glm::inverse(res);
glMultMatrixf(&res[0][0]);
glTranslatef(-eye.x, -eye.y, -eye.z);
glutWireCube(1.5);
}


Don't know exactly why this works.

This topic is closed to new replies.

Advertisement