Android OpenGL ES:对任意轴旋转?

时间:2011-06-29 18:55:05

标签: android opengl-es rotation axis

我有一个我在Android OpenGL-ES上渲染的点云。我可以正确地翻译它(我想)但是当我旋转它时,我无法让它像它想要的那样工作。我希望它围绕点云的中心旋转(我有这个3D点),但我不知道该怎么做。

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);
}

我更改_translate和_angle变量以响应用户交互,反过来OpenGl会对它们起作用。你可以看到我在我的透视设置后立即在我的prCloud上运行绘图例程。我会告诉你:

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);
}

与create surface方法一样,因为我不确定它是否会影响任何东西:

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 modelview matrix
    gl.glLoadIdentity();
}

以下是我的lookat默认变量:

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;

这些点已经缩放到(0,0,0)到(120,180,38)的范围内。我也不知道如何找到一个眼睛位置,它将显示整个模型提供的随机最大点值...

任何人都可以猜到为什么它不会旋转我的预期?

1 个答案:

答案 0 :(得分:2)

翻译后旋转!

转换按照您告诉的顺序进行。如果在平移之前旋转,则平移会受到旋转等的影响。如果在旋转之前进行平移,则旋转之前会旋转旋转,因此它将位于对象的中心。

有关详细信息,请参阅这些页面:

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

相关问题