title: Tone mapping shader OpenGL
body: <p>I'm trying to implement tone mapping to an HDR image in OpenGL.</p>
  
  <p>Previously, I make manual mip mapping of HDR image, to get a 1x1 texture (<code>RGBA32F</code> format) with the average value of luminance.</p>
  
  <p>I find an example of tone mapping function and put it on my fragment shader.</p>
  
  <p>In my render code,I have on <code>GL_TEXTURE0</code> the HDR image (format <code>GL_RGBA32F</code>), on <code>GL_TEXTURE1</code> the 1x1 texture with average value.</p>
  
  <p>The LDR texture (<code>RGBA8</code> format) where I want to render, is allocated in a FrameBuffer (fboTN) on <code>GL_COLOR_ATTACHMENT0</code>.</p>
  
  <p>I use FreeImage library to upload the HDR image (OpenEXR format). </p>
  
  <p>Here's the code: </p>
  
  <pre><code>void toneMapping(){
    glBindFramebuffer(GL_FRAMEBUFFER, fboTN);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    // Set viewport and textures
  
    setMVP();
    glActiveTexture(GL_TEXTURE0); HDRtex-&gt;bind();     // Source HDR tex
    glActiveTexture(GL_TEXTURE1); DSavgTex0-&gt;bind();  // 1x1 tex with avgLum
    toneMappingShad-&gt;bind();
      float key = 0.18f, Ywhite = 1e6f, sat = 1.0f;
      toneMappingShad-&gt;setUniformValue1f("key", key);
      toneMappingShad-&gt;setUniformValue1f("Ywhite", Ywhite);
      toneMappingShad-&gt;setUniformValue1f("sat", sat);
      toneMappingShad-&gt;setUniformValue("width", im-&gt;getWidth());
      toneMappingShad-&gt;setUniformValue("height", im-&gt;getHeight());
      toneMappingShad-&gt;setUniformValue4f("MVP", glm::value_ptr(MVP));
      drawQuad(toneMappingShad);
    toneMappingShad-&gt;unbind();
    glActiveTexture(GL_TEXTURE1); DSavgTex0-&gt;unbind();    // 1x1 tex with avgLum
    glActiveTexture(GL_TEXTURE0); HDRtex-&gt;unbind();   // Source HDR tex
    glBindFramebuffer(GL_FRAMEBUFFER, 0);
  }
  
  // RENDER CODE
  toneMapping();
  texShad-&gt;bind();
      texShad-&gt;setUniformValue4f("MVP", glm::value_ptr(MVP));
      LDRtex-&gt;bind();
      drawQuad(texShad);
      LDRtex-&gt;unbind();
  texShad-&gt;bind();
  </code></pre>
  
  <p>My output is almost black screen ( I can guess the quad I draw).
  <code>texShad</code> shaders are very simple; just draws texture on screen. </p>
  
  <p>I'm going to show tone mapping shaders:</p>
  
  <p>Here is the vertex shader (simple):</p>
  
  <pre><code>#version 420
  
  in vec2 vUV;
  in vec4 vVertex;
  uniform int width;  // size of texture
  uniform int height;
  smooth out vec2 vTexCoord;
  
  uniform mat4 MVP;
  void main()
  {
    gl_Position = MVP*vVertex;
    vTexCoord = vec2(vUV.x*width,vUV.y*height);
  }
  </code></pre>
  
  <p>And the fragment:</p>
  
  <pre><code>#version 420
  
  uniform float key;
  uniform float Ywhite;
  uniform float sat;
  layout(binding=0) uniform sampler2D hdrSampler;     // HDR texture
  layout(binding=1) uniform sampler2D downSampler;    // Texture 1x1 with average of luminance
  smooth in vec2 vTexCoord;           
  layout(location=0) out vec4 color;  // LDR sample output (tone mapped image)
  
  const mat3 RGB2XYZ = mat3(0.4124, 0.3576, 0.1805,
                             0.2126, 0.7152, 0.0722,
                             0.0193, 0.1192, 0.9505);
  
  
  vec3 tonemap(vec3 RGB, float logAvgLum){
      vec3 XYZ = RGB2XYZ * RGB;
      float Y = (key / logAvgLum) * XYZ.y;
      float Yd = (Y * (1.0 + Y /(Ywhite * Ywhite))) / (1.0 + Y);
      return pow(RGB / XYZ.y, vec3(sat,sat,sat)) * Yd;
  }   
  
  
  void main(){
      vec3 hdr = texture(hdrSampler, vTexCoord).rgb;
      float logAvgLum = exp(texture(downSampler,vec2(0.0,0.0)).a);
      color.rgb = tonemap(hdr, logAvgLum);
  }
  </code></pre>
  
  <p>If I put something like <code>color.a = X</code> output doesn't change.</p>
  
  <p>One important doubt: is correct this (<code>texture(downSampler,vec2(0.0,0.0)).a</code>) to get the average value? I don't know if <code>0,0</code> are the correct value coords to get, because it's 1x1 the size of the texture.</p>
  
  <p>I have spent a lot of time with this, but I can't see what's wrong.</p>
  
  <p>Here's the output:</p>
  
  <p><img src="https://i.stack.imgur.com/6HKTT.jpg" alt="Output"></p>