How to use the template buffer in FBO

Source: Internet
Author: User
Document directory
  • Common FBO frameworks
  • How to add stencel buffer in FBO
  • Use stencel buffer correctly in FBO
  • Reference
FBO common frameworks generally only require color buffer and depth buffer. The former generally uses textures, and the latter generally uses RBO. The standard procedure is as follows:
// Create // frame buffer objectglgenframebuffers (1, & fboid); glbindframebuffer (gl_framebuffer, fboid); // color buffer with texture objectglgentextures (1, & color_rboid); glbindtexture (callback, callback, color_rboid); glteximage2d (latency, 0, gl_rgba, targetwidth, targetheight, 0, gl_rgba, latency, 0); gltexparameterf (latency, latency, gl_nearest); gltexparameterf (gl_texture_2d, Response, gl_linear); gltexparameterf (response, response, gl_repeat); gltexparameterf (response, response, gl_repeat); // depth buffer with render buffer objectglgenrenderbuffers (1, & response ); glbindrenderbuffer (gl_renderbuffer, depth_rboid); glrenderbufferstorage (gl_renderbuffer, gl_depth_component24, targetwidth, targetheight); // attach color buffer to FBO Struct (gl_framebuffer, struct, gl_texture_2d, color_rboid, 0); // attach depth buffer to terminate (gl_framebuffer, callback, gl_renderbuffer, callback); // also attach as a struct (gl_framebuffer, gl_stencil_attachment, gl_renderbuffer, depth_stencil_rb); // Delete if (fboid! = 0) {gldeleteframebuffers (1, & fboid); fboid = 0;} If (depth_rboid! = 0) {gldeleterenderbuffers (1, & depth_rboid); depth_rboid = 0;} If (color_rboid) {average (1, & color_rboid); color_rboid = 0 ;}
In FBO, how to add stencel buffer to normal people will think that stencel buffer is similar to depth buffer, and thus write a framework similar to depth buffer:
// Create stencel buffer in the form of RBO glgenrenderbuffers (1, & cached); glbindrenderbuffer (gl_renderbuffer, buffers); glrenderbufferstorage (gl_renderbuffer, gl_stencil_index, targetwidth, targetheight ); // associate stencel buffer with fboglframebufferrenderbuffer (gl_framebuffer, latency, gl_renderbuffer, stencil_rboid); // Delete stencel buffergldeleterenderbuffers (1, & stencil_rboid );
However, in actual tests, it is found that stencel buffer does not work at all. The following is a test function:
void stencilRenderTest(){GLdouble dRadius = 0.1; // Initial radius of spiralGLdouble dAngle;        // Looping variablefloat x = 100;float y = 100;float rsize = 25;// Clear blue windowglClearColor(0.0f, 0.0f, 1.0f, 0.0f);// Use 0 for clear stencil, enable stencil testglClearStencil(0.0f);glEnable(GL_STENCIL_TEST);// Clear color and stencil bufferglClear(GL_COLOR_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);// All drawing commands fail the stencil test, and are not// drawn, but increment the value in the stencil buffer. glStencilFunc(GL_NEVER, 0x0, 0x0);glStencilOp(GL_INCR, GL_INCR, GL_INCR);// Spiral pattern will create stencil pattern// Draw the spiral pattern with white lines. We // make the lines  white to demonstrate that the // stencil function prevents them from being drawnglColor3f(1.0f, 1.0f, 1.0f);glBegin(GL_LINE_STRIP);for(dAngle = 0; dAngle < 400.0; dAngle += 0.1){glVertex2d(dRadius * cos(dAngle), dRadius * sin(dAngle));dRadius *= 1.002;}glEnd();// Now, allow drawing, except where the stencil pattern is 0x1// and do not make any further changes to the stencil bufferglStencilFunc(GL_NOTEQUAL, 0x1, 0x1);glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);// Now draw red bouncing square// (x and y) are modified by a timer functionglColor3f(1.0f, 0.0f, 0.0f);glRectf(x, y, x + rsize, y - rsize);}

When stenpencil is correct, the result shows a blue background, a small red rectangle, and an arc inside the rectangle, right.

The incorrect result is the concentric ring of the blue background and the red block of a rectangle. In FBO, use stencel buffer correctly. In the official wiki tutorial, find the answer:

Ever ever make a stencel buffer. all GPUs and all drivers do not support an independent stencel buffer. if you need a stencel buffer, then you need to make a depth = 24, stencel = 8 buffer, also called d24s8. please search for the example about gl_ext_packed_depth_stencer
On this page.
Never create a single template buffer. All GPUs and drivers do not support separate template buffers. If you need to create a template buffer in depth and share a buffer with the template, the format is depth_24_stencil_8.

The Code is as follows:

// Create // frame buffer objectglgenframebuffers (1, & fboid); glbindframebuffer (gl_framebuffer, fboid); // color buffer with texture objectglgentextures (1, & color_rboid); glbindtexture (callback, callback, color_rboid); glteximage2d (latency, 0, gl_rgba, targetwidth, targetheight, 0, gl_rgba, latency, 0); gltexparameterf (latency, latency, gl_nearest); gltexparameterf (gl_texture_2d, Response, gl_linear); gltexparameterf (gl_texture_2d, response, gl_repeat); gltexparameterf (response, response, gl_repeat); // depth buffer and stenpencil terminate (1, & response ); glbindrenderbuffer (gl_renderbuffer, depth_stencil_rb); glrenderbufferstorage (gl_renderbuffer, gl_depth24_stencil8, targetwidth, targetheight); // not gl_depth_component2 4 but buffers // attach color buffer to encode (gl_framebuffer, gl_color_attachment0, gl_texture_2d, color_rboid, 0); // attach depth buffer to encode (gl_framebuffer, callback, gl_renderbuffer, callback ); // also attach as a stencilglframebufferrenderbuffer (gl_framebuffer, gl_stencil_attachment, gl_renderbuffer, depth_stencil_rb );// Delete if (fboid! = 0) {gldeleteframebuffers (1, & fboid); fboid = 0;} If (depth_stencil_rb! = 0) {gldeleterenderbuffers (1, & depth_stencil_rb); Limit = 0;} If (color_rboid) {gldeletextures (1, & color_rboid); color_rboid = 0 ;}
Reference

OpenGL official wiki: https://www.opengl.org/wiki/Framebuffer_Object_Examples

History:

1. Correct FBO frame buffer creation bug on February 14,. the renderbuffer format should be gl_depth24_stencil8 instead of gl_depth_component24.

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.