Frame Buffer

 

  Frame buffer graphics rendering images will be stored in the default frame buffer in, including attachments have color, depth, stencil buffer.

  We can create your own frame buffer.

glGenFramebuffers(1, &this->framebuffer);
glBindFramebuffer(GL_FRAMEBUFFER, this->framebuffer);

  We need to attach some accessories (such as color, depth, stencil buffer).

  For color, we may sometimes need to do after rendering post-processing, then you need to get the rendered pixel values, in this case a good option is to use color texture as an attachment.

glGenTextures ( . 1 , & texColorBuffer); 
glBindTexture (GL_TEXTURE_2D, texColorBuffer); 
glTexImage2D (GL_TEXTURE_2D, 0 , GL_RGB, 800 , 600 , 0 , GL_RGB, of GL_UNSIGNED_BYTE, NULL); 
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); 
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER , GL_LINEAR); 
glBindTexture (GL_TEXTURE_2D, 0 );
 // attach it to the frame buffer object currently bound 
glFramebufferTexture2D (GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texColorBuffer, 0 );

  The depth and stencil values ​​we do not get it, do it yourself system depth and stencil tests on the line, then we use the render buffer object for depth and stencil attachments.

unsigned int RBO; 
glGenRenderbuffers ( . 1 , & RBO); 
glBindRenderbuffer (GL_RENDERBUFFER, RBO); 
glRenderbufferStorage (GL_RENDERBUFFER, GL_DEPTH24_STENCIL8, 800 , 600 ); 
glBindRenderbuffer (GL_RENDERBUFFER, 0 );
 // render buffer object to a frame buffer and depth template accessories 
glFramebufferRenderbuffer (GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_RENDERBUFFER, rbo);

  Check the frame buffer we created is complete

//检查帧缓冲是否完整
if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE)
    std::cout << "ERROR::FRAMEBUFFER:: Framebuffer is not complete!" << std::endl;
else std::cout << "Successful:: Framebuffer is complete..." << std::endl;

  When rendering, the first to use the frame buffer we created the scene to render color texture. The frame buffer is then changed back to the default screen frame buffer, texture rendering directly to the screen and in the post-processing done in the fragment shader.

  

void SceneRendering :: the Draw () {
     // first process stage (Pass) 
    glBindFramebuffer (GL_FRAMEBUFFER, the this -> the framebuffer); 
    glClearColor ( 0.1f , 0.1f , 0.1f , 1.0f ); 
    the glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); / / we do not use the stencil buffer 
    glEnable (GL_DEPTH_TEST);
     the this -> NormalBlendRendering (); 

    // second processing stage 
    glBindFramebuffer (GL_FRAMEBUFFER, 0 ); // return the default 
    glClearColor ( 1.0f , 1.0f ,1.0f, 1.0f);
    glClear(GL_COLOR_BUFFER_BIT);
    glDisable(GL_DEPTH_TEST);
    this->shader_quad->use();
    glBindTexture(GL_TEXTURE_2D, this->texColorBuffer);
    quad->Draw();
}    

Showing the effect of several post-processing:

1, an inverter, with 1 - color

2, the gradation of the three color components weighted output rgb

3, core processing

#version 330 core
out vec4 FragColor;

in vec2 TexCoords;

uniform sampler2D screenTexture;

const float offset=1.0f/300.0f;

void main()
{ 
    vec2 offsets[9]=vec2[](
        vec2(-offset,offset),    // 左上
        vec2( 0.0f,    offset), // 正上
        vec2( offset,  offset), // 右上
        vec2(-offset,  0.0f),   //
        vec2( 0.0f ,     0.0f ),    // the 
        vec2 (offset,   0.0f ),    // the right 
        vec2 (-offset, -offset), // lower left 
        vec2 ( 0.0f , -offset), // immediately below 
        vec2 (offset, -offset)   // bottom right 
    );
     a float Kernel [ . 9 ] = a float [] (
         . 1 , . 1 , . 1 ,
         . 1 , - . 8 , . 1 ,
         . 1 , . 1 , . 1
    );
    vec3 sampleTex[9];
    for(int i = 0; i < 9; i++)
    {
        sampleTex[i] = vec3(texture(screenTexture, TexCoords.st + offsets[i]));
    }
    vec3 col = vec3(0.0);
    for(int i = 0; i < 9; i++)
        col += sampleTex[i] * kernel[i];

    FragColor = vec4(col, 1.0);
}

Sharpen:

blurry:

Edge detection:

 

Guess you like

Origin www.cnblogs.com/chen9510/p/11419773.html