Step by step how to use EXT_framebuffer_object.
Check extenstion
- - (BOOL) initFBO
- {
- // check extension
- const GLubyte* extensions = glGetString ( GL_EXTENSIONS );
- if ( extensions )
- {
- if(gluCheckExtension((GLubyte*)("GL_EXT_framebuffer_object"), extensions))
- return YES;
- }
- return NO;
- }
сreate the texture for framebuffer
- GLuint textureId
- glGenTextures ( 1, &textureId );
- glBindTexture ( GL_TEXTURE_2D, textureId );
- glTexParameteri ( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST );
- glTexParameteri ( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST );
- glTexImage2D ( GL_TEXTURE_2D, 0, 4, TextureWidth, TextureHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0 );
сreate framebuffer (FBO)
- GLuint frameBufferId;
- glGenFramebuffersEXT ( 1, &frameBufferId ); // 0 - main buffer, framebufferId > 0
- glBindFramebufferEXT ( GL_FRAMEBUFFER_EXT, frameBufferId );
- glFramebufferTexture2DEXT ( GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT,
- GL_TEXTURE_2D, textureId, 0 ); // attach texture to fbo
- // check fbo ( glCheckFramebufferStatusEXT )
- // ...
- glBindFramebufferEXT ( GL_FRAMEBUFFER_EXT, 0 ); // select main buffer
сheck framebuffer
- GLenum status = glCheckFramebufferStatusEXT ( GL_FRAMEBUFFER_EXT );
- if ( status != GL_FRAMEBUFFER_COMPLETE_EXT )
- {
- switch ( status )
- {
- case GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT_EXT:
- NSLog(@"FrameBuffer Error - GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT_EXT");
- break;
- case GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT_EXT:
- NSLog(@"FrameBufferErGL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT_EXT");
- break;
- case GL_FRAMEBUFFER_INCOMPLETE_DIMENSIONS_EXT:
- NSLog(@"FrameBuffer Error - GL_FRAMEBUFFER_INCOMPLETE_DIMENSIONS_EXT");
- break;
- case GL_FRAMEBUFFER_INCOMPLETE_FORMATS_EXT:
- NSLog(@"FrameBuffer Error - GL_FRAMEBUFFER_INCOMPLETE_FORMATS_EXT");
- break;
- case GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER_EXT:
- NSLog(@"FrameBuffer Error - GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER_EXT");
- break;
- case GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER_EXT:
- NSLog(@"FrameBuffer Error - GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER_EXT");
- break;
- case GL_FRAMEBUFFER_UNSUPPORTED_EXT:
- NSLog(@"FrameBuffer Error - GL_FRAMEBUFFER_UNSUPPORTED_EXT");
- break;
- default:
- NSLog(@"FrameBuffer Error - Unknown error");
- break;
- }
- return NO;
- }
use in the code
- glBindFramebufferEXT ( GL_FRAMEBUFFER_EXT, frameBufferId ); // select fbo
- glClearColor ( 0.0f, 0.0f, 1.0f, 1.0f );
- glClear ( GL_COLOR_BUFFER_BIT );
- // render into texture ( update model view transforms )
- // ...
- glBindFramebufferEXT ( GL_FRAMEBUFFER_EXT, 0 ); // select main buffer
We can use the texture for post-effects. Do not forget to delete resources ;-)
Example OpenGL FBO (github)