3
\$\begingroup\$

There seems to be a problem with my current Phong Shading implementation. I have been using the per fragment approach. The specular highlight aspect of the technique is not rendering as it should, as a matter of fact it is not rendering at all. My shininess is at 100, but nothing seems to appear. What could be the problem?

glUniform1f(Shin_location, 100.0f);

Vertex Shader:

  #version 400

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

  out vec3 fragPosition;
  out vec3 fragNormal;

  uniform mat4 rotation_matrix;
  uniform mat4 ModelViewMatrix;
  uniform mat3 NormalMatrix;
  uniform mat4 PVM;

  void main()
   {

       fragNormal   = normalize(NormalMatrix * VertexNormal);

       fragPosition = vec3(ModelViewMatrix * vec4(VertexPosition, 1.0));

       gl_Position = PVM * rotation_matrix * vec4(VertexPosition, 1.0);
    }

Fragment Shader:

                                     #version 400

 in vec3 fragPosition;
 in vec3 fragNormal;

 uniform vec4 LightPosition;
 uniform vec3 LightIntensity;
 uniform vec3 Kd;
 uniform vec3 Ka;
 uniform vec3 Ks;
 uniform float Shininess;

layout (location = 0) out vec4 FragColor;

vec3 ads()
  {
    vec3 n = normalize(fragNormal);
    vec3 s = normalize(vec3(LightPosition) - fragPosition);
    vec3 v = normalize(vec3(-fragPosition));

    vec3 r = reflect(-s, n);

    return LightIntensity * (Ka + Kd * max(dot(s, n), 0.0) + Ks *  pow(max(dot(r, v), 0.0), Shininess));
   }

  void main()
   {
      FragColor = vec4(ads(), 1.0);
   }

Main:

    // Vertex Position
    glBindBuffer(GL_ARRAY_BUFFER, VBO[0]);
    glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
    glVertexAttribPointer(VERTEX_POS, 3, GL_FLOAT, GL_FALSE, 0, nullptr);
    glEnableVertexAttribArray(VERTEX_POS);

    // Color
    glBindBuffer(GL_ARRAY_BUFFER, VBO[1]);
    glBufferData(GL_ARRAY_BUFFER, sizeof(color), color, GL_STATIC_DRAW);
    glVertexAttribPointer(COLOR, 3, GL_FLOAT, GL_FALSE,  0, nullptr);
    glEnableVertexAttribArray(COLOR);

    // Normal Vertex
    glBindBuffer(GL_ARRAY_BUFFER, VBO[2]);
    glBufferData(GL_ARRAY_BUFFER, sizeof(normals), normals, GL_STATIC_DRAW);
    glVertexAttribPointer(NORMALS_POS, 3, GL_FLOAT, GL_FALSE, 0, nullptr);
    glEnableVertexAttribArray(NORMALS_POS);

    GLint rot_location  = shader.getUniformRotLocation();
    GLint proj_Location = shader.getUniformProjLocation();
    GLint PVM_Location  = shader.getUniformPVMLocation(); 

    GLint LightPos_location        = shader.getUniformLightPositionLocation();
    GLint LightIntensity_location  = shader.getUniformLightIntensityLocation();
    GLint KD_location              = shader.getUniformKDLocation();
    GLint KA_location              = shader.getUniformKALocation();
    GLint KS_location              = shader.getUniformKSLocation(); 
    GLint Shin_location            = shader.getUniformShininessLocation();
    GLint MV_location              = shader.getUniformModelViewMatrixLocation();
    GLint NM_location              = shader.getUniformNormalMatrixLocation();


    glm::vec4 lightpos = glm::vec4(5.2f, 3.1f, 2.0f, 1.0f);


    glm::mat4 model_matrix(1.0f);
    glm::mat4 view_matrix = glm::lookAt(glm::vec3(0.0f, 3.0f, 5.0f), glm::vec3(0.0f, 0.75f, 0.0f), glm::vec3(0.0f, 1.0f, 0.0f));

    glm::mat4 ModelViewMatrix =  view_matrix * model_matrix;

    glm::mat3 NormalMatrix    =  glm::mat3(1.0f);

    glm::mat4x4 rot_matrix(1.0f);
    glm::mat4x4 proj_matrix = glm::perspective(45.0f, 4.0f/ 3.0f, 0.1f, 100.0f);

    glm::mat4x4 PVM = proj_matrix * view_matrix * model_matrix;

    float angle = 0.957283f;
    const float angle_in_degrees = 0.01f;
    float Rotate = 0.0f; 

