Ejemplo n.º 1
0
            public void CalculateNormals()
            {
                Vector3[] normals = new Vector3[vertices.Count];

                for (int i = 0; i < normals.Length; i++)
                {
                    normals[i] = new Vector3(0, 0, 0);
                }

                List <int> f = GetRenderingVertexIndices();

                for (int i = 0; i < displayFaceSize; i += 3)
                {
                    Vertex  v1  = vertices[f[i]];
                    Vertex  v2  = vertices[f[i + 1]];
                    Vertex  v3  = vertices[f[i + 2]];
                    Vector3 nrm = VectorUtils.CalculateNormal(v1.pos, v2.pos, v3.pos);

                    normals[f[i + 0]] += nrm * (nrm.Length / 2);
                    normals[f[i + 1]] += nrm * (nrm.Length / 2);
                    normals[f[i + 2]] += nrm * (nrm.Length / 2);
                }

                for (int i = 0; i < normals.Length; i++)
                {
                    vertices[i].nrm = normals[i].Normalized();
                }
            }
Ejemplo n.º 2
0
        public void PositiveNormal()
        {
            // Vertices facing the camera should be in counter-clockwise order.
            Vector3 v1     = new Vector3(-5, 5, 1);
            Vector3 v2     = new Vector3(-5, 0, 1);
            Vector3 v3     = new Vector3(0, 0, 1);
            Vector3 normal = VectorUtils.CalculateNormal(v1, v2, v3).Normalized();

            Assert.AreEqual(0, normal.X);
            Assert.AreEqual(0, normal.Y);
            Assert.AreEqual(1, normal.Z);
        }
Ejemplo n.º 3
0
        public void NegativeNormal()
        {
            // Vertices facing the camera in clockwise order.
            Vector3 v1     = new Vector3(-5, 5, 1);
            Vector3 v2     = new Vector3(-5, 0, 1);
            Vector3 v3     = new Vector3(0, 0, 1);
            Vector3 normal = VectorUtils.CalculateNormal(v3, v2, v1).Normalized();

            Assert.AreEqual(0, normal.X);
            Assert.AreEqual(0, normal.Y);
            Assert.AreEqual(-1, normal.Z);
        }
Ejemplo n.º 4
0
            public void SmoothNormals()
            {
                Vector3[] normals = new Vector3[vertices.Count];

                List <int> f = GetRenderingVertexIndices();

                for (int i = 0; i < displayFaceSize; i += 3)
                {
                    Vertex  v1  = vertices[f[i]];
                    Vertex  v2  = vertices[f[i + 1]];
                    Vertex  v3  = vertices[f[i + 2]];
                    Vector3 nrm = VectorUtils.CalculateNormal(v1.pos, v2.pos, v3.pos);

                    normals[f[i + 0]] += nrm;
                    normals[f[i + 1]] += nrm;
                    normals[f[i + 2]] += nrm;
                }

                for (int i = 0; i < normals.Length; i++)
                {
                    vertices[i].nrm = normals[i].Normalized();
                }

                // Compare each vertex with all the remaining vertices. This might skip some.
                for (int i = 0; i < vertices.Count; i++)
                {
                    Vertex v = vertices[i];

                    for (int j = i + 1; j < vertices.Count; j++)
                    {
                        Vertex v2 = vertices[j];

                        if (v == v2)
                        {
                            continue;
                        }
                        float dis = (float)Math.Sqrt(Math.Pow(v.pos.X - v2.pos.X, 2) + Math.Pow(v.pos.Y - v2.pos.Y, 2) + Math.Pow(v.pos.Z - v2.pos.Z, 2));
                        if (dis <= 0f) // Extra smooth
                        {
                            Vector3 nn = ((v2.nrm + v.nrm) / 2).Normalized();
                            v.nrm  = nn;
                            v2.nrm = nn;
                        }
                    }
                }
            }