Page 1 of 1

Gouraud vs Phong shading

Posted: Thu Jun 10, 2010 12:27 pm
by LordNaikon
Hi

I was a long time absent from irrlicht and in general 3D stuff. currently im back and try to better understand 3D in general.

so i decided to write a software renderer/rasterizer to get an idea of lots of basic important stuff.

my basic software renderer/rasterizer is working and now i try to implement some shading/lightning.

i implemented both, gouraud and phong shading. let the specular part aside poth look the same?! is this right? i thought these blocky steps between the poly edges are also removed with phong shading, not only the better locking with specular light reflection.

whats the problem my understanding of how phong should look like .. or my implementation?!

Phong
Image
Gouraud
Image

Posted: Thu Jun 10, 2010 9:21 pm
by Lonesome Ducky
With phong, the normal is interpolated across the triangle per pixel. It seems like your phong lighting is still doing per-vertex.

Posted: Fri Jun 11, 2010 6:49 am
by LordNaikon
you'r the same opinion ...

actually i DO interpolate the normals per pixel!

besides i implemented specular lightning, and this seems to behave like i exspected. (in comparison to gouraud shading)
in phong you can see this specular reflects from the light as little highlighted dots, which is impossible in gouraud shading, where only the vertexes can be "white colored". in phong, these dots can simply cross over the polygon cuz of the interpolated normal in every pixel. beside this the "shadowedges" looks much smoother with phong, my problem is the appearance of visible vertexes and poly edges .. :(

Phong
Image
Image

Gouraud
Image
Image


i think this is a proof of existing (maybe wrong) interpolated normals per pixel. the question is, how to do the right interpolation?! Am I right that in phong you should not see the vertexes and edges from the polys, what could be the mistake?

what i did is(like i did in gouraud), take a vertex, get all connected polys and its normals and get the averange normal for that vertex from all poly normals.
to interpolate for every pixel i walk on 2 edges from A to B and from A to C and interpolate the normals between. then i interpolate from point AB to AC to get all pixel normals.
Image

is that the right procedure? to be honest, i didn't read much of implementations of this before cuz i wanted my mind to be free and think on my own .. how can i do this ..

Posted: Fri Jun 11, 2010 7:55 pm
by Lonesome Ducky
You interpolation does seem correct, yes, but I'm not exactly and expert :lol: . The visible vertices and edges leads me to believe that your method is correct and the model's normals are incorrect. Have you tried another model?

Posted: Sun Jun 13, 2010 3:29 pm
by LordNaikon
yes i have used other models .. same here

i calculate the model normals my self

Posted: Sun Jun 13, 2010 4:39 pm
by devsh
I know the problem
in the pixel shader you get an interpolated normal, the thing about normals is that their length must == 1

visualize this 2D situation vertex A and B, A has a normal of -0.707,0.707 and B has a normal of 0.707,0.707 and you are currently shading a spot right in the middle of this line with an interpolated normal of 0.0,0.707.... oops, the normal is short! even though you can do dot products per pixel, you need to normalize your normal every pixel.

The artifacts on your model are a classic example of that

Posted: Mon Jun 14, 2010 2:44 pm
by LordNaikon
@devsh:
yeah, you are totally right. i dont normalize that interpolated normal after its "interpolation".

when i have time to go on (like tomorrow or so) i will do that! big thanks for that hint! i think thats the problem, too ... without really implemented that!

if i had spend only a few moments for reading ... like wiki ... i could have saved yours and my time :roll: :
...The surface normal is interpolated and normalized at each pixel and then used in the Phong reflection model to obtain the final pixel color. ...