コード例 #1
0
ファイル: DrawHelper.cs プロジェクト: catontheway/AxTools
        /// <summary> Draw outlined box.</summary>
        /// <remarks> Nesox, 2013-12-28.</remarks>
        /// <param name="center"> The min. </param>
        /// <param name="length"> The length. </param>
        /// <param name="width">  The width. </param>
        /// <param name="height"> The height. </param>
        /// <param name="color">  The color. </param>
        public static void DrawOutlinedBox(Vector3 center, float length, float width, float height, Color color)
        {
            float w = width / 2;
            float h = height / 2;
            float d = length / 2;

            Matrix mat = Matrix.Identity * Matrix.Scaling(w, d, h) * Matrix.Translation(center);

            Device.SetTransform(TransformState.World, mat);

            var min = new Vector3(-1, -1, 0);
            var max = new Vector3(1, 1, 1);

            var vtx = new[]
            {
                new ColoredVertex(new Vector3(min.X, max.Y, max.Z), color.ToArgb()),
                new ColoredVertex(new Vector3(max.X, max.Y, max.Z), color.ToArgb()),
                new ColoredVertex(new Vector3(min.X, min.Y, max.Z), color.ToArgb()),
                new ColoredVertex(new Vector3(max.X, min.Y, max.Z), color.ToArgb()),
                new ColoredVertex(new Vector3(min.X, min.Y, min.Z), color.ToArgb()),
                new ColoredVertex(new Vector3(max.X, min.Y, min.Z), color.ToArgb()),
                new ColoredVertex(new Vector3(min.X, max.Y, min.Z), color.ToArgb()),
                new ColoredVertex(new Vector3(max.X, max.Y, min.Z), color.ToArgb())
            };

            var ind = new short[]
            {
                //Top           [_]
                0, 1, 1, 3,
                3, 2, 2, 0,
                //Bottom        [_]
                6, 7, 7, 5,
                5, 4, 4, 6,
                // Back         | |
                0, 6, 1, 7,
                // Front        | |
                2, 4, 3, 5,
                // Left         | |
                0, 6, 2, 4,
                // Right        | |
                1, 7, 3, 5
            };

            var oldDecl = Device.VertexDeclaration;

            using (var newDecl = ColoredVertex.GetDecl(Device))
            {
                Device.VertexDeclaration = newDecl;
                Device.DrawIndexedUserPrimitives(PrimitiveType.LineList, 0, 8, 12, ind, Format.Index16, vtx, 16);
                Device.VertexDeclaration = oldDecl;
            }
        }
コード例 #2
0
 public HermiteDataGrid createCubeGrid()
 {
     return(HermiteDataGrid.FromIntersectableGeometry(gridWorldSize, subdivision, Matrix.Scaling(new Vector3(4)) * Matrix.Translation(5, 5, 5),
                                                      new IntersectableCube()));
 }
コード例 #3
0
ファイル: DrawHelper.cs プロジェクト: catontheway/AxTools
        /// <summary> Draws a line. </summary>
        /// <param name="vecStartPos"> The vector start position. </param>
        /// <param name="vecEndPos">   The vector end position. </param>
        /// <param name="width">       The width. </param>
        /// <param name="color">       The color. </param>
        public static void DrawLineEx(Vector3 vecStartPos, Vector3 vecEndPos, float width, Color color)
        {
            const int numCorners = 20;

            Vector3 start = new Vector3(0.5f, 0.0f, 0.0f);
            Vector3 end   = new Vector3(-0.5f, 0.0f, 0.0f);

            if (_lineVertices == null)
            {
                _lineVertices = new ColoredVertex[numCorners * 2];

                for (int i = 0; i < numCorners; i++)
                {
                    Vector3 offset = new Vector3(0.0f, (float)Math.Cos((2.0f * Math.PI) / numCorners * i),
                                                 (float)Math.Sin((2.0f * (float)Math.PI) / numCorners * i));

                    Vector3 startPoint = start + offset;

                    ColoredVertex startVert = new ColoredVertex(new Vector3(startPoint.X, startPoint.Y, startPoint.Z), color.ToArgb());
                    _lineVertices[i] = startVert;

                    Vector3       endPoint = end + offset;
                    ColoredVertex endVert  = new ColoredVertex(new Vector3(endPoint.X, endPoint.Y, endPoint.Z), color.ToArgb());
                    _lineVertices[numCorners + i] = endVert;
                }
            }

            if (_lineFaceIndices == null)
            {
                _lineFaceIndices = new int[numCorners * 2 * 3]; // The number of faces is the same as the number of corners: 2 tris per face, 3 inds per tri

                for (int i = 0; i < numCorners; i++)
                {
                    _lineFaceIndices[i * 6 + 0] = i;                                                     // ring 1, vert i
                    _lineFaceIndices[i * 6 + 1] = numCorners + i;                                        // ring 2, vert i
                    _lineFaceIndices[i * 6 + 2] = (i + 1) % numCorners;                                  // ring 1, vert i + 1

                    _lineFaceIndices[i * 6 + 3] = (i + 1) % numCorners;                                  // ring 1, vert i + 1
                    _lineFaceIndices[i * 6 + 4] = i == numCorners - 1 ? numCorners : numCorners + i + 1; // ring 2, vert i + 1
                    _lineFaceIndices[i * 6 + 5] = numCorners + i;                                        // ring 2, vert i
                }
            }

            Vector3 startToEnd = vecEndPos - vecStartPos;
            Matrix  scaling    = Matrix.Scaling(Vector3.Distance(vecStartPos, vecEndPos), width / 2, width / 2);

            Matrix rotationY = Matrix.RotationY(-(float)Math.Asin(startToEnd.Z / Vector3.Distance(vecStartPos, vecEndPos)));
            Matrix rotationZ = Matrix.RotationZ((float)Math.Atan2(startToEnd.Y, startToEnd.X));

            Matrix rotation    = Matrix.Multiply(rotationY, rotationZ);
            Matrix translation = Matrix.Translation(start.X + startToEnd.X / 2, start.Y + startToEnd.Y / 2, start.Z + startToEnd.Z / 2);

            Matrix world = Matrix.Multiply(Matrix.Multiply(scaling, rotation), translation);

            Device.SetTransform(TransformState.World, world);

            var oldDecl = Device.VertexDeclaration;

            using (var newDecl = ColoredVertex.GetDecl(Device))
            {
                Device.VertexDeclaration = newDecl;
                Device.DrawIndexedUserPrimitives(PrimitiveType.TriangleList, 0, numCorners * 2, numCorners * 2, _lineFaceIndices, Format.Index32, _lineVertices, ColoredVertex.Stride);
                Device.VertexDeclaration = oldDecl;
            }
        }