# Calculate normals for a polygon

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

## Recommended Posts

Hi, I'm trying to calculate the normals for a set of polygons... but I got a problem. I calculate the normals and when I display theses normals they sounds correct :-( You can see this on the first picture here : http://picasaweb.google.com/viewon01/OpenGL#5391730635084390018 I don't know why my model sounds so "faceted" and that sometimes I got a face that looks strange !! Take a look at : http://picasaweb.google.com/viewon01/OpenGL#5391730633418058914 or http://picasaweb.google.com/viewon01/OpenGL#5380203925050881714 Can you help me please ? You can find my application at http://rs.codeplex.com or ... here is the source code for the normals calculation :
        override public void CalculateNormals()
{
base.CalculateNormals();

if (Indices.Count == 0)
return;

Normals.Clear();

Vector3[] sum = new Vector3[Vertices.Count];
Vector3 surfaceNormal;
Vector3 e1;
Vector3 e2;

int posInIndices = 0;
for (int index = 0; index < VerticesPerPolygon.Count; index++)
{
int verticesCount = VerticesPerPolygon[index];
for (int index0 = 0; index0 < verticesCount; index0++)
{
Vector3 vertex = Vector3.Invalid.Clone();
Vector3 previousVertex = Vector3.Invalid.Clone();
Vector3 nextVertex = Vector3.Invalid.Clone();

int indice = Indices[posInIndices];
vertex = Vertices[indice];

if (index0 == 0)
{
int previousIndice = Indices[posInIndices + verticesCount - 1];
previousVertex = Vertices[previousIndice];

int nextIndice = Indices[posInIndices + 1];
nextVertex = Vertices[nextIndice];
}
else if (index0 > 0 && index0 < verticesCount - 1)
{
int previousIndice = Indices[posInIndices - 1];
previousVertex = Vertices[previousIndice];

int nextIndice = Indices[posInIndices + 1];
nextVertex = Vertices[nextIndice];
}
else if (index0 == verticesCount - 1)
{
int previousIndice = Indices[posInIndices - 1];
previousVertex = Vertices[previousIndice];

int nextIndice = Indices[posInIndices - verticesCount + 1];
nextVertex = Vertices[nextIndice];
}

//---- Calculate the surface normal
e1 = previousVertex - vertex;
e2 = vertex - nextVertex;

surfaceNormal = e1.Cross(e2);
surfaceNormal.Normalize();

// Sum it at the vertex index
sum[indice] += surfaceNormal;

//---- next
posInIndices++;
}
}

for (int index = 0; index < Vertices.Count; index++)
{
sum[index].Normalize();
}
}



##### Share on other sites
It appears you implemented flat shading.

There are two steps for smoother shading:

1) For each vertex, the normal should be the weighted average of all vectors that contain that vertex. It appears you did this.

1. 1
2. 2
Rutin
16
3. 3
4. 4
5. 5

• 26
• 9
• 11
• 9
• 9
• ### Forum Statistics

• Total Topics
633710
• Total Posts
3013486
×