public void ResetRotation()
        {
            ScientificCamera camera = this.Camera;
            if (camera == null) { return; }
            ScientificCamera originalCamera = this.originalCamera;
            if (originalCamera == null) { return; }

            camera.Position = originalCamera.Position;
            camera.UpVector = originalCamera.UpVector;
        }
        public void MouseMove(int x, int y)
        {
            if (this.mouseDownFlag)
            {
                ScientificCamera camera = this.Camera;
                if (camera == null) { return; }

                Vertex back = this.back;
                Vertex right = this.right;
                Vertex up = this.up;
                Size bound = this.bound;
                Point downPosition = this.downPosition;
                {
                    float deltaX = -horizontalRotationFactor * (x - downPosition.X) / bound.Width;
                    float cos = (float)Math.Cos(deltaX);
                    float sin = (float)Math.Sin(deltaX);
                    Vertex newBack = new Vertex(
                        back.X * cos + right.X * sin,
                        back.Y * cos + right.Y * sin,
                        back.Z * cos + right.Z * sin);
                    back = newBack;
                    right = up.VectorProduct(back);
                    back.Normalize();
                    right.Normalize();
                }
                {
                    float deltaY = verticalRotationFactor * (y - downPosition.Y) / bound.Height;
                    float cos = (float)Math.Cos(deltaY);
                    float sin = (float)Math.Sin(deltaY);
                    Vertex newBack = new Vertex(
                        back.X * cos + up.X * sin,
                        back.Y * cos + up.Y * sin,
                        back.Z * cos + up.Z * sin);
                    back = newBack;
                    up = back.VectorProduct(right);
                    back.Normalize();
                    up.Normalize();
                }

                camera.Position = camera.Target +
                    back * (float)((camera.Position - camera.Target).Magnitude());
                camera.UpVector = up;
                this.back = back;
                this.right = right;
                this.up = up;
                this.downPosition.X = x;
                this.downPosition.Y = y;
            }
        }
        /// <summary>
        /// Handles the Resized event of the openGLControl control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void openGLControl_Resized(object sender, EventArgs e)
        {
            //  TODO: Set the projection matrix here.

            ScientificCamera camera = this.camera;

            camera.AspectRatio = (double)Width / (double)Height;
            camera.Far         = 100;
            camera.Near        = 0.01;
            camera.FieldOfView = 60;

            //  Get the OpenGL object.
            OpenGL gl = openGLControl.OpenGL;

            camera.Project(gl);
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="SharpGLForm"/> class.
        /// </summary>
        public SharpGLForm()
        {
            InitializeComponent();

            ScientificCamera camera = this.camera;

            this.satelliteRotation.Camera = camera;

            camera.Position = new SharpGL.SceneGraph.Vertex(-2, 2, -2);
            camera.Target   = new SharpGL.SceneGraph.Vertex();
            camera.UpVector = new SharpGL.SceneGraph.Vertex(0, 1, 0);

            this.openGLControl_Resized(this.openGLControl, EventArgs.Empty);

            this.openGLControl.MouseDown  += openGLControl_MouseDown;
            this.openGLControl.MouseMove  += openGLControl_MouseMove;
            this.openGLControl.MouseUp    += openGLControl_MouseUp;
            this.openGLControl.MouseWheel += openGLControl_MouseWheel;

            this.openGLControl.KeyDown += openGLControl_KeyDown;
        }
示例#5
0
        public void Scale(int delta)
        {
            ScientificCamera camera = this;
            var target2Position     = camera.Position - camera.Target;

            if (target2Position.Magnitude() < 0.01)
            {
                target2Position.Normalize();
                target2Position.X *= 0.01f;
                target2Position.Y *= 0.01f;
                target2Position.Z *= 0.01f;
            }
            var scaledTarget2Position = target2Position * (1 - delta * 0.001f);

            camera.Position = camera.Target + scaledTarget2Position;
            double lengthDiff = scaledTarget2Position.Magnitude() - target2Position.Magnitude();

            // Increase ortho camera's Near/Far property in case the camera's position changes too much.
            camera.Far += lengthDiff;
            //camera.Near += lengthDiff;
        }
        private void PrepareCamera()
        {
            var camera = this.Camera;
            if (camera != null)
            {
                Vertex back = camera.Position - camera.Target;
                Vertex right = Camera.UpVector.VectorProduct(back);
                Vertex up = back.VectorProduct(right);
                back.Normalize();
                right.Normalize();
                up.Normalize();

                this.back = back;
                this.right = right;
                this.up = up;

                if (this.originalCamera == null)
                { this.originalCamera = new ScientificCamera(); }
                this.originalCamera.Position = camera.Position;
                this.originalCamera.UpVector = camera.UpVector;
            }
        }
 /// <summary>
 /// Rotates a camera on a sphere, whose center is camera's Target.
 /// <para>Just like a satellite moves around a fixed star.</para> 
 /// </summary>
 /// <param name="camera"></param>
 public SatelliteRotation(ScientificCamera camera = null)
 {
     this.Camera = camera;
 }