Exemple #1
0
        public static void AddGeometry(this DynamicPrimitive dynamicPrimitive, IGeometry geometry, Color color)
        {
            Vector3[] positions;
            ushort[]  indices;
            geometry.TryGetTriangles(out positions, out indices);
            var transform = geometry.Transform;

            dynamicPrimitive.BeginPrimitive(PrimitiveType.LineList, null, transform);
            {
                foreach (Vector3 position in positions)
                {
                    dynamicPrimitive.AddVertex(position, color);
                }

                for (int i = 0; i < indices.Length; i += 3)
                {
                    dynamicPrimitive.AddIndex(indices[i + 0]);
                    dynamicPrimitive.AddIndex(indices[i + 1]);
                    dynamicPrimitive.AddIndex(indices[i + 1]);
                    dynamicPrimitive.AddIndex(indices[i + 2]);
                    dynamicPrimitive.AddIndex(indices[i + 2]);
                    dynamicPrimitive.AddIndex(indices[i + 0]);
                }
            }
            dynamicPrimitive.EndPrimitive();
        }
Exemple #2
0
        public static void AddSolidCone(this DynamicPrimitive dynamicPrimitive, Vector3 position, float height, float radius, int tessellation, Matrix?world, Color color)
        {
            if (tessellation < 3)
            {
                throw new ArgumentOutOfRangeException("tessellation");
            }

            dynamicPrimitive.BeginPrimitive(PrimitiveType.TriangleList, null, world);
            {
                dynamicPrimitive.AddVertex(position + Vector3.Up * height, color);
                dynamicPrimitive.AddVertex(position, color);

                for (int i = 0; i < tessellation; ++i)
                {
                    float angle = i * MathHelper.TwoPi / tessellation;

                    float dx = (float)Math.Cos(angle);
                    float dz = (float)Math.Sin(angle);

                    Vector3 normal = new Vector3(dx, 0, dz);

                    dynamicPrimitive.AddVertex(position + normal * radius, color);

                    dynamicPrimitive.AddIndex(0);
                    dynamicPrimitive.AddIndex(2 + i);
                    dynamicPrimitive.AddIndex(2 + (i + 1) % tessellation);

                    dynamicPrimitive.AddIndex(1);
                    dynamicPrimitive.AddIndex(2 + (i + 1) % tessellation);
                    dynamicPrimitive.AddIndex(2 + i);
                }
            }
            dynamicPrimitive.EndPrimitive();
        }
Exemple #3
0
        public static void AddRectangle(this DynamicPrimitive dynamicPrimitive, BoundingRectangle bound, Rectangle?Source, Texture2D texture, Color color, Flip flip, Matrix?world)
        {
            Vector3[] Verts =
            {
                new Vector3(bound.X,               bound.Y,                0),
                new Vector3(bound.X,               bound.Y + bound.Height, 0),
                new Vector3(bound.X + bound.Width, bound.Y + bound.Height, 0),
                new Vector3(bound.X + bound.Width, bound.Y,                0),
            };

            var texCoords = Nine.Graphics.UI.UIExtensions.TextureCoords(flip);

            // TODO: Calculate Source Rectangle with texCoords
            dynamicPrimitive.BeginPrimitive(PrimitiveType.TriangleList, texture, world);
            {
                dynamicPrimitive.AddVertex(Verts[0], color, texCoords[0]);
                dynamicPrimitive.AddVertex(Verts[1], color, texCoords[1]);
                dynamicPrimitive.AddVertex(Verts[2], color, texCoords[2]);
                dynamicPrimitive.AddVertex(Verts[3], color, texCoords[3]);

                dynamicPrimitive.AddIndex(0);
                dynamicPrimitive.AddIndex(1);
                dynamicPrimitive.AddIndex(2);
                dynamicPrimitive.AddIndex(0);
                dynamicPrimitive.AddIndex(3);
                dynamicPrimitive.AddIndex(2);
            }
            dynamicPrimitive.EndPrimitive();
        }
        public static void AddCircle(this DynamicPrimitive dynamicPrimitive, Vector3 center, float radius, Vector3 up, int tessellation, Matrix?world, Color color, float lineWidth)
        {
            Matrix transform = Matrix.CreateScale(radius) *
                               MatrixHelper.CreateRotation(Vector3.Up, up) *
                               Matrix.CreateTranslation(center);

            dynamicPrimitive.BeginPrimitive(PrimitiveType.LineStrip, null, world, lineWidth);
            {
                if (tessellation < 3)
                {
                    throw new ArgumentOutOfRangeException("tessellation");
                }

                int horizontalSegments = tessellation;

                // Create a single ring of vertices at this latitude.
                for (int j = 0; j <= horizontalSegments; j++)
                {
                    float longitude = j * MathHelper.TwoPi / horizontalSegments;

                    float dx = (float)Math.Cos(longitude);
                    float dy = (float)Math.Sin(longitude);

                    Vector3 normal = new Vector3(dx, dy, 0);

                    dynamicPrimitive.AddVertex(Vector3.Transform(normal, transform), color);
                }
            }
            dynamicPrimitive.EndPrimitive();
        }
