OpenGL ES 2.0 相机问题 [英] OpenGL ES 2.0 Camera Issues
问题描述
我正在使用 Android 和 OpenGL ES 2.0,但我遇到了一个我无法真正表述为一个可靠问题的问题.在图像中,http://i.imgur.com/XuCHF.png,我基本上有代表船在中间的形状,当它移动到一侧时,它会被拉向消失点.我想要完成的是让船在移动时保持其大部分形状.我相信这可能是由于我的矩阵,但我看过的每个资源似乎都使用相同的方法.
I'm working with Android and OpenGL ES 2.0 and I am having an issue that I can't really formulate into a solid question. In the image, http://i.imgur.com/XuCHF.png, I basically have a shape to represent a ship in the middle and when it's moved to the side it gets stretched toward the vanishing point. What I am wanting to accomplish is to have the ship maintain most of its shape when it is moved. I believe it may be due to my matrices but every resource I've looked seems to use the same method.
//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);"
+ "}";
非常感谢任何想法/见解.
Any thoughts/insight is greatly appreciated.
谢谢.
推荐答案
这很正常——透视投影应该是这样的.虽然在你的情况下它看起来真的很拉伸 - 视野很广.
That is normal - that is how perspective projection should look like. Although in your case it looks really stretched one - with wide field of view.
尝试使用 frustumM
方法代替 perspectiveM
(projection_matrix, 0, 45.0f, ratio, near, far).或者如果你必须使用 frustumM
,计算 left/right/bottom/top 像这样:
Try using instead of frustumM
method perspectiveM
(projection_matrix, 0, 45.0f, ratio, near, far).
Or if you must use frustumM
, calculate left/right/bottom/top like this:
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;
这篇关于OpenGL ES 2.0 相机问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!