简体   繁体   English

OpenGL ES:渲染时使用glFrustrum

[英]OpenGL ES: glFrustrum at rendering time

I am working on a camera system in a 3d scene on Android. 我正在使用Android 3d场景中的摄像头系统。 So far, I used this initialisation code to: 到目前为止,我已使用此初始化代码执行以下操作:

public void onSurfaceCreated(GL10 gl, EGLConfig config) {
        Log.i(LOG_TAG, "onSurfaceCreated()");

        TextureLibrary.loadTextures(m_context, gl);

        //Set initial perspective and viewport
        gl.glMatrixMode(GL10.GL_PROJECTION); 
        float fov = .01f * (float) Math.tan(Math.toRadians(45.0 / 2.0));
        float aspectRatio = 1.9541f;
        gl.glFrustumf(-fov, fov, -fov / aspectRatio, fov / aspectRatio, 0.01f, 100.0f);
        gl.glViewport(0, 0, (int) _viewportWidth, (int) _viewportHeight);
        gl.glMatrixMode(GL10.GL_MODELVIEW);

        gl.glEnable(GL10.GL_DEPTH_TEST);
        gl.glEnable(GL10.GL_CULL_FACE);
        gl.glEnable(GL10.GL_TEXTURE_2D);
        gl.glFrontFace(GL10.GL_CW);
        gl.glCullFace(GL10.GL_BACK);

        initialise();
    }

But now, I would like to update the frustum in the render method, to perform zooms and/or image distortions. 但是现在,我想在render方法中更新视锥,以执行缩放和/或图像失真。 However, it seems like the the frustum update . 但是,这似乎是平截头体更新

Here is the render code (trimmed of the some fat, which does not contain any GL State altering code, apart from the usual Push and PopMatrix): 这是呈现代码(除了一些普通的Push和PopMatrix之外,还包含了一些不包含任何GL状态更改代码的脂肪):

private void render(GL10 gl)
    {
      //Frustrum update code
        gl.glMatrixMode(GL10.GL_PROJECTION); 
        float fov = .01f * (float) Math.tan(Math.toRadians(45.0 / 2.0));
        float aspectRatio = 1.9541f;
        gl.glFrustumf(-fov, fov, -fov / aspectRatio, fov / aspectRatio, 0.01f, 100.0f);
        gl.glMatrixMode(GL10.GL_MODELVIEW);

        gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
        gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);

        gl.glClearColor(0.3f, 0.3f, 0.3f, 1.0f);
        gl.glLoadIdentity();
        gl.glClear(GL10.GL_COLOR_BUFFER_BIT);         

       //Render all the things here!

        gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
        gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
    }

As you can see, the snippet where the frustum is set is almost identical, and the values used are the same. 如您所见,设置了截锥体的代码段几乎相同,并且所使用的值也相同。 However, when glFrustum is called in the render method, the program will not show anything being drawn . 但是,当在render方法中调用glFrustum时, 程序将不会显示正在绘制的任何内容 I find this strange, since the values used are exactly the same (when forcing the aspect ratio). 我发现这很奇怪,因为使用的值是完全相同的(强制使用宽高比时)。

The viewport is still set/updated in the OnSurfaceChanged method, which is hit at least once. 视口仍在OnSurfaceChanged方法中设置/更新,该方法至少被命中一次。

public void onSurfaceChanged(GL10 gl, int width, int height) {
        _viewportWidth = width;
        _viewportHeight = height;
        gl.glMatrixMode(GL10.GL_PROJECTION); 
        gl.glViewport(0, 0, (int) _viewportWidth, (int) _viewportHeight);
        gl.glMatrixMode(GL10.GL_MODELVIEW);
    }

I think this may have something to do with OpenGL States, but I haven't yet found a solution, out of everything I tried. 我认为这可能与OpenGL状态有关,但在我尝试过的所有方法中,我还没有找到解决方案。

glFrustum() concatenates with the current matrix. glFrustum()与当前矩阵连接。 As you haven't done a glLoadIdentity() , this means you're going to be multiplying with the previous frustum. 由于您还没有完成glLoadIdentity() ,这意味着您将要与以前的平截头体相乘。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM