Sign in to follow this  
alejandro

Applying Look At matrix

Recommended Posts

alejandro    142
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

[CODE]
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);
}
[/CODE]

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)

[img]http://dl.dropbox.com/u/597069/quickimage.gif[/img]

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 [url="http://pyopengl.sourceforge.net/documentation/manual/gluLookAt.3G.html"]glulookat[/url].

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

Any ideas on how to fix the translation?

Share this post


Link to post
Share on other sites
SiCrane    11839
I'm not 100% sure, but it looks like you're applying the eye transform twice. glm::lookAt() should already include the eye transform.

Share this post


Link to post
Share on other sites
alejandro    142
Solved, if I inverse the matrix I get the results that I expected, at the end the code is like the following

[CODE]
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);
}
[/CODE]

Don't know exactly why this works.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this