Graphics Reference
In-Depth Information
After the objects have been created, we call
glBindTexture(texture)
to
make the texture the currently bound texture object. The texture mip level
is then specified using
glTexImage2D
. Note that the
pixels
argument is
NULL
: We are rendering to the entire texture region, so there is no reason
to specify any input data (this data will be overwritten).
The
depthRenderbuffer
object is bound using
glBindRenderbuffer
, and
glRenderbufferStorage
is called to allocate storage for a 16-bit depth buffer.
The
framebuffer
object is bound using
glBindFramebuffer
.
texture
is
attached as a color attachment to
framebuffer
, and
depthRenderbuffer
is attached as a depth attachment to
framebuffer
.
We next check the framebuffer status to see if it is complete before we
begin drawing into
framebuffer
. Once framebuffer rendering is complete,
we reset the currently bound framebuffer to the window system-provided
framebuffer by calling
glBindFramebuffer(GL_FRAMEBUFFER, 0)
. We
can now use
texture
, which was used as a render target in
framebuffer
,
to draw to the window system-provided framebuffer.
In Example 12-2, the depth buffer attachment to
framebuffer
was a
renderbuffer object. In Example 12-3, we consider how to use a depth
texture as a depth buffer attachment to
framebuffer
. Applications can
render to the depth texture used as a framebuffer attachment from the
light source. The rendered depth texture can then be used as a shadow
map to calculate the percentage in shadow for each fragment. Figure 12-3
shows the generated image.
Example 12-3
Render to Depth Texture
#define COLOR_TEXTURE 0
#define DEPTH_TEXTURE 1
GLuint framebuffer;
GLuint textures[2];
GLint texWidth = 256, texHeight = 256;
// generate the framebuffer and texture object names
glGenFramebuffers ( l, &framebuffer );
glGenTextures ( 2, textures );
// bind color texture and load the texture mip level 0
// texels are RGB565
// no texels need to specified as we are going to draw into
// the texture
(continues)