(OpenGL ES的)物体远离视图中心被拉长 [英] (OpenGL ES) Objects away from view centre are stretched

查看:521
本文介绍了(OpenGL ES的)物体远离视图中心被拉长的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我已经产生了球体在OpenGL ES(具体而言,OpenGL ES 2.0的,在Java中,Android版)。当这个球体被放置在相同的位置上使用我的看法矩阵的中心,它的罚款,但是当偏离中心的球体是pretty的严重扭曲(见下文)。

So I have generated a sphere in OpenGL ES (specifically, OpenGL ES 2.0, in Java, for Android). When this sphere is placed at the same position as the centre used for my view matrix, it's fine, but when off centre the sphere is pretty badly warped (see below).

这究竟是为什么,以及如何阻止它?

Why is this happening, and how do I stop it?

这是同样的领域。一个在右上只是平移x和y(未z)的

That's the same sphere. The one in the upper right is just translated in x and y (not z).

的code从我实现GLSurfaceView.renderer的一些片段,

Some snippets of the code from my implementation of GLSurfaceView.renderer,

public void onSurfaceCreated(GL10 unused, EGLConfig config) {
    GLES20.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
    GLES20.glEnable(GLES20.GL_CULL_FACE);
    GLES20.glEnable(GLES20.GL_DEPTH_TEST);

    // Both centred on (0,0,0), of radius 1.0.
    outerSphere = new Sphere();
    centreSphere = new Sphere();
}

public void onSurfaceChanged(GL10 unused, int width, int height) {
    GLES20.glViewport(0, 0, width, height);

    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 = 100.0f;

    Matrix.frustumM(projMatrix, 0, left, right, bottom, top, near, far);
}

public void onDrawFrame(GL10 unused) {
    GLES20.glClear(GLES20.GL_DEPTH_BUFFER_BIT | GLES20.GL_COLOR_BUFFER_BIT);

    float eyeX = 0.0f;
    float eyeY = 0.0f;
    float eyeZ = 10.0f;

    final float lookX = 0.0f;
    final float lookY = 0.0f;
    final float lookZ = 0.0f;

    final float upX = 0.0f;
    final float upY = 1.0f;
    final float upZ = 0.0f;

    Matrix.setLookAtM(viewMatrix, 0, eyeX, eyeY, eyeZ, lookX, lookY, lookZ,
                      upX, upY, upZ);

    // Set identity matrix as input for translations.
    Matrix.setIdentityM(outerModelMatrix, 0);

    // Translate outer sphere by 5 in x and y.
    Matrix.translateM(outerModelMatrix, 0, 5.0f, 5.0f, 0.0f);

    // MVP matrix = Projection * View * Model.
    Matrix.multiplyMM(centreMVPMatrix, 0, viewMatrix, 0, centreModelMatrix, 0);
    Matrix.multiplyMM(centreMVPMatrix, 0, projectionMatrix, 0, centreMVPMatrix, 0);
    Matrix.multiplyMM(outerMVPMatrix, 0, viewMatrix, 0, outerModelMatrix, 0);
    Matrix.multiplyMM(outerMVPMatrix, 0, projectionMatrix, 0, outerMVPMatrix, 0);

    outerSphere.draw(outerMVPMatrix);
    centreSphere.draw(outerMVPMatrix);

}

和我的着色器是简单,

private final static String vertexShaderCode =
    "uniform mat4 u_MVPMatrix;" +
    "attribute vec4 a_Position;" +
    "uniform vec4 u_Color;" +
    "void main() {" +
    "    gl_Position = u_MVPMatrix * a_Position;" +
    "}";
private final static String fragmentShaderCode = 
    "precision mediump float;" +
    "uniform vec4 u_Color;" +
    "void main() {" +
    "    gl_FragColor = u_Color;" +
    "}";

我已经离开了几乎所有code从球体类,我不认为其他的东西是必要的(?),但如果在需要时我把他们。

I've left out almost all code from the Sphere class, and other things that I don't think are necessary(?), but if they are needed I'll put them up.

推荐答案

欢迎到的透视畸变

一个更详细一点:你现场-OF-看法过于狭隘,你就必须塑造锥台,使得它有点大

A little more detail: your Field-Of-View is too narrow, you'll have to shape the frustum so that it's a bit larger.

这篇关于(OpenGL ES的)物体远离视图中心被拉长的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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