1
votes

diffuse only specular only diffues plus specular

Here is my vertex shader:

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

And here is the fragment:

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

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

Here are the corresponding light intensity and reflectivity coefficients:

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;

When LightIntensity = spec; I see green triangles that should be shining and the rest of triangles are black. When LightIntensity = diffuse; I see very nice diffuse red shading on all triangles that light falls on and reflect to the camera position. But when I write LightIntensity = /*ambient +*/ diffuse + spec; triangles get black is in the case of ONLY SPECULAR shading. And here is my question: How can you add red to green and get black?? Why when I add Specular lighting in Phong shading colors don't get added correctly?

1
Adding images. Please consider that my shapes rotate around Y axis. Thanks!Narek

1 Answers

0
votes

The dog shit was digged deep. I was setting the name with Material.Shineness name and using with Material.Shininess name. I was setting from program one uniform but using in shader another uniform. Just a typo.

Lesson learned: OpenGL and GLSL work totally unexpectedly when you make a minor mistake. Math starts to not work. Turing machine dies. :)