Thursday, November 5, 2015

OpenGL ES 2.0: Moving Camera in Orthogonal (2D) Projection


I have quite large 2D game scene. The scene is much larger than the screen of the LCD. Therefore, I have to move the camera (view) in desired directions, to display particular parts of the scene.


What is the correct way of moving viewport over the scene, in Orthogonal (2D) projection, please?


There are two solutions coming to my mind:




  1. Using GluLookAt(), which is designed to move camera moslty in Perspective Projection, but it could work well for 2D as well. However, not all of the GluLookAt() parameters would be utilized in Ortho configuration.

  2. Using opposite View Translate transformation. This would mean, if I need to move my 2D camera 10 units to the right(positive x axis), I would apply opposite Translate transformation to every scene vertex (negative x axis). This way, I would create illusion, the camera moves to the right.


These are the solutions coming from my mind. However, because, i am self taught, is there any correct and recommended way of moving camera over the 2D scene, please?



Answer



There are several correct ways -- both of the methods you described are fine. I would suggest the first method, using gluLookAt or otherwise directly manipulating the view transformation matrix to transform it sideways. All parameters to gluLookAt are still going to be used.


GL combines the world and view matrices into one, so depending on the level to which you abstract that yourself it may not matter if you manipulate the "view" matrix or the "object" matrix (since they're the same). If you do maintain a separation of these concepts in your own code, I would say that it's more readable to manipulate the view matrix (via gluLookAt or more direct transformation) than the object matrix, even the math would end up being equivalent.


No comments:

Post a Comment

Simple past, Present perfect Past perfect

Can you tell me which form of the following sentences is the correct one please? Imagine two friends discussing the gym... I was in a good s...