Archived

This topic is now archived and is closed to further replies.

smooth shading

This topic is 4979 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, Im wondering how to do smooth shading aka gourad shading? Right now I''m calculating the normals for each face. I think I should be calculating normals for each vertex instead right? Someone fill me out here. Thanks

Share this post


Link to post
Share on other sites
quote:
Original post by dario_s
I think I should be calculating normals for each vertex instead right?


Right. And since I''m guesing your next question will be "How do I calculate per-vertex normals", I''m giving you anwser for that too: Search gamedev forums or google. There are *alot* of posts about this.


You should never let your fears become the boundaries of your dreams.

Share this post


Link to post
Share on other sites
The trick is (which works pretty well for most cases) to average the face normals of all faces adjacent to a vertex. And this is how it works:


Init all vertex normals to zero

for all faces:
compute face normal
for each vertex in that face
add face normal to vertex normal

for all vertex normals
normalize vertex normal

Share this post


Link to post
Share on other sites
an addition to what lutz said


Init all vertex normals to zero

for all faces:
compute face normal

for every vertex in every face:
add face normal to vertex normal
for all adjacent faces:
if the dotproduct of the face normal and the adjacentface normal is > 0.71:
add adjacentface normal to vertex normal

for all vertex normals:
normalize vertex normal


What it basicly does is that it preserves all edges/creases below 90 degrees.
OR to put it simple, round things get smoothed and boxes not.

---------------------------------
For an overdose of l33tness, flashbang.nu


[edited by - lc_overlord on April 24, 2004 2:22:55 PM]

Share this post


Link to post
Share on other sites
Hmmm im having a problem here...

Im calculating normals for every face, but how do I add those normals to each vertex??

My code:

// calculate the normal of the triangle
glNormal3fv(CalculateNormal(vertex[0], vertex[2], vertex[1]).v);

// render properly textured triangle
glTexCoord2f(st[triIndex.stIndex[0]].s,
st[triIndex[i].stIndex[0]].t);
glVertex3fv(vertex[0].v);

// calculate the normal of the triangle
//glNormal3fv(CalculateNormal(vertex[0], vertex[2], vertex[1]).v);

glTexCoord2f(st[triIndex[i].stIndex[2]].s ,
st[triIndex[i].stIndex[2]].t);
glVertex3fv(vertex[2].v);

// calculate the normal of the triangle
//glNormal3fv(CalculateNormal(vertex[0], vertex[2], vertex[1]).v);

glTexCoord2f(st[triIndex[i].stIndex[1]].s,
st[triIndex[i].stIndex[1]].t);
glVertex3fv(vertex[1].v);



I mean, normally a polygon is rendered like this right?

glNormal3f(...)
glVertex3f(...)

How would I set a normal for every polygon with this method?


[edited by - dario_s on April 24, 2004 5:02:32 PM]

Share this post


Link to post
Share on other sites
um. pre calcylate the normals on load time and save them in large arrays.
like this one.

typedef vec3 float[3];
vec3 normals[num of polys][4];

then when you render
then just call them like this

glNormal3fv(normals[j][0]);
glTexCoord2f(st[triIndex[j].stIndex[0]].s, st[triIndex[j].stIndex[0]].t);
glVertex3fv(vertex[0].v);

glNormal3fv(normals[j][2]);
glTexCoord2f(st[triIndex[j].stIndex[2]].s, st[triIndex[j].stIndex[2]].t);
glVertex3fv(vertex[2].v);

glNormal3fv(normals[j][1]);
glTexCoord2f(st[triIndex[j].stIndex[j]].s, st[triIndex[j].stIndex[1]].t);
glVertex3fv(vertex[1].v);

the fourth normal is where you put the facenormal, its good for other things like stencil shadows and colition testing.




---------------------------------
For an overdose of l33tness, flashbang.nu



[edited by - lc_overlord on April 24, 2004 7:44:14 PM]

Share this post


Link to post
Share on other sites