Exemple #5
0
 public static void AddSkeleton(this DynamicPrimitive dynamicPrimitive, Microsoft.Xna.Framework.Graphics.Model model, Matrix?world, Color color, float lineWidth)
 {
     dynamicPrimitive.BeginPrimitive(PrimitiveType.LineList, null, world, lineWidth);
     {
         AddSkeleton(dynamicPrimitive, model.Root, Matrix.Identity, world, color);
     }
     dynamicPrimitive.EndPrimitive();
 }
Exemple #6
0
 public static void AddSkeleton(this DynamicPrimitive dynamicPrimitive, Skeleton skeleton, Matrix?world, Color color, float lineWidth)
 {
     dynamicPrimitive.BeginPrimitive(PrimitiveType.LineList, null, world, lineWidth);
     {
         AddSkeleton(dynamicPrimitive, skeleton, 0, Matrix.Identity, color);
     }
     dynamicPrimitive.EndPrimitive();
 }
 public static void AddLine(this DynamicPrimitive dynamicPrimitive, Vector3 v1, Vector3 v2, Matrix?world, Color color, float lineWidth)
 {
     dynamicPrimitive.BeginPrimitive(PrimitiveType.LineList, null, world, lineWidth);
     {
         dynamicPrimitive.AddVertex(v1, color);
         dynamicPrimitive.AddVertex(v2, color);
     }
     dynamicPrimitive.EndPrimitive();
 }
Exemple #8
0
 public static void AddTriangle(this DynamicPrimitive dynamicPrimitive, Triangle triangle, Matrix?world, Color color, float lineWidth)
 {
     dynamicPrimitive.BeginPrimitive(PrimitiveType.LineList, null, world, lineWidth);
     dynamicPrimitive.AddVertex(ref triangle.V1, color);
     dynamicPrimitive.AddVertex(ref triangle.V2, color);
     dynamicPrimitive.AddVertex(ref triangle.V2, color);
     dynamicPrimitive.AddVertex(ref triangle.V3, color);
     dynamicPrimitive.AddVertex(ref triangle.V3, color);
     dynamicPrimitive.AddVertex(ref triangle.V1, color);
     dynamicPrimitive.EndPrimitive();
 }
 public static void AddLine(this DynamicPrimitive dynamicPrimitive, IEnumerable <Vector3> lineStrip, Matrix?world, Color color, float lineWidth)
 {
     dynamicPrimitive.BeginPrimitive(PrimitiveType.LineStrip, null, world, lineWidth);
     {
         foreach (Vector3 position in lineStrip)
         {
             dynamicPrimitive.AddVertex(position, color);
         }
     }
     dynamicPrimitive.EndPrimitive();
 }
