FBO: frame buffer object is mainly used for render-to-texture and off-screen rendering.
Follow these steps:
1. initialize glewInit () and check whether the GPU supports the GL_EXT_FRAMEBUFFER_OBJECT extension.
2. Create FBO,
GLuint fbo, color;
// Create an FBO
GlGenFramebuffersEXT (1, & fbo );
// Create color texture
GlGenTextures (1, & color );
GlBindTexture (GL_TEXTURE_2D, color );
GlTexImage2D (GL_TEXTURE_2D, 0, GL_RGBA8, width, height, 0,
GL_RGBA, GL_UNSIGNED_BYTE, NULL );
GlGenerateMipmapEXT (GL_TEXTURE_2D); // generate mipmap
// Bind the FBO and attach color texture to it
GlBindFramebufferEXT (GL_FRAMEBUFFER_EXT, fbo );
GlFramebufferTexture2DEXT (GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_2D, color, 0 );
For an fbo, You can bind multiple GL_COLOR_ATTACHMENT (I) _ EXT, that is, you can have multiple colors.
For deep cache: Use a 32-bit float texture
Gluint depth,
GlGenTextures (1, & detph );
GlBindTexture (GL_TEXTURE_RECTANGLE_ARB, depth );
GlTexParameteri (GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_WRAP_S, GL_CLAMP );
GlTexParameteri (GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_WRAP_T, GL_CLAMP );
GlTexParameteri (GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_MIN_FILTER, GL_NEAREST );
GlTexParameteri (GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_MAG_FILTER, GL_NEAREST );
GlTexImage2D (GL_TEXTURE_RECTANGLE_ARB, 0, GL_DEPTH_COMPONENT32F_NV,
Widht, height, 0, GL_DEPTH_COMPONENT, GL_FLOAT, NULL );
GlFramebufferTexture2DEXT (GL_FRAMEBUFFER_EXT, GL_STENCIL_ATTACHMENT_EXT, GL_TEXTURE_RECTANGLE_EXT, depth, 0 );
For stencel buffer, it is currently bound with depth buffer and must support gl_ext_packed_depth_stencel extension.
The definition is as follows:
Gluint depthstencel;
GlGenTextures (1, & detphstencel );
GlBindTexture (GL_TEXTURE_2D, depthstencel );
GlTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
GlTexImage2D (GL_TEXTURE_2D, 0, GL_DEPTH24_STENCIL8_EXT, widht, height, 0,
GL_DEPTH_STENCIL_EXT, GL_UNSIGNED_INT_24_8_EXT, NULL );
GlFramebufferTexture2DEXT (GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT,
GL_TEXTURE_2D, depthstencel, 0 );
GlFramebufferTexture2DEXT (GL_FRAMEBUFFER_EXT, GL_STENCIL_ATTACHMENT_EXT,
GL_TEXTURE_2D, depthstencel, 0 );
Mipmap is not supported for the textures bound to depth and stencel buffer.
You can also use renderbuffer to bind to fbo.
Note: If you do not specify depth buffer and stencel buffer in fbo, only enable depth test will not write depth buffer!
3. Before using fbo, use glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glBindFramebufferEXT (GL_FRAMEBUFFER_EXT, fbo );
Only
GlBindFramebufferEXT (GL_FRAMEBUFFER_EXT, 0 );
4. If you specify to render multiple color buffers to one of them, use glDrawBuffer
// Render to color0
GlBindFramebufferEXT (GL_FRAMEBUFFER_EXT, fbo );
GlDrawBuffer (GL _ COLOR_ATTACHMENT0_EXT );
// Render to color1
GlDrawBuffer (GL _ COLOR_ATTACHMENT1_EXT );
5. If multiple color buffers are rendered at a time, It is Mulitple rendering target (MRT ). If you want to specify the starting buffer for rendering, use glDrawBuffers
GlBindFramebufferEXT (GL_FRAMEBUFFER_EXT, fbo );
GlDrawBuffers (GL _ COLOR_ATTACHMENT (I) _ EXT );
6. There are two ways to read the content rendered by fbo:
Define output image:
Unsigned char output_image = new unsigned char [width * height];
A, directly read the buffer,
GlReadBuffer (GL _ COLOR_ATTACHMENT (I) _ EXT)
GlReadPixels (0, 0, width, height, GL_RGB, GL_FLOAT, output_image );
B. Read the bound texture. color is the bound texture.
GlBindTexture (GL_TEXTURE_RECTANGLE_ARB, color );
GlGetTexImage (GL_TEXTURE_RECTANGLE_ARB, 0, GL_DEPTH_COMPONENT, GL_UNSIGNED_BYTE, output_image );
7. delete FBO
GlDeleteFramebuffersEXT (1, & fbo );
Differences between RBO and FBO To Texture:
When you want to make OpenGL do depth/stencel tests, but don't care about the actual values in the buffer, you will make a depth/stencel renderbuffer (no need to use textures if don't care about the values-and you can't for stencel ).
(Same if you don't care about the values in the color-buffer (perhaps you only need depth values ))
When you need to process the values of a buffer on the CPU (not the GPU) you shoshould use a renderbuffer instead of a texture
(Faster and easier than using a texture)
If you want build-in AA on the color-buffer, you have no choice but to use a renderbuffer