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
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. LikeglOrtho,glFrustumorgluPerspectivethis function just modifies the currently selected matrix (usually the modelview matrix), nothing different from theglTranslate,glRotateorglScalefunctions. ThegluLookAtfunction comes in handy when you want to position a classical camera, but its functionality can also be achieved by calls toglTranslateandglRotatewithout problems and sometimes (depending on your requirements) this is even easier than artificially mapping your view parameters togluLookAtparameters.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
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
glTranslatecomes whatever other camera or scene transformations you already have (be itgluLookAtor just someglTranslate/glRotate/glScalecalls). Just make sure that theglTranslate(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 likeSo everything you need to do is plug the x and y values determined from the touch movement into the first
glTranslatecall (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 bygluLookAtto 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
which is in turn equivalent to a simple
since you use the negative z-axis as viewing direction, which is also OpenGL’s default.