0
\$\begingroup\$

I'm practicing the Phong lighting model with glsl, and here's my shaders: vertex shader:

#version 330 core
layout (location = 0) in vec3 aPos;
layout (location = 1) in vec2 aTexCoord;
layout (location = 2) in vec3 aNormal;

out vec2 TexCoord;
out vec3 LightColor;
uniform mat4 model;
layout (std140) uniform coord_mats {
    mat4 view;
    mat4 projection;
    vec3 lightPos;
    vec3 lightColor;
};

void main()
{
    vec3 vertex_position = vec3(model * vec4(aPos, 1.0));
//    vec3 vertex_position = vec3(view * model * vec4(aPos, 1.0));
    vec3 vertex_normal = normalize(vec3(model * vec4 (aNormal, 1.0)));
//    vec3 vertex_normal = normalize(vec3(view * model * vec4 (aNormal, 1.0)));
    vec3 light_position = vec3(vec4(lightPos, 1.0));
//    vec3 light_position = vec3(view * vec4(lightPos, 1.0));
    vec3 light_dir = normalize(light_position - vertex_position);
    float diff = max(dot( light_dir,vertex_normal), 0.0);
    vec3 diffuse = diff * lightColor;
    vec3 ambient = 0.1 * lightColor;
    LightColor = diffuse + ambient;

    gl_Position = projection * view * model * vec4(aPos, 1.0);
    TexCoord = vec2(aTexCoord.x, aTexCoord.y);
}

fragment shader:

#version 330 core
out vec4 FragColor;

in vec2 TexCoord;
in vec3 LightColor;
// texture sampler
uniform sampler2D texture1;

void main()
{
    FragColor = vec4(LightColor,1.0) * texture(texture1, TexCoord);
}

The diffuse light works right, that is, faces which are facing to the light source, are bright, when backing the light source, they are dark(basically ambient color).

Now I want to calculate the specular light, in order to get the camera position for free, I changes the coordinate to camera view coordinate(using the commented code, just multiply the view matrix to the vertex position, normal and light position), but weird things happened: all faces which are facing the light is dark, all faces which are backing to the light is bright.

How could this happened?

\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

Sorry I cannot put a comment yet, however, I noticed that you have 'w' set to 1 in your normal i.e. it would be translated as well. It might have caused this issue.

\$\endgroup\$
4
  • \$\begingroup\$ Hi, I think you find the root case, but I'm confused, since when translating vertices, we add w to 1.0, but when translating normals, we set w to 0, why? \$\endgroup\$ Commented Jun 14, 2020 at 11:49
  • \$\begingroup\$ It is because normals are directions and not positions. It makes sense to translate your player's position. For example, lets say your player's position is {0,0,0}. Now move the player 5 units in x positive direction. Now your player's position is {5,0,0}, however, lets say your player's up vector is {0,1,0}, it does not make sense to move the player's up vector by 5 units to the right. If you do that your normal would become {5,1,0} which is wrong. In nutshell, it does not make any sense to translate a direction. \$\endgroup\$ Commented Jun 14, 2020 at 11:57
  • \$\begingroup\$ You can also watch 3D fundamentals videos on Youtube. This tutorial explains all the 3d fundamentals in really depth. \$\endgroup\$ Commented Jun 14, 2020 at 12:00
  • \$\begingroup\$ thanks so much, I will investigate' \$\endgroup\$ Commented Jun 14, 2020 at 12:07

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.