PDA

View Full Version : Rotate an object

Inquirer
07-26-2010, 03:46 PM
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?

marshats
07-26-2010, 09:33 PM
take a look at tutorial (http://www.songho.ca/opengl/gl_transform.html) , glLoadMatrix (http://www.opengl.org/sdk/docs/man/xhtml/glLoadMatrix.xml), and glGet (http://www.opengl.org/sdk/docs/man/xhtml/glGet.xml).

Basically use

GLfloat M[16];
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].

M*R=Y
M^-1*M*R=M^-1*Y

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[1] M[5] M[9] M[13] ), hence the axis that is pointing vertical is
x=M[1]
y=M[5]
z=M[9]

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.

Inquirer
08-02-2010, 01:14 PM
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?

marshats
08-02-2010, 07:08 PM
I found a reference to glGetFloatv (http://developer.android.com/reference/javax/microedition/khronos/opengles/GL11.html) on the Anroid developer's webpage. And another post on Khronos (http://www.khronos.org/message_boards/viewtopic.php?f=4&amp;t=2676) that seems to talk more to this directly.

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

Inquirer
08-03-2010, 01:53 PM
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....

marshats
08-03-2010, 06:28 PM
Is your code something like this

float ReqArray[] = new float[16];
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).