【问题标题】:Android OpenGL weirdness with the setLookAtM method使用 setLookAtM 方法的 Android OpenGL 怪异
【发布时间】:2012-08-12 16:57:30
【问题描述】:

作为 android 和 openGL 2.0 es 的初学者,我正在测试一些简单的东西,看看效果如何。

我在http://developer.android.com/training/graphics/opengl/touch.html 下载了示例。

我更改了代码以检查是否可以为相机围绕 (0,0,0) 点(正方形中心)的旋转设置动画。

所以我这样做了:

public void onDrawFrame(GL10 unused) {
    // Draw background color
    GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT);

    // Set the camera position (View matrix)
    long time = SystemClock.uptimeMillis() % 4000L;
    float angle = ((float) (2*Math.PI)/ (float) 4000) * ((int) time);
    Matrix.setLookAtM(mVMatrix, 0, (float) (3*Math.sin(angle)), 0, (float) (3.0f*Math.cos(angle)),     0 ,0, 0,     0f, 1.0f, 0.0f);

    // Calculate the projection and view transformation
    Matrix.multiplyMM(mMVPMatrix, 0, mProjMatrix, 0, mVMatrix, 0);

    // Draw square
    mSquare.draw(mMVPMatrix);
}

我希望相机始终注视正方形的中心((0,0,0) 点),但事实并非如此。相机确实在围绕正方形旋转,但正方形并没有停留在屏幕的中心......而是沿着 X 轴移动......:

我还期望如果我们给 eyeX 和 eyeY 赋予与 centerX 和 centerY 相同的值,就像这样:

Matrix.setLookAtM(mVMatrix, 0, 1, 1, -3,     1 ,1, 0,     0f, 1.0f, 0.0f);

正方形会保持它的形状(我的意思是,您的视野会被拖动,但会沿着一个平行于正方形的平面),但这也不会发生:

这是我的投影矩阵:

float ratio = (float) width / height;

// this projection matrix is applied to object coordinates
// in the onDrawFrame() method
Matrix.frustumM(mProjMatrix, 0, -ratio, ratio, -1, 1, 2, 7);

这是怎么回事?

【问题讨论】:

    标签: android opengl-es matrix


    【解决方案1】:

    查看您下载的示例的源代码,我知道您为什么会遇到这个问题,这与矩阵乘法的顺序有关。

    通常在 OpenGL 源代码中,您会看到这样设置的矩阵

    transformed vertex = projMatrix * viewMatrix * modelMatrix * input vertex

    但是在您下载的源示例程序中,它们的着色器设置如下:

    " gl_Position = vPosition * uMVPMatrix;"

    与矩阵另一侧的位置。您可以通过这种方式使用 OpenGL,但它要求您反转矩阵乘法的 lhs/rhs。

    长话短说,在您的情况下,您应该将着色器更改为:

    " gl_Position = uMVPMatrix * vPosition;"

    然后我相信你会得到预期的行为。

    【讨论】:

    • 是的,它是这样工作的,但现在旋转被绿色三角形“破坏”了(你知道,他们在教程中的那个)。我已经看到了你的新问题,我'一定要留意它。当我学习我的 CG 课程时,我做过一个 Matlab 迷你 3d 渲染器,我曾经很了解这些转换。但是现在,我有点生疏了。这很令人困惑,因为有很多方法可以做到这一点。改变乘法的顺序,转置矩阵等。而且我对辅助函数的熟悉程度不足以突破这一点目前。顺便说一句,是否有可用的打印矩阵功能?
    • @joxnas : Arrays.toString(matrix)
    • 至于现在不工作的旋转,有什么线索吗?他们有:Matrix.setRotateM(mRotationMatrix, 0, mAngle, 0, 0, -1.0f);Matrix.multiplyMM(mMVPMatrix, 0, mRotationMatrix, 0, mMVPMatrix, 0); 三角形围绕 z 轴奇怪地旋转,尽管如果我将投影矩阵设置为Matrix.frustumM(mProjMatrix, 0, -1, 1, -1, 1, 2, 7); 而不是Matrix.frustumM(mProjMatrix, 0, -ratio, ratio, -1, 1, 2, 7) 似乎没问题,当然会得到屏幕偏度。我尝试在乘法中将旋转矩阵与 MVPMatrix 交换,但没有得到任何好的结果。
    • 我相信它应该是 mvpMatrix * mRotationMatrix,但是你不应该使用与该函数的输入和输出相同的矩阵,你需要使用一个临时矩阵。 Android.opengl.Matrix " 可以为结果、lhs 和/或 rhs 传递相同的浮点数组。但是,如果结果元素与 lhs 或 rhs 元素重叠,则结果元素的值是未定义的。" 如果这无济于事,然后发布您的整个代码。
    • 是的,这行得通:Matrix.setRotateM(mRotationMatrix, 0, angle, 0.0f, 0.0f, -1.0f);Matrix.multiplyMM(mMVPMatrix, 0, dupMatrix(mMVPMatrix), 0, mRotationMatrix, 0); 感谢您的帮助! (我有点震惊的想法......本教程使用相同的矩阵进行输入和输出......)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多