Phong Shading giving a black line

I’m not sure I’m understanding the problem here, but for a simple cube, wouldn’t the normals be just:

1,0,0 for the right face
-1,0,0 for the left face
0,1,0 for the top face
etc?

Am I missing something?

for (int i = 0; i < vertexData.length; i += 9) {
   float[] v1 = {vertexData[i],     vertexData[i + 1], vertexData[i + 2]}
   float[] v2 = {vertexData[i + 3], vertexData[i + 4], vertexData[i + 5]}
   float[] v3 = {vertexData[i + 6], vertexData[i + 7], vertexData[i + 8]}
   
   float[] triangleNormal = getTriangleNormal(v1[0], v1[1], v1[2], v2[0], v2[1], v2[2], v3[0], v3[1], v3[2]);

   normalData[i] =     triangleNormal[0];
   normalData[i + 1] = triangleNormal[1];
   normalData[i + 2] = triangleNormal[2];
   normalData[i + 3] = triangleNormal[0];
   normalData[i + 4] = triangleNormal[1];
   normalData[i + 5] = triangleNormal[2];
   normalData[i + 6] = triangleNormal[0];
   normalData[i + 7] = triangleNormal[1];
   normalData[i + 8] = triangleNormal[2];
}

Not the cleanest way. It should work; but I haven’t tested it.

Alright, I think its working, but I still see this;

If you make your shader just a normal passthrough, what does it look like? (without the phong).

Without colors:

With color, and without phong:

Now that is just odd o:

May I see your complete source? I’ll see if my machine yields the same results.

I kinda made this over the course of 30 minuets, using other peoples code, so it might be a bit sloppy.

/libraries/
lwjgl.jar
lwjgl-util.jar
jinput.jar

/libraries/natives/

/passage/games/
Game.java: http://pastebin.com/EcMfCdVL
Camera.java: http://pastebin.com/d4Wv0FEL
Block.java: http://pastebin.com/e4WvSniL
Shader.java: http://pastebin.com/36p6jrup

/assets/
Box.frag: http://pastebin.com/6FVWdJ3N
Box.vert: http://pastebin.com/DkyFNBGi

It’s something to do with how you calculate your diffuse variable in the fragment shader. I commented that part out of the shader, and the black spots no longer appeared.

However, I’ve never written a phong shader, so if someone else knows more than me, feel free to jump in :slight_smile:

Removing color from the diffuse makes the black part less intense.

vec4 diffuse = max(dot(normal, light), 0.0);

EDIT: Also, I forgot to remove this;

glUniform3f(glGetUniformLocation(shader.programID, "lightLocation"), 10, 10, 10);

I really can’t get this to work, I’ve been working on it for 2 days now. Does anyone know how to fix this?

My only “advice” is to look in to how you create your diffuse variable in the shader. You are doing something incorrect when calculating it. It appears as though all the vertices/normals are being passed in properly.

Think of it in a mathematical sense, not from a coding perspective. Step through all the different mathematical operations and make sure that they make sense. If you don’t know what one line does, figure it out. This will make sure that you know exactly what’s going on and you will start to see where stuff could go wrong.

I know most about what it does (Normals, Vectors, and Interpolation), but not all of it. I’ve looked this code over plenty of times, and it should be working… I’ve tried everything I could think of…

It’s been a while since I’ve written a Phong shader, but I’m curious what happens if you change the light color from 100, 100, 100, to 1.0, 1.0, 1.0

I tried that a while ago, It just gave me the same result, but the black-face just faded out slower during movement than before.