0

我在 GLSurfaceArea 上驾驶一些对象(类似于:http ://www.droidnova.com/android-3d-game-tutorial-part-ii,328.html )。一切正常,但只显示坐标在 -1.0 - +1.0 区间的点。有没有办法调整可视区域的大小以显示不在该区域中的坐标(例如(-2.0、2.0、0.0))?

4

1 回答 1

0

你想改变视锥体。这就是我在我的 android Renderer 类中所做的:

int viewportWidth = -1;
int viewportHeight = -1;
int zoom = 0.5f;
float nearPlane = 3.0f;
float farPlane = 7.0f;
float FOV = 60.0f

@Override
public void onSurfaceChanged(GL10 gl, int width, int height) {

    viewportWidth = width;
    viewportHeight = height;

    gl.glViewport(0, 0, width, height);

    setProjectionMatrix(gl);

}

@Override
public void onSurfaceCreated(GL10 gl, EGLConfig config) {

    setProjectionMatrix(gl);

}

protected void setProjectionMatrix(GL10 gl){
    if(viewportWidth <0 || viewportHeight <0){
        gl.glMatrixMode(GL10.GL_PROJECTION);
        gl.glLoadIdentity();
        GLU.gluPerspective(gl, FOV*zoom, 1.0f, nearPlane, farPlane);        
    } else {
        float ratio = (float) viewportWidth / viewportHeight;
        gl.glMatrixMode(GL10.GL_PROJECTION);
        gl.glLoadIdentity();
        gl.glFrustumf(-ratio*zoom, ratio*zoom, -1*zoom, 1*zoom, nearPlane, farPlane);

    }
}

如您所见,我主要使用glFrustumf,并没有真正使用GLU.gluPerspective,而且我根本不使用glOrthof,但这不是问题。根据您使用的方法,您将获得不同的结果。想象一下,您有一组从您面前开始并远离您的火车轨道。使用正交投影,轨道在到达地平线时的距离仍然与在您面前的距离相同。通过透视投影,它们似乎会聚在某个遥远的“消失点”。

如果您使用我上面的代码,请尝试更改近平面和远平面变量以及缩放变量以查看它对您的程序的影响

于 2011-11-22T16:27:46.420 回答