






Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!







mjb – July 31, 2007
mjb – July 31, 2007
Color Lighting Transf. Color
Texture Display Dest.
Texture1 Texture2 Texture
mjb – July 31, 2007
More Background – What is an OpenGL “Object”?
mjb – July 31, 2007
More Background – How do you Create an OpenGL “Object”?
mjb – July 31, 2007
The Overall Render-to-Offscreen-Framebuffer Process
mjb – July 31, 2007
1. In InitGraphics( ), generate Framebuffer and Renderbuffer handles:
GLuint FrameBuffer; GLuint ColorBuffer; GLuint DepthBuffer;
glGenFramebuffers( 1, &FrameBuffer ); glGenRenderbuffers( 1, &ColorBuffer ); glGenRenderbuffers( 1, &DepthBuffer );
2. Setup the size you want the offscreen rendering to be :
int sizeX = 2048; int sizeY = 2048;
You will reference these sizes a few times, so it is a good idea to use variables for the sizes and not hardcode them as numbers in function calls!
3. Bind the offscreen framebuffer to be the current output display:
glBindFramebuffer( GL_FRAMEBUFFER, FrameBuffer );
Code for the Render-to-Offscreen-Framebuffer Process
FBO
mjb – July 31, 2007
5. Bind the Color Buffer to the context, allocate its storage, and attach it to the Framebuffer:
glBindRenderbufferEXT( GL_RENDERBUFFER_EXT, ColorBuffer ); glRenderbufferStorageEXT( GL_RENDERBUFFER_EXT, GL_RGBA, sizeX, sizeY ); glFramebufferRenderbufferEXT( GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_RENDERBUFFER_EXT, ColorBuffer );
4. Bind the Depth Buffer to the context, allocate its storage, and attach it to the Framebuffer:
glBindRenderbufferEXT( GL_RENDERBUFFER_EXT, DepthBuffer ); glRenderbufferStorageEXT( GL_RENDERBUFFER_EXT, GL_DEPTH_COMPONENT, sizeX, sizeY ); glFramebufferRenderbufferEXT( GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_RENDERBUFFER_EXT, DepthBuffer );
6. Check to see if OpenGL thinks the framebuffer is complete enough to use :
GLenum status = glCheckFramebufferStatusEXT( GL_FRAMEBUFFER_EXT ); if( status != GL_FRAMEBUFFER_COMPLETE_EXT ) fprintf( stderr, "FrameBuffer is not complete.\n" );
Code for the Render-to-Offscreen-Framebuffer Process
FBO
Depth RBO
Depth RBO
FBO
Color RBO
FBO
Color RBO
Depth RBO
mjb – July 31, 2007
7. Now, render as you normally would. Be sure to set the viewport to match the size of the color and depth buffers:
glClearColor( 0.0, 0.2, 0.0, 1. ); glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ); glEnable( GL_DEPTH_TEST ); glShadeModel( GL_FLAT ); glViewport( 0, 0, sizeX, sizeY );
glMatrixMode( GL_PROJECTION ); glLoadIdentity( ); gluPerspective( 90., 1.,0.1, 1000. );
glMatrixMode( GL_MODELVIEW ); glLoadIdentity(); gluLookAt( 0., 0., 3., 0., 0., 0., 0., 1., 0. );
glTranslatef( TransXYZ[0], TransXYZ[1], TransXYZ[2] ); glMultMatrixf( RotMatrix ); glScalef( scale, scale, scale ); glColor3f( 1., 1., 1. );
glutWireTeapot( 1. );
Code for the Render-to-Offscreen-Framebuffer Process
mjb – July 31, 2007
The Overall Render-to-Texture Process
mjb – July 31, 2007
1. In InitGraphics( ), generate a FrameBuffer handle, a RenderBuffer handle, and a Texture handle:
GLuint FrameBuffer; GLuint DepthBuffer; GLuint Texture;
glGenFramebuffers( 1, &FrameBuffer ); glGenRenderBuffers( 1, &DepthBuffer ); glGenTextures( 1, &Texture );
2. Setup the size you want the texture rendering to be :
int sizeX = 2048; int sizeY = 2048;
4. Bind the Depth Buffer to the context, allocate its storage, and attach it to the Framebuffer:
glBindRenderbufferEXT( GL_RENDERBUFFER_EXT, DepthBuffer ); glRenderbufferStorageEXT( GL_RENDERBUFFER_EXT, GL_DEPTH_COMPONENT, sizeX, sizeY ); glFramebufferRenderbufferEXT( GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_RENDERBUFFER_EXT, DepthBuffer );
Code for the Render-to-Texture Process
3. Bind the offscreen framebuffer to be the current output display:
glBindFramebufferEXT( GL_FRAMEBUFFER, FrameBuffer );
mjb – July 31, 2007
5. Bind the Texture to the Context:
glBindTexture( GL_TEXTURE_2D, Texture );
7. Tell OpenGL that you are going to render into the color planes of the Texture:
glFramebufferTexture2DEXT( GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, Texture, 0 );
6. Setup a NULL texture of the size you want to render into and set its properties:
glTexImage2D( GL_TEXTURE_2D, 0, 4, sizeX, sizeY, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL ); glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP ); glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP ); glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR ); glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE );
8. Check to see if OpenGL thinks the framebuffer is complete enough to use :
GLenum status = glCheckFramebufferStatusEXT( GL_FRAMEBUFFER_EXT ); if( status != GL_FRAMEBUFFER_COMPLETE_EXT ) fprintf( stderr, "FrameBuffer is not complete.\n" );
Code for the Render-to-Texture Process
mjb – July 31, 2007
9. Now, render as you normally would. Be sure to set the viewport to match the size of the color and depth buffers:
glClearColor( 0.0, 0.2, 0.0, 1. ); glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ); glEnable( GL_DEPTH_TEST ); glShadeModel( GL_FLAT ); glViewport( 0, 0, sizeX, sizeY );
glMatrixMode( GL_PROJECTION ); glLoadIdentity( ); gluPerspective( 90., 1.,0.1, 1000. );
glMatrixMode( GL_MODELVIEW ); glLoadIdentity( ); gluLookAt( 0., 0., 3., 0., 0., 0., 0., 1., 0. );
glTranslatef( TransXYZ[0], TransXYZ[1], TransXYZ[2] ); glMultMatrixf( RotMatrix ); glScalef( scale, scale, scale ); glColor3f( 1., 1., 1. );
glutWireTeapot( 1. );
10. Tell OpenGL to go back to rendering to the hardware framebuffer:
glBindFramebufferEXT( GL_FRAMEBUFFER, 0 );
Code for the Render-to-Texture Process
mjb – July 31, 2007
Rendering to a Texture and then using that Texture as both a Rendering
Texture and as an Input to the Next Iteration: The Game of Life
mjb – July 31, 2007
Using GLSL, you Can Bind Multiple Color Framebuffers
and Write Separately to Each of Them
Usually in a GLSL Fragment Shader, you write color information to the vec4 gl_FragColor:
gl_FragColor = vec4( r, g, b, 1. ); // full color
Instead, you can write color information to an array of vec4’s called gl_FragData[ ]:
gl_FragData[0] = vec4( r, 0., 0., 1. ); // red component gl_FragData[1] = vec4( 0., g, 0., 1. ); // green component gl_FragData[2] = vec4( 0., 0., b, 1. ); // blue component
Your OpenGL program must then declare what color attachment each gl_FragData[ ] element corresponds to:
gluint buffers[ ] = { GL_COLOR_ATTACHMENT2_EXT, // gl_FragData[0] GL_COLOR_ATTACHMENT1_EXT, // gl_FragData[1] GL_COLOR_ATTACHMENT3_EXT // gl_FragData[2] };
glDrawBuffers( 3, buffers );