美文网首页
深度缓冲

深度缓冲

作者: 一毛钱 | 来源:发表于2020-03-21 19:29 被阅读0次

    深度测试相关知识点:

    • 1、frambuffer 绑定能够管理 多个renderbuffer,然后renderbuffer 可以是深度缓冲区 模板缓冲区以及颜色缓冲区等。

    • 2、默认情况下苹果在创建工程时,并没有提供深度缓冲区。所以iOS openGLES必须设置深度缓冲区才能开启深度检测,设置深度相关操作。同时必须绑定颜色缓冲区进行渲染,当然这里不需离屏渲染,所以不需要给myColorRenderBuffer设置纹理内存空间。

    相应设置代码如下:

    //3.清空缓存区
    -(void)deleteBuffer
    {
        if (_myColorRenderBuffer) {
            glDeleteRenderbuffers(1, &_myColorRenderBuffer);
            _myColorRenderBuffer = 0;
        }
        
        if (_myDepthRenderBuffer) {
            glDeleteRenderbuffers(1, &_myDepthRenderBuffer);
            _myDepthRenderBuffer = 0;
        }
        
        if (_myColorFrameBuffer) {
            glDeleteFramebuffers(1, &_myColorFrameBuffer);
        }
    }
    //4.设置RenderBuffer 关联颜色缓冲区
    -(void)setupColorRenderBuffer
    {
        glGenRenderbuffers(1, &_myColorRenderBuffer);
        glBindRenderbuffer(GL_RENDERBUFFER, _myColorRenderBuffer);
        
        [self.myContext renderbufferStorage:GL_RENDERBUFFER fromDrawable:self.myEagLayer];
        
        glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_WIDTH, &_viewWidth);
        glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_HEIGHT, &_viewHeight);
        glBindRenderbuffer(GL_RENDERBUFFER, 0);
    }
    
    //关联深度缓冲区
    - (void)setDepthRenderBuffer{
        
        glGenRenderbuffers(1, &_myDepthRenderBuffer);
        glBindRenderbuffer(GL_RENDERBUFFER, _myDepthRenderBuffer);
        glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT16, _viewWidth, _viewHeight);
        glBindRenderbuffer(GL_RENDERBUFFER, 0);
        
    }
    //5.设置FrameBuffer 并且绑定颜色和深度的渲染缓冲区 来管理renderbuffer
    -(void)setupFrameBuffer{
        glGenFramebuffers(1, &_myColorFrameBuffer);
        glBindFramebuffer(GL_FRAMEBUFFER, _myColorFrameBuffer);
        
        if (_myColorRenderBuffer) {
            glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, _myColorRenderBuffer);
        }
        
        if (_myDepthRenderBuffer) {
            glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, _myDepthRenderBuffer);
        }
    }
    

    效果如下:


    相关文章

      网友评论

          本文标题:深度缓冲

          本文链接:https://www.haomeiwen.com/subject/yemuyhtx.html