# Rotate an object

I am having trouble rotating a 3d object. When starting, the y-axis points upwards, x-axis points to the right and the z-axis points out of the screen.

My algorithm is

//if cursor moves horizontally
rotate about y axis (using glRotatef)

//if cursor moves vertically
rotate about x axis (using glRotatef)

The problem is if I rotate the object 90 degrees on the x axis, the y axis now points out of the screen and the z-axis points downwards. But in this case, if the cursor moves horizontaly, I want it to rotate it about the z axis not the y axis.

How do I find out which axis points vertically on the screen?

take a look at tutorial , glLoadMatrix, and glGet.

Basically use

GLfloat M;
glGetFloatv(GL_MODELVIEW_MATRIX,M)

Then understand openGL transforms any point R into the Eye Coordinates as

M*R=R’

When you call glRotatef you are changing the modelview matrix.

In your case you are asking to find the inverse condition ie what is value of R given a known axis pointing vertically Y=[0 1 0 1].

MR=Y
M^-1
MR=M^-1Y

Hence R=M^-1*Y and M^-1=M^T (transpose simplification for translation and rotation operations)

so R=M^T*Y which by plugging in Y=[0 1 0 1]
gives the second row of the Matrix M

R = ( M M M M ), hence the axis that is pointing vertical is
x=M
y=M
z=M

I did no check this in detail so you may get benefit of observing the values of this at each of your 90 rotation steps described and confirm that it calculates the values you expect.

Thanks your reply was very helpful. Unfortunately, it seems the methods to get the ModelView matrix on an android doesn’t exist.

Any ideas on how to go around it?

I found a reference to glGetFloatv on the Anroid developer’s webpage. And another post on Khronos that seems to talk more to this directly.

Edit: What version of openGL ES? 1.0 or 2.0?

glGetFloatv seems to only be availble for GL11 not for GL10. I changed the code so that everything is GL11, and my previous code works. I then tried to retrieve the modelView matrix using gl.glGetFloatv(GL11.GL_MODELVIEW_MATRIX, ReqArray, 0); but ReqArray returns a zero matrix. I’m not sure what I am doing wrong…

Is your code something like this

``````
float ReqArray[] = new float;
gl.glGetFloatv(GL11.GL_MODELVIEW_MATRIX, ReqArray, 0);

``````

That would seem OK.

What is the actual value of GL11.GL_MODELVIEW_MATRIX? It should be 0x0BA6 = 2982 (from gl.h C header).