OpenGL ES 2.0 相机问题

发布于 2024-12-27 07:13:16 字数 1545 浏览 1 评论 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);"
     + "}";

非常感谢任何想法/见解。

谢谢。

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, https://i.sstatic.net/IUXLt.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);

The shaders are very basic as well:

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.

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

夏末染殇 2025-01-03 07:13:16

这是正常的——透视投影应该是这样的。尽管在你的情况下,它看起来确实很拉伸——视野很宽。

尝试使用 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;

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 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;
丿*梦醉红颜 2025-01-03 07:13:16

如果您根本不想要任何透视效果,请使用 Matrix.orthoM 而不是 Matrix.frustumM。

为了使透视效果不那么极端,您需要减小视野——即增加near或增加topbottom接近于零。 (您可能需要 right = top *ratio,如果您要摆弄 topbottom<,则与 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 bring top and bottom closer to zero. (You probably want right = top * ratio, and similarly with left if you're going to fiddle with top and bottom values.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文