# OpenGL 2D gluLookAt

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

## 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 on other sites
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 on other sites
This is how I set up my projection for my 2d engine:

// assuming screen size of 800x600glViewport(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 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 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.

1. 1
Rutin
27
2. 2
3. 3
4. 4
5. 5

• 11
• 9
• 9
• 9
• 14
• ### Forum Statistics

• Total Topics
633312
• Total Posts
3011312
• ### Who's Online (See full list)

There are no registered users currently online

×