1

I have a Opengl ES 1.x ANdroid 1.5 app that shows a Square with Perspective projection, on the center of the screen.

I need to move the camera (NOT THE SQUARE) when the user moves the finger on the screen, for example, if the user moves the finger to the right, the camera must be moved to the left, it must be shown like if the user is moving the square.

I need to do it without translating the square. The square must be on the opengl position 0,0,-1 allways.

I DONT WANT to rotate the camera arround the square, no, what i want is to move the camera side to side. Code examples are welcome, my opengl skills are very low, and i can't find good examples for this in google

I know that i must use this function: public static void gluLookAt (GL10 gl, float eyeX, float eyeY, float eyeZ, float centerX, float centerY, float centerZ, float upX, float upY, float upZ), but i dont understand where and how to get the values for the parameters. Because this, i will apreciate code examples for doing this.

for example:

I have a cube on the position 0,0,-1. I want that my camera points the cube. I tryed with this: GLU.gluLookAt(gl, 0, 0, 2, 0, 0, 0, 0, 0, 1);, but the cube is not in the screen, i just donmt understand what im doing wrong

NullPointerException
  • 36,107
  • 79
  • 222
  • 382
  • Since OpenGL only has a modelview matrix (instead of separate model and view matrices) woving the camera is exactly the same thing as moving the square in the opposite direction. OpenGL just doesn't know any difference, so you are always translating the square. Keep in mind that translating the square is not the same as changing the square's vertices. Delve a little deeper into OpenGL's transformation pipeline for more insight. – Christian Rau Nov 10 '11 at 15:55
  • i need a little help, i have a cube on the position 0,0,-1. I want that my camera points the cube. I tryed with this: GLU.gluLookAt(gl, 0, 0, 2, 0, 0, 0, 0, 0, 1);, but the cube is not in the screen, i just donmt understand what im doing wrong – NullPointerException Nov 10 '11 at 16:04
  • You don't need `gluLookAt`. LookAt my answer. – Christian Rau Nov 10 '11 at 16:42

1 Answers1

4

First of all, you have to understand that in OpenGL there are not distinct model and view matrices. There is only a combined modelview matrix. So OpenGL doesn't care (or even know) if you translate the camera (what is a camera anyway?) or the object, so your requirement not to move the square is entirely artificial. Though it may be that this is a valid requirement and the distinction between model and view transformation often is very practical, just don't think that translating the square is any different from translating the camera from OpenGL's point of view.

Likewise don't you neccessarily need to use gluLookAt. Like glOrtho, glFrustum or gluPerspective this function just modifies the currently selected matrix (usually the modelview matrix), nothing different from the glTranslate, glRotate or glScale functions. The gluLookAt function comes in handy when you want to position a classical camera, but its functionality can also be achieved by calls to glTranslate and glRotate without problems and sometimes (depending on your requirements) this is even easier than artificially mapping your view parameters to gluLookAt parameters.

Now to your problem, which is indeed solvable quite easily without gluLookAt: What you want to do is move the camera in a direction parallel to the screen plane and this in turn is equivalent to moving the camera in the x-y-plane in view space (or camera space, if you want). And this in turn is equivalent to moving the scene in opposite direction in the x-y-plane in view space.

So all that needs to be done is

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(x, y, 0.0f);
//camera setup...

Where (x, y) is the movement vector determined from the touch events, appropriately scaled (try dividing the touch coords you get by the screen dimensions or something similar for example). After this glTranslate comes whatever other camera or scene transformations you already have (be it gluLookAt or just some glTranslate/glRotate/glScale calls). Just make sure that the glTranslate(x, y, ...) is the first transformation you do on the modelview matrix after setting it to identity, since we want to move in view space.

So you don't even need gluLookAt. From your other questions I know your code already looks something like

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(x, y, z);
glRotatef(...);
...

So everything you need to do is plug the x and y values determined from the touch movement into the first glTranslate call (or add them to already existing x and y values), since multiple translations are perfectly commutative.

For more insight into OpenGL's transformation pipeline (which is definitely needed before progressing further), you may also look at the asnwers to this question.

EDIT: If you indeed want to use gluLookAt (be it instead or after the above mentioned translation), here some small words about its workings. It defines a camera using three 3d vectors (passed in as 3 consecutive values each). First the camera's position (in your case (0, 0, 2)), then the point at which the camera looks (in your case (0, 0, 0), but (0, 0, 1) or (0, 0, -42) would result in the same camera, the direction matters). And last comes an up-vector, defining the approximate up-direction of the camera (which is further orthogonalized by gluLookAt to make an appropriate orthogonal camera frame).

But since the up-vector in your case is the z-axis, which is also the negative viewing direction, this results in a singular matrix. You probably want the y-axis as up-direction, which would mean a call to

gluLookAt(0,0,2, 0,0,0, 0,1,0);

which is in turn equivalent to a simple

glTranslate(0, 0, -2);

since you use the negative z-axis as viewing direction, which is also OpenGL's default.

Community
  • 1
  • 1
Christian Rau
  • 45,360
  • 10
  • 108
  • 185
  • thanks a lot christian, i tryed this way becase i'm having very hard to understand and solve problem with gluUnProject. gluUnProject is working fine if i pass 0.9f as winZ parameter, i can rotate (x and y axis), translate the object on the screen with the finger, scale it, all works fine, but...... when i rotate on the Z axis, then the translating of the object brokens... :S when i translate with a Z axis rotation on the object, gluUnProject function start's to given me rare values for X and Y. – NullPointerException Nov 11 '11 at 09:16
  • I think that gluUnproject fails because when i rotate on the Z axis the winZ i pass as third parameter of gluUnProject must not be 0.9f, but then i dont know what i have to pass on that parameter, i'm really stuck.... i dont know how to continue with this... i thought that i can solve this with gluLookAt instead of "translate"... but you have opened my eyes, i can't solve it... im really stuck :S – NullPointerException Nov 11 '11 at 09:17
  • @AndroidUser99 I cannot really help you there (with the gluUnProject problem), because you need the depth of the object to pass into and in ES 1 there are not many possibilities to query the depth, as you already found out. But maybe you don't need gluUnProject? You could rephrase your actual problem in another question and maybe there's a solution without gluUnProject. But for moving the scene/object in view space based on a touch event you definitely don't need it, which is achievable quite easily as detailed above. – Christian Rau Nov 11 '11 at 13:34