bool stop = false;

glEnable(GL_DEPTH_TEST);

while loop:

    glm::mat4x4 PVM = proj_matrix * view_matrix * model_matrix;

    glUniformMatrix4fv(rot_location,  1, GL_FALSE, glm::value_ptr(rot_matrix));
    glUniformMatrix4fv(PVM_Location, 1, GL_FALSE, glm::value_ptr(PVM));
    glUniformMatrix4fv(MV_location, 1, GL_FALSE, glm::value_ptr(ModelViewMatrix));
    glUniformMatrix3fv(NM_location, 1, GL_FALSE, glm::value_ptr(glm::mat3(glm::vec3(ModelViewMatrix[0]), glm::vec3(ModelViewMatrix[1]), glm::vec3(ModelViewMatrix[2]))));

    glm::vec4 worldLight = view_matrix * lightpos;

    glm::vec4 vec = glm::vec4(10.0f * cos(angle), 3.0f, 10.0f * sin(angle), 1.0f);


    glUniform4f(LightPos_location, vec.x, vec.y,vec.z, vec.w);

    glUniform3f(KD_location, 0.9f, 0.5f, 0.3f);
    glUniform3f(KS_location, 0.95f, 0.9f, 0.9f);
    glUniform3f(KA_location, 0.1f, 0.1f, 0.1f);
    glUniform1f(Shin_location, 100.0f);

    //Clear the framebuffer with dark green
    static const GLfloat dark_green[] = {0.0f, 0.25f, 0.0f, 1.0f};
    static const GLfloat one = 1.0f;
    glClearBufferfv(GL_COLOR, 0, dark_green);
    glClearBufferfv(GL_DEPTH, 0, &one);

enter image description here

enter image description here

\$\endgroup\$
0
\$\begingroup\$
vec3 viewDir = normalize(viewPos - FragPos);    
float spec = pow(max(dot(viewDir, reflectDir), 0.0), shininess);
vec3 specular = specularStrength * spec * lightColor;  

You need viewDir and reflectionDir.

Your reflectionDirection looks ok.

Your viewDirection looks messed up though.

You also have them backwards from the norm but it shouldn't matter.

If that is not the problem then make sure your Kd/specularStrength is not zero.

\$\endgroup\$
  • \$\begingroup\$ You say viewDirection is messed up, what is it suppose to look like, the one you posted about? is your viewPos vector the same as my LightPosition vector? what dimension is lightColor? And Kd is not zero, its 3d. vec3 normal = normalize(fragNormal); vec3 viewDir = normalize(vec3(LightPosition) - fragPosition); vec3 reflectDir = reflect(-viewDir, normal); float spec = pow(max(dot(viewDir, reflectDir), 0.0), Shininess); vec3 specular = Kd * spec * lightColor; \$\endgroup\$ – user40247 Jul 18 '16 at 3:52
  • \$\begingroup\$ lightDir = normalize(lightPos - FragPos); vec3 reflectDir = reflect(-lightDir, normal); \$\endgroup\$ – Yudrist Jul 18 '16 at 4:33
  • \$\begingroup\$ learnopengl.com/#!Lighting/Basic-Lighting This tutorial should straighten you out. \$\endgroup\$ – Yudrist Jul 18 '16 at 4:34
  • \$\begingroup\$ I don't remember what I saw back then and don't want to spend a long time on it again. But I think you had something backwards if I remember correctly. Anyway everything looks find but the shader formula so you should be able to get going quickly from the tutorial. \$\endgroup\$ – Yudrist Jul 18 '16 at 4:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.