How to resize viewable area to show all objects/coordinates?

旧街凉风 提交于 2020-01-06 14:19:58

问题


I'm draving some objects on GLSurfaceArea (similar to this: http://www.droidnova.com/android-3d-game-tutorial-part-ii,328.html). It all works fine, but there are shown only points which coordinates are on -1.0 - +1.0 interval. Is there a way to resize viewable area to show coordinates that are not in this area (for example (-2.0, 2.0, 0.0))?


回答1:


You want to change the view frustum. This is how I've done it in my android Renderer class:

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);

    }
}

As you can see, I mostly use glFrustumf, don't really use GLU.gluPerspective, and I don't use glOrthof at all, but that's not a problem. Depending on which method you use, you will get different results. Imagine you have a set of train tracks starting in front of you and going away from you. Using Orthographic projection, the tracks will still be the same distance apart when they hit the horizon as they do in front of you. With perspective projection, they appear to convege at some distant 'vanishing point'.

If you use my code as above, try also changing the near and far planes variables and also the zoom variable to see the effects it has on your program



来源:https://stackoverflow.com/questions/8212130/how-to-resize-viewable-area-to-show-all-objects-coordinates

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!