smooth shading

Started by
6 comments, last by dario_s 19 years, 11 months ago
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
Advertisement
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.
You should never let your fears become the boundaries of your dreams.
Hmm yes... I did not find any good ones though?
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

ah, I''ll try that, thanks!
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]
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.stIndex[0]].t);<br> glVertex3fv(vertex[0].v);<br><br> // calculate the normal of the triangle<br> //glNormal3fv(CalculateNormal(vertex[0], vertex[2], vertex[1]).v);<br><br> glTexCoord2f(st[triIndex.stIndex[2]].s ,<br> st[triIndex.stIndex[2]].t);<br> glVertex3fv(vertex[2].v);<br><br> // calculate the normal of the triangle<br> //glNormal3fv(CalculateNormal(vertex[0], vertex[2], vertex[1]).v);<br><br> glTexCoord2f(st[triIndex.stIndex[1]].s,<br> st[triIndex.stIndex[1]].t);<br> glVertex3fv(vertex[1].v); </i> <br><br><br>I mean, normally a polygon is rendered like this right?<br><br>glNormal3f(…)<br>glVertex3f(…)<br><br>How would I set a normal for every polygon with this method?<br> <br><br><SPAN CLASS=editedby>[edited by - dario_s on April 24, 2004 5:02:32 PM]</SPAN>
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]

This topic is closed to new replies.

Advertisement