【问题标题】:Oren-Nayar lighting in OpenGL (how to calculate view direction in fragment shader)OpenGL 中的 Oren-Nayar 光照(如何在片段着色器中计算视图方向)
【发布时间】:2017-03-27 19:02:34
【问题描述】:

我正在尝试在片段着色器中实现 Oren-Nayar 光照,如 here 所示。

但是,我在地形上得到了一些奇怪的光照效果,如下所示。

我目前正在向着色器发送“视图方向”统一作为相机的“前”向量。我不确定这是否正确,因为移动相机会改变伪影。

将“前”向量乘以 MVP 矩阵会得到更好的结果,但从某些角度查看地形时,伪影仍然非常明显。在黑暗区域和屏幕边缘周围尤其明显。

什么可能导致这种效果?

工件示例

场景的外观

顶点着色器

#version 450

layout(location = 0) in vec3 position;
layout(location = 1) in vec3 normal;

out VS_OUT {
    vec3 normal;
} vert_out;

void main() {
    vert_out.normal = normal;
    gl_Position = vec4(position, 1.0);
}

镶嵌控制着色器

#version 450

layout(vertices = 3) out;

in VS_OUT {
    vec3 normal;
} tesc_in[];

out TESC_OUT {
    vec3 normal;
} tesc_out[];

void main() { 
    if(gl_InvocationID == 0) {
        gl_TessLevelInner[0] = 1.0;
        gl_TessLevelInner[1] = 1.0;

        gl_TessLevelOuter[0] = 1.0;
        gl_TessLevelOuter[1] = 1.0;
        gl_TessLevelOuter[2] = 1.0;
        gl_TessLevelOuter[3] = 1.0;
    }

    tesc_out[gl_InvocationID].normal = tesc_in[gl_InvocationID].normal;
    gl_out[gl_InvocationID].gl_Position = gl_in[gl_InvocationID].gl_Position;
}

镶嵌评估着色器

#version 450

layout(triangles, equal_spacing) in;

in TESC_OUT {
    vec3 normal;
} tesc_in[];

out TESE_OUT {
    vec3 normal;
    float height;
    vec4 shadow_position;
} tesc_out;

uniform mat4 model_view;
uniform mat4 model_view_perspective;
uniform mat3 normal_matrix;
uniform mat4 depth_matrix;

vec3 lerp(vec3 v0, vec3 v1, vec3 v2) {
    return (
        (vec3(gl_TessCoord.x) * v0) + 
        (vec3(gl_TessCoord.y) * v1) + 
        (vec3(gl_TessCoord.z) * v2)
    );
}

vec4 lerp(vec4 v0, vec4 v1, vec4 v2) {
    return (
        (vec4(gl_TessCoord.x) * v0) + 
        (vec4(gl_TessCoord.y) * v1) + 
        (vec4(gl_TessCoord.z) * v2)
    );
}

void main() {
    gl_Position = lerp(
        gl_in[0].gl_Position,
        gl_in[1].gl_Position,
        gl_in[2].gl_Position
    );

    tesc_out.normal = normal_matrix * lerp(
        tesc_in[0].normal,
        tesc_in[1].normal,
        tesc_in[2].normal
    );

    tesc_out.height = gl_Position.y;

    tesc_out.shadow_position = depth_matrix * gl_Position;
    gl_Position = model_view_perspective * gl_Position;
}

片段着色器

#version 450

in TESE_OUT {
    vec3 normal;
    float height;
    vec4 shadow_position;
} frag_in;

out vec4 colour;

uniform vec3 view_direction;
uniform vec3 light_position;

#define PI 3.141592653589793

