+ + Nuclear gray frame buffer (convolution matrix) + + Fuzzy edge detection

Here it comes to more advanced topics, the difficulty is higher

A: frame buffer

  1. Color values ​​for writing the color buffer, depth buffer for writing depth information and allow us to discard a particular segment based on some conditions stencil buffer. The combination is called the frame buffer buffer (Framebuffer), which is stored in memory. OpenGL allows us to define our own frame buffer
  2. We are all operations are done in the default frame buffer rendering the buffer conducted. The default frame buffer is generated and configured when you create a window of (GLFW help us to do these). With our own frame buffer, we can have more ways to render the
  3. Create a frame buffer:
    3.1 We will use a function called glGenFramebuffers to create a frame buffer object (Framebuffer Object, FBO).
    3.2 First, we create a frame buffer object, it will bind to the active (Active) frame buffer, do something, after unbundling the frame buffer. We used to bind glBindFramebuffer frame buffer.
    After 3.3 GL_FRAMEBUFFER bound to the target, all reads and writes to the frame buffer will affect the operation of the frame buffer currently bound.
    3.4 a complete frame buffer need to meet the following conditions:
    • At least one additional cushion (color, depth or stencil buffer).
    • At least one color attachment (Attachment).
    • All attachments must be complete (reserved memory).
    • Each buffer should have the same number of samples.
    3.5 We need to create some attachment to the frame buffer, and annex attached to the frame buffer. After the completion of all conditions, we can GL_FRAMEBUFFER call glCheckFramebufferStatus as a parameter, check the frame buffer is complete. It will detect the frame buffer currently bound, and specification of which returns one of these values. If it returns GL_FRAMEBUFFER_COMPLETE, the frame buffer is full.
    3.6 After all the rendering operation will render the attachment in the currently bound frame buffer. Because of our frame buffer than the default frame buffer, the rendering visual output instruction will not have any impact on the window. For this reason, to render a different frame buffer is called off-screen rendering (Off-screen Rendering). To ensure that all visual effects rendering operation in the main window, we need to re-activate the default frame buffer, to bind it to 0.
    3.7 After all frame buffer operation, do not forget to remove the frame buffer objects:
    3.8 Prior to the implementation of an integrity check, we need to attach an attachment to the frame buffer. Attached is a memory location, it can be used as a buffer of the frame buffer, it can be thought of as one image. When you create an attachment when we have two options: textures or render buffer object (Renderbuffer Object).
  4. Texture Annex:
    4.1 when the texture is attached to a frame buffer when all rendering commands will be written into the texture , it is wanted an ordinary color / or depth buffer as a template. The advantage of using textures is that all the rendering operation results will be stored in a texture image, then we can use it easily in the shader.
    4.2 In order to create a frame buffer and texture to create a texture similar to normal: The main difference is that we will set the dimensions for the screen size (although this is not necessary), and we give texture data parameters passed NULL. For this texture, we only allocate memory without filling it. We will fill this texture to render the frame buffer subsequently carried out. Also note that we do not care surround mode or multi-stage texture and further away, we will not need them in most cases.
    4.3 Now that we have created a texture, the last thing to do is attach it to the frame buffer: glFramebufferTexture2D (GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, the Texture, 0);
    4.4 glFrameBufferTexture2D has the following parameters:
    • target: target frame buffer (rendering, or both read)
    • attachment: We want to attach the attachment type. We are currently attach a color accessories. Note that the last of 0 means that we can attach multiple color attachments. We will be mentioned later in the tutorial.
    • textarget: you want to attach to the texture type
    • texture: the texture you want to attach itself
    • level: the level of multi-level further away textures. We leave it to zero.
    4.5 Annex addition to the color, we also attach a texture to the depth and stencil buffer frame buffer object. To attach a depth buffer, we will attachment type to GL_DEPTH_ATTACHMENT. Note that the texture format (Format) and the internal format (Internalformat) type becomes GL_DEPTH_COMPONENT, to reflect the depth buffer storage format. To attach stencil buffer, you want the second parameter is set to GL_STENCIL_ATTACHMENT, and the texture format is set to GL_STENCIL_INDEX.
    4.6 and depth buffer may be attached as a separate buffer template texture. Each 32-bit value containing the texture information and 24-bit depth of 8 template information. To depth and stencil buffer attached as a texture, we use GL_DEPTH_STENCIL_ATTACHMENT type, format and configure the texture, depth and stencil values it contains merged.
  5. Render buffer object attachments
    5.1 render buffer object (Renderbuffer Object) is introduced into the after OpenGL texture in a frame buffer as the type of attachment is available, so in the past is the only available attachment texture. And the same texture image, render buffer object is a real buffer, i.e., number of bytes, integers, pixels and the like. Additional benefits buffer object is rendered, it will store data for the OpenGL rendering the native format, which is rendered into the offscreen frame buffer optimized.
    5.2 render buffer object directly to store all of its data to render buffer, will not do any conversion format for the texture, it quickly becomes a writable storage medium. However, the rendering buffer objects are usually write-only, so you can not read them (for example, using textures access) . Of course, you can still use glReadPixels still read it, it would be from the frame buffer currently bound, rather than the annex itself, returned in a specific area of the pixel.
    5.3 because the data is already in the native format, or copy it when writing data into the other buffer is very fast . Therefore, the buffer exchange buffer will be very fast when rendering objects using such an operation. We glfwSwapBuffers render each iteration last used, can also be achieved by rendering buffer object: just write a rendered image buffer, and finally switched to another rendering buffer on it.
    5.4 to create a buffer of code and object code frame buffer rendering is very similar. All rendering buffer operation similar impact, we need to bind the render buffer object, so that after the current rbo.
    5.5 When we do not need these samples from the buffer, usually choose to render buffer object, because it would be a little more optimized.
    5.6 to create a depth and stencil buffer object rendering can be done by calling glRenderbufferStorage function.
    5.7 Creating a render buffer object and texture similar objects, except that the object is specifically designed for use as an image, not as common texture data buffer (General Purpose Data Buffer). Here we choose GL_DEPTH24_STENCIL8 as an internal format, which encapsulates the 24-bit and 8-bit depth stencil buffer.
    5.8 The last thing this additional render buffer object.
  6. Render buffer object can provide you with some optimization of the frame buffer objects, but know when to use render buffer object, when to use the texture is very important.
    6.1 The general rule is, if you do not need to sample data from a buffer, then use this buffer to render buffer object would be a wise choice.
    6.2 If you need to sample color data or the like from the depth buffer values, then you should select the texture attachment. Terms of performance it does not have a very big impact.

