Archived

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

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

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 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 on other sites
Hmm yes... I did not find any good ones though?

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 zerofor all faces:  compute face normal  for each vertex in that face    add face normal to vertex normalfor all vertex normals  normalize vertex normal

Share on other sites
ah, I''ll try that, thanks!

Share on other sites
an addition to what lutz said

Init all vertex normals to zerofor 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 normalfor 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 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 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]

1. 1
2. 2
3. 3
4. 4
Rutin
12
5. 5

• 12
• 18
• 10
• 14
• 10
• Forum Statistics

• Total Topics
632662
• Total Posts
3007702
• Who's Online (See full list)

There are no registered users currently online

×