Exemple #10
0
        public static void AddArrow(this DynamicPrimitive dynamicPrimitive, Vector3 start, Vector3 end, Color color, float lineWidth)
        {
            const float Ratio = 0.2f;
            var         mid   = Vector3.Lerp(end, start, Ratio);
            var         head  = (end - start).Length() * Ratio;

            dynamicPrimitive.AddLine(start, mid, null, color, lineWidth);
            dynamicPrimitive.AddSolidCone(Vector3.Zero, head, head * 0.5f, 24,
                                          MatrixHelper.CreateRotation(Vector3.Up, Vector3.Normalize(end - start)) *
                                          Matrix.CreateTranslation(mid), color);
        }
        public static void AddPlane(this DynamicPrimitive dynamicPrimitive, Microsoft.Xna.Framework.Plane plane, float size, int tessellation, Matrix?world, Color color, float lineWidth)
        {
            Matrix transform = MatrixHelper.CreateRotation(Vector3.Up, plane.Normal) *
                               Matrix.CreateTranslation(plane.Normal * plane.D);

            if (world.HasValue)
            {
                transform *= world.Value;
            }

            AddGrid(dynamicPrimitive, 0, 0, 0, size, size, tessellation, tessellation, transform, color, lineWidth);
        }
Exemple #12
0
        public static void AddAxis(this DynamicPrimitive dynamicPrimitive, Matrix world, float length, Color colorX, Color colorY, Color colorZ, float lineWidth)
        {
            Vector3    scale;
            Vector3    translation;
            Quaternion rotation;

            world.Decompose(out scale, out rotation, out translation);
            world             = Matrix.CreateFromQuaternion(rotation);
            world.Translation = translation;

            AddArrow(dynamicPrimitive, world.Translation, Vector3.Transform(Vector3.UnitX * length, world), colorX, lineWidth);
            AddArrow(dynamicPrimitive, world.Translation, Vector3.Transform(Vector3.UnitY * length, world), colorY, lineWidth);
            AddArrow(dynamicPrimitive, world.Translation, Vector3.Transform(Vector3.UnitZ * length, world), colorZ, lineWidth);
        }
        public static void AddConstrainedBillboard(this DynamicPrimitive dynamicPrimitive, Texture2D texture, Vector3 start, Vector3 end, float width, Matrix?textureTransform, Matrix?world, Color color, Vector3 cameraPosition)
        {
            //      aa --- ab
            //       |     |
            //       |     |
            //      ba --- bb
            VertexPositionColorTexture aa;
            VertexPositionColorTexture ab;
            VertexPositionColorTexture ba;
            VertexPositionColorTexture bb;

            CreateBillboard(start, end, width, cameraPosition, out aa.Position, out ab.Position,
                            out ba.Position, out bb.Position);

            if (textureTransform != null)
            {
                aa.TextureCoordinate = TextureTransform.Transform(textureTransform.Value, Vector2.One);
                ab.TextureCoordinate = TextureTransform.Transform(textureTransform.Value, Vector2.UnitX);
                ba.TextureCoordinate = TextureTransform.Transform(textureTransform.Value, Vector2.UnitY);
                bb.TextureCoordinate = TextureTransform.Transform(textureTransform.Value, Vector2.Zero);
            }
            else
            {
                aa.TextureCoordinate = Vector2.One;
                ab.TextureCoordinate = Vector2.UnitX;
                ba.TextureCoordinate = Vector2.UnitY;
                bb.TextureCoordinate = Vector2.Zero;
            }

            aa.Color     = ab.Color =
                ba.Color = bb.Color = color;

            dynamicPrimitive.BeginPrimitive(PrimitiveType.TriangleList, texture, world);
            {
                // Add new vertices and indices
                dynamicPrimitive.AddIndex((ushort)(0));
                dynamicPrimitive.AddIndex((ushort)(1));
                dynamicPrimitive.AddIndex((ushort)(2));
                dynamicPrimitive.AddIndex((ushort)(1));
                dynamicPrimitive.AddIndex((ushort)(3));
                dynamicPrimitive.AddIndex((ushort)(2));

                dynamicPrimitive.AddVertex(ref aa);
                dynamicPrimitive.AddVertex(ref ab);
                dynamicPrimitive.AddVertex(ref ba);
                dynamicPrimitive.AddVertex(ref bb);
            }
            dynamicPrimitive.EndPrimitive();
        }
Exemple #14
0
        private static void AddSkeleton(this DynamicPrimitive dynamicPrimitive, Skeleton skeleton, int bone, Matrix parentTransform, Color color)
        {
            Matrix start = parentTransform;
            Matrix end   = skeleton.GetBoneTransform(bone) * parentTransform;

            if (Vector3.Subtract(end.Translation, start.Translation).LengthSquared() > 0)
            {
                dynamicPrimitive.AddVertex(start.Translation, color);
                dynamicPrimitive.AddVertex(end.Translation, color);

                Vector3.Subtract(end.Translation, start.Translation).Length();
            }

            foreach (int child in skeleton.GetChildBones(bone))
            {
                AddSkeleton(dynamicPrimitive, skeleton, child, end, color);
            }
        }
