PDA

View Full Version : smooth normals



SED
10-28-2000, 06:08 PM
Ive put together a simple *.stl viewer. Im loading a std::vector in
the format:

stlVertices[0] == normal
stlVertices[1] == vertex1
stlVertices[2] == vertex2
stlVertices[3] == vertex3
stlVertices[4] == normal
stlVertices[5] == vertex1
stlVertices[6] == vertex2
stlVertices[7] == vertex3
...
...
...

then I loop thru rendering each:

glBegin(GL_TRIANGLES);
for (int i=0; i<stlVertices.size(); i+=4)
{
glNormal3f( stlVertices[i]->m_pt[X] ,
stlVertices[i]->m_pt[Y],
stlVertices[i]->m_pt[Z]);

glVertex3f( stlVertices[i+1]->m_pt[X] ,
stlVertices[i+1]->m_pt[Y],
stlVertices[i+1]->m_pt[Z]);

glVertex3f( stlVertices[i+2]->m_pt[X] ,
stlVertices[i+2]->m_pt[Y],
stlVertices[i+2]->m_pt[Z]);

glVertex3f( stlVertices[i+3]->m_pt[X] ,
stlVertices[i+3]->m_pt[Y],
stlVertices[i+3]->m_pt[Z]);
}
glEnd();

This will give you an idea of what Im looking at: http://personal.lig.bellsouth.net/lig/s/_/s_dolan/stl_capture.jpg

Do I have to use a normal on a per vertex basis to get a smoother
model, instead of a per triangle basis? I sort of understand how to do
this. You average the normals of the surrounding faces per vertex,
correct?

I think the stl formats triangle winding is reverse of what OpenGL
uses by default. If I reverse the winding as I load the vector and
remove the normal,is there a way OpenGL will smooth the normals for
me?

Sean

10-28-2000, 07:49 PM
>Do I have to use a normal on a per vertex
>basis to get a smoother model, instead of
>a per triangle basis?

Yes, if you want smooth lighting. Note that
you can get the normal for a triangle by
just taking the cross product, so saving the
normals of vertexes is oftentimes better.
Also, if you save the actual vertex data of
each triangle, you get lots of duplicated
data; better save all the vertexes once, and
then a list of which vertexes make up each
triangle.

>I sort of understand how to do this. You
>average the normals of the surrounding
>faces per vertex, correct?

And normalize once you're done. Also, this
works best if your triangles are "close" in
angle versus the viewer. If there is a sharp
corner, the arithmetic mean isn't going to
cut it. Of course, for sharp corners, you
probably don't want a smooth surface anyway
(and thus need multiple instances of the
same vertex, with different normals).

As far as winding order goes, I didn't know
that STL defined any winding order at all.
Instead, I think it is whatever is giving
you the data in the first place which has
some winding order in it.

SED
10-28-2000, 08:44 PM
Thanks bgl, for clearning those questions up for me That model in my link has ~7k triangles in it. It's actually an assymbly I exported out of SolidWorks. I think I'll export something a bit simpler to work with http://www.opengl.org/discussion_boards/ubb/smile.gif Testing for the angles has to be kind of tough to implement. You'd have to know where to set the break angle between adjacent triangles. Something like an extruded block letter 'D' would be simple. Set the break angle at say > 80deg. This would give you boundaries for the face sets that make up the solid. But take something with more complex surfaces, and it would get a bit more complicated to find the boundaries. A lot of work ahead it looks like http://www.opengl.org/discussion_boards/ubb/smile.gif

Sean

tbjerman
11-22-2001, 10:21 PM
i need your help .. i also writing a stl viewer , i would like to know how you implement the smooth shading on the stl model
???

Rob The Bloke
11-23-2001, 09:57 AM
read above