使用setLookAtM方法的Android OpenGL怪异 [英] Android OpenGL weirdness with the setLookAtM method

查看:113
本文介绍了使用setLookAtM方法的Android OpenGL怪异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

作为android和openGL 2.0 es的初学者,我正在测试简单的东西,看看它如何进行.

As a beginner to android and openGL 2.0 es, I'm testing simple things and see how it goes.

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

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

I changed the code to check if I could animate a rotation of the camera around the (0,0,0) point, the center of the square.

所以我做到了:

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轴移动...:

I expected the camera to look always to the center of the square (the (0,0,0) point) but that's not what happens. The camera is indeed rotating around the square but the square does not stay in the center of the screen.. instead it is moving along the X axis...:

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

I also expected that if we gave the eyeX and eyeY the same values as centerX and centerY,like this:

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

正方形将保持其形状(我的意思是,您的视野将被拖动,但会沿着与正方形平行的平面拖动),但事实并非如此:

the square would keep it's shape (I mean, your field of vision would be dragged but along a plane which would be paralel to the square), but that's also not what happens:

这是我的投影矩阵:

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

这是怎么回事?

推荐答案

看一下您下载的示例的源代码,我可以看到您为什么遇到这个问题,它与矩阵乘法的顺序有关

Looking at the source code to the example you downloaded, I can see why you're having that problem, it has to do with the order of the matrix multiplication.

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

Typically in OpenGL source you see matrices set up such that

transformed vertex = projMatrix * viewMatrix * modelMatrix * input vertex

但是在您下载的源示例程序中,其着色器是这样设置的:

However in the source example program that you downloaded, their shader is setup like this:

" gl_Position = vPosition * uMVPMatrix;"

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

With the position on the other side of the matrix. You can work with OpenGL in this way, but it requires that you reverse the lhs/rhs of your matrix multiplications.

长话短说,对于您的情况,应将着色器更改为:

Long story short, in your case, you should change your shader to read:

" gl_Position = uMVPMatrix * vPosition;"

然后我相信您会得到预期的行为.

and then I believe you will get the expected behavior.

这篇关于使用setLookAtM方法的Android OpenGL怪异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