Exemple #15
0
        public static void AddSolidBox(this DynamicPrimitive dynamicPrimitive, BoundingBox boundingBox, Matrix?world, Color color)
        {
            Vector3[] corners = boundingBox.GetCorners();

            dynamicPrimitive.BeginPrimitive(PrimitiveType.TriangleList, null, world);
            {
                for (int i = 0; i < BoundingBox.CornerCount; ++i)
                {
                    dynamicPrimitive.AddVertex(ref corners[i], color);
                }

                for (int i = 0; i < BoundingBoxExtensions.TriangleIndices.Length; ++i)
                {
                    dynamicPrimitive.AddIndex(BoundingBoxExtensions.TriangleIndices[i]);
                }
            }
            dynamicPrimitive.EndPrimitive();
        }
Exemple #16
0
        public static void AddBox(this DynamicPrimitive dynamicPrimitive, BoundingBox boundingBox, Matrix?world, Color color, float lineWidth)
        {
            dynamicPrimitive.BeginPrimitive(PrimitiveType.LineList, null, world, lineWidth);
            {
                Vector3[] corners = boundingBox.GetCorners();

                dynamicPrimitive.AddVertex(corners[0], color);
                dynamicPrimitive.AddVertex(corners[1], color);

                dynamicPrimitive.AddVertex(corners[1], color);
                dynamicPrimitive.AddVertex(corners[2], color);

                dynamicPrimitive.AddVertex(corners[2], color);
                dynamicPrimitive.AddVertex(corners[3], color);

                dynamicPrimitive.AddVertex(corners[3], color);
                dynamicPrimitive.AddVertex(corners[0], color);

                dynamicPrimitive.AddVertex(corners[4], color);
                dynamicPrimitive.AddVertex(corners[5], color);

                dynamicPrimitive.AddVertex(corners[5], color);
                dynamicPrimitive.AddVertex(corners[6], color);

                dynamicPrimitive.AddVertex(corners[6], color);
                dynamicPrimitive.AddVertex(corners[7], color);

                dynamicPrimitive.AddVertex(corners[7], color);
                dynamicPrimitive.AddVertex(corners[4], color);

                dynamicPrimitive.AddVertex(corners[0], color);
                dynamicPrimitive.AddVertex(corners[4], color);

                dynamicPrimitive.AddVertex(corners[1], color);
                dynamicPrimitive.AddVertex(corners[5], color);

                dynamicPrimitive.AddVertex(corners[2], color);
                dynamicPrimitive.AddVertex(corners[6], color);

                dynamicPrimitive.AddVertex(corners[3], color);
                dynamicPrimitive.AddVertex(corners[7], color);
            }
            dynamicPrimitive.EndPrimitive();
        }
Exemple #17
0
        private static void AddSkeleton(this DynamicPrimitive dynamicPrimitive, ModelBone node, Matrix parentTransform, Matrix?world, Color color)
        {
            Matrix start = parentTransform;
            Matrix end   = node.Transform * parentTransform;

            if (node.Parent != null)
            {
                if (Vector3.Subtract(end.Translation, start.Translation).LengthSquared() > 0)
                {
                    dynamicPrimitive.AddVertex(start.Translation, color);
                    dynamicPrimitive.AddVertex(end.Translation, color);
                }
            }

            foreach (ModelBone child in node.Children)
            {
                AddSkeleton(dynamicPrimitive, child, end, world, color);
            }
        }
        public static void AddRectangle(this DynamicPrimitive dynamicPrimitive, Vector2 min, Vector2 max, Matrix?world, Color color, float lineWidth)
        {
            dynamicPrimitive.BeginPrimitive(PrimitiveType.LineList, null, world, lineWidth);
            {
                dynamicPrimitive.AddVertex(new Vector3(min.X, min.Y, 0), color);
                dynamicPrimitive.AddVertex(new Vector3(min.X, max.Y, 0), color);
                dynamicPrimitive.AddVertex(new Vector3(max.X, max.Y, 0), color);
                dynamicPrimitive.AddVertex(new Vector3(max.X, min.Y, 0), color);

                dynamicPrimitive.AddIndex(0);
                dynamicPrimitive.AddIndex(1);
                dynamicPrimitive.AddIndex(1);
                dynamicPrimitive.AddIndex(2);
                dynamicPrimitive.AddIndex(2);
                dynamicPrimitive.AddIndex(3);
                dynamicPrimitive.AddIndex(3);
                dynamicPrimitive.AddIndex(0);
            }
            dynamicPrimitive.EndPrimitive();
        }
Exemple #19
0
        /// <summary>
        /// Adds a wireframe'ish rectangle.
        /// </summary>
        /// <param name="dynamicPrimitive"></param>
        /// <param name="bound"></param>
        /// <param name="color"></param>
        /// <param name="world"></param>
        public static void AddRectangle(this DynamicPrimitive dynamicPrimitive, BoundingRectangle bound, Color color, float lineWidth, Matrix?world)
        {
            dynamicPrimitive.BeginPrimitive(PrimitiveType.LineList, null, world, lineWidth);
            {
                dynamicPrimitive.AddVertex(new Vector3(bound.X, bound.Y, 0), color);
                dynamicPrimitive.AddVertex(new Vector3(bound.X, bound.Y + bound.Height, 0), color);
                dynamicPrimitive.AddVertex(new Vector3(bound.X + bound.Width, bound.Y + bound.Height, 0), color);
                dynamicPrimitive.AddVertex(new Vector3(bound.X + bound.Width, bound.Y, 0), color);

                dynamicPrimitive.AddIndex(0);
                dynamicPrimitive.AddIndex(1);
                dynamicPrimitive.AddIndex(1);
                dynamicPrimitive.AddIndex(2);
                dynamicPrimitive.AddIndex(2);
                dynamicPrimitive.AddIndex(3);
                dynamicPrimitive.AddIndex(3);
                dynamicPrimitive.AddIndex(0);
            }
            dynamicPrimitive.EndPrimitive();
        }
        public static void AddGrid(this DynamicPrimitive dynamicPrimitive, float x, float y, float z, float width, float height, int countX, int countZ, Matrix?world, Color color, float lineWidth)
        {
            dynamicPrimitive.BeginPrimitive(PrimitiveType.LineList, null, world, lineWidth);
            {
                float incU = width / countX;
                float incV = height / countZ;

                for (int u = 0; u <= countX; u++)
                {
                    dynamicPrimitive.AddVertex(new Vector3(x + 0, y, z + u * incU), color);
                    dynamicPrimitive.AddVertex(new Vector3(x + height, y, z + u * incU), color);
                }

                for (int v = 0; v <= countZ; v++)
                {
                    dynamicPrimitive.AddVertex(new Vector3(x + v * incV, y, z + 0), color);
                    dynamicPrimitive.AddVertex(new Vector3(x + v * incV, y, z + width), color);
                }
            }
            dynamicPrimitive.EndPrimitive();
        }
Exemple #21
0
        public static void AddFrustum(this DynamicPrimitive dynamicPrimitive, BoundingFrustum boundingFrustum, Matrix?world, Color color, float lineWidth)
        {
            Vector3[] corners = boundingFrustum.GetCorners();

            // near plane
            dynamicPrimitive.AddLine(corners[0], corners[1], color, lineWidth);
            dynamicPrimitive.AddLine(corners[1], corners[2], color, lineWidth);
            dynamicPrimitive.AddLine(corners[2], corners[3], color, lineWidth);
            dynamicPrimitive.AddLine(corners[3], corners[0], color, lineWidth);

            // connections
            dynamicPrimitive.AddLine(corners[0], corners[4], color, lineWidth);
            dynamicPrimitive.AddLine(corners[1], corners[5], color, lineWidth);
            dynamicPrimitive.AddLine(corners[2], corners[6], color, lineWidth);
            dynamicPrimitive.AddLine(corners[3], corners[7], color, lineWidth);

            // far plane
            dynamicPrimitive.AddLine(corners[4], corners[5], color, lineWidth);
            dynamicPrimitive.AddLine(corners[5], corners[6], color, lineWidth);
            dynamicPrimitive.AddLine(corners[6], corners[7], color, lineWidth);
            dynamicPrimitive.AddLine(corners[7], corners[4], color, lineWidth);
        }
        public static void AddRectangle(this DynamicPrimitive dynamicPrimitive, Vector2 min, Vector2 max, Vector3 up, Matrix?world, Color color, float lineWidth)
        {
            Matrix transform = MatrixHelper.CreateRotation(Vector3.Up, up);

            dynamicPrimitive.BeginPrimitive(PrimitiveType.LineList, null, world, lineWidth);
            {
                dynamicPrimitive.AddVertex(Vector3.TransformNormal(new Vector3(min.X, min.Y, 0), transform), color);
                dynamicPrimitive.AddVertex(Vector3.TransformNormal(new Vector3(min.X, max.Y, 0), transform), color);
                dynamicPrimitive.AddVertex(Vector3.TransformNormal(new Vector3(max.X, max.Y, 0), transform), color);
                dynamicPrimitive.AddVertex(Vector3.TransformNormal(new Vector3(max.X, min.Y, 0), transform), color);

                dynamicPrimitive.AddIndex(0);
                dynamicPrimitive.AddIndex(1);
                dynamicPrimitive.AddIndex(1);
                dynamicPrimitive.AddIndex(2);
                dynamicPrimitive.AddIndex(2);
                dynamicPrimitive.AddIndex(3);
                dynamicPrimitive.AddIndex(3);
                dynamicPrimitive.AddIndex(0);
            }
            dynamicPrimitive.EndPrimitive();
        }
Exemple #23
0
        public static void AddCollision(this DynamicPrimitive dynamicPrimitive, Microsoft.Xna.Framework.Graphics.Model model, Matrix?world, Color color)
        {
            if (bones == null || bones.Length < model.Bones.Count)
            {
                bones = new Matrix[model.Bones.Count];
            }

            model.CopyAbsoluteBoneTransformsTo(bones);

            Matrix transform = world.HasValue ? world.Value : Matrix.Identity;

            ModelTag tag = model.Tag as ModelTag;

            // Add collision tree
            if (tag != null && tag.Collision != null)
            {
                transform = bones[model.Meshes[0].ParentBone.Index] * transform;
                Octree <bool> tree = tag.Collision.CollisionTree;

                tree.Traverse(node =>
                {
                    if (!node.hasChildren && node.value)
                    {
                        dynamicPrimitive.AddSolidBox(node.bounds, transform, color);
                    }
                    return(TraverseOptions.Continue);
                });
            }
            // Add collision sphere
            else
            {
                for (int i = 0; i < model.Meshes.Count; ++i)
                {
                    var mesh = model.Meshes[i];
                    dynamicPrimitive.AddSolidSphere(mesh.BoundingSphere, 18, bones[mesh.ParentBone.Index] * transform, color);
                }
            }
        }
Exemple #24
0
 public static void AddSolidBox(this DynamicPrimitive dynamicPrimitive, Vector3 center, Vector3 size, Matrix?world, Color color)
 {
     AddSolidBox(dynamicPrimitive, new BoundingBox(center - size / 2, center + size / 2), world, color);
 }
Exemple #25
0
 public static void AddSphere(this DynamicPrimitive dynamicPrimitive, Vector3 center, float radius, Color color, float lineWidth)
 {
     AddSphere(dynamicPrimitive, new BoundingSphere(center, radius), 4, null, color, lineWidth);
 }
Exemple #26
0
 public static void AddSolidSphere(this DynamicPrimitive dynamicPrimitive, Vector3 center, float radius, Color color)
 {
     AddSolidSphere(dynamicPrimitive, new BoundingSphere(center, radius), 8, null, color);
 }
Exemple #27
0
 public static void AddSolidSphere(this DynamicPrimitive dynamicPrimitive, Vector3 center, float radius, int tessellation, Matrix?world, Color color)
 {
     AddSolidSphere(dynamicPrimitive, new BoundingSphere(center, radius), tessellation, world, color);
 }
Exemple #28
0
 public static void AddAxis(this DynamicPrimitive dynamicPrimitive, Matrix world, float lineWidth)
 {
     AddArrow(dynamicPrimitive, world.Translation, Vector3.Transform(Vector3.UnitX, world), new Color(255, 0, 0), lineWidth);
     AddArrow(dynamicPrimitive, world.Translation, Vector3.Transform(Vector3.UnitY, world), new Color(0, 255, 0), lineWidth);
     AddArrow(dynamicPrimitive, world.Translation, Vector3.Transform(Vector3.UnitZ, world), new Color(0, 0, 255), lineWidth);
 }
Exemple #29
0
        public static void AddSolidSphere(this DynamicPrimitive dynamicPrimitive, BoundingSphere boundingSphere, int tessellation, Matrix?world, Color color)
        {
            dynamicPrimitive.BeginPrimitive(PrimitiveType.TriangleList, null, world);
            {
                if (tessellation < 3)
                {
                    throw new ArgumentOutOfRangeException("tessellation");
                }

                int currentVertex      = 2;
                int verticalSegments   = tessellation;
                int horizontalSegments = tessellation * 2;

                // Start with a single vertex at the bottom of the sphere.
                dynamicPrimitive.AddVertex(Vector3.Down * boundingSphere.Radius + boundingSphere.Center, color);

                // Create rings of vertices at progressively higher latitudes.
                for (int i = 0; i < verticalSegments - 1; ++i)
                {
                    float latitude = ((i + 1) * MathHelper.Pi /
                                      verticalSegments) - MathHelper.PiOver2;

                    float dy  = (float)Math.Sin(latitude);
                    float dxz = (float)Math.Cos(latitude);

                    // Create a single ring of vertices at this latitude.
                    for (int j = 0; j < horizontalSegments; j++)
                    {
                        float longitude = j * MathHelper.TwoPi / horizontalSegments;

                        float dx = (float)Math.Cos(longitude) * dxz;
                        float dz = (float)Math.Sin(longitude) * dxz;

                        Vector3 normal = new Vector3(dx, dy, dz);

                        dynamicPrimitive.AddVertex(normal * boundingSphere.Radius + boundingSphere.Center, color);
                        currentVertex++;
                    }
                }

                // Finish with a single vertex at the top of the sphere.
                dynamicPrimitive.AddVertex(Vector3.Up * boundingSphere.Radius + boundingSphere.Center, color);

                // Create a fan connecting the bottom vertex to the bottom latitude ring.
                for (int i = 0; i < horizontalSegments; ++i)
                {
                    dynamicPrimitive.AddIndex(0);
                    dynamicPrimitive.AddIndex(1 + (i + 1) % horizontalSegments);
                    dynamicPrimitive.AddIndex(1 + i);
                }

                // Fill the sphere body with triangles joining each pair of latitude rings.
                for (int i = 0; i < verticalSegments - 2; ++i)
                {
                    for (int j = 0; j < horizontalSegments; j++)
                    {
                        int nextI = i + 1;
                        int nextJ = (j + 1) % horizontalSegments;

                        dynamicPrimitive.AddIndex(1 + i * horizontalSegments + j);
                        dynamicPrimitive.AddIndex(1 + i * horizontalSegments + nextJ);
                        dynamicPrimitive.AddIndex(1 + nextI * horizontalSegments + j);

                        dynamicPrimitive.AddIndex(1 + i * horizontalSegments + nextJ);
                        dynamicPrimitive.AddIndex(1 + nextI * horizontalSegments + nextJ);
                        dynamicPrimitive.AddIndex(1 + nextI * horizontalSegments + j);
                    }
                }

                // Create a fan connecting the top vertex to the top latitude ring.
                for (int i = 0; i < horizontalSegments; ++i)
                {
                    dynamicPrimitive.AddIndex(currentVertex - 1);
                    dynamicPrimitive.AddIndex(currentVertex - 2 - (i + 1) % horizontalSegments);
                    dynamicPrimitive.AddIndex(currentVertex - 2 - i);
                }
            }
            dynamicPrimitive.EndPrimitive();
        }
Exemple #30
0
 public static void AddRay(this DynamicPrimitive dynamicPrimitive, Ray ray, float length, Color color, float lineWidth)
 {
     AddArrow(dynamicPrimitive, ray.Position, ray.Position + ray.Direction * length, color, lineWidth);
 }