Jump to content
  • Advertisement
Sign in to follow this  
dande

OpenGL 2D gluLookAt

This topic is 4864 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi! I've never did 2D games in OpenGL, but since it is a part of my Uni project, I have to. My question: I am writing a car "simulator" sort of GTA(1) as far as camera handling goes (top-down 2D graphics). So, I've set up a glOrtho projection and works quite good, but I just cannot figure out, how to use gluLookAt with it. To my best knowledge, glOrtho operates only on GL_PROJECTION matrix, and gluLookAt operates on GL_MODELVIEW matrix. So gluLookAt should work, but I cannot get it to work. Can someone please give me some simple code to look at (tried googling on the net for it, but find results for Perspective projection only). I have no idea how to set up (where the upvector should go, etc...). Any help is very appriciated.

Share this post


Link to post
Share on other sites
Advertisement
Perhaps you forgot to change the current matrix mode. Anyway, have a look at the following code segment.


glMatrixMode( GL_PROJECTION );
glLoadIdentity();
gluPerspective( camera->FOV, ((f32)windowWidth/(f32)windowHeight, camera->nearPlane, camera->farPlane );

glMatrixMode( GL_MODELVIEW );
glLoadIdentity();
gluLookAt( camera->position.x, camera->position.y, camera->position.z,
camera->view.x, camera->view.y, camera->view.z,
camera->upVector.x, camera->upVector.y, camera->upVector.z );


Also, check the OpenGL docs.

Share this post


Link to post
Share on other sites
This is how I set up my projection for my 2d engine:


// assuming screen size of 800x600
glViewport(0, 0, 800, 600);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();

//glOrtho(left,right,top,bottom,near,far);
//use the same values in here to keep your scene rendering at the
//same scale regardless of resolution.
//take the left as -400 and the top as -300, so that the origin will be in the centre of
//the screen, rather than the top left corner (very important when attempting camera rotations!)
glOrtho(-400,400,300,-300,-40,40);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();





Then, this is my camera:


glLoadIdentity ();
gluLookAt(m_vPosition.v[0], m_vPosition.v[1], 20,
m_vPosition.v[0], m_vPosition.v[1], 0,
m_vUp.v[0], m_vUp.v[1], m_vUp.v[2]);

// the "up" vector will typically be (0,-1,0) unless you plan to impliment rotation.





it is important to note, that the "from" and "at" need to be in the same place (albeit, "from" needs to be at a different distance!)
Having the origin of the scene in the middle of the viewport makes everything that little bit easier, especially when you attach the camera to a moving object.

Hope this is useful.

Check out this demo of my engine to see the camera in action (controls, w, a, s, d, q, e and space).

[Edited by - JohnHurt on April 27, 2005 6:44:14 AM]

Share this post


Link to post
Share on other sites
Thank you very much for your reply! I've tried what you said, but currently does nothing but shows a black screen if I set the Ortho projection the way you've showed. If I set it normally(top left is [0,0]), there is just one problemo: I set the current m_vPosition.v[0] and m_vPosition.v[1] to the car's position (say car1.getX() and car1.getY()) the car jumps to the top left corner of the screen.

Share this post


Link to post
Share on other sites
Don't use gluLookAt for 2d (ortho) apps. Instead setup your ortho view (on the perspective matrix) and use a glTranslatef (on the modelview matrix) to slide the camera around.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!