Пример #1
0
        public TriangleMesh CreateTriangleMesh()
        {
            TriangleMesh triangleMesh;

            TriangleMeshDesc desc = new TriangleMeshDesc();

            desc.Name   = Name;
            desc.Flags |= Is16BitIndices ? MeshFlag.BIT_INDICES_16 : 0;
            desc.Flags |= MeshFlag.HARDWARE_MESH;

            desc.NumTriangles        = FaceCount;
            desc.NumVertices         = VertexCount;
            desc.TriangleStrideBytes = 3 * (Is16BitIndices ? sizeof(short) : sizeof(int));
            desc.PointStrideBytes    = VertexDescriptor.SizeOf(IASemantic.Position, 0);
            int stride = desc.PointStrideBytes;

            var ibData   = _ib.Map(MapType.Read);
            var vbStream = _vb.Map(MapType.Read);

            try
            {
                int posOffset = VertexDescriptor.OffsetOf(IASemantic.Position, 0);
                int size      = _vd.Size;

                byte[] positions = new byte[desc.NumVertices * stride];
                unsafe
                {
                    fixed(byte *desPter = positions)
                    {
                        byte *srcPter = (byte *)vbStream + posOffset;
                        byte *pter    = desPter;

                        for (int i = 0; i < desc.NumVertices; i++, srcPter += size, pter += stride)
                        {
                            *(Vector3 *)pter = *(Vector3 *)(srcPter);
                        }

                        desc.Points    = (IntPtr)desPter;
                        desc.Triangles = ibData;

                        //Cooking.Create();

                        //Cooking.InitCooking();
                        //byte[] stream = Cooking.CookTriangleMesh(desc);

                        //Cooking.CloseCooking();

                        //desc.Dispose();

                        //triangleMesh = new TriangleMesh(stream) { Name = Name, UserData = this };

                        triangleMesh             = PhysicManager.Sigleton.CreateTriangleMesh(desc);
                        triangleMesh.GraphicMesh = this;
                    }
                }
            }
            finally
            {
                _vb.Unmap();
                _ib.Unmap();
            }

            return(triangleMesh);
        }