最近,我在引擎中添加了延迟着色支持。但是我遇到了一些衰减问题:c++ - 延迟阴影和衰减-LMLPHP

如您所见,当我渲染光量(球体)时,它与图像的周围部分不能很好地融合!

这是我声明点光源的方式:

PointLight pointlight;
pointlight.SetPosition(glm::vec3(0.0, 6.0, 0.0));
pointlight.SetIntensity(glm::vec3(1.0f, 1.0f, 1.0f));

这是我计算光球半径的方法:
Attenuation attenuation = pointLights[i].GetAttenuation();
    float lightMax = std::fmaxf(std::fmax(pointLights[i].GetIntensity().r, pointLights[i].GetIntensity().g),
        pointLights[i].GetIntensity().b);

    float pointLightRadius = (-attenuation.linear +
        std::sqrtf(std::pow(attenuation.linear, 2.0f) - 4.0f * attenuation.exponential *
            (attenuation.constant - (256.0f / 5.0f) * lightMax))) / (2.0f * attenuation.exponential);

最后,这是我的PointLightPass片段着色器:
#version 450 core

struct BaseLight
{
    vec3 intensities;//a.k.a color of light
    float ambientCoeff;
};

struct Attenuation
{
    float constant;
    float linear;
    float exponential;
};

struct PointLight
{
    BaseLight base;
    Attenuation attenuation;
    vec3 position;
};

struct Material
{
    float shininess;
    vec3  specularColor;
    float ambientCoeff;
};


layout (std140) uniform Viewport
{
    uniform mat4 Projection;
    uniform mat4 View;
    uniform mat4 ViewProjection;
    uniform vec2 scrResolution;
};

layout(binding = 0) uniform sampler2D gPositionMap;
layout(binding = 1) uniform sampler2D gAlbedoMap;
layout(binding = 2) uniform sampler2D gNormalMap;
layout(binding = 3) uniform sampler2D gSpecularMap;

uniform vec3 cameraPosition;
uniform PointLight pointLight;

out vec4 fragmentColor;

vec2 FetchTexCoord()
{
      return gl_FragCoord.xy / scrResolution;
}

void main()
{
      vec2 texCoord = FetchTexCoord();
      vec3 gPosition =      texture(gPositionMap, texCoord).xyz;
      vec3 gSurfaceColor =  texture(gAlbedoMap, texCoord).xyz;
      vec3 gNormal =        texture(gNormalMap, texCoord).xyz;
    vec3 gSpecColor =   texture(gSpecularMap, texCoord).xyz;
    float gSpecPower =  texture(gSpecularMap, texCoord).a;

    vec3 totalLight = gSurfaceColor * 0.1; //TODO remove hardcoded ambient light
    vec3 viewDir = normalize(cameraPosition - gPosition);

    vec3 lightDir = normalize(pointLight.position - gPosition);
    vec3 diffuse = max(dot(gNormal, lightDir), 0.0f) * gSurfaceColor *
                    pointLight.base.intensities;

    vec3 halfWayDir = normalize(lightDir + viewDir);
    float spec = pow(max(dot(gNormal, halfWayDir), 0.0f), 1.0f);
    vec3 specular = pointLight.base.intensities * spec /** gSpecColor*/;

    float distance = length(pointLight.position - gPosition);
    float attenuation = 1.0f / (1.0f + pointLight.attenuation.linear * distance
                    + pointLight.attenuation.exponential * distance * distance +
                     pointLight.attenuation.constant);



    diffuse *= attenuation;
    specular *= attenuation;
    totalLight += diffuse + specular;

    fragmentColor = vec4(totalLight, 1.0f);
}

那么,您有什么建议可以解决这个问题呢?

编辑:这里有更多详细信息:

对于延迟着色,
  • 我填充了我的GBuffer;
  • 我在渲染全屏四边形时进行了环境光传递
    与环境颜色:

    #version 420核心
    layout (std140) uniform Viewport
    {
        uniform mat4 Projection;
        uniform mat4 View;
        uniform mat4 ViewProjection;
        uniform vec2 scrResolution;
    };
    
    layout(binding = 1) uniform sampler2D gAlbedoMap;
    out vec4 fragmentColor;
    
    vec2 FetchTexCoord()
    {
          return gl_FragCoord.xy / scrResolution;
    }
    
    void main()
    {
        vec2 texCoord = FetchTexCoord();
        vec3 gSurfaceColor =    texture(gAlbedoMap, texCoord).xyz;
    
        vec3 totalLight = gSurfaceColor * 1.2; //TODO remove hardcoded ambient light
        fragmentColor = vec4(totalLight, 1.0f);
    }
    

  • 然后,我通过点光源(请参见上面的代码);

    最佳答案

    您遇到此问题的原因是您使用的是“轻量级”(这一事实您在此问题中并未完全清楚,但已在your other question中提出)。

    您正在使用普通的光衰减方程式。好吧,您会注意到,该方程式并没有神奇地停在某个任意半径处。它定义为从0到无穷大的所有距离。

    照明量的目的是防止超过一定距离的照明贡献。好吧,如果您的光衰减在该距离处不为零,那么您将在光量的边缘看到不连续。

    如果要使用光体积,则需要使用光衰减方程,该方程实际上可以保证在体积边缘达到零。否则,您应该为体积选择一个半径,以使光的衰减强度几乎为零。而且您的半径太小了。

    不断增大半径,直到无法分辨它的存在为止。

    关于c++ - 延迟阴影和衰减,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/34121885/

    10-09 03:15