Jump to content
  • Advertisement
Sign in to follow this  
polar01

Calculate normals for a polygon

This topic is 3265 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, 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();
                Normals.Add(sum[index]);
            }
        }

Share this post


Link to post
Share on other sites
Advertisement
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.

2) Enable Gouraud shading. (In OpenGL it's ::glShadeModel(GL_SMOOTH);)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!