如何调整可视区域的大小以显示所有对象/坐标?

发布于 2024-12-17 03:42:11 字数 280 浏览 1 评论 0原文

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

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

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

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

发布评论

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

评论(1

最终幸福 2024-12-24 03:42:11

您想要更改视锥体。这就是我在 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,但这不是问题。根据您使用的方法,您会得到不同的结果。想象一下,您有一组火车轨道,从您面前开始,然后远离您。使用正交投影,当轨道到达地平线时,它们之间的距离仍然与在您面前的距离相同。通过透视投影,它们似乎会聚在某个遥远的“消失点”。

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

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

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