镜面光出现在两侧

时间:2013-11-18 20:50:25

标签: c++ opengl freeglut specular phong

我有一种非常奇怪的镜面光(phong light model)光的行为。它似乎在所有物体的两边都有效。有谁知道可能是什么问题? 实际计算似乎没问题,因为我可以看到光在物体旋转时改变了它的位置。

#version 330

in vec4 CameraPos0;
in vec3 Pos0;
in vec4 Colour0;
in vec3 Normal0;

out vec4 FragColor;

 // Ambient light parameters
uniform vec3 gAmbientLightIntensity;

// Directional light parameters
uniform vec3 gDirectionalLightIntensity;
uniform vec3 gDirectionalLightDirection;

// Specular light parameter
uniform vec3 gSpecularLightIntensity;
uniform vec3 gLightSourcePosition;
uniform vec3 gCameraPosition;

// Material constants
uniform float gKa;
uniform float gKd;
uniform float gKs;

void main()
{

    // Calculate the ambient light intensity at the vertex
    // Ia = Ka * ambientLightIntensity
    vec4 ambientLightIntensity = gKa * vec4(gAmbientLightIntensity, 1.0);

    // Setup the light direction and normalise it
    vec3 lightDirection = normalize(-gDirectionalLightDirection);

    //lightDirection = normalize(gDirectionalLightDirection);
    // Id = kd * lightItensity * N.L
    // Calculate N.L
    float diffuseFactor = dot(Normal0, lightDirection);
    diffuseFactor = clamp(diffuseFactor, 0.0, 1.0);
    // N.L * light source colour * intensity
    vec4 diffuseLightIntensity = gKd * vec4(gDirectionalLightIntensity, 1.0f) * diffuseFactor;

    // Phong light
    vec3 L = normalize(gLightSourcePosition - Pos0);   
    vec3 V = normalize(-Pos0); 
    vec3 R = normalize(2 * Normal0 * dot(Normal0, L) - L);

    float specularFactor = pow(dot(R, V), 0.1f);
    vec4 specularLightIntensity = gKs * vec4(gSpecularLightIntensity, 1.0f) * specularFactor;
    specularLightIntensity = clamp(specularLightIntensity, 0.0, 1.0);

    // Final vertex colour is the product of the vertex colour
    // and the total light intensity at the vertex 
    vec4 lightedFragColor = Colour0 * (ambientLightIntensity + diffuseLightIntensity + specularLightIntensity);

    FragColor = lightedFragColor;
}

顶点着色器

#version 330

layout (location = 0) in vec3 Position;
layout (location = 1) in vec3 Normal;
layout (location = 2) in vec4 Colour;

out vec3 Pos0;
out vec4 Colour0;
out vec3 Normal0;
out vec4 CameraPos0;

uniform mat4 gModelToWorldTransform;
uniform mat4 gWorldToViewTransform;
uniform mat4 gProjectionTransform;

void main()
{  
    vec4 vertexPositionInModelSpace = vec4(Position, 1);
    vec4 vertexInWorldSpace = gModelToWorldTransform * vertexPositionInModelSpace;
    vec4 vertexInViewSpace = gWorldToViewTransform * vertexInWorldSpace;
    vec4 vertexInHomogeneousClipSpace = gProjectionTransform * vertexInViewSpace;
    gl_Position = vertexInHomogeneousClipSpace;

    vec3 normalInWorldSpace = (gModelToWorldTransform * vec4(Normal, 0.0)).xyz;
    normalInWorldSpace = normalize(normalInWorldSpace);

    Normal0 = normalInWorldSpace;
    CameraPos0 = vertexInViewSpace;
    Pos0 = vertexInWorldSpace.xyz;
    Colour0 = Colour;
}

enter image description here

1 个答案:

答案 0 :(得分:0)

您需要clamp来自饱和度计算的dot结果,因为在背面,结果为负数,pow可以返回正数而不是将其钳制为零。

float specularFactor = pow(clamp(dot(R, V),0.0,1.0), 0.1f);

编辑:

V也应该是指向摄像机位置的向量,而不是世界空间中的顶点位置:

vec3 V = normalize(CameraPos0 - Pos0);