如何将红色添加到绿色并变黑?

时间:2016-11-12 13:09:57

标签: opengl glsl shader vertex-shader phong

diffuse only specular only diffues plus specular

这是我的顶点着色器:

#version 400

layout (location = 0) in vec3 VertexPosition;
layout (location = 1) in vec3 VertexNormal;

out vec3 LightIntensity;

struct LightInfo {
    vec3 Position; // Light position in eye coords.
    vec3 La; // Ambient light intensity
    vec3 Ld; // Diffuse light intensity
    vec3 Ls; // Specular light intensity
};

uniform LightInfo Light;

struct MaterialInfo {
    vec3 Ka; // Ambient reflectivity
    vec3 Kd; // Diffuse reflectivity
    vec3 Ks; // Specular reflectivity
    float Shininess; // Specular shininess factor
};

uniform MaterialInfo Material;

uniform mat4 ModelViewMatrix;
uniform mat3 NormalMatrix;
uniform mat4 ProjectionMatrix;
uniform mat4 MVP;

void main()
{



    mat4 normalMatrixBetter = transpose(inverse(ModelViewMatrix));
    vec3 normalEyeSpace = normalize(vec3(normalMatrixBetter * vec4(VertexNormal, 0.0)));

    //vec3 tnorm = normalize(normalMatrixBetter * VertexNormal);

    vec4 vertexPositionInEyeCoords = ModelViewMatrix * vec4(VertexPosition, 1.0);
    vec3 s = normalize(vec3(Light.Position.xyz - vertexPositionInEyeCoords.xyz));

    vec3 v = normalize(-vertexPositionInEyeCoords.xyz);
    vec3 r = reflect(-s, normalEyeSpace);

    vec3 ambient = Light.La * Material.Ka;

    float sDotN = max(dot(s, normalEyeSpace), 0.0);
    vec3 diffuse = Light.Ld * Material.Kd * sDotN;

    vec3 spec = vec3(0.0);
    if (sDotN > 0.0)
    {
        spec = Light.Ls * Material.Ks * pow(max(dot(r, v), 0.0), Material.Shininess);
    }


//  LightIntensity = ambient;
//  LightIntensity = diffuse;
//  LightIntensity = spec;
//  LightIntensity = ambient + diffuse;
    LightIntensity = /*ambient +*/ diffuse + spec;


    gl_Position = MVP * vec4(VertexPosition, 1.0);
}

这是片段:

#version 400 
in vec3 LightIntensity; 
layout( location = 0) out vec4 FragColor; 

void main() 
{
    FragColor = vec4(LightIntensity, 1.0) * 0.3;
}

以下是相应的光强度和反射率系数:

vec3 lightSourceAmbientIntensity(1.0f, 1.0f, 1.0f);     // La, Light source ambient intensity
vec3 lightSourceDiffuseIntensity(10.0f, 10.0f, 10.0f);     // Ld, Light source diffuse intensity
vec3 lightSourceSpecularIntensity(1.0f, 1.0f, 1.0f);    // Ls, Light source specular intensity

vec3 ambientReflectivity(0.5f, 0.0f, 0.0f);         // Ka, 
vec3 diffuseReflectivity(1.0f, 0.0f, 0.0f);         // Kd,
vec3 specularReflectivity(0.0f, 0.9f, 0.0f);        // Ks,

GLfloat materialShineness = 1.0f;

当我LightIntensity = spec;时,我看到绿色的三角形应该是闪亮的,其余的三角形是黑色的。当LightIntensity = diffuse;我在所有三角形上看到非常好的漫反射红色阴影时,光线落在并反射到相机位置。但是当我写LightIntensity = /*ambient +*/ diffuse + spec;三角形时,黑色就是仅有明显的阴影。这是我的问题:如何将红色添加到绿色并变黑?为什么当我添加Phong着色颜色的高光照明时,不能正确添加?

1 个答案:

答案 0 :(得分:0)

狗屎挖得很深。我使用Material.Shineness名称设置名称并使用Material.Shininess名称。我是从一个制服设置制服但在着色器中使用另一个制服。只是一个错字。

获得的经验:当你犯了一个小错误时,OpenGL和GLSL完全意外地工作。数学开始不起作用。图灵机模具。 :)