private void DrawGeometry()
        {
            // bigger cube
            var angle       = GetDeltaTime(new TimeSpan(0, 0, 0, 5)) * Math.PI * 2;
            var matrixModel =
                MatrixEx.Scale(0.5) *
                MatrixEx.Rotate(new UnitVector3D(1, 0, 0), angle) *
                MatrixEx.Translate(1, 0, 0);

            foreach (var cubePolyline in CubePolylines)
            {
                DrawPolyline(matrixModel.Transform(cubePolyline), Space.World, Pens.White);
            }

            // smaller cube
            angle       = GetDeltaTime(new TimeSpan(0, 0, 0, 1)) * Math.PI * 2;
            matrixModel =
                MatrixEx.Scale(0.5) *
                MatrixEx.Rotate(new UnitVector3D(0, 1, 0), angle) *
                MatrixEx.Translate(0, 1, 0) *
                matrixModel;

            foreach (var cubePolyline in CubePolylines)
            {
                DrawPolyline(matrixModel.Transform(cubePolyline), Space.World, Pens.Yellow);
            }
        }
示例#2
0
        private void DrawGeometry()
        {
            // screen space
            DrawPolyline(new[] { new Point3D(3, 20, 0), new Point3D(140, 20, 0) }, Space.Screen, Pens.Gray);

            // view space
            DrawPolyline(new[] { new Point3D(-0.9, -0.9, 0), new Point3D(0.9, -0.9, 0) }, Space.View, Pens.Gray);


            // world space bigger cube
            var angle       = GetDeltaTime(new TimeSpan(0, 0, 0, 5)) * Math.PI * 2;
            var matrixModel =
                MatrixEx.Scale(0.5) *
                MatrixEx.Rotate(new UnitVector3D(1, 0, 0), angle) *
                MatrixEx.Translate(1, 0, 0);

            foreach (var cubePolyline in CubePolylines)
            {
                DrawPolyline(matrixModel.Transform(cubePolyline), Space.World, Pens.White);
            }

            // world space smaller cube
            angle       = GetDeltaTime(new TimeSpan(0, 0, 0, 1)) * Math.PI * 2;
            matrixModel =
                MatrixEx.Scale(0.5) *
                MatrixEx.Rotate(new UnitVector3D(0, 1, 0), angle) *
                MatrixEx.Translate(0, 1, 0) *
                matrixModel;

            foreach (var cubePolyline in CubePolylines)
            {
                DrawPolyline(matrixModel.Transform(cubePolyline), Space.World, Pens.Yellow);
            }
        }
        /// <summary>
        /// Create new <see cref="ICameraInfo"/> based on mouse offset in view space and orbit origin.
        /// </summary>
        public static ICameraInfo Orbit(ICameraInfo cameraInfoStart, Vector3D mouseOffsetView, Point3D orbitOrigin)
        {
            // default input
            var eye    = cameraInfoStart.Position;
            var target = cameraInfoStart.Target;

            // create local coordinate system
            var zAxis              = cameraInfoStart.UpVector;
            var yzPlane            = new Plane(new Point3D(), cameraInfoStart.GetEyeDirection().ToPoint3D(), zAxis.ToPoint3D());
            var xAxis              = yzPlane.Normal;
            var xzPlane            = new Plane(new Point3D(), zAxis.ToPoint3D(), xAxis.ToPoint3D());
            var yAxis              = xzPlane.Normal;
            var matrixWorldToLocal = (Matrix <double>) new CoordinateSystem(new Point3D(), xAxis, yAxis, zAxis);

            // transform to local system
            orbitOrigin = matrixWorldToLocal.Transform(orbitOrigin);
            eye         = matrixWorldToLocal.Transform(eye);
            target      = matrixWorldToLocal.Transform(target);

            // figure out angles (how much to rotate)
            GetSphereAngles(mouseOffsetView, (target - eye).Normalize(), out var thetaDelta, out var phiDelta);

            // rotate horizontally
            var matrixRotationHorizontal = MatrixEx.Rotate(UnitVector3D.ZAxis, thetaDelta.Radians).TransformAround(orbitOrigin);

            eye    = matrixRotationHorizontal.Transform(eye);
            target = matrixRotationHorizontal.Transform(target);

            // rotate vertically
            var phiPlane = new Plane(eye, target, target + UnitVector3D.ZAxis);
            var matrixRotationVertical = MatrixEx.Rotate(phiPlane.Normal, phiDelta.Radians).TransformAround(orbitOrigin);

            eye    = matrixRotationVertical.Transform(eye);
            target = matrixRotationVertical.Transform(target);

            // transform back to world system
            var matrixLocalToWorld = matrixWorldToLocal.Inverse();

            eye    = matrixLocalToWorld.Transform(eye);
            target = matrixLocalToWorld.Transform(target);

            // update camera info
            return(new CameraInfo(eye, target, cameraInfoStart.UpVector, cameraInfoStart.Projection.Cloned(), cameraInfoStart.Viewport));
        }
示例#4
0
        /// <summary>
        /// Get some primitives to demonstrate hierarchical matrix multiplication.
        /// </summary>
        private static IEnumerable <IPrimitive> GetPrimitivesCubes()
        {
            var duration = new TimeSpan(DateTime.UtcNow.Ticks);

            // world space bigger cube
            var angle       = GetTimeSpanPeriodRatio(duration, new TimeSpan(0, 0, 0, 5)) * Math.PI * 2;
            var matrixModel =
                MatrixEx.Scale(0.5) *
                MatrixEx.Rotate(new UnitVector3D(1, 0, 0), angle) *
                MatrixEx.Translate(1, 0, 0);

            foreach (var cubePolyline in CubePolylines)
            {
                yield return(new Materials.Position.Primitive
                             (
                                 new PrimitiveBehaviour(Space.World),
                                 PrimitiveTopology.LineStrip,
                                 matrixModel.Transform(cubePolyline).Select(position => new Materials.Position.Vertex(position)).ToArray(),
                                 Color.White
                             ));
            }

            // world space smaller cube
            angle       = GetTimeSpanPeriodRatio(duration, new TimeSpan(0, 0, 0, 1)) * Math.PI * 2;
            matrixModel =
                MatrixEx.Scale(0.5) *
                MatrixEx.Rotate(new UnitVector3D(0, 1, 0), angle) *
                MatrixEx.Translate(0, 1, 0) *
                matrixModel;

            foreach (var cubePolyline in CubePolylines)
            {
                yield return(new Materials.Position.Primitive
                             (
                                 new PrimitiveBehaviour(Space.World),
                                 PrimitiveTopology.LineStrip,
                                 matrixModel.Transform(cubePolyline).Select(position => new Materials.Position.Vertex(position)).ToArray(),
                                 Color.Yellow
                             ));
            }
        }