Exemplo n.º 1
0
        protected override void OnUpdateFrame(FrameEventArgs e)
        {
            base.OnUpdateFrame(e);
            UpdateMousePosition();
            this.Title =
                "Mode: " + wm.Waypoints.Count +
                " Physics Bodies: " + physics_world.physics_world.CollisionObjectArray.Count +
                " FPS: " + Math.Floor(1000 / lastFrames.Average()) +
                " Camera: X: " + cameraLocation.X.ToString("N1") +
                " Y: " + cameraLocation.Y.ToString("N1") +
                " Z: " + cameraLocation.Z.ToString("N1") +
                " Pointing: H: " + rotateOffset.X.ToString("N1") +
                " V: " + rotateOffset.Y.ToString("N1") +
                " X: " + rtLocation.X.ToString("N1") +
                " Y: " + rtLocation.Y.ToString("N1") +
                " Z: " + rtLocation.Z.ToString("N1");
            #region Change Movement Speed
            bool  moving         = false;
            float imovementSpeed = movementSpeed;
            if (OpenTK.Input.Keyboard.GetState(0).IsKeyDown(OpenTK.Input.Key.ShiftLeft))
            {
                imovementSpeed *= 5;
            }
            #endregion

            #region Perspective Warping
            UpdateZoom(imovementSpeed);
            Matrix4 projection = Matrix4.CreatePerspectiveFieldOfView((float)Math.PI / zoomDivisor, Width / (float)Height, 1f, float.MaxValue);
            GL.MatrixMode(MatrixMode.Projection);
            GL.LoadMatrix(ref projection);
            #endregion

            #region Input Handlers
            if (OpenTK.Input.Keyboard.GetState(0).IsKeyDown(OpenTK.Input.Key.A))
            {
                moving            = true;
                cameraLocation.X -= (float)Math.Sin((Math.PI / 180) * rotateOffset.X + Math.PI / 2) * imovementSpeed;
                cameraLocation.Y -= (float)Math.Cos((Math.PI / 180) * rotateOffset.X + Math.PI / 2) * imovementSpeed;
            }
            if (OpenTK.Input.Keyboard.GetState(0).IsKeyDown(OpenTK.Input.Key.D))
            {
                moving            = true;
                cameraLocation.X += (float)Math.Sin((Math.PI / 180) * rotateOffset.X + Math.PI / 2) * imovementSpeed;
                cameraLocation.Y += (float)Math.Cos((Math.PI / 180) * rotateOffset.X + Math.PI / 2) * imovementSpeed;
            }
            if (OpenTK.Input.Keyboard.GetState(0).IsKeyDown(OpenTK.Input.Key.W))
            {
                moving = true;
                float HypotenuseXY = (float)Math.Cos((Math.PI / 180) * rotateOffset.Y) * imovementSpeed;
                cameraLocation.X += HypotenuseXY * (float)Math.Sin((Math.PI / 180) * rotateOffset.X);
                cameraLocation.Y += HypotenuseXY * (float)Math.Cos((Math.PI / 180) * rotateOffset.X);
                cameraLocation.Z += (float)Math.Sin((Math.PI / 180) * rotateOffset.Y) * imovementSpeed;
            }
            if (OpenTK.Input.Keyboard.GetState(0).IsKeyDown(OpenTK.Input.Key.S))
            {
                moving = true;
                float HypotenuseXY = (float)Math.Cos((Math.PI / 180) * rotateOffset.Y) * imovementSpeed;
                cameraLocation.X -= HypotenuseXY * (float)Math.Sin((Math.PI / 180) * rotateOffset.X);
                cameraLocation.Y -= HypotenuseXY * (float)Math.Cos((Math.PI / 180) * rotateOffset.X);
                cameraLocation.Z -= (float)Math.Sin((Math.PI / 180) * rotateOffset.Y) * imovementSpeed;
            }
            if (OpenTK.Input.Keyboard.GetState(0).IsKeyDown(OpenTK.Input.Key.Z))
            {
                moving            = true;
                cameraLocation.Z += imovementSpeed;
            }
            if (OpenTK.Input.Keyboard.GetState(0).IsKeyDown(OpenTK.Input.Key.X))
            {
                moving            = true;
                cameraLocation.Z -= imovementSpeed;
            }
            #endregion

            #region Physics Engine Step
            if (OpenTK.Input.Keyboard.GetState(0).IsKeyDown(OpenTK.Input.Key.LControl))
            {
                physics_world.Step(0.0001f);
            }
            else
            {
                physics_world.Step((float)lastFrames.Average() / 1000);
            }
            #endregion
            UpdateMoveSpeed(moving);
            modelview = Matrix4.LookAt(cameraLocation, cameraLocation + rtLocation, Vector3.UnitZ);
            GL.MatrixMode(MatrixMode.Modelview);

            GL.LoadMatrix(ref modelview);

            //and move the camera to the light

            float[] lightLocation = { cameraLocation.X, cameraLocation.Y, cameraLocation.Z };
            GL.Light(LightName.Light0, LightParameter.Position, lightLocation);
        }