Advanced OpenGLFrom last time …Topics for todayOpenGL extensionsGLEWImmediate mode versus display listsDisplay listsAn exampleDisplay listsDisplay Lists and HierarchyEfficient primitive representationsVertex arraysOpenGL Vertex ArraysMultipass renderingRender-to-textureInitializing an FBOUsing an FBOOpenGL accumulation bufferOpenGL accumulation bufferFull Scene AntialiasingFull Scene AntialiasingFull Scene AntialiasingDepth of FieldGetting to the framebufferScissor TestAlpha TestStencil BufferStencil BufferCreating a MaskAlpha blendingAlpha Blending ModesDitheringLogical Operations on PixelsAntialiasingFogAssignment #3Next time3/05/071Advanced OpenGLComputer GraphicsCOMP 770 (236)Spring 2007Instructor: Brandon Lloyd3/05/072From last time …■ Flexible shading° shading networks° shading languages■ GLSL° language features° interfacing shaders with the application3/05/073Topics for today■ Extensions■ Performance optimizations° Display lists° Triangle strips and fans° Vertex arrays / vertex buffer objects■ Multipass rendering° Render-to-texture■ Accumulation buffer■ Back end operations° Scissor test, stencil test, alpha test, blending, etc.3/05/074OpenGL extensions■ OpenGL has a flexible extension mechanism■ Tokens an functions from extension have special suffixes° _NV or _ATI - vendor specific° _EXT - general extensions° _ARB - approved by architecture review board■ ARB extensions are then folded into later versions of OpenGL. ° features from later versions are accessed through extension mechanism3/05/075GLEW■ The OpenGL Extension Wrangler (GLEW) hides details of initializing extensions° http://glew.sourceforge.net/glewInit();if (GLEW_EXT_framebuffer_object){/* It is safe to use the EXT_framebuffer_object extension here. */}if (GLEW_VERSION_2_0){/* It is safe to use version 2.0 functions. */glAttachShader(programID, codeStr);}3/05/076Immediate mode versus display listsImmediate ModeDisplayListPolynomialEvaluatorPer VertexOperations &PrimitiveAssemblyRasterizationPer FragmentOperationsTextureMemoryCPPixelOperationsFrameBuffer3/05/077Display lists■ Creating a display listdef init(void):global idid = glGenLists( 1 )glNewList( id, GL_COMPILE )# various OpenGL routinesglEndList()■ Call a created listdef display():glCallList( id )CPUCPUDLDLPoly.Poly.PerVertexPerVertexRasterRasterFragFragFBFBPixelPixelTextureTextureglNewList also accepts the constant GL_COMPILE_AND_EXECUTE, which both creates and executes a display list.If a new list is created with the same identifying number as an existing display list, the old list is replaced with the new calls.3/05/078An examplecow = Nonedef drawCow(color):global cow, cowIDif (cow == None):cow = WaveFrontOBJ(‘cow.obj’)cowID = glGenLists(1)glNewList(cowID, GL_COMPILE)glPushMatrix()glScaled(0.8,0.8,0.8)glTranslated(0,-cow.min.y,0)cow.draw()glPopMatrix()glEndList()glEnable(GL_LIGHTING)glMaterialfv(GL_FRONT, GL_AMBIENT, color)glMaterialfv(GL_FRONT, GL_DIFFUSE, color)glCallList(cowID)def drawScene():drawFloor()glTranslated(0,0,6)drawCow([0.6, 0.0, 0.8, 1.0])glTranslated(0,0,6)drawCow([0.8, 0.6, 0.0, 1.0])glTranslated(-12,0,-6)drawCow([0.0, 0.6, 0.8, 1.0]). . .3/05/079Display lists■ Not all OpenGL routines can be stored in display lists■ Display lists can call other display lists■ Display lists are not editable, but you can fake it° make a list (A) which calls other lists (B, C, and D)° delete and replace B, C, and D, as needed■ Display lists can provide a significant speed-ups over immediate mode3/05/0710Display Lists and Hierarchy■ Consider model of a car° Create display list for chassis° Create display list for wheelglNewList( CAR, GL_COMPILE )glCallList( CHASSIS )glTranslatef( … )glCallList( WHEEL )glTranslatef( … )glCallList( WHEEL )…glEndList()3/05/0711Efficient primitive representations■ Triangle strips and fans avoid specifying redundant vertex information■ Each triangle specified with about 1 vertex on average123456789101112Triangle Strip123456789Triangle Fan3/05/0712Vertex arrays■ Vertex arrays allow for more efficient memory access■ Vertex arrays can specify a batch of vertices in a single call■ Indexed vertex arrays avoid specifying redundant vertex information■ Can specify vertex data in a wide variety of formats■ Vertex arrays can be stored on the GPU in Vertex Buffer Objects (VBOs)3/05/0713OpenGL Vertex Arrays■ Specify locations of the vertex attribute arrays ■ Enable the arrays and render■ Render with glDrawArrays or glDrawElements()glVertexPointer( 3, GL_FLOAT, 0, coords)glColorPointer( 4, GL_FLOAT, 0, colors)glEnableClientState( GL_VERTEX_ARRAY)glEnableClientState( GL_COLOR_ARRAY)glDrawArrays( GL_TRIANGLE_STRIP, 0, numVerts)-or-glDrawElements( GL_TRIANGLE_STRIP, 20, GL_SHORT, indices )ColordataVertexdataProcesses each vertex sequentiallyAllows random access to vertices, thus supporting sharing3/05/0714Multipass rendering■ Breaks up complex rendering into more than one rendering pass. ■ Can combine passes into the frame buffer with blending■ Can use multiple passes to store results in texturesthat can be used in subsequent rendering passes° Dynamic shadow maps° Dynamic cube maps° GPGPU3/05/0715Render-to-texture■ glCopyTexImage2D() copies the frame buffer to a texture –but slow■ The Frame Buffer Object (FBO) extension allows rendering directly to a texture° Allows you to attach textures to use as color and depth buffers° Buffers have to be the same size° Not all possible combinations are allowed° Can also attach render buffersin place of textures■ Aaron Lefohn’s FBO class provides encapsulation of details° http://sourceforge.net/projects/gpgpu/3/05/0716Initializing an FBO// initialize texturesglBindTexture( GL_TEXTURE_2D, colorTexID );... set texture parameters ...glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, fboW, fboH, 0,GL_RGBA, GL_UNSIGNED_BYTE, NULL ); glBindTexture( GL_TEXTURE_2D, depthTexID );... set texture parameters ...glTexImage2D( GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, fboW, fboH, GL_DEPTH_COMPONENT, GL_UNSIGNED_INT, NULL );// initialize fbofbo = new FramebufferObject();fbo->Bind();fbo->AttachTexture( GL_TEXTURE_2D, colorTexID,GL_COLOR0_ATTACHEMENT_EXT );fbo->AttachTexture( GL_TEXTURE_2D, depthTexID,GL_DEPTH_ATTACHMENT_EXT );if( !fbo->IsValid() )printf("FBO format not supported.\n" );fbo->Disable();3/05/0717Using an FBOfbo->Bind();glClearColor(1,1,1,1);glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );// setup the
View Full Document