public static TriangleMeshShapeDesc CreateTriangleMesh(this Physics physics, StaticMeshData meshData)
        {
            // create descriptor for triangle mesh
            TriangleMeshShapeDesc triangleMeshShapeDesc = null;
            TriangleMeshDesc      triangleMeshDesc      = new TriangleMeshDesc();

            triangleMeshDesc.PinPoints <float>(meshData.Points, 0, sizeof(float) * 3);
            triangleMeshDesc.PinTriangles <uint>(meshData.Indices, 0, sizeof(uint) * 3);
            triangleMeshDesc.VertexCount   = (uint)meshData.Vertices.Length;
            triangleMeshDesc.TriangleCount = (uint)meshData.TriangleCount;

            MemoryStream stream = new MemoryStream(1024);

            CookingInterface.InitCooking();

            if (CookingInterface.CookTriangleMesh(triangleMeshDesc, stream))
            {
                stream.Seek(0, SeekOrigin.Begin);
                TriangleMesh triangleMesh = physics.CreateTriangleMesh(stream);
                triangleMeshShapeDesc = new TriangleMeshShapeDesc(triangleMesh);
                CookingInterface.CloseCooking();
            }

            triangleMeshDesc.UnpinAll();
            return(triangleMeshShapeDesc);
        }
示例#2
0
        public static TriangleMeshShapeDesc CreateTriangleMesh(StaticMeshData meshData)
        {
            // create descriptor for triangle mesh
            TriangleMeshShapeDesc triangleMeshShapeDesc = null;
            TriangleMeshDesc triangleMeshDesc = new TriangleMeshDesc();
            triangleMeshDesc.PinPoints<float>(meshData.Points, 0, sizeof(float) * 3);
            triangleMeshDesc.PinTriangles<uint>(meshData.Indices, 0, sizeof(uint) * 3);
            triangleMeshDesc.VertexCount = (uint)meshData.Vertices.Length;
            triangleMeshDesc.TriangleCount = (uint)meshData.TriangleCount;

            MemoryStream stream = new MemoryStream(1024);
            CookingInterface.InitCooking();

            if (CookingInterface.CookTriangleMesh(triangleMeshDesc, stream))
            {
                stream.Seek(0, SeekOrigin.Begin);
                TriangleMesh triangleMesh = OgreWindow.Instance.physics.CreateTriangleMesh(stream);
                triangleMeshShapeDesc = new TriangleMeshShapeDesc(triangleMesh);
                CookingInterface.CloseCooking();
            }

            triangleMeshDesc.UnpinAll();
            return triangleMeshShapeDesc;
        }
示例#3
0
        /// <summary>
        /// Create a shape from a <seealso cref="ShapeType"/> and the BaseEntity information.
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        protected ShapeDesc CreateShapeFromType(ShapeType type)
        {
            float scale = this.parentEntity.Scale;

            this.height   *= scale;
            this.width    *= scale;
            this.depth    *= scale;
            this.diameter *= scale;

            switch (type)
            {
            case ShapeType.Box:
            {
                if (null == this.physMesh)
                {
                    var shape = new BoxShapeDesc();
                    shape.Extents = new Vector3(this.width, this.height, this.depth);
                    return(shape);
                }
                else
                {
                    return(CreateBoxShapeFromMesh(this.physMesh, scale));
                }
            }

            case ShapeType.Sphere:
            {
                if (null == this.physMesh)
                {
                    var shape = new SphereShapeDesc();
                    shape.Radius = this.diameter / 2.0f;
                    return(shape);
                }
                else
                {
                    return(CreateSphereShapeFromMesh(this.physMesh, scale));
                }
            }

            case ShapeType.Heightfield:
            {
                // Unsupported by this method, use CreateHeightfieldShape()
                return(null);
            }

            case ShapeType.Capsule:
            {
                var shape = new CapsuleShapeDesc();
                shape.Radius = this.diameter / 2.0f;
                shape.Length = this.height;
                return(shape);
            }

            case ShapeType.Cylinder:
            {
                var shape = new CylinderShapeDesc();
                shape.Height = this.height;
                shape.Radius = this.diameter / 2.0f;
                return(shape);
            }

            case ShapeType.Cone:
            {
                var shape = new ConeShapeDesc();
                shape.Height = this.height;
                shape.Radius = this.diameter / 2.0f;
                return(shape);
            }

            case ShapeType.TriangleMesh:
            {
                if (isDynamic)
                {
                    throw new Exception("Triangle Mesh shapes do not support dynamic physics");
                }

                TriangleMeshShapeDesc shape = new TriangleMeshShapeDesc();

                if (this.physMesh == null)
                {
                    MsgGetModelVertices msgGetVerts = ObjectPool.Aquire <MsgGetModelVertices>();
                    msgGetVerts.UniqueTarget = this.parentEntity.UniqueID;
                    this.parentEntity.Game.SendMessage(msgGetVerts);

                    shape.Vertices = msgGetVerts.Vertices;

                    MsgGetModelIndices msgGetInds = ObjectPool.Aquire <MsgGetModelIndices>();
                    msgGetInds.UniqueTarget = this.parentEntity.UniqueID;
                    this.parentEntity.Game.SendMessage(msgGetInds);

                    shape.Indices = msgGetInds.Indices;
                }
                else
                {
                    shape.Vertices = physMesh.GetModelVertices(this.parentEntity.Scale);
                    shape.Indices  = physMesh.GetModelIndices();
                }

                if ((shape.Vertices.Count == 0) || (shape.Indices.Count == 0))
                {
                    return(null);
                }

                return(shape);
            }

            default:
                // Throw exception
                return(null);
            }
            ;
        }