Creating a Shadow Map

The first step is to create a texture map of depth values as seen from the light’s point of view. You create this by rendering the scene with the viewpoint located at the light’s position. Before we can render depth into a depth texture, we need to create the depth texture and attach it to a framebuffer object. Example 7.15 shows how to do this. This code is included in the initialization sequence for the application.

Example 7.15. Creating a Framebuffer Object with a Depth Attachment

// Create a depth textureglGenTextures(1, &depth_texture);glBindTexture(GL_TEXTURE_2D, depth_texture);// Allocate storage for the texture dataglTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT32,             DEPTH_TEXTURE_SIZE, ...

Get OpenGL Programming Guide: The Official Guide to Learning OpenGL, Version 4.3, Eighth Edition now with the O’Reilly learning platform.

O’Reilly members experience live online training, plus books, videos, and digital content from nearly 200 publishers.