开发者

Android OpenGL ES: Rotation against arbitrary axis?

I have a point cloud that I've rendered on the Android OpenGL-ES. I can translate it correctly (I think) but when I rotate it, I can't make it work like it want. I want it to rotate about the center of the point cloud (I have this 3D point), but I don't know how to do that.

public void onDrawFrame(GL10 gl) {
    // Clears the screen and depth buffer.
    gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);

    gl.glMatrixMode(GL10.GL_MODELVIEW);
    // Replace the current matrix with the identity matrix
    gl.glLoadIdentity();
    // Translates 4 units into the screen.

    GLU.gluLookAt(gl,   eyeX, eyeY, eyeZ, 
                        centerX, centerY, centerZ, 
                        upX, upY, upZ);

    // rotate
    gl.glRotatef(_xAngle, 1f, 0f, 0f);
    gl.glRotatef(_yAngle, 0f, 1f, 0f);
    gl.glRotatef(_zAngle, 0f, 0f, 1f);

    gl.glTranslatef(_xTranslate, _yTranslate, _zTranslate);

    // Draw things
    ptCloud.draw(gl);
    aBox.draw(gl);
}

I change the _translate and _angle variables in response to user interaction, and in turn the OpenGl would act upon them. You can see I run the draw routin on my prCloud right after my perspective is setup. I'll show you that:

public void draw(GL10 gl) {
    gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
    gl.glEnableClientState(GL10.GL_COLOR_ARRAY);
    gl.glColorPointer(4, GL10.GL_FLOAT, 0, colorBuffer);
    gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexBuffer);
    gl.glPointSize(0.5f);
    gl.glDrawArrays(GL10.GL_POINTS, 0, numVertices);
    gl.glDisableClientState(GL10.GL_COLOR_ARRAY);
    gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
}

As well as the create surface method, because I'm not sure if it affects anything:

public void onSurfaceChanged(GL10 gl, int width, int height) {
    // Sets the current view port to the new size.
    gl.glViewport(0, 0, width, height);
    // Select the projection matrix
    gl.glMatrixMode(GL10.GL_PROJECTION);
    // Reset the projection matrix
    gl.glLoadIdentity();
    // Calculate the aspect ratio of the window
    GLU.gluPerspective(gl, 45.0f, (float) width / (float) height, 0.001f,
            1000000.0f);

    // Select the modelview matrix
    gl.glMatrixMode(GL10.GL_MODELVIEW);
    // Reset the modelvie开发者_运维技巧w matrix
    gl.glLoadIdentity();
}

Here are my lookat default variables:

private float eyeX = 20;
private float eyeY = -150; 
private float eyeZ = 60; 
private float centerX = 0;
private float centerY = 0;
private float centerZ = 0;
private float upX = 0;
private float upY = 0;
private float upZ = 1;

The points have been scaled to be in the range of (0,0,0) to (120,180,38). I also don't know how to find a eye position that will show the whole model provided random Maximum point values...

Can anyone guess why it won't rotate how I would expect?


Rotate after you translate!

Transformation works in the order you tell it to. If you rotate before you translate then the translation is affected by the rotation etc. If you translate before you rotate then the rotation is translated before rotating so it will be at the center of your object.

See these pages for more information:

http://www.3dcodingtutorial.com/Basic-OpenGL-functions/Translate-and-Rotate-functions.html

http://www.swiftless.com/tutorials/opengl/rotation.html

http://www.opengl.org/resources/faq/technical/transformations.htm

http://www.falloutsoftware.com/tutorials/gl/gl5.htm

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