0

我有一个非常奇怪的高光(phong 光模型)光行为。它似乎出现在所有物体的两侧。有谁知道可能是什么问题?实际计算似乎没问题,因为我可以看到光线随着物体旋转而改变其位置。

#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;
}

在此处输入图像描述

4

1 回答 1

0

您需要饱和演算clampdot结果,因为在背面,结果为负数,并且pow可以返回正数而不是将其钳制为零。

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

编辑:

V应该是一个指向相机位置的向量,而不是世界空间中的顶点位置:

vec3 V = normalize(CameraPos0 - Pos0);

于 2013-11-18T20:55:52.677 回答