Example #1
0
        public static BoundingBox BoundingBoxFromXMesh(SlimDX.Direct3D9.Mesh mesh)
        {
            var c = mesh.LockVertexBuffer(SlimDX.Direct3D9.LockFlags.ReadOnly);
            var b = SlimDX.BoundingBox.FromPoints(c, mesh.VertexCount, mesh.BytesPerVertex);

            c.Close();
            mesh.UnlockVertexBuffer();
            return(b);
        }
Example #2
0
        public Mesh(SlimDX.Direct3D9.Mesh mesh)
        {
            if (mesh.BytesPerVertex != Vertex.PositionNormalTexcoord.Instance.Size ||
                mesh.VertexFormat != Vertex.PositionNormalTexcoord.Instance.VertexFormat)
            {
                throw new Exception("Mesh vertex buffer needs to be in the format Position3Normal3Texcoord3");
            }

            var vb = mesh.LockVertexBuffer(SlimDX.Direct3D9.LockFlags.ReadOnly);

            NVertices = (int)vb.Length /
                        Vertex.PositionNormalTexcoord.Instance.Size;
            var vbs = vb.ReadRange <Vertex.PositionNormalTexcoord>(NVertices);

            VertexBuffer = new VertexBuffer <Vertex.PositionNormalTexcoord>(vbs);
            mesh.UnlockVertexBuffer();
            vb.Close();


            var ib = mesh.LockIndexBuffer(SlimDX.Direct3D9.LockFlags.ReadOnly);

            NFaces = mesh.FaceCount;
            if ((mesh.CreationOptions & SlimDX.Direct3D9.MeshFlags.Use32Bit) == 0)
            {
                List <int> ibs = new List <int>();
                foreach (var v in ib.ReadRange <short>(NFaces * 3))
                {
                    ibs.Add(v);
                }
                IndexBuffer = new IndexBuffer(ibs.ToArray());
            }
            else
            {
                IndexBuffer = new IndexBuffer(ib.ReadRange <int>(NFaces * 3));
            }
            mesh.UnlockIndexBuffer();
            ib.Close();

            VertexStreamLayout = Vertex.PositionNormalTexcoord.Instance;
            MeshType           = MeshType.Indexed;
        }