public VertexTriangle GetFace(int index)
        {
            IndexTriangle t   = _triangles[index];
            FacePoint     fp0 = _facePoints[t.Point0];
            FacePoint     fp1 = _facePoints[t.Point1];
            FacePoint     fp2 = _facePoints[t.Point2];
            Vertex        v0  = new Vertex(fp0, _buffers);
            Vertex        v1  = new Vertex(fp1, _buffers);
            Vertex        v2  = new Vertex(fp2, _buffers);

            return(new VertexTriangle(v0, v1, v2));
        }
예제 #2
0
        public void GetData(FacePoint facepoint, List <IDataBuffer> buffers)
        {
            if (facepoint.BufferIndices == null)
            {
                return;
            }

            for (int i = 0; i < facepoint.BufferIndices.Count; ++i)
            {
                IDataBuffer b     = buffers[i];
                int         index = facepoint.BufferIndices[i];
                EBufferType type  = b.Type;
                switch (type)
                {
                case EBufferType.Position:
                    Position = (Vec3)b[index];
                    break;

                case EBufferType.Normal:
                    Normal = (Vec3)b[index];
                    break;

                case EBufferType.Binormal:
                    Binormal = (Vec3)b[index];
                    break;

                case EBufferType.Tangent:
                    Tangent = (Vec3)b[index];
                    break;

                case EBufferType.Color:
                    Color = (ColorF4)b[index];
                    break;

                case EBufferType.TexCoord:
                    TexCoord = (Vec2)b[index];
                    break;
                }
            }
        }
예제 #3
0
        public void SetData(FacePoint facepoint, List <IDataBuffer> buffers)
        {
            if (facepoint.BufferIndices == null)
            {
                return;
            }
            for (int i = 0; i < facepoint.BufferIndices.Count; ++i)
            {
                IDataBuffer b     = buffers[i];
                int         index = facepoint.BufferIndices[i];
                EBufferType type  = b.Type;
                switch (type)
                {
                case EBufferType.Position:
                    b[index] = Position;
                    break;

                case EBufferType.Normal:
                    b[index] = Normal;
                    break;

                case EBufferType.Binormal:
                    b[index] = Binormal;
                    break;

                case EBufferType.Tangent:
                    b[index] = Tangent;
                    break;

                case EBufferType.Color:
                    b[index] = Color;
                    break;

                case EBufferType.TexCoord:
                    b[index] = TexCoord;
                    break;
                }
            }
        }
        public void GenerateBinormalTangentBuffers(int positionIndex, int uvIndex, bool addBinormals, bool addTangents)
        {
            IDataBuffer[] pBuffs = GetAllBuffersOfType(EBufferType.Position);
            if (pBuffs.Length == 0)
            {
                Collada.WriteLine("No position buffers found.");
                return;
            }
            if (!pBuffs.IndexInRange(positionIndex))
            {
                Collada.WriteLine("Position index out of range of available position buffers.");
                return;
            }
            //IDataBuffer[] nBuffs = GetAllBuffersOfType(EBufferType.Normal);
            //if (nBuffs.Length == 0)
            //{
            //    Console.WriteLine("No normal buffers found.");
            //    return;
            //}
            //if (!nBuffs.IndexInRange(normalIndex))
            //{
            //    Console.WriteLine("Normal index out of range of available normal buffers.");
            //    return;
            //}
            IDataBuffer[] tBuffs = GetAllBuffersOfType(EBufferType.TexCoord);
            if (tBuffs.Length == 0)
            {
                Collada.WriteLine("No texcoord buffers found.");
                return;
            }
            if (!tBuffs.IndexInRange(uvIndex))
            {
                Collada.WriteLine("UV index out of range of available texcoord buffers.");
                return;
            }

            Vec3 pos1, pos2, pos3;
            //Vec3 n0, n1, n2;
            Vec2 uv1, uv2, uv3;

            IDataBuffer pBuff = pBuffs[positionIndex];
            //VertexBuffer nBuff = pBuffs[normalIndex];
            IDataBuffer tBuff      = tBuffs[uvIndex];
            int         pointCount = _triangles.Count * 3;
            List <Vec3> binormals  = new List <Vec3>(pointCount);
            List <Vec3> tangents   = new List <Vec3>(pointCount);

            for (int i = 0; i < _triangles.Count; ++i)
            {
                IndexTriangle t = _triangles[i];

                FacePoint fp0 = _facePoints[t.Point0];
                FacePoint fp1 = _facePoints[t.Point1];
                FacePoint fp2 = _facePoints[t.Point2];

                pos1 = (Vec3)pBuff[fp0.BufferIndices[pBuff.Index]];
                pos2 = (Vec3)pBuff[fp1.BufferIndices[pBuff.Index]];
                pos3 = (Vec3)pBuff[fp2.BufferIndices[pBuff.Index]];

                uv1 = (Vec2)tBuff[fp0.BufferIndices[tBuff.Index]];
                uv2 = (Vec2)tBuff[fp1.BufferIndices[tBuff.Index]];
                uv3 = (Vec2)tBuff[fp2.BufferIndices[tBuff.Index]];

                Vec3 deltaPos1 = pos2 - pos1;
                Vec3 deltaPos2 = pos3 - pos1;

                Vec2 deltaUV1 = uv2 - uv1;
                Vec2 deltaUV2 = uv3 - uv1;

                Vec3 tangent;
                Vec3 binormal;

                float m = deltaUV1.X * deltaUV2.Y - deltaUV1.Y * deltaUV2.X;
                if (m == 0.0f)
                {
                    tangent  = Vec3.UnitY;
                    binormal = Vec3.UnitY;
                }
                else
                {
                    float r = 1.0f / m;
                    tangent  = (deltaPos1 * deltaUV2.Y - deltaPos2 * deltaUV1.Y) * r;
                    binormal = (deltaPos2 * deltaUV1.X - deltaPos1 * deltaUV2.X) * r;
                }

                binormals.Add(binormal);
                binormals.Add(binormal);
                binormals.Add(binormal);

                tangents.Add(tangent);
                tangents.Add(tangent);
                tangents.Add(tangent);
            }

            AddBuffer(binormals, EBufferType.Binormal);
            AddBuffer(tangents, EBufferType.Tangent);
            _bufferInfo.HasBinormals = true;
            _bufferInfo.HasTangents  = true;
        }
예제 #5
0
 public Vertex(FacePoint facepoint, List <IDataBuffer> buffers)
 => GetData(facepoint, buffers);