渲染静态和动态图形OpenGL [英] Rendering Static and Dynamic Graphics OpenGL

查看:400
本文介绍了渲染静态和动态图形OpenGL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用OpenGL管道开发IOS游戏.我已经能够将想要的图形渲染到屏幕上,但是,我多次调用glDrawElements,并且最终会遇到性能问题.我的游戏中有几个静态元素,不需要在每个渲染周期都进行渲染.有什么方法可以将静态元素渲染到一个帧缓冲区,将动态元素渲染到另一个帧缓冲区?

I am working on an IOS game using the OpenGL pipeline. I have been able to render the graphics I want to the screen, however, I am calling glDrawElements too many times and have some concerns about running into performance issues eventually. I have several static elements in my game that do not need to be render on every render cycle. Is there a way I can render static elements to one frame buffer and dynamic elements to another?

这是我尝试过的代码:

static BOOL renderThisFrameBuffer = YES;
    if (renderThisFrameBuffer) {
        glBindFramebuffer(GL_FRAMEBUFFER, interFrameBuffer);
        glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
        glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
        glEnable(GL_BLEND);
        glEnable(GL_DEPTH_TEST);

        // Set up projection matrix
        CC3GLMatrix *projection = [CC3GLMatrix matrix];
        float h = 4.0f * OpenGLView.frame.size.height / OpenGLView.frame.size.width;
        [projection populateFromFrustumLeft:-2 andRight:2 andBottom:-h/2 andTop:h/2 andNear:4 andFar:50];
        glUniformMatrix4fv(projectionUniform, 1, 0, projection.glMatrix);

        glViewport(0, 0, OpenGLView.frame.size.width, OpenGLView.frame.size.height);

        for (BoardSpace *boardSpace in spaceArray){
            [boardSpace drawWithMatrix:modelView];
        }

        [context presentRenderbuffer:GL_RENDERBUFFER];
        glBindFramebuffer(GL_FRAMEBUFFER, constFrameBuffer);
        renderThisFrameBuffer = false;
    }

在这种情况下,我之前创建了两个帧缓冲区:interFrameBuffer用于静态元素,而constFrameBuffer用于动态元素.我想念什么吗?

In this case I have created two frame buffers previously: interFrameBuffer for static elements and constFrameBuffer for dynamic elements. Am I missing something?

推荐答案

应该可行.最直接的方法是将静态元素渲染到FBO一次.然后在每次重绘时,将静态FBO的内容复制到默认的帧缓冲区中,然后在其上方绘制动态元素.

It should be possible. The most straightforward approach is that you render your static elements to an FBO once. Then on each redraw, copy the content of the static FBO to your default framebuffer, and then draw the dynamic elements on top of it.

ES 2.0的功能集有点支持这一点.例如,您没有glBlitFramebuffer(),这是将FBO内容复制到默认帧缓冲区的便捷方法.但是,您可以使用简单的着色器程序进行复制,该程序会馈送顶点着色器中的坐标,并且仅在片段着色器中使用纹理采样操作.然后将您渲染了静态内容的纹理绑定到其中,并绘制一个屏幕大小的四边形.

The feature set of ES 2.0 is kind of borderline to support this. For example, you don't have glBlitFramebuffer(), which would be a convenient way to copy the FBO content to the default framebuffer. But you can do the copy with a simple shader program that feeds through the coordinates in the vertex shader, and only uses a texture sample operation in the fragment shader. Then bind the texture you rendered the static content to, and draw a screen sized quad.

如果您的动态渲染需要静态渲染中的深度缓冲区,则它将变得更加棘手.例如,如果动态内容被静态内容遮盖了,那就是这种情况.在这种情况下,您还需要复制深度缓冲区.通过快速刷新规格说明,我看不出有什么能在ES 2.0中实现这一目标. glBlitFramebuffer()不存在.而且看起来ES 2.0不支持深度纹理.如果您要进入ES 3.0领域,则进入该领域.

It gets more tricky if your dynamic rendering needs the depth buffer from the static rendering. That would for example be the case if dynamic content is obscured by static content. In that case, you would also need to copy the depth buffer. From quickly refreshing my memory of the specs, I don't see anything that would make this possible in ES 2.0. glBlitFramebuffer() is not there. And it looks like ES 2.0 does not support depth textures. You're entering ES 3.0 territory if you want to go there.

所有这些,您显然希望确保它确实对您的性能有所帮助.复制帧缓冲区不是免费的.只有比渲染静态元素便宜时,它才会有所回报.在做出决定之前,两种方法的基准测试肯定是有序的.

With all of this, you obviously want to make sure that it really helps your performance. Copying the framebuffer is not free. It only pays off if it's cheaper than rendering your static elements. Benchmarking of both approaches is definitely in order before you make a decision.

这篇关于渲染静态和动态图形OpenGL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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