GLSL镜面光照旋转

时间:2015-11-28 16:46:11

标签: c++ glsl lighting

我正在尝试点亮旋转模型,但是当尝试应用高光照明时,它似乎是不正确的并且在对象周围移动并且会在某些角度消失。

这是顶点着色器的一部分:

void main() {
    frag_vertex = vec3(mvpMatrix * vec4(position, 1.0));
    frag_textureCoord = textureCoord;
    frag_colour = colour;
    mat3 normalMatrix = transpose(inverse(mat3(mMatrix)));
    frag_normal = normalize(normal * normalMatrix);
    frag_worldPosition =  (vec4(position, 1.0) * mvpMatrix).xyz;

    gl_Position = mvpMatrix * vec4(position, 1.0);
}

这是片段着色器:

vec4 calculateLight(BaseLight base, vec3 direction, vec3 normal) {
    float diffuseFactor = max(dot(normal, -direction), 0.0);
    vec4 diffuseColour = vec4(0.0, 0.0, 0.0, 0.0);
    vec4 specularColour = vec4(0.0, 0.0, 0.0, 0.0);
    if (diffuseFactor > 0.0) {
        diffuseColour = vec4(base.colour, 1.0) * base.intensity * diffuseFactor;

        vec3 directionToEye = normalize(eyePosition - frag_worldPosition);
        vec3 halfDirection = normalize(directionToEye - direction);
        float specularFactor = max(dot(frag_normal, halfDirection), 0.0);
        specularFactor = pow(specularFactor, specularPower);

        if (specularFactor > 0.0) {
            specularColour = vec4(base.colour, 1.0) * specularIntensity * specularFactor;
        }
    }
    return (ambientLight + diffuseColour + specularColour);
}
void main() {
    gl_FragColor = frag_colour * material.diffuseColour * texture2D(material.diffuseTexture, frag_textureCoord) * calculateDirectionalLight(directionalLight, frag_normal);
}

在此,方向为(0,0,1),eyePosition表示相机的位置。

使用它而不旋转对象会给出: Without rotating the object

然后将其旋转80度会将高光移动到左侧:

Rotated 80 degrees

我做错了什么?

谢谢。

1 个答案:

答案 0 :(得分:0)

我认为问题可能由以下原因引起:

 vec3 halfDirection = normalize(directionToEye - direction);

半矢量定义为

 Half = normalize(View + Light)

您可能会看到http://content.gpwiki.org/D3DBook:(Lighting)_Blinn-Phong了解详情