using System; using System.Drawing; using OpenTK.Graphics; using OpenTK.Graphics.OpenGL; using OpenTK; using org.nhengine.graphics; using org.nhengine.graphics.primitives; namespace nhengine { public class OpenGLWindow : GameWindow { private GLCamera camera; private Point mouseCapturePosition; private bool mouseCaptured; private float arcUpDown, arcLeftRight; private Cube cube, c1, c2, c3; private Triangle triangle; public OpenGLWindow() :base(800, 600, GraphicsMode.Default, "nhEngine", GameWindowFlags.Default, DisplayDevice.Default, 3, 3, GraphicsContextFlags.ForwardCompatible) { VSync = VSyncMode.On; camera = GlobalDefaults.instance().ActiveCamera; camera.Distance = 1000; camera.Position = new Vector3(1600, 1600, 1000); } protected override void OnLoad(EventArgs e) { base.OnLoad(e); GL.ClearColor(0.1f, 0.2f, 0.5f, 0.0f); GL.Enable(EnableCap.DepthTest); GL.Enable(EnableCap.Normalize); cube = new Cube(new Vector3(0,0,512), 128.0f); c1 = new Cube(new Vector3(512,0,512), 128.0f); c2 = new Cube(new Vector3(0,512,512), 128.0f); c3 = new Cube(new Vector3(512,512,512), 128.0f); triangle = new Triangle(new Vector3(0, 0, 0), new Vector3(600, 0, 0), new Vector3(600, 600, 0)); } protected override void OnResize(EventArgs e) { base.OnResize(e); GL.Viewport(ClientRectangle.X, ClientRectangle.Y, ClientRectangle.Width, ClientRectangle.Height); camera.setViewport(ClientRectangle.Width, ClientRectangle.Height); } protected override void OnRenderFrame(FrameEventArgs e) { base.OnRenderFrame(e); BootStrap boot = BootStrap.instance(); GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit); cube.paint(camera); c1.paint(camera); c2.paint(camera); c3.paint(camera); boot.SquaredMap.paint(camera); //triangle.paint(camera); SwapBuffers(); } private void captureMouse(){ mouseCapturePosition = new Point(Mouse.X, Mouse.Y); mouseCaptured = true; CursorVisible = false; } private void releaseMouse(){ mouseCaptured = false; CursorVisible = true; } protected override void OnMouseMove(OpenTK.Input.MouseMoveEventArgs e) { if (mouseCaptured){ Point delta = new Point(e.X - mouseCapturePosition.X, e.Y - mouseCapturePosition.Y); onMouseCapturedMove(delta); mouseCapturePosition = e.Position; } } protected virtual void onMouseCapturedMove(Point delta){ rotateLeftRight(((float)delta.X) / 10.0f); rotateUpDown(((float)delta.Y) / 10.0f); } protected override void OnMouseDown(OpenTK.Input.MouseButtonEventArgs e) { switch (e.Button){ case OpenTK.Input.MouseButton.Left: break; case OpenTK.Input.MouseButton.Right: captureMouse(); break; } } protected override void OnMouseUp(OpenTK.Input.MouseButtonEventArgs e) { releaseMouse(); } protected override void OnMouseWheel(OpenTK.Input.MouseWheelEventArgs e) { camera.Distance += e.DeltaPrecise * 16.0f; if (camera.Distance < 0) camera.Distance = 0; applyRotation(); } protected override void OnKeyPress(KeyPressEventArgs e) { } protected override void OnKeyDown(OpenTK.Input.KeyboardKeyEventArgs e) { switch (e.Key){ case OpenTK.Input.Key.Up: rotateUpDown(MathHelper.Pi / 90.0f); break; case OpenTK.Input.Key.Down: rotateUpDown(-MathHelper.Pi / 90.0f); break; case OpenTK.Input.Key.Left: rotateLeftRight(MathHelper.Pi / 90.0f); break; case OpenTK.Input.Key.Right: rotateLeftRight(-MathHelper.Pi / 90.0f); break; } } public void rotateUpDown(float arc) { arcUpDown += arc; applyRotation(); } public void rotateLeftRight(float arc) { arcLeftRight += arc; applyRotation(); } private void applyRotation(){ Console.WriteLine("Rotation: {0} {1}", arcLeftRight, arcUpDown); camera.MRotation = Matrix4.CreateRotationZ(arcLeftRight) * Matrix4.CreateRotationX(arcUpDown); } protected override void OnKeyUp(OpenTK.Input.KeyboardKeyEventArgs e) { } } }