コード例 #1
0
ファイル: Mesh.cs プロジェクト: xorza/NetGL
        public void CalculateTangents()
        {
            Disposer.Dispose(Tangents);

            Tangents = new Vector3Buffer(Vertices.Length);

            for (int i = 0; i < Vertices.Length; i += 3)
            {
                Vector3 v1 = Vertices[i + 0];
                Vector3 v2 = Vertices[i + 1];
                Vector3 v3 = Vertices[i + 2];

                Vector2 w1 = TexCoords[i + 0];
                Vector2 w2 = TexCoords[i + 1];
                Vector2 w3 = TexCoords[i + 2];

                float x1 = v2.X - v1.X;
                float x2 = v3.X - v1.X;
                float y1 = v2.Y - v1.Y;
                float y2 = v3.Y - v1.Y;
                float z1 = v2.Z - v1.Z;
                float z2 = v3.Z - v1.Z;

                float s1 = w2.X - w1.X;
                float s2 = w3.X - w1.X;
                float t1 = w2.Y - w1.Y;
                float t2 = w3.Y - w1.Y;

                float   r    = 1.0f / (s1 * t2 - s2 * t1);
                Vector3 sdir = new Vector3((t2 * x1 - t1 * x2) * r, (t2 * y1 - t1 * y2) * r, (t2 * z1 - t1 * z2) * r);
                Vector3 tdir = new Vector3((s1 * x2 - s2 * x1) * r, (s1 * y2 - s2 * y1) * r, (s1 * z2 - s2 * z1) * r);

                Tangents[i + 0] = sdir;
                Tangents[i + 1] = sdir;
                Tangents[i + 2] = sdir;
            }

            for (int a = 0; a < Vertices.Length; a++)
            {
                Vector3 n = Normals[a];
                Vector3 t = Tangents[a];

                // Gram-Schmidt orthogonalize
                Tangents[a] = Vector3.Normalize((t - n * Vector3.Dot(n, t)));

                // Calculate handedness
                //tangents[a].w = (dot(cross(n, t), bitangents[a]) < 0.0F) ? -1.0F : 1.0F;
            }
        }
コード例 #2
0
ファイル: Mesh.cs プロジェクト: xorza/NetGL
        public void FromStream(BinaryReader reader)
        {
            if (reader == null)
            {
                throw new ArgumentNullException("reader");
            }

            if (reader.ReadBoolean())
            {
                Vertices = new Vector3Buffer(reader);
            }
            if (reader.ReadBoolean())
            {
                Tangents = new Vector3Buffer(reader);
            }
            if (reader.ReadBoolean())
            {
                Colors = new Vector3Buffer(reader);
            }
            if (reader.ReadBoolean())
            {
                Normals = new Vector3Buffer(reader);
            }
            if (reader.ReadBoolean())
            {
                TexCoords = new Vector2Buffer(reader);
            }
            if (reader.ReadBoolean())
            {
                Indices = new UInt32Buffer(reader);
            }

            Type      = (PrimitiveType)reader.ReadUInt32();
            DrawStyle = (PolygonMode)reader.ReadUInt32();
            FrontFace = (FrontFaceDirection)reader.ReadUInt32();
            Name      = reader.ReadNullbaleString();

            var boundsCenter = reader.ReadVector3();
            var boundsSize   = reader.ReadVector3();
            var boundsRadius = reader.ReadSingle();

            Bounds = new BoundingVolume(boundsCenter, boundsSize, boundsRadius);
        }
コード例 #3
0
ファイル: Mesh.cs プロジェクト: xorza/NetGL
        public void CalculateNormals()
        {
            Disposer.Dispose(ref _normals);
            _normals = new Vector3Buffer(_vertices.Length);

            if (Indices != null)
            {
                var normalsList = new List <Vector3> [_vertices.Length];
                var indices     = Indices;

                for (int i = 0; i < indices.Length; i++)
                {
                    Int32 pindex = (Int32)indices[i];
                    Int32 aindex, bindex;

                    switch (i % 3)
                    {
                    case 0:
                        aindex = (Int32)indices[i + 1];
                        bindex = (Int32)indices[i + 2];
                        break;

                    case 1:
                        aindex = (Int32)indices[i + 1];
                        bindex = (Int32)indices[i - 1];
                        break;

                    case 2:
                        aindex = (Int32)indices[i - 2];
                        bindex = (Int32)indices[i - 1];
                        break;

                    default:
                        throw new ArithmeticException();
                    }

                    var vertexNormals = normalsList[pindex];
                    if (vertexNormals == null)
                    {
                        vertexNormals = normalsList[pindex] = new List <Vector3>(1);
                    }

                    var p = _vertices[pindex];
                    var a = _vertices[aindex];
                    var b = _vertices[bindex];

                    var normal = Vector3.Cross(a - p, b - p).Normalized;
                    vertexNormals.Add(normal);
                }
                for (int i = 0; i < _vertices.Length; i++)
                {
                    _normals[i] = normalsList[i]
                                  .Sum()
                                  .Normalized;
                }
            }
            else
            {
                for (int i = 0; i < _vertices.Length; i++)
                {
                    var pindex = i;
                    int aindex, bindex;

                    switch (i % 3)
                    {
                    case 0:
                        aindex = i + 1;
                        bindex = i + 2;
                        break;

                    case 1:
                        aindex = i + 1;
                        bindex = i - 1;
                        break;

                    case 2:
                        aindex = i - 2;
                        bindex = i - 1;
                        break;

                    default:
                        throw new ArithmeticException();
                    }

                    var p = _vertices[pindex];
                    var a = _vertices[aindex];
                    var b = _vertices[bindex];

                    _normals[i] = Vector3
                                  .Cross(a - p, b - p)
                                  .Normalized;
                }
            }
        }