Sign in to follow this  
muye

How can I move my camera

Recommended Posts

Hello, everyone!
I currently have to implement a program--I put the object at somewhere such as (x0,y0,z0), and I have a camera whose position is (x1,y1,z1) relative to the position (x0,y0,z0) and the camera is pointing at negative z, now I need to point the camera directly to object, how can I do to achieve that?

Share this post


Link to post
Share on other sites
Quote:
Original post by Erik Rufelt
You can use for example gluLookAt.


I do use the function like follows:
glPushMatrix();
gluLookAt(0,0,0,-curcamera.x,-curcamera.y,-curcamera.z,0,1,0);
glTranslatef(curcamera.x,curcamera.y,curcamera.z);
glCallList(m_cameraDisplist);
glPopMatrix();

But it seems that the code doesn't work

Share this post


Link to post
Share on other sites
if your going to use gluLookAt you dont need to use glTranslatef

eyeX, eyeY, eyeZ
Specifies the position of the eye point.
centerX, centerY, centerZ
Specifies the position of the reference point.
upX, upY, upZ
Specifies the direction of the up vector.

eye is position (translation)
center is camera lookat point
up is up (usually x=0, y=1, z=0)

try something like this:

// Setup camera
glMatrixMode( GL_PROJECTION );
glLoadIdentity();

// build projection matrix
gluPerspective(mFieldOfView, mAspectRatio, mNearPlane, mFarPlane);

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

gluLookAt(mPosition.x,mPosition.y, mPosition.z,
mLookAt.x, mLookAt.y, mLookAt.z,
mUp.x, mUp.y, mUp.z);


// Draw stuff
glPushMatrix();
// Translate/Rotate by the object position etc
// Draw object
glCallList(m_cameraDisplist);
glPopMatrix();

Share this post


Link to post
Share on other sites
Quote:
Original post by muyeBut it seems that the code doesn't work


do some research on glpush/popmatrix. all gluLookAt does is change the current matrix. if you undo that shortly after using popmatrix, then you won't get the result you aimed for.

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