OpenGL ES 2.0 相机问题
我正在使用 Android 和 OpenGL ES 2.0,但遇到一个问题,我无法真正将其表述为一个可靠的问题。在图像中,https://i.sstatic.net/IUXLt.png,我基本上有一个代表中间一艘船的形状,当它移动到一侧时,它会向消失点拉伸。我想要完成的是让船在移动时保持大部分形状。我相信这可能是由于我的矩阵造成的,但我看过的每个资源似乎都使用相同的方法。
//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);"
+ "}";
非常感谢任何想法/见解。
谢谢。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是正常的——透视投影应该是这样的。尽管在你的情况下,它看起来确实很拉伸——视野很宽。
尝试使用
frustumM
方法perspectiveM
(projection_matrix, 0, 45.0f,ratio,near,far) 代替。或者,如果您必须使用
frustumM
,请像这样计算左/右/下/上: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.
Try using instead of
frustumM
methodperspectiveM
(projection_matrix, 0, 45.0f, ratio, near, far).Or if you must use
frustumM
, calculate left/right/bottom/top like this:如果您根本不想要任何透视效果,请使用 Matrix.orthoM 而不是 Matrix.frustumM。
为了使透视效果不那么极端,您需要减小视野——即增加
near
或增加top
和bottom
接近于零。 (您可能需要right = top *ratio
,如果您要摆弄top
和bottom<,则与
left
类似。 /code> 值。)If you don't want any perspective effect at all, then use Matrix.orthoM instead of Matrix.frustumM.
To just make the perspective effect less extreme, you need to reduce the field of view -- That is, increase
near
or bringtop
andbottom
closer to zero. (You probably wantright = top * ratio
, and similarly withleft
if you're going to fiddle withtop
andbottom
values.)