void main() {
    const vec3 ambient = vec3(0.1, 0.1, 0.1);
    const float roughness = 0.8;

    const vec4 water = vec4(0.0, 0.0, 0.8, 1.0);
    const vec4 sand = vec4(0.93, 0.87, 0.51, 1.0);
    const vec4 grass = vec4(0.0, 0.8, 0.0, 1.0);
    const vec4 ground = vec4(0.49, 0.27, 0.08, 1.0);
    const vec4 snow = vec4(0.9, 0.9, 0.9, 1.0);

    if(frag_in.height == 0.0) {
        colour = water;
    } else if(frag_in.height < 0.2) {
        colour = sand;
    } else if(frag_in.height < 0.575) {
        colour = grass;
    } else if(frag_in.height < 0.8) {
        colour = ground;
    } else {
        colour = snow;
    }

    vec3 normal = normalize(frag_in.normal);
    vec3 view_dir = normalize(view_direction);
    vec3 light_dir = normalize(light_position);

    float NdotL = dot(normal, light_dir);
    float NdotV = dot(normal, view_dir); 

    float angleVN = acos(NdotV);
    float angleLN = acos(NdotL);

    float alpha = max(angleVN, angleLN);
    float beta = min(angleVN, angleLN);
    float gamma = dot(view_dir - normal * dot(view_dir, normal), light_dir - normal * dot(light_dir, normal));

    float roughnessSquared = roughness * roughness;
    float roughnessSquared9 = (roughnessSquared / (roughnessSquared + 0.09));

    // calculate C1, C2 and C3
    float C1 = 1.0 - 0.5 * (roughnessSquared / (roughnessSquared + 0.33));
    float C2 = 0.45 * roughnessSquared9;

    if(gamma >= 0.0) {
        C2 *= sin(alpha);
    } else {
        C2 *= (sin(alpha) - pow((2.0 * beta) / PI, 3.0));
    }

    float powValue = (4.0 * alpha * beta) / (PI * PI);
    float C3  = 0.125 * roughnessSquared9 * powValue * powValue;

    // now calculate both main parts of the formula
    float A = gamma * C2 * tan(beta);
    float B = (1.0 - abs(gamma)) * C3 * tan((alpha + beta) / 2.0);

    // put it all together
    float L1 = max(0.0, NdotL) * (C1 + A + B);

    // also calculate interreflection
    float twoBetaPi = 2.0 * beta / PI;

    float L2 = 0.17 * max(0.0, NdotL) * (roughnessSquared / (roughnessSquared + 0.13)) * (1.0 - gamma * twoBetaPi * twoBetaPi);

    colour = vec4(colour.xyz * (L1 + L2), 1.0);
}

【问题讨论】:

  • 如果 Oren-Nayar 的成本太高,您可以考虑使用wrap lighting

标签: c++ opengl glsl lighting


【解决方案1】:

首先,我已将您的片段着色器与我的视图/法线/光矢量一起插入我的渲染器中,并且效果很好。所以问题一定出在你计算这些向量的方式上。

接下来,您说您将view_dir 设置为相机的前向量。我假设您的意思是“世界空间中相机的前矢量”,这是不正确的。由于您在相机空间中使用向量计算点积,因此 view_dir 也必须在相机空间中。那是vec3(0,0,1) 将是一种简单的检查方法。如果它有效 - 我们发现了您的问题。

但是,当您进行透视投影时,使用(0,0,1) 作为视图方向并不完全正确,因为从片段到相机的方向取决于片段在屏幕上的位置。那么正确的公式是view_dir = normalize(-pos),其中pos 是片段在相机空间中的位置(即在没有投影的情况下应用模型视图矩阵)。此外,这个数量现在仅取决于屏幕上的片段位置,因此您可以计算为:

view_dir = normalize(vec3(-(gl_FragCoord.xy - frame_size/2) / (frame_width/2), flen))

flen是你相机的焦距,你可以计算为flen = cot(fovx/2)

【讨论】:

  • 感谢您的回复,我会尽快查看。
  • 我尝试在片段着色器中计算 view_dir,正如您在答案底部显示的那样。什么是弗兰?我将 flen 的值设置为 1.0,将 frame_size 的窗口大小设置为,在有阴影的区域中仍然可以看到伪影。如果光线在其上方,则地形的底部看起来特别糟糕。有任何想法吗?感谢您的帮助。
  • 我的公式不正确。参见编辑,也关于flen。我不知道你在“底面”看到了什么,但你可能应该在渲染另一面时反转你的法线。然而,你为什么要关心地形的“底面”呢?不应该被淘汰吗?
  • 应该,我只是认为这可能是出了什么问题的线索。
  • 你真的解决了吗?因为我根本没有时间查看您的最新编辑。
【解决方案2】:

我知道这是一个很长的死线程,但我一直遇到同样的问题(好几年了),终于找到了解决方案......

可以通过固定表面法线的方向以匹配多边形缠绕方向来部分解决,但您也可以通过更改以下两行来消除着色器中的伪影...

float angleVN = acos(cos_nv);
float angleLN = acos(cos_nl);

到这个...

float angleVN = acos(clamp(cos_nv, -1.0, 1.0));
float angleLN = acos(clamp(cos_nl, -1.0, 1.0));

多田!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-12-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-27
    相关资源
    最近更新 更多