jma*_*edo 4 android opengl-es matrix
作为android和openGL 2.0的初学者,我正在测试简单的东西,看看它是怎么回事.
我在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);
}
Run Code Online (Sandbox Code Playgroud)
我期望相机总是看到正方形的中心((0,0,0)点),但事实并非如此.相机确实在广场周围旋转,但广场并没有停留在屏幕的中心..相反,它正沿着X轴移动......:
我也期望如果我们给eyeX和eyeY提供与centerX和centerY相同的值,就像这样:
Matrix.setLookAtM(mVMatrix, 0, 1, 1, -3, 1 ,1, 0, 0f, 1.0f, 0.0f);
Run Code Online (Sandbox Code Playgroud)
这个方块会保持它的形状(我的意思是,你的视野会被拖动,但是沿着一个平面的方向拖动),但这也不会发生什么:
这是我的投影矩阵:
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);
Run Code Online (Sandbox Code Playgroud)
这里发生了什么?
查看您下载的示例的源代码,我可以看到您遇到该问题的原因,它与矩阵乘法的顺序有关.
通常在OpenGL源代码中,您可以看到矩阵设置为
transformed vertex = projMatrix * viewMatrix * modelMatrix * input vertex
但是,在您下载的源示例程序中,它们的着色器设置如下:
" gl_Position = vPosition * uMVPMatrix;"
位于矩阵另一侧的位置.您可以通过这种方式使用OpenGL,但它需要您反转矩阵乘法的lhs/rhs.
简而言之,在您的情况下,您应该将着色器更改为:
" gl_Position = uMVPMatrix * vPosition;"
然后我相信你会得到预期的行为.
归档时间: |
|
查看次数: |
5336 次 |
最近记录: |