Two: Render to Texture

  1. We will be appended to the scene render to a texture color frame buffer object, then this will draw a texture across the quadrangle on the entire screen. Such visual output and did not use the frame buffer is exactly the same, but this time to print on a quadrilateral.
  2. First create a frame buffer object and bind it, these are very intuitive.
  3. Next we need to create a texture image, we will attach it to the frame buffer as a color attachment. We set the texture dimension to the width and height of the window, and does not initialize its data:
    // 生成纹理 unsigned int texColorBuffer; glGenTextures(1, &texColorBuffer); glBindTexture(GL_TEXTURE_2D, texColorBuffer); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 800, 600, 0, GL_RGB, 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); // 将它附加到当前绑定的帧缓冲对象 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texColorBuffer, 0);
  4. We also want to be able to OpenGL depth test (if you need it as well as template test), so we also need to add a depth (and template) attachment to the frame buffer. Since we only want to sample the color buffer, rather than the other buffer , we can create a render buffer object for them. Remember when we do not need sample buffer, render buffer object it is a better choice
  5. Create a render buffer object is not very complicated. The only thing we need to remember is that we will render it creates a buffer object for the depth and stencil attachments. We will set its internal format GL_DEPTH24_STENCIL8, for us, this accuracy is sufficient.
    unsigned int rbo; glGenRenderbuffers(1, &rbo); glBindRenderbuffer(GL_RENDERBUFFER, rbo); glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH24_STENCIL8, 800, 600); glBindRenderbuffer(GL_RENDERBUFFER, 0); glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_RENDERBUFFER, rbo);
  6. Finally, we want to check whether the frame buffer is full, if not, we will print an error message.
  7. Remember to unbundling frame buffer to ensure that we do not accidentally rendered to the wrong frame buffer.
  8. Now the frame buffer to complete, and we only need to bind the frame buffer objects, let rendered to the frame buffer of the buffer rather than the default frame buffer. After rendering frame buffer instruction will affect the current binding. All the depth and stencil operations will (if any) read from the depth and stencil attachments currently bound frame buffer of. If you ignore the depth buffer, then all depth testing operations will no longer work, because there is no depth buffer currently bound frame buffer.
  9. So, in order to render the scene to a texture, we need to take the following steps:
    • The new frame buffer to bind to the active frame buffer, and render the scene as usual
    • Binds the default frame buffer
    • Draw a quadrilateral across the entire screen, the color of the frame buffer buffer as its texture.

Four: Invert:

Guess you like

Origin www.cnblogs.com/GarrettWale/p/11344498.html