How to do it...

To create an OpenGL application that creates shadows using the shadow mapping technique, perform the following steps. We'll start by setting up a framebuffer object (FBO) to contain the shadow map texture, and then move on to the required shader code:

  1. In the main OpenGL program, set up an FBO with a depth buffer only. Declare a GLuint variable named shadowFBO to store the handle to this framebuffer. The depth buffer storage should be a texture object. You can use something similar to the following code to accomplish this:
GLfloat border[]={1.0f,0.0f,0.0f,0.0f}; //The shadowmap texture GLuint depthTex; glGenTextures(1,&depthTex); glBindTexture(GL_TEXTURE_2D,depthTex); glTexStorage2D(GL_TEXTURE_2D, 1, GL_DEPTH_COMPONENT24, ...

Get OpenGL 4 Shading Language Cookbook - Third Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.