From c6aafb8cc931d605c3b6ea5b58b687a560e23f8b Mon Sep 17 00:00:00 2001 From: Harald Christian Joachim Wolff Date: Tue, 25 Apr 2017 22:06:21 +0200 Subject: [PATCH] WIP --- GLObject.cs | 10 ++ NHEngine/OpenGLWindow.cs~Stashed changes | 182 +++++++++++++++++++++++ org.nhengine/graphics/GLCamera.cs | 6 +- org.nhengine/graphics/GlobalDefaults.cs | 2 +- 4 files changed, 196 insertions(+), 4 deletions(-) create mode 100644 GLObject.cs create mode 100644 NHEngine/OpenGLWindow.cs~Stashed changes diff --git a/GLObject.cs b/GLObject.cs new file mode 100644 index 0000000..bce81f1 --- /dev/null +++ b/GLObject.cs @@ -0,0 +1,10 @@ +using System; +namespace nhengine +{ + public class GLObject + { + public GLObject() + { + } + } +} diff --git a/NHEngine/OpenGLWindow.cs~Stashed changes b/NHEngine/OpenGLWindow.cs~Stashed changes new file mode 100644 index 0000000..b6bdd76 --- /dev/null +++ b/NHEngine/OpenGLWindow.cs~Stashed changes @@ -0,0 +1,182 @@ +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) + { + } + + } +} diff --git a/org.nhengine/graphics/GLCamera.cs b/org.nhengine/graphics/GLCamera.cs index 8dbcba3..fb311bb 100644 --- a/org.nhengine/graphics/GLCamera.cs +++ b/org.nhengine/graphics/GLCamera.cs @@ -27,7 +27,7 @@ namespace org.nhengine.graphics mProjection = Matrix4.CreatePerspectiveFieldOfView( MathHelper.PiOver4, 1.0f, - 0.1f, + 50.0f, 100000.0f ); @@ -41,8 +41,8 @@ namespace org.nhengine.graphics mProjection = Matrix4.CreatePerspectiveFieldOfView( MathHelper.PiOver4, width / (float)height, - 1.0f, - 1000000.0f + 50.0f, + 100000.0f ); } diff --git a/org.nhengine/graphics/GlobalDefaults.cs b/org.nhengine/graphics/GlobalDefaults.cs index 3b99069..11b3f2c 100644 --- a/org.nhengine/graphics/GlobalDefaults.cs +++ b/org.nhengine/graphics/GlobalDefaults.cs @@ -83,7 +83,7 @@ void main() if (cosTheta < 0){ visibility = 1.0; } - color = vec4( iv_color.xyz * (0.5 + (0.5 * cosTheta * cosTheta)), 0.5f ); + color = vec4( iv_color.xyz * (0.5 + (0.5 * cosTheta * cosTheta)), 0.5f ); } ";