private void GenerateIndices()
        {
            List <StaticVertex> tempVertices = new List <StaticVertex>();
            List <int>          tempIndices  = new List <int>();

            foreach (StaticVertex v in VertexData)
            {
                //verifica se esiste un vertice già nella lista
                int  i     = 0;
                bool found = false;
                foreach (StaticVertex v2 in tempVertices)
                {
                    if (StaticVertex.Compare(v, v2))
                    {
                        //travato
                        found = true;
                        break;
                    }
                    i++;
                }

                if (found)
                {
                    tempIndices.Add(i);
                    StaticVertex v2 = tempVertices[i];
                    //somma le normali
                }
                else
                {
                    i = tempVertices.Count;
                    tempVertices.Add(v);
                    tempIndices.Add(i);
                }

                //normali
                StaticVertex vTemp = tempVertices[i];
                vTemp.Normal   += v.Normal;
                tempVertices[i] = vTemp;
            }


            //normalizzazione finale
            VertexData.Clear();
            //foreach (VertexFormat v in tempVertices)
            //{
            //    v.Normal.Normalize();
            //    Vertices.Add(v);
            //}
            VertexData.AddRange(tempVertices);

            IndexData.Clear();
            IndexData.AddRange(tempIndices);
        }
        private void GenerateNormal()
        {
            for (int i = 0; i < VertexData.Count; i++)
            {
                StaticVertex v = VertexData[i];
                v.Normal      = new Vector3();
                VertexData[i] = v;
            }

            for (int i = 0; i < IndexData.Count; i += 3)
            {
                StaticVertex p1 = VertexData[IndexData[i]];
                StaticVertex p2 = VertexData[IndexData[i + 1]];
                StaticVertex p3 = VertexData[IndexData[i + 2]];

                Vector3 V1 = p2.Position - p1.Position;
                Vector3 V2 = p3.Position - p1.Position;

                Vector3 N = Vector3.Cross(V1, V2);
                N.Normalize();

                p1.Normal += N;
                p2.Normal += N;
                p3.Normal += N;

                VertexData[IndexData[i]]     = p1;
                VertexData[IndexData[i + 1]] = p2;
                VertexData[IndexData[i + 2]] = p3;
            }

            //normalize
            for (int i = 0; i < VertexData.Count; i++)
            {
                StaticVertex v = VertexData[i];
                v.Normal.Normalize();
                VertexData[i] = v;
            }
        }
Exemple #3
0
 /// <summary>
 /// Compare 2 vertices on Position and Texture Coordinate
 /// </summary>
 /// <param name="a">First vertex</param>
 /// <param name="b">Second vertex</param>
 /// <returns></returns>
 internal static bool Compare(StaticVertex a, StaticVertex b)
 {
     return(a.Position == b.Position &&
            a.TextureCoordinate == b.TextureCoordinate);
 }
        private static WaveFrontModel CreateGeom(string name, List<Vector3> position, List<Vector3> normals, List<Vector2> texture, List<int> faces, Material mate)
        {
            WaveFrontModel geom = new WaveFrontModel();
            geom.Name = name;
            geom.VertexData = new List<StaticVertex>();
            geom.IndexData = new List<int>();

            int stride = 0;

            if (position.Count > 0)
                stride++;

            if (normals.Count > 0)
                stride++;

            if (texture.Count > 0)
                stride++;

            int vertexCount = faces.Count / stride;

            int k = 0;

            for (int i = 0; i < vertexCount; i++)
            {
                StaticVertex v = new StaticVertex();
                if (position.Count > 0)
                {
                    v.Position = position[faces[k] - 1];
                    k++;
                }

                if (texture.Count > 0)
                {
                    v.TextureCoordinate = texture[faces[k] - 1];
                    k++;
                }

                if (normals.Count > 0)
                {
                    v.Normal = normals[faces[k] - 1];
                    k++;
                }
                geom.VertexData.Add(v);

            }

            for (int i = 0; i < (geom.VertexData.Count); i++)
            {
                geom.IndexData.Add((short)i);
            }

            geom.MeshMaterial.Add(mate);
            geom.FaceCounts.Add(geom.IndexData.Count);
            return geom;
        }
Exemple #5
0
 /// <summary>
 /// Compare 2 vertices on Position and Texture Coordinate
 /// </summary>
 /// <param name="a">First vertex</param>
 /// <param name="b">Second vertex</param>
 /// <returns></returns>
 internal static bool Compare(StaticVertex a, StaticVertex b)
 {
     return a.Position == b.Position &&
         a.TextureCoordinate == b.TextureCoordinate;
 }
        private static WaveFrontModel CreateGeom(string name, List <Vector3> position, List <Vector3> normals, List <Vector2> texture, List <int> faces, Material mate)
        {
            WaveFrontModel geom = new WaveFrontModel();

            geom.Name       = name;
            geom.VertexData = new List <StaticVertex>();
            geom.IndexData  = new List <int>();

            int stride = 0;


            if (position.Count > 0)
            {
                stride++;
            }


            if (normals.Count > 0)
            {
                stride++;
            }


            if (texture.Count > 0)
            {
                stride++;
            }


            int vertexCount = faces.Count / stride;

            int k = 0;


            for (int i = 0; i < vertexCount; i++)
            {
                StaticVertex v = new StaticVertex();
                if (position.Count > 0)
                {
                    v.Position = position[faces[k] - 1];
                    k++;
                }

                if (texture.Count > 0)
                {
                    v.TextureCoordinate = texture[faces[k] - 1];
                    k++;
                }

                if (normals.Count > 0)
                {
                    v.Normal = normals[faces[k] - 1];
                    k++;
                }
                geom.VertexData.Add(v);
            }

            for (int i = 0; i < (geom.VertexData.Count); i++)
            {
                geom.IndexData.Add((short)i);
            }

            geom.MeshMaterial.Add(mate);
            geom.FaceCounts.Add(geom.IndexData.Count);
            return(geom);
        }