Ejemplo n.º 1
0
        /// <summary>
        /// Loads this class with data from a primitive.
        /// </summary>
        /// <param name="graphicsDevice">The graphicsDevice device.</param>
        /// <param name="primitive">The primitive to load.</param>
        public void FromPrimitive(GraphicsDevice graphicsDevice, Geometric primitive)
        {
            List <Mesh> meshes = new List <Mesh>();

            // Get Initial BoundingBox
            var min = new Vector3(float.MaxValue, float.MaxValue, float.MaxValue);
            var max = new Vector3(float.MinValue, float.MinValue, float.MinValue);

            var primitiveVertices = primitive.Vertices;

            for (int i = 0; i < primitiveVertices.Length; i++)
            {
                Vector3 vertex = primitiveVertices[i].Position;

                Vector3.Min(ref vertex, ref min, out min);
                Vector3.Max(ref vertex, ref max, out max);
            }

            BoundingBox bbox = new Common.Math.BoundingBox(min, max);

            // Load Primitive
            int vertexCount    = primitive.Vertices.Length;
            int indexCount     = primitive.Indices.Length;
            int primitiveCount = indexCount / 3;

            var meshVBuffer = new VertexBuffer(VertexPositionNormalTangentColorDualTexture.VertexFormat);

            meshVBuffer.SetData(primitive.ByteVertices, vertexCount);
            var meshIBuffer = new IndexBuffer(primitive.Indices);

            var baseMesh = new Mesh(0, vertexCount, 0, primitiveCount, meshVBuffer, meshIBuffer, PrimitiveType.TriangleList);

            baseMesh.Name        = "Primitive";
            baseMesh.BoundingBox = bbox;
            meshes.Add(baseMesh);

            primitive.Dispose();

            this.BoundingBox = bbox;

            this.FromMeshes(graphicsDevice, meshes, bbox);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Loads this class with data from a primitive.
        /// </summary>
        /// <param name="graphicsDevice">The graphicsDevice device.</param>
        /// <param name="primitive">The primitive to load.</param>
        public void FromPrimitive(GraphicsDevice graphicsDevice, Geometric primitive)
        {
            this.graphics = graphicsDevice;

            // Get Initial BoundingBox
            var min = new Vector3(float.MaxValue, float.MaxValue, float.MaxValue);
            var max = new Vector3(float.MinValue, float.MinValue, float.MinValue);

            List <Vector3> collisionVertices = new List <Vector3>();

            for (int i = 0; i < primitive.Vertices.Length; i++)
            {
                Vector3 vertex = primitive.Vertices[i].Position;
                collisionVertices.Add(vertex);

                Vector3.Min(ref vertex, ref min, out min);
                Vector3.Max(ref vertex, ref max, out max);
            }

            this.BoundingBox.Min = min;
            this.BoundingBox.Max = max;

            // Load Primitive
            int vertexCount    = primitive.Indices.Length;
            int primitiveCount = vertexCount / 3;

            var meshVBuffer = new VertexBuffer(VertexPositionNormalTexture.VertexFormat);

            meshVBuffer.SetData(primitive.ByteVertices, vertexCount);
            var meshIBuffer = new IndexBuffer(primitive.Indices);

            var baseMesh = new StaticMesh(0, vertexCount, 0, primitiveCount, meshVBuffer, meshIBuffer, true, collisionVertices.ToArray(), primitive.Indices, PrimitiveType.TriangleList);

            this.Meshes.Add(baseMesh);

            var rootBone = new Bone(0, -1, "Root", Matrix.Identity);

            this.Bones.Add(rootBone);

            this.MeshBonePairs.Add(0, 0);

            for (int i = 0; i < this.Bones.Count; i++)
            {
                Bone bone = this.Bones[i];
                if (bone.ParentIndex == -1)
                {
                    this.Bones[i].AbsoluteTransform = bone.LocalTransform;
                }
                else
                {
                    this.Bones[i].AbsoluteTransform = bone.LocalTransform
                                                      * this.Bones[bone.ParentIndex].AbsoluteTransform;
                }
            }

            for (int i = 0; i < this.Meshes.Count; i++)
            {
                StaticMesh m = this.Meshes[i];
                this.graphics.BindIndexBuffer(m.IndexBuffer);
                this.graphics.BindVertexBuffer(m.VertexBuffer);
            }

            primitive.Dispose();
        }