diff --git a/NHEngine/BootStrap.cs b/NHEngine/BootStrap.cs index c86e044..bbcad3d 100644 --- a/NHEngine/BootStrap.cs +++ b/NHEngine/BootStrap.cs @@ -1,4 +1,7 @@ using System; +using System.IO; +using System.Drawing; +using System.Drawing.Imaging; using OpenTK.Graphics.OpenGL4; using OpenTK; @@ -6,6 +9,9 @@ using OpenTK; using OpenTK.Audio.OpenAL; using OpenTK.Audio; +using ImageSharp; + + using org.nhengine.graphics; namespace nhengine @@ -50,8 +56,15 @@ namespace nhengine AL.Enable(ALCapability.Invalid); } - public void bootMap(){ - map = new SquaredMap(25,25); + public void bootMap(){ + ImageSharp.Image i = ImageSharp.Image.Load( new FileStream("M3-wip.png",FileMode.Open)); + i.Flip(ImageSharp.Processing.FlipType.Vertical); + map = new SquaredMap(i); + + glWindow.addGLObject(map); + + + } diff --git a/NHEngine/M3-wip.bmp b/NHEngine/M3-wip.bmp new file mode 100644 index 0000000..c5eea46 Binary files /dev/null and b/NHEngine/M3-wip.bmp differ diff --git a/NHEngine/M3-wip.png b/NHEngine/M3-wip.png new file mode 100644 index 0000000..b8eb750 Binary files /dev/null and b/NHEngine/M3-wip.png differ diff --git a/NHEngine/OpenGLWindow.cs b/NHEngine/OpenGLWindow.cs index f0ef597..a019b18 100644 --- a/NHEngine/OpenGLWindow.cs +++ b/NHEngine/OpenGLWindow.cs @@ -1,5 +1,6 @@ using System; using System.Drawing; +using System.Collections.Generic; using OpenTK.Graphics; using OpenTK.Graphics.OpenGL; @@ -11,12 +12,8 @@ using org.nhengine.graphics.primitives; namespace nhengine { - - public class OpenGLWindow : GameWindow { - private GLCamera camera; - private Point mouseCapturePosition; private bool mouseCaptured; @@ -28,8 +25,10 @@ namespace nhengine GraphicsMode gmode = new GraphicsMode(); - - + GLCamera scene; + + List glObjects; + public OpenGLWindow() :base(800, 600, GraphicsMode.Default, @@ -39,10 +38,9 @@ namespace nhengine 3, 3, GraphicsContextFlags.ForwardCompatible) { + glObjects = new List(); VSync = VSyncMode.On; - camera = GlobalDefaults.instance().ActiveCamera; - camera.Distance = 1000; - camera.Position = new Vector3(1600, 1600, 1000); + scene = new GLCamera(); } protected override void OnLoad(EventArgs e) @@ -51,11 +49,23 @@ namespace nhengine GL.ClearColor(0.1f, 0.2f, 0.5f, 0.0f); GL.Enable(EnableCap.DepthTest); GL.Enable(EnableCap.Normalize); + GL.Enable(EnableCap.Blend); + GL.BlendFunc(BlendingFactorSrc.SrcAlpha,BlendingFactorDest.OneMinusSrcAlpha); 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); + + addGLObject( cube ); + addGLObject( c1 ); + addGLObject( c2 ); + addGLObject( c3 ); + + scene.Position = new Vector3(16000,-5000,8000); + scene.View = new Vector3(0f,1f,-0.250f); + scene.Top = new Vector3(0,0,1); + scene.setFoV(MathHelper.PiOver4); } @@ -64,29 +74,29 @@ namespace nhengine base.OnResize(e); GL.Viewport(ClientRectangle.X, ClientRectangle.Y, ClientRectangle.Width, ClientRectangle.Height); - camera.setViewport(ClientRectangle.Width, ClientRectangle.Height); + scene.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); + foreach (GLObject glo in glObjects){ + glo.paint(scene); + } SwapBuffers(); } + public void addGLObject(GLObject glObject){ + this.glObjects.Add( glObject ); + } + public void removeGLObject(GLObject glObject){ + this.glObjects.Remove( glObject ); + } + private void captureMouse(){ mouseCapturePosition = new Point(Mouse.X, Mouse.Y); mouseCaptured = true; @@ -130,11 +140,7 @@ namespace nhengine protected override void OnMouseWheel(OpenTK.Input.MouseWheelEventArgs e) { - camera.Distance += e.DeltaPrecise * 16.0f; - if (camera.Distance < 0) - camera.Distance = 0; - - applyRotation(); + walk(e.ValuePrecise); } protected override void OnKeyPress(KeyPressEventArgs e) @@ -156,6 +162,13 @@ namespace nhengine case OpenTK.Input.Key.Right: rotateLeftRight(-MathHelper.Pi / 90.0f); break; + case OpenTK.Input.Key.W: + walk(100.0f); + break; + case OpenTK.Input.Key.S: + walk(-100.0f); + break; + } } @@ -169,11 +182,13 @@ namespace nhengine arcLeftRight += arc; applyRotation(); } + + public void walk(float distance){ + scene.Position += scene.View * distance; + } 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/NHEngine/SquaredMap.cs b/NHEngine/SquaredMap.cs index 2612aba..3177dc6 100644 --- a/NHEngine/SquaredMap.cs +++ b/NHEngine/SquaredMap.cs @@ -1,6 +1,9 @@ using System; using System.IO; +using ImageSharp; +using ImageSharp.PixelFormats; + using OpenTK.Graphics.OpenGL4; using OpenTK; @@ -30,6 +33,24 @@ namespace nhengine computeGL(); updateGL(); } + + public SquaredMap(Image heightMap) + :base(12 * heightMap.Width * heightMap.Height) + { + this.width = heightMap.Width; + this.height = heightMap.Height; + + this.heightMap = new float[ (width + 1) * (height + 1) ]; + + normalsEnabled = true; + + loadHeightMap(heightMap); + + computeGL(); + updateGL(); + + } + public void generateHeightMap(){ Random rand = new Random(); @@ -43,6 +64,21 @@ namespace nhengine } } } + + public void loadHeightMap(Image _heightMap){ + + Rgba32[] pixels = _heightMap.Pixels; + + for (int y = 0; y <= height; y++) + { + for (int x = 0; x <= width; x++) + { + Rgba32 pixel = pixels[ x + (y * _heightMap.Width)]; + float h = (pixel.R + pixel.G + pixel.B); + heightMap[x + (y * width)] = (float)(h); + } + } + } public void computeGL(){ float left, @@ -52,6 +88,7 @@ namespace nhengine vcenter, hcenter; + Console.WriteLine("Creating geometry"); for (int x = 0; x < width; x++) { @@ -101,25 +138,25 @@ namespace nhengine new Vector3(left, top, h[2]), new Vector3(hcenter, vcenter, ah) ); - - setColor(new Vector4(1.0f, 1.0f, 0.5f, 1.0f)); - - for (int n = 0; n < vertices.Length / 3;n++){ - float _h = vertices[(3 * n) + 2] / 256.0f; - setColor( - n, - new Vector4( - 0.3f + (0.7f * _h), - 0.2f + (0.8f * _h), - 0.4f + (0.6f * _h), - 1.0f - ) - ); - } - } } + Console.WriteLine("Coloring..."); + + for (int n = 0; n < vertices.Length / 3;n++){ + float _h = vertices[(3 * n) + 2] / 512.0f; + setColor( + n, + new Vector4( + 0.3f + (0.7f * _h), + 0.2f + (0.8f * _h), + 0.4f + (0.6f * _h), + 1.0f + ) + ); + } + + } diff --git a/NHEngine/nhengine.csproj b/NHEngine/nhengine.csproj index c81e846..66197c7 100644 --- a/NHEngine/nhengine.csproj +++ b/NHEngine/nhengine.csproj @@ -34,6 +34,28 @@ ..\packages\OpenTK.2.0.0\lib\net20\OpenTK.dll + + ..\packages\System.Buffers.4.3.0\lib\netstandard1.1\System.Buffers.dll + + + + + + ..\packages\System.Numerics.Vectors.4.3.0\lib\portable-net45+win8+wp8+wpa81\System.Numerics.Vectors.dll + + + + ..\packages\System.Runtime.CompilerServices.Unsafe.4.3.0\lib\netstandard1.0\System.Runtime.CompilerServices.Unsafe.dll + + + ..\packages\System.Runtime.InteropServices.RuntimeInformation.4.3.0\lib\net45\System.Runtime.InteropServices.RuntimeInformation.dll + + + + + + ..\packages\ImageSharp.1.0.0-alpha7-00006\lib\netstandard1.1\ImageSharp.dll + @@ -48,6 +70,12 @@ + + PreserveNewest + + + PreserveNewest + diff --git a/NHEngine/packages.config b/NHEngine/packages.config index b371766..2460151 100644 --- a/NHEngine/packages.config +++ b/NHEngine/packages.config @@ -1,4 +1,40 @@  + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file