OpenGL Blinn-Phong着色问题

时间:2014-12-03 13:08:57

标签: opengl fragment vertex

我无法正确渲染图像,我认为镜面高光效果正常,但我无法在图像中看到太多细节(下图)

enter image description here

如您所见,您无法弄清楚脸部的细节。我不确定我是否正确地这样做,所以如果有人能指出任何问题,我会很感激。

我试图分解顶点和片段着色器之间的工作,但它证明了原先预期的更难。

vertexShader

#version 330

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

out vec4 Colour0;
out vec3 normalInWorldSpace;

uniform mat4 viewMatrix;

// Transforms
uniform mat4 gModelToWorldTransform;
uniform mat4 gWorldToViewToProjectionTransform;

//send to fragment shader
out vec3 worldPos;
out vec3 worldNormal;

void main()
{
    // Calculate the directional light intensity at the vertex
    // Find the normal in world space and normalise it
    normalInWorldSpace = (gModelToWorldTransform * vec4(Normal, 0.0)).xyz;
    normalInWorldSpace = normalize(normalInWorldSpace);

    worldPos = mat3(gModelToWorldTransform) * Position;
    worldNormal = normalize(mat3(gModelToWorldTransform) * Normal);

    gl_Position = gWorldToViewToProjectionTransform * gModelToWorldTransform * vec4(Position, 1);
}

fragmentShader

#version 330

in vec4 Colour0;

out vec4 FragColor;

// Ambient light parameters
uniform vec3 gAmbientLightIntensity;

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

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

in vec3 worldPos;
in vec3 worldNormal;
in vec3 normalInWorldSpace;

uniform vec3 eyePosition;

void main()
{
    vec3 L = normalize(gDirectionalLightDirection - worldPos); //Light direction
    vec3 V = normalize(eyePosition - worldPos); //view direction

    float LdotN = max(0, dot(L, worldNormal));

    float diffuse = gKd * LdotN;

    float ambient = gKa * 1.0f;

    float specular = 0;

    if(LdotN > 0.0f)
    {
        vec3 R = -normalize(reflect(L, worldNormal)); //Reflection
        specular = gKs * pow(max(0, dot(R, V)), gKsStrength);

        vec3 H = normalize(L + V );//Halfway
        specular = gKs * pow(max(0, dot(H, worldNormal)), gKsStrength);

    }

    vec4 colour = vec4(1.0f, 1.0f, 1.0f, 1.0f);
    float light = diffuse + specular + ambient;

    FragColor = colour * (light, light, light);
}

0 个答案:

没有答案