How to do it...

To implement HDR tone mapping, we'll perform the following steps:

  1. In the first pass, we want to just render the scene to the high-resolution texture. Bind to the framebuffer that has the texture attached and render the scene normally. 
  2. Compute the log average luminance of the pixels in the texture. To do so, we'll pull the data from the texture and loop through the pixels on the CPU side. We do this on the CPU for simplicity; a GPU implementation, perhaps with a compute shader, would be faster:
int size = width * height;std::vector<GLfloat> texData(size*3);glActiveTexture(GL_TEXTURE0);glBindTexture(GL_TEXTURE_2D, hdrTex);glGetTexImage(GL_TEXTURE_2D, 0, GL_RGB, GL_FLOAT, texData.data());float sum = 0.0f;for( int i = 0; i ...

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.