FBO (framebuffer object)

Source: Internet
Author: User

Let's sort out my understanding of FBO. FBO is the abbreviation of framebuffer object. The translation means the frame buffer. According to my understanding, OpenGL, as a graphic API, can be regarded as a paint brush, and the frame buffer can be compared to a canvas. We use OpenGL to "paint" the frame buffer ).

First, we need to establish OpenGL context to obtain a set of "Painting" tools. Taking Win32 OpenGL as an example, we need to establish a standard OpenGL context as follows:

1. Create a window class (wndclass/wndclassex ).

2. register the registerclass ).

3. Call createwindow/createwindow Wex to create a new window and return the window handle hwnd.

4. Create a device descriptor pixelformatdescriptor PFD. This struct contains various information about the OpenGL context to be created, including whether the two buffers, the number of pixel digits, the number of Z-buffer digits, and other buffer bit widths are supported.

5. Obtain the device context handle HDC of the new window, which is obtained by calling getdc (hwnd.

6. Call choosepixelformat (HDC, & PFD) to select the appropriate pixel format. If successful, return the index gluint npixelformat in pixel format.

7. Call setpixelformat (HDC, npixelformat, & PFD) using the result of the previous step to set the pixel format.

8. Call wglcreatecontext (HDC) to obtain the OpenGL context handle hglrc HRC;

9. Set the OpenGL context obtained in the previous step to the current context by calling wglmakecurrent (HDC, HRC );

I don't know the summary. I found that there are still many steps step by step. Note that most of the functions in the preceding nine steps are called to generate/apply for related windows or handles. To improve the robustness of the program, you must check the validity of the returned values in a timely manner.

Note: In Step 1 above, we set the bit width of each buffer zone in the device descriptor through parameters. If the bit width is 0, it does not mean this buffer zone.

 

 

After creating an OpenGL rendering environment, we get a paint brush. At this time, we have a default canvas, that is, our screen, default framebuffer. The rendering destination is our screen. What we draw will be displayed on the screen. This default framebuffer is associated with a series of buffers (the specific buffers and bit buffers are customized when OpenGL context is created. Generally, it is necessary to have a color buffer and a depth buffer. The template buffer and accumulate buffer are optional .). We need a color buffer to store the color of our rendered objects, and a depth buffer to perform a deep test. The specific rendering process is what OpenGL render pipeline does. The table is not detailed here.

With the emergence of new demands and the Development of OpenGL, the off-screen render technology emerged, namely, off-screen rendering (offline rendering ). I'm going to, off-screen render, offline rendering. It's actually very easy to hear. We render the object directly to the screen, that is, "online rendering ". Similarly, if we render an object somewhere else without rendering it to the screen, isn't it "offline rendering. So where can we render the screen without rendering it?

OpenGL introduced framebuffer object after a certain version. XXXX object, we have seen a lot, such as vertex buffer object and vertex array object. What is this framebuffer object?

According to my understanding, FBO (framebuffer object) is an object that can be used as a "canvas" by OpenGL to simulate the function and structure of default framebuffer. That is to say, you generate a FBO. Based on your rendering needs, you need to render all the items you want to render to the FBO you just generated, instead of rendering it directly to the screen, it looks like this. The default framebuffer has a lot of buffers that support rendering behavior, and FBO can also, but you need to manually generate, set, and bind them.

It is worth noting that the role of FBO is more like a manager who manages all the renderbuffers and textures supporting rendering. OpenGL does not allocate memory space for FBO to store the geometric and pixel data required for rendering. However, FBO has many attachment points. As the name suggests, we attach the renderbuffer and texutures that actually work and occupy the actual memory space to FBO, and FBO plays a management role. This is a bit similar to VAO. It is a set of batch "States. I think it is necessary to sort out my ideas about VAO, but I will not press it here for the time being.

Recently I fell in love with viso plotting, as shown below:

   

  

 

It seems that PDF files are not supported. Only images can be inserted. Not clear, here there is PDF: http://files.cnblogs.com/chandler00x/FBO.pdf

The following describes the usage of the two cases:

The following code describes how to bind renderbuffer to FBO.

1 gluint hfbo, htex, hdepth, hcolor; 2 3 glgenframebuffers (1, & hfbo); 4 glbindframebuffer (gl_framebuffer, hfbo); 5 6 glgenrenderbuffers (1, & hdepth ); 7 glbindrenderbuffer (gl_renderbuffer, hdepth); 8 9 // allocate space for the current renderbuffer. The format is gl_depth_component. As the name suggests, 10 // This renderbuffer must be related to depth test. 11 glrenderbufferstorage (gl_renderbuffer, gl_depth_component, m_width, m_height); 12 13 // connect the current renderbuffer to the gl_depth_attachment of the framebuffer object. 14. As the name implies, it must be related to depth test. 15 glframebufferrenderbuffer (gl_framebuffer, gl_depth_attachment, gl_renderbuffer, hdepth); 16 17 glgenrenderbuffers (1, & hcolor); 18 glbindrenderbuffer (gl_renderbuffer, hcolor ); 19 20 // allocate space for the current renderbuffer, in the format of gl_rgba21 glrenderbufferstorage (gl_renderbuffer, gl_rgba, m_width, m_height); 22 23 // connect to gl_color_attachment0 of FBO. 24 glframebufferrenderbuffer (gl_framebuffer, gl_color_attachment0, gl_renderbuffer, hcolor); 25 26 // check the integrity of framebuffer. It is necessary !!!!! 27 // very necessary !!!!! 28 glenum status = glcheckframebufferstatus (gl_framebuffer); 29 If (status! = Gl_framebuffer_complete) 30 printf ("framebuffer incomplete! \ N "); 31 else32 printf (" framebuffer complete! \ N ");

 

The following code describes how to bind texture to FBO.

 

1 gluint hfbo, htex, hdepth, hcolor; 2 3 glgenframebuffers (1, & hfbo); 4 glbindframebuffer (gl_framebuffer, hfbo); 5 6 glgenrenderbuffers (1, & hdepth ); 7 glbindrenderbuffer (gl_renderbuffer, hdepth); 8 9 // allocate space for the current renderbuffer. The format is gl_depth_component. As the name suggests, 10 // This renderbuffer must be related to depth test. 11 glrenderbufferstorage (gl_renderbuffer, gl_depth_component, m_width, m_height); 12 13 // connect the current renderbuffer to the gl_depth_attachment of the framebuffer object. 14. As the name implies, it must be related to depth test. 15 glframebufferrenderbuffer (gl_framebuffer, buffers, gl_renderbuffer, hdepth); 16 17 glactivetexture (gl_texture0); 18 glgentextures (1, & htex); 19 glbindtexture ); 20 21 // allocate space for the texture. 22 glteximage2d (gl_texture_2d, 0, gl_rgba, m_width, m_height, 0, gl_rgba, gl_unsigned_byte, null); 23 24 // bind with gl_color_attachment0 and color of FBO .. Color .. 25 glframebuffertexture2d (gl_framebuffer, gl_color_attachment0, gl_texture_2d, htex, 0); 26 27 28 // check the integrity of framebuffer. It is necessary !!!!! 29 // very necessary !!!!! 30 glenum status = glcheckframebufferstatus (gl_framebuffer); 31 if (status! = Gl_framebuffer_complete) 32 printf ("framebuffer incomplete! \ N "); 33 else34 printf (" framebuffer complete! \ N ");

 

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.