I'm trying to implement phong shading to no avail. I am building the shaders bit by bit (which could be an issue in itself) but here is where I am at currently. I would expect this to output my result (the Utah teapot) as a white image on my black background. It does this correctly when I remove line 18 from the vertex shader, but when the line is there there is no output, and no error. Any ideas?
Fragment Shader:
1 #version 120
2
3 varying vec3 vertexNormal;
4 varying vec3 cameraSpacePos;
5
6 vec3 calcSpecular(vec3 l, vec3 n, vec3 v, vec3 cL, vec3 cS, float p)
7 {
8 vec3 r = 2.0 * n * (n * l) - l;
9 return cL * cS * pow(max(0.0, dot(r, v)), p);
10 }
11
12 vec3 calcDiffuse(vec3 l, vec3 n, vec3 cL, vec3 cD)
13 {
14 return cL * cD * max(0.0, dot(n, l));
15 }
16
17 void main(void)
18 {
19 vec3 lightDirection = normalize(vec3(-1.0, 0.0, -1.0));
20 vec3 objectColor = vec3(0.5, 0.0, 0.0);
21 vec3 ambientColor = vec3(0.1, 0.1, 0.1);
22 vec3 lightColor = vec3(0.3, 0.0, 0.3);
23 vec3 specularColor = vec3(1.0, 1.0, 1.0);
24
25 gl_FragColor = vec4(1.0, 1.0, 1.0, 1.0);
26 }
Vertex Shader:
1 #version 120
2
3 uniform mat4 modelView;
4 uniform mat4 normalModelView;
5 uniform mat4 projection;
6
7 attribute vec3 position;
8 attribute vec3 normal;
9
10 varying vec3 vertexNormal;
11 varying vec3 cameraSpacePos;
12
13 void main(void)
14 {
15 vec4 tmpCameraSpacePos = modelView * vec4(position, 1.0);
16 gl_Position = projection * tmpCameraSpacePos;
17
18 vertexNormal = (normalModelView * vec4(normal, 0.0)).xyz;
19 cameraSpacePos = (tmpCameraSpacePos).xyz;
20
21 }
22
It may also be worth mentioning that normal and normalModelView are not currently being set. I had tried with them enabled previously to similar results. My understanding is that they should be set to default values.
Since you mentioned that normal
and normalModelView
are not being set, my guess is that they are not being set to default values either, and are technically invalid data. When invalid data is used in a shader, the results are undefined, part of the reason why shaders are difficult to debug. Make sure that you're providing values for those variables before you try to reference them in the code.