色调映射着色器OpenGL

时间:2014-12-26 20:24:39

标签: opengl mapping textures shader hdr

我试图在OpenGL中实现色调映射到HDR图像。

以前,我会对HDR图像进行手动mip映射,以获得具有平均亮度值的1x1纹理(RGBA32F格式)。

我找到了一个色调映射功能的例子,并把它放在我的片段着色器上。

在我的渲染代码中,我有GL_TEXTURE0 HDR图像(格式为GL_RGBA32F),GL_TEXTURE1平均值为1x1纹理。

我要渲染的LDR纹理(RGBA8格式)在GL_COLOR_ATTACHMENT0上的FrameBuffer(fboTN)中分配。

我使用FreeImage库上传HDR图像(OpenEXR格式)。

以下是代码:

void toneMapping(){
  glBindFramebuffer(GL_FRAMEBUFFER, fboTN);
  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  // Set viewport and textures

  setMVP();
  glActiveTexture(GL_TEXTURE0); HDRtex->bind();     // Source HDR tex
  glActiveTexture(GL_TEXTURE1); DSavgTex0->bind();  // 1x1 tex with avgLum
  toneMappingShad->bind();
    float key = 0.18f, Ywhite = 1e6f, sat = 1.0f;
    toneMappingShad->setUniformValue1f("key", key);
    toneMappingShad->setUniformValue1f("Ywhite", Ywhite);
    toneMappingShad->setUniformValue1f("sat", sat);
    toneMappingShad->setUniformValue("width", im->getWidth());
    toneMappingShad->setUniformValue("height", im->getHeight());
    toneMappingShad->setUniformValue4f("MVP", glm::value_ptr(MVP));
    drawQuad(toneMappingShad);
  toneMappingShad->unbind();
  glActiveTexture(GL_TEXTURE1); DSavgTex0->unbind();    // 1x1 tex with avgLum
  glActiveTexture(GL_TEXTURE0); HDRtex->unbind();   // Source HDR tex
  glBindFramebuffer(GL_FRAMEBUFFER, 0);
}

// RENDER CODE
toneMapping();
texShad->bind();
    texShad->setUniformValue4f("MVP", glm::value_ptr(MVP));
    LDRtex->bind();
    drawQuad(texShad);
    LDRtex->unbind();
texShad->bind();

我的输出几乎是黑屏(我可以猜到我绘制的四边形)。 texShad着色器非常简单;只是在屏幕上绘制纹理。

我将展示色调映射着色器:

这是顶点着色器(简单):

#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);
}

片段:

#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);
}

如果我输入color.a = X之类的内容,则输出不会改变。

一个重要的疑问:这是正确的(texture(downSampler,vec2(0.0,0.0)).a)来获得平均值吗?我不知道0,0是否是正确的值坐标,因为它的纹理大小是1x1。

我花了很多时间来做这件事,但我看不出有什么不对。

这是输出:

Output

0 个答案:

没有答案
相关问题