* * * *

Moving Camera: OpenGL & Android Programming

April Fools’ pranks?

Today is April 1, April Fools’ Day! Does your friend fall for your prank hook, line and sinker?

Correction of viewing

In last blog I mentioned viewing with camera analogy. My understanding was not correct in some part. I did not understand correctly about the correspondance between viewing concept and coding. As I said before, There are 4 parts in transformation process, such as ①viewing ②modeling ③projection ④viewport. Multiplying Matrix to object coordinates yield window coordinates. Matrix consists of ModelView Matrix, Projection Matrix, Perspectice Division, Viewport Transformation. Let’s check the code, now. At Function “OnDrawFrame()”, there is a line “gluLookAt”. This function sets ModelView Matrix. Before this line, there is “glMatrixMode(GL_MODELVIEW)”. This indicates that “only” ModelView Matrix value is going to be changed. Then comes “glLoadIdentity”, which clears ModelView Matrix with Identity Matrix. Now let’s move on the other part. At Function “OnSurfaceChanged()”, there is a line “glFrustum()”. This function sets Projection Matrix. Before this line, “gMatrixMode(GL_PROJECTION)” is called, which indicates that only Projection Matrix is going to be changed after this line. “glLoadIdentity” follows, which clears Projection Matrix. My understanding is not still complete. Which function actually do Perspective Divison?( I guess it occurs inside “glFrustum()”, but I am not sure.) Anyway, this is what I undersand so far.

Let’s move the camera!

There are 2 methods to make object move. One method is to move camera, so that object moves in opposite direction. The other method is to move objects itself. The former is attained by viewing transformation. Function “gluLookAt()” sets Camera position and angle. The latter is attained by modeling transformation. Function “glScale()”,”glTranslate()”,”glRoate()” change the modeling position. Yesterday, I use the latter method to move object. Today, I want to use the former method, so that I change the parameters of “gluLookAt()” function.

Take input!

As is often the case with games, it is essential to take user’s input. To do that, you need to subclass GLSurfaceView. “onTouchEvent()” function catches user’s event.

Let’s check the code!

My example is really simple. By clicking any place in the window, (which calls “onTouchEvent()”), the value of “eyeX”, “eyeY”, “eyeZ” are incremented. At “gluLookAt()”, updated values of “eyeX”,”eyeY”,”eyeZ” are used as parameters. These are eye position,aiming the camera lens towards(0,0,0), where camera’s orientation is (0,1,0).

My sweet home!

This time, as modeling, I made “My sweet home”, with 3 cubes and 1 square pyramid. These objects are scaled and translated to form a simple home. By clicking, it changes the eye position, so that you see this home from different angle. It disappears in the end, as it gets out of clipping area. Please refer to this Youtube Videoin case it does not show up.

Thanks for reading! Have a nice day!


back to top