Download Multipass Rendering and Framebuffer Objects in OpenGL and more Study notes Computer Science in PDF only on Docsity!
Multipass rendering
Combine results of multiple rendering passes to achieve
some effect.
Some parameters differ from one pass to the next.
๎ Textures differ
๎ Multitexturing, lightmapping
๎ Note: the hardware directly supports single pass multitexturing now
๎ Lighting differs
๎ Large numbers of light sources, soft shadows
๎ Object position differs
๎ Motion blur
๎ Camera position changes
๎ Antialiasing, Depth-of-field
๎ Camera aperture / exposure time
๎ High dynamic range images
Multipass rendering
The way multiple passes are combined depends on the
application
๎ Addition, multiplication and other simple arithmetic
operations can be applied using alpha blending or the
accumulation buffer.
๎ More complex interactions can be achieved in the
programmable fragment shaders.
๎ Shaders can read from textures, so we would like to render-
to-texture for each individual pass.
๎ A final pass combines (or composites) all passes/textures.
Other multipass effects
Antialiasing Motion blur Depth-of-field Soft shadows
High dynamic range images
Framebuffer Objects (FBOs)
An EXT extension
๎ Function names and enumerated values have EXT suffix.
FBO state consists of a set of โattachment pointsโ
๎ Can attach multiple textures and render to multiple textures
๎ Multiple color textures
๎ Depth texture
๎ Stencil texture
Some constraints
๎ Attached textures must have same width, height
Creating a Framebuffer Object
๎ glGenFrameBuffers(n, *ids)
๎ Get an unused id
๎ glBindFrameBuffer(target, id)
๎ Make FBO active
๎ glFrameBufferTexture2D(target, attachment,
texture_target, mipmap_level)
๎ Attach a texture image to the FBO
๎ Warning: textures cannot support simultaneous read/write
access.
๎ Unbind texture from texture target (disable reading) before attaching
to FBO.
๎ Unbind FBO or detach texture from FBO (disable writing) before
rendering with the texture.
Creating a Framebuffer Object
๎ glFrameBufferTexture2D(target, attachment,
texture_target, texture_id, mipmap_level)
๎ texture_id
๎ ID of texture to attach
๎ Use 0 to detach textures
๎ mipmap_level: 0 when not using mipmaps
๎ Use glFrameBufferTexture3D to attach slices of a 3D
texture
Renderbuffers
๎ The FBO extension introduces a new object type: the
renderbuffer
๎ Renderbuffer images cannot be read from (like a texture
can), but may be needed during the render phase
๎ Ex. You may need a depth buffer to achieve hidden surface removal
when rendering to texture. If you do not need to read the depth
values later, attach a renderbuffer rather than a texture.
๎ Creation
๎ glGenRenderBuffers(n, *ids)
๎ glBindRenderBuffer(target, id)
๎ target = GL_RENDERBUFFER_EXT
Selecting Render Targets
To use the FBO you must bind it and tell OpenGL to
render into it
๎ glDrawBuffer(buffer)
๎ Subsequent rendering calls will draw to buffer
๎ GL_NONE
๎ GL_FRONT, GL_BACK for double buffering
๎ GL_COLOR_ATTACHMENT0_EXT or another FBO
attachment
๎ glDrawBuffers(n, *buffers)
๎ Specify multiple render targets
๎ Later we will use fragment shaders to write to multiple
buffers
Framebuffer Completeness
๎ The texture attached to GL_DEPTH_ATTACHMENT must
have format GL_DEPTH_COMPONENT
๎
At least one image must be attached to FBO
๎ All images must have same width, height
๎ All color attachments have the same internal format
๎ There may be hardware specific combinations which are
unsupported.
๎
Use glCheckFrameBufferStatus to check for errors
Sample Creation
glGenRenderbuffersEXT(1, &Depthbuffer_ID); glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, Depthbuffer_ID); glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_DEPTH_COMPONENT, 256, 256); glGenFramebuffersEXT(1, &FBO_ID); glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, FBO_ID); glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_2D, TexID, 0); glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_RENDERBUFFER_EXT, Depthbuffer_ID); CheckFramebufferStatus(); glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0); //unbind FBO
Sample Usage
glPushAttrib(GL_VIEWPORT_BIT); //push old viewport onto attribute stack glViewport(0, 0, 256, 256); //set viewport to size of attachedtexture glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, FBO_ID); glDrawBuffer(GL_COLOR_ATTACHMENT0_EXT); //select which attachment to render into glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); //clear attached texture and renderbuffer glEnable(GL_DEPTH_TEST); //draw objects into FBO glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0); //unbind FBO glDrawBuffer(GL_BACK); //resume drawing to back buffer glPopAttrib(); //restore viewport
Application: Dynamic Cubemap
//Create cubemap glBindTexture(GL_TEXTURE_CUBE_MAP, CubeID); for(int i=0; i<6; i++) { glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X+i, 0, GL_RGB, 256, 256, 0, GL_RGB, GL_UNSIGNED_BYTE, 0); // note: cubemap image targets are sequential, so we can use a loop here. // note: pointer is 0 so texture image is allocated, but not initialized. } //set cubemap parameters glTexParameteri( GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S,GL_CLAMP_TO_EDGE ); glTexParameteri( GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE ); glTexParameteri( GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_LINEAR ); glTexParameteri( GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_LINEAR ); glBindTexture(GL_TEXTURE_CUBE_MAP, 0); //Create renderbuffer for depth glGenRenderbuffersEXT(1, &Depthbuffer_ID); glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, Depthbuffer_ID); glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_DEPTH_COMPONENT, 256, 256); //note: size is the same as color textures Creation
Application: Dynamic Cubemap
//Create FBO glGenFramebuffersEXT(1, &FBO_ID); glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, FBO_ID); //attach cube faces to the FBO color attachments for(int i=0; i<6; i++) { glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT+i, GL_TEXTURE_CUBE_MAP_POSITIVE_X+i, CubeID, 0); // note: FBO attachments are sequential, so we can use a loop here. } //attach depth renderbuffer to FBO glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_RENDERBUFFER_EXT, Depthbuffer_ID); CheckFramebufferStatus(); glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0); Creation (cont'd)