Sign in to follow this  
fabiom

Simple gluLookAt question

Recommended Posts

Hi, Can anyone help me with the following code? I am trying to draw a simple triangle and then use the gluLookAt function to change the camera view. Really simple stuff. Here is the display func: void display(void) { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glPushMatrix(); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); gluLookAt( eye.x, eye.y, eye.z, center.x, center.y, center.z, 0, 0, 1 ); GLfloat r = 0.5f; glBegin(GL_TRIANGLES); glVertex3f(-r ,1.0f,-r); glVertex3f(-r,1.0f,r); glVertex3f( r,1.0f,r); glEnd(); glPopMatrix(); glutReportErrors(); glutSwapBuffers(); } Ok, so my problem is: When I set the eye vector to [0,0,0] and the center vector to [0,1,0] I see the triangle. But when I change the center vector to [0,-1,0], I can see other triangle, but up side down... and as far as I know, I have only draw one triangle. I know its a noob question, but does anyone know what the problem is?

Share this post


Link to post
Share on other sites
You are using gluLookAt on the modelview-matrix, it should be used on the projection matrix.

Your code rewritten:

void display(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluLookAt( eye.x, eye.y, eye.z, center.x, center.y, center.z, 0, 0, 1 );

glMatrixMode(GL_MODELVIEW);
glLoadIdentity ();

GLfloat r = 0.5f;
glBegin(GL_TRIANGLES);
glVertex3f(-r ,1.0f,-r);
glVertex3f(-r,1.0f,r);
glVertex3f( r,1.0f,r);
glEnd();

glutReportErrors();
glutSwapBuffers();
}

I hope this code works, havent tried it...

EDIT: Hmm, im sorry, i don't think that is correct, you should use gluLookAt on the modelview-matrix, however, have you specified a projection matrix at all?

EDIT2: If youre using an orthographic view (glOrtho, gluOrtho2D), i think you would get an effect like this, since there is no clipping (objects behind you apear even though they shouldnt), specify a projection matrix with gluProjection (60.0f, (GLfloat) w / (GLfloat) h, 0.1f, 100.0f); You only have to specify this when the user resizes the window (w and h are the width and height)

[Edited by - gardin on May 3, 2007 5:56:22 AM]

Share this post


Link to post
Share on other sites
Quote:
Original post by Brother Bob
If you change the viewpoint setup, you change the way you view the triangle. If you change the way you view the triangle, there are, obviously, no guarantees the triangle will look the same.


I know, but I don't think I'm changing the position of the camera. I'm only changing the way it points at. When I point the camera to two different directions ([0,1,0] and [0,-1,0]), I shouldn't see two different triangles each time.
But thx :)

Share this post


Link to post
Share on other sites
Quote:
Original post by gardin
You are using gluLookAt on the modelview-matrix, it should be used on the projection matrix.

Your code rewritten:

void display(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluLookAt( eye.x, eye.y, eye.z, center.x, center.y, center.z, 0, 0, 1 );

glMatrixMode(GL_MODELVIEW);
glLoadIdentity ();

GLfloat r = 0.5f;
glBegin(GL_TRIANGLES);
glVertex3f(-r ,1.0f,-r);
glVertex3f(-r,1.0f,r);
glVertex3f( r,1.0f,r);
glEnd();

glutReportErrors();
glutSwapBuffers();
}

I hope this code works, havent tried it...

EDIT: Hmm, im sorry, i don't think that is correct, you should use gluLookAt on the modelview-matrix, however, have you specified a projection matrix at all?

EDIT2: If youre using an orthographic view (glOrtho, gluOrtho2D), i think you would get an effect like this, since there is no clipping (objects behind you apear even though they shouldnt), specify a projection matrix with gluProjection (60.0f, (GLfloat) w / (GLfloat) h, 0.1f, 100.0f); You only have to specify this when the user resizes the window (w and h are the width and height)


Woho :)
Thanks for the help gardin. Its working fine now

Share this post


Link to post
Share on other sites
Quote:
Original post by fabiom
Quote:
Original post by Brother Bob
If you change the viewpoint setup, you change the way you view the triangle. If you change the way you view the triangle, there are, obviously, no guarantees the triangle will look the same.


I know, but I don't think I'm changing the position of the camera. I'm only changing the way it points at. When I point the camera to two different directions ([0,1,0] and [0,-1,0]), I shouldn't see two different triangles each time.
But thx :)

The viewpoint setup is not position only, it includes direction. Look in another direction and you see something else. Just like you said you did, and just like I said was expected to happen.

Share this post


Link to post
Share on other sites
No, you didnt read his question, he moved the camera in a way that would make the triangle go outside the viewport, but he could still see it, but, obviously, the projection matrix was the problem...

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