点光源随相机移动

Posted

技术标签:

【中文标题】点光源随相机移动【英文标题】:Point light move with the camera 【发布时间】:2020-03-26 19:05:45 【问题描述】:

我目前正在使用 OpenGL 在我的场景中添加一些灯光(定向光、点光源和聚光灯)。 我的定向灯和聚光灯似乎可以工作,但我的点灯仍然跟随相机。我想将该点光源的位置设置在任意位置,例如 (10, 5, 2)。 因此,我创建了一个片段着色器来管理具有这种结构的点光源的多个光源:

struct PointLight     
   vec3 position;
   vec3  color;

   float constant;
   float linear;
   float quadratic;  ;

这里是我将数据发送到片段着色器的代码:

glm::vec3 pos(-10.f, 5.f, 0.f);
pos = glm::normalize(glm::vec3(viewMatrix * glm::vec4(pos, 1.)));
glUniform3f(pointLightPositionLocation, pos.x, pos.y, pos.z);
glUniform3f(pointLightColorLocation, 0.0f, 1.0f, 0.0f);
glUniform1f(pointLightConstantLocation, 1.0f);
glUniform1f(pointLightLinearLocation, 0.09f);
glUniform1f(pointLightQuadraticLocation, 0.032f);

点的光源位置在视图空间中表示。

顶点着色器:

#version 330

layout(location = 0) in vec3 aPosition;
layout(location = 1) in vec3 aNormal;
layout(location = 2) in vec2 aTexCoords;

out vec3 vViewSpacePosition;
out vec3 vViewSpaceNormal;
out vec2 vTexCoords;
out vec3 vFragPos; 

uniform mat4 uModelViewProjMatrix;
uniform mat4 uModelViewMatrix;
uniform mat4 uNormalMatrix;

void main()

    vViewSpacePosition = vec3(uModelViewMatrix * vec4(aPosition, 1));
    vViewSpaceNormal = normalize(vec3(uNormalMatrix * vec4(aNormal, 0)));
    vTexCoords = aTexCoords;
    gl_Position =  uModelViewProjMatrix * vec4(aPosition, 1);

最后,我从点光源计算片段颜色的函数:

vec3 calculatePointLight(PointLight light) 
    vec3 N = normalize(vViewSpaceNormal);
    vec3 L = normalize(light.position - vViewSpacePosition);
    vec3 V = normalize(-vViewSpacePosition);
    vec3 H = normalize(L + V);

    vec4 baseColorFromTexture = SRGBtoLINEAR(texture(uBaseColorTexture, vTexCoords));
    vec4 baseColor = baseColorFromTexture * uBaseColorFactor;
    float NdotL = clamp(dot(N, L), 0, 1);
    float NdotV = clamp(dot(N, V), 0, 1);
    float NdotH = clamp(dot(N, H), 0, 1);
    float VdotH = clamp(dot(V, H), 0, 1);

    float metallic = texture(uMetallicRoughnessTexture, vTexCoords).z * uMetallicFactor;
    float roughness = texture(uMetallicRoughnessTexture, vTexCoords).y * uRoughnessFactor;

    vec3 cDiff = mix(baseColor.rgb * (1 - dielectricSpecular.r), black, metallic);
    vec3 f0 = mix(dielectricSpecular, baseColor.rgb, metallic);
    float a = uRoughnessFactor * roughness;
    float a2 = a * a;

    // You need to compute baseShlickFactor first
    float baseShlickFactor = (1 - VdotH);
    float shlickFactor = baseShlickFactor * baseShlickFactor; // power 2
    shlickFactor *= shlickFactor; // power 4
    shlickFactor *= baseShlickFactor; // power 5
    vec3 F = f0 + (1 - f0) * shlickFactor;

    float deno = NdotL * sqrt(NdotV * NdotV * (1 - a2) + a2) + NdotV * sqrt(NdotL* NdotL * (1 - a2) + a2);
    float Vis;
    if (deno == 0.) 
        Vis = 0;
    
    else 
        Vis = 0.5 / deno;
    

    deno = M_PI * (NdotH * NdotH * (a2 - 1) + 1) * (NdotH * NdotH * (a2 - 1) + 1);
    float D;
    if (deno == 0.) 
        D = 0;
    
    else 
        D = a2 / deno;
    

    vec3 diffuse = cDiff * M_1_PI;
    vec3 f_diffuse = (1 - F) * diffuse;
    vec3 f_specular = F * Vis * D;

    vec4 emissive = texture(uEmissiveTexture, vTexCoords) * vec4(uEmissiveFactor, 1);

    vec4 occlusion = texture(uOcclusionTexture, vTexCoords);

    // attenuation
    float distance    = length(light.position - vViewSpacePosition);
    float attenuation = 1.0 / (light.constant + light.linear * distance + 
                 light.quadratic * (distance * distance));

    f_diffuse *= attenuation;
    f_specular *= attenuation;

    vec3 color = (f_diffuse + f_specular) * light.color * NdotL + emissive.xyz;
    color = mix(color, color * occlusion.x, uOcclusionStrength);
    color = LINEARtoSRGB(color);
    return color;

这个功能有点复杂,因为金属粗糙,很多线条与问题无关。 综上所述,这个函数与 calcDirectionalLight (可以工作)非常相似,但不同之处在于我添加了一个衰减,而 L 是 light.position - FragPos 而不是 light.direction。 我不明白问题出在哪里,看起来数据没有在正确的空间中表达或者我忘记了什么。

【问题讨论】:

【参考方案1】:

我找到了解决方案,而且非常简单。 我从定向光中复制了这一行:pos = glm::normalize(glm::vec3(viewMatrix * glm::vec4(pos, 1.)));(我将 0 替换为 1,将 dirLight 替换为位置) 不幸的是,我忘记删除归一化,所以点光源接近 (0, 0, 0) :相机位置。这个结果让我确信数据在错误的空间中表达,但事实并非如此。

【讨论】:

以上是关于点光源随相机移动的主要内容,如果未能解决你的问题,请参考以下文章

IfcLightSourcePositional

一步步学OpenGL(20) -《点光源》

如何为 Phong 着色实现选择光源?

线阵相机镜头和光源选型

关于Unity中的光照

频闪控制器+光源的应用说明