2012-01-17 131 views
5

我正在使用Android和OpenGL ES 2.0,并且我遇到了一个问题,我无法真正将其解释为一个可靠的问题。在图片中,http://i.imgur.com/XuCHF.png,我基本上有一个形状来代表中间的一艘船,当它移动到一边时,它会向着消失点伸展。我想要完成的是让船在移动时保持其大部分形状。我相信这可能是由于我的矩阵,但我看过的每个资源似乎都使用相同的方法。OpenGL ES 2.0相机问题

//Setting up the projection matrix 
final float ratio = (float) width/height; 
final float left = -ratio; 
final float right = ratio; 
final float bottom = -1.0f; 
final float top = 1.0f; 
final float near = 1.0f; 
final float far = 1000.0f; 
Matrix.frustumM(projection_matrix, 0, left, right, bottom, top, near, far); 

//Setting the view matrix 
Matrix.setLookAtM(view_matrix, 0, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, -1.0f, 0.0f, 1.0f, 0.0f); 

//Setting the model matrix 
Matrix.setIdentityM(model_matrix, 0); 
Matrix.translateM(model_matrix, 0, 2f, 0f, 0f); 

//Setting the model-view-projection matrix 
Matrix.multiplyMM(mvp_matrix, 0, view_matrix, 0, model_matrix, 0); 
Matrix.multiplyMM(mvp_matrix, 0, GL.projection_matrix, 0, mvp_matrix, 0); 
GLES20.glUniformMatrix4fv(mvp_matrix_location, 1, false, mvp_matrix, 0); 

着色器是非常基本的还有:

private final static String vertex_shader = 
     "uniform mat4 u_mvp_matrix;" 
    + "attribute vec4 a_position;" 
    + "void main()" 
    + "{" 
    + " gl_Position = u_mvp_matrix * a_position;" 
    + "}"; 

private final static String fragment_shader = 
     "precision mediump float;" 
    + "void main()" 
    + "{" 
    + " gl_FragColor = vec4(0.0, 0.0, 1.0, 1.0);" 
    + "}"; 

任何想法/见解是极大的赞赏。

谢谢。

回答

7

这是正常的 - 这是透视投影应该是什么样子。虽然在你的情况下,它看起来确实拉伸了一个 - 与广泛的视野。

尝试使用而不是frustumM方法perspectiveM(projection_matrix,0,45.0f,ratio,near,far)。 或者,如果你必须使用frustumM,计算这样的左/右/底/顶部:

float fov = 60; // degrees, try also 45, or different number if you like 
top = tan(fov * PI/360.0f) * near; 
bottom = -top; 
left = ratio * bottom; 
right = ratio * top; 
+0

感谢您的支持!我一直在试图找出如何以有意义的方式改变观点,直到我遇到了这个答案。我错过了视野逻辑。有一个尤里卡时刻,当我看到你的答案。 –

0

如果你不希望任何透视效果的话,那么使用Matrix.orthoM代替Matrix.frustumM。

为了使透视效果不那么极端,您需要减小视场 - 也就是增加near或使topbottom接近于零。 (您可能想要right = top * ratio,如果您打算拨动topbottom值,则与left类似。)