Docsity

Rendering to Offscreen Framebuffer and Texture in OpenGL, Study notes of Computer Science

Toggle sidebar
bg1
1
mjb – July 31, 2007
Rendering to an Offscreen Framebuffer
and
Rendering to a Texture
Mike Bailey
Oregon State University
mjb – July 31, 2007
Preliminary Background – the OpenGL Rendering Context
The OpenGL Rendering Context contains all the characteristic information
necessary to produce an image from geometry. This includes
transformations, colors, lighting, textures, where to send the display, etc.
Context
Color Transf.Lighting Color
Texture0 Display
Dest.
Texture1 Texture2 Texture3
Some of these characteristics have a default value (e.g., lines are white, the
display goes to the screen) and some have nothing (e.g., no textures exist)
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download Rendering to Offscreen Framebuffer and Texture in OpenGL and more Study notes Computer Science in PDF only on Docsity!

mjb – July 31, 2007

Rendering to an Offscreen Framebuffer

and

Rendering to a Texture

Mike Bailey

Oregon State University

mjb – July 31, 2007

Preliminary Background – the OpenGL Rendering Context

The OpenGL Rendering Context contains all the characteristic information

necessary to produce an image from geometry. This includes

transformations, colors, lighting, textures, where to send the display, etc.

Context

Color Lighting Transf. Color

Texture Display Dest.

Texture1 Texture2 Texture

Some of these characteristics have a default value (e.g., lines are white, the

display goes to the screen) and some have nothing (e.g., no textures exist)

mjb – July 31, 2007

More Background – What is an OpenGL “Object”?

An OpenGL Object is pretty much the same as a C++, C#, or Java object: it

encapsulates a group of data items and allows you to treat them as a single

whole. For example, a Texture Object could be created in C++ by:

class TextureObject

enum minFilter, maxFilter;

enum storageType;

int numComponents;

int numDimensions;

int numS, numT, numR;

void *image;

Then, you could create any number of Texture Object instances, each with its

own characteristics encapsulated within it. When you want to make that

combination current, you just need to bring in (“bind”) that entire object. You

don’t need to deal with the information one piece of information at a time.

mjb – July 31, 2007

More Background – How do you Create an OpenGL “Object”?

In C++, objects are pointed to by their address.

In OpenGL, objects are pointed to by an unsigned integer handle. You can

assign a value for this handle yourself (not recommended), or have OpenGL

generate one for you that is guaranteed to be unique. For example:

GLuint texA;

glGenTextures( 1, &texA );

This doesn’t actually allocate memory for the texture object yet, it just

acquires a unique handle. To allocate memory, you need to bind this handle to

the Context.

mjb – July 31, 2007

Bind the Color Renderbuffer Object to the Context

Assign storage attributes to it

Attach it to the Framebuffer Object

Bind the Depth Renderbuffer Object to the Context

Assign storage attributes to it

Attach it to the Framebuffer Object

The Overall Render-to-Offscreen-Framebuffer Process

You will be changing the Display Destination. Generate a handle for a

Framebuffer Object. Generate handles for two (color+depth) Renderbuffer

Objects. (These will later be attached to the Framebuffer Object)

Bind the Framebuffer Object to the Context

Render as Normal

Un-bind the Framebuffer Object from the Context

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

Bind the Depth Renderbuffer Object to the Context

Assign storage attributes to it

Attach it to the Framebuffer Object

The Overall Render-to-Texture Process

You will be changing the Display Destination. Generate a handle for a

Framebuffer Object. Generate handles for one (depth) Renderbuffer Object and

for a Texture Object. (These will later be attached to the Framebuffer Object)

Bind the Framebuffer Object to the Context

Bind the Texture Object to the Context

Assign storage attributes to it

Assign texture parameters to it

Attach it to the Framebuffer Object

Render as Normal

Un-bind the Framebuffer Object from the

Context

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

Ping-pong between two different textures. One texture is being read from

(the previous state) and the other is being written into (the next state).

mjb – July 31, 2007

Using GLSL, you Can Bind Multiple Color Framebuffers

and Write Separately to Each of Them

This is called “Multiple Render Targets (MRT)

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 );