Multipass Rendering and Framebuffer Objects in OpenGL, Study notes of Computer Science

Multipass rendering, a technique used in computer graphics to combine the results of multiple rendering passes to achieve various effects such as antialiasing, motion blur, depth-of-field, and soft shadows. The document also covers the use of framebuffer objects (fbos) in opengl, which enable offscreen rendering and storage of intermediate results for multipass rendering. Topics include creating and using fbos, renderbuffers, and handling multiple color attachments.

Typology: Study notes

Pre 2010

Uploaded on 07/30/2009

koofers-user-15p
koofers-user-15p ๐Ÿ‡บ๐Ÿ‡ธ

7 documents

1 / 23

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
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
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17

Partial preview of the text

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)