1

我一直在尝试使用以下资源来实现基于物理的渲染。它描述了可以用来实现基于物理的着色的方程式。这是我的着色器代码:

#define M_PI 3.1415926535897932384626433832795

#ifdef SHADER_VERTEX

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

out vec3 Position;
out vec3 Normal;

uniform mat4 model;
uniform mat4 mvp;

void main()
{
    gl_Position = mvp * vec4(position, 1.0);
    Position = vec3(model * vec4(position, 1.0));
    Normal = vec3(model * vec4(normal, 0.0));
}

#endif

#ifdef SHADER_FRAGMENT

in vec3 Position;
in vec3 Normal;

out vec4 color;

uniform vec3 lightPos;
uniform vec3 objectColor;
uniform vec3 cameraPos;
uniform float roughness;
uniform vec3 specularColor;

vec3 D(vec3 normal, vec3 h)
{
    float a2 = roughness * roughness * roughness * roughness;
    float NdotH = dot(normal, h);
    float x = (NdotH * NdotH) * (a2 - 1) + 1;
    return vec3(a2 / (M_PI * (x * x)));
}

vec3 G1(vec3 normal, vec3 v)
{
    float k = ((roughness + 1) * (roughness + 1)) / 8.0f;
    float NdotV = dot(normal, v);

    return vec3(NdotV / ((NdotV * (1 - k) + k)));
}

vec3 G(vec3 normal, vec3 l, vec3 v)
{
    return G1(normal, l) * G1(normal, v);
}

vec3 F(vec3 v, vec3 h)
{
    vec3 f0 = specularColor;
    float VdotH = dot(v, h);
    float exponent = (-5.55473 * VdotH - 6.98316) * VdotH;
    return vec3(f0 + ((vec3(1, 1, 1) - f0) * pow(2, exponent)));
}

vec3 specular(vec3 n, vec3 l, vec3 h, vec3 v)
{
    float NdotL = dot(n, l);
    float NdotV = dot(n, v);
    return (D(n, h) * F(v, h) * G(n, l, v)) / ((4 * NdotL * NdotV));
}

vec3 diffuse(vec3 normal, vec3 lightDir)
{
    float NdotL = dot(normal, lightDir);
    vec3 result = objectColor / M_PI;
    return result * NdotL;
}

void main()
{
    vec3 norm = normalize(Normal);
    vec3 lightDir = normalize(lightPos - Position);
    vec3 eyeVector = normalize(cameraPos - Position);
    vec3 halfVector = normalize(lightDir + eyeVector);

    color = vec4(diffuse(norm, lightDir) + specular(norm, lightDir, halfVector, eyeVector), 1.0);
}

#endif

结果如下:图片

如您所见,中间有一条奇怪的白色带。灯光位置在球体上方,相机在球体前面。粗糙度变量当前设置为 0.2。我不知道我是否错误地执行了方程式,或者我传入的参数应该是什么样子。

4

1 回答 1

0

问:n垂直于时会发生什么l

vec3 specular(vec3 n, vec3 l, vec3 h, vec3 v)
{
    float NdotL = dot(n, l);
    float NdotV = dot(n, v);
    return (D(n, h) * F(v, h) * G(n, l, v)) / ((4 * NdotL * NdotV));
}

A:这个函数将返回 0/0,除了 0 之外会有舍入错误,所以你可以得到任何结果。

你需要重新排列方程,这样你就不会在任何地方得到 0/0。

于 2015-01-16T22:26:17.770 回答