Android OpenGL ES:针对任意轴旋转?

发布于 2024-11-18 06:37:48 字数 2409 浏览 3 评论 0原文

我有一个在 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);
}

以及创建曲面方法,因为我不确定它是否会影响任何内容:

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

这是我查看的默认变量:

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)。我也不知道如何找到一个眼睛位置来显示提供随机最大点值的整个模型...

有人能猜到为什么它不会按照我的预期旋转吗?

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 modelview 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

绮烟 2024-11-25 06:37:48

翻译后旋转!

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

有关详细信息,请参阅以下页面:

http://www.3dcodingtutorial.com/Basic-OpenGL-functions/Translate-and-Rotate-functions.html 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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文