Erste Version mit laden einer HeightMap zu demonstrationszwecken

HWO-base
Harald Christian Joachim Wolff 2017-04-28 11:51:20 +02:00
parent 29a1bdea77
commit f3d0fd6932
7 changed files with 174 additions and 45 deletions

View File

@ -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);
}

BIN
NHEngine/M3-wip.bmp 100644

Binary file not shown.

After

Width:  |  Height:  |  Size: 185 KiB

BIN
NHEngine/M3-wip.png 100644

Binary file not shown.

After

Width:  |  Height:  |  Size: 40 KiB

View File

@ -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<GLObject> glObjects;
public OpenGLWindow()
:base(800, 600,
GraphicsMode.Default,
@ -39,10 +38,9 @@ namespace nhengine
3, 3,
GraphicsContextFlags.ForwardCompatible)
{
glObjects = new List<GLObject>();
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)

View File

@ -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
)
);
}
}

View File

@ -34,6 +34,28 @@
<HintPath>..\packages\OpenTK.2.0.0\lib\net20\OpenTK.dll</HintPath>
</Reference>
<Reference Include="System.Drawing" />
<Reference Include="System.Buffers">
<HintPath>..\packages\System.Buffers.4.3.0\lib\netstandard1.1\System.Buffers.dll</HintPath>
</Reference>
<Reference Include="System.Core" />
<Reference Include="System.IO.Compression" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Numerics.Vectors">
<HintPath>..\packages\System.Numerics.Vectors.4.3.0\lib\portable-net45+win8+wp8+wpa81\System.Numerics.Vectors.dll</HintPath>
</Reference>
<Reference Include="System.ComponentModel.Composition" />
<Reference Include="System.Runtime.CompilerServices.Unsafe">
<HintPath>..\packages\System.Runtime.CompilerServices.Unsafe.4.3.0\lib\netstandard1.0\System.Runtime.CompilerServices.Unsafe.dll</HintPath>
</Reference>
<Reference Include="System.Runtime.InteropServices.RuntimeInformation">
<HintPath>..\packages\System.Runtime.InteropServices.RuntimeInformation.4.3.0\lib\net45\System.Runtime.InteropServices.RuntimeInformation.dll</HintPath>
</Reference>
<Reference Include="System.Numerics" />
<Reference Include="System.Xml" />
<Reference Include="System.Xml.Linq" />
<Reference Include="ImageSharp">
<HintPath>..\packages\ImageSharp.1.0.0-alpha7-00006\lib\netstandard1.1\ImageSharp.dll</HintPath>
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="Properties\AssemblyInfo.cs" />
@ -48,6 +70,12 @@
<None Include="packages.config" />
<None Include="shader\simple_fragment.shader" />
<None Include="shader\simple_vertex.shader" />
<None Include="M3-wip.bmp">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Include="M3-wip.png">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\org.nhengine\org.nhengine.csproj">

View File

@ -1,4 +1,40 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="ImageSharp" version="1.0.0-alpha7-00006" targetFramework="net45" />
<package id="Microsoft.NETCore.Platforms" version="1.1.0" targetFramework="net45" />
<package id="NETStandard.Library" version="1.6.1" targetFramework="net45" />
<package id="OpenTK" version="2.0.0" targetFramework="net45" />
<package id="System.Buffers" version="4.3.0" targetFramework="net45" />
<package id="System.Collections" version="4.3.0" targetFramework="net45" />
<package id="System.Collections.Concurrent" version="4.3.0" targetFramework="net45" />
<package id="System.Diagnostics.Debug" version="4.3.0" targetFramework="net45" />
<package id="System.Diagnostics.Tools" version="4.3.0" targetFramework="net45" />
<package id="System.Diagnostics.Tracing" version="4.3.0" targetFramework="net45" />
<package id="System.Globalization" version="4.3.0" targetFramework="net45" />
<package id="System.IO" version="4.3.0" targetFramework="net45" />
<package id="System.IO.Compression" version="4.3.0" targetFramework="net45" />
<package id="System.Linq" version="4.3.0" targetFramework="net45" />
<package id="System.Linq.Expressions" version="4.3.0" targetFramework="net45" />
<package id="System.Net.Http" version="4.3.0" targetFramework="net45" />
<package id="System.Net.Primitives" version="4.3.0" targetFramework="net45" />
<package id="System.Numerics.Vectors" version="4.3.0" targetFramework="net45" />
<package id="System.ObjectModel" version="4.3.0" targetFramework="net45" />
<package id="System.Reflection" version="4.3.0" targetFramework="net45" />
<package id="System.Reflection.Extensions" version="4.3.0" targetFramework="net45" />
<package id="System.Reflection.Primitives" version="4.3.0" targetFramework="net45" />
<package id="System.Resources.ResourceManager" version="4.3.0" targetFramework="net45" />
<package id="System.Runtime" version="4.3.0" targetFramework="net45" />
<package id="System.Runtime.CompilerServices.Unsafe" version="4.3.0" targetFramework="net45" />
<package id="System.Runtime.Extensions" version="4.3.0" targetFramework="net45" />
<package id="System.Runtime.InteropServices" version="4.3.0" targetFramework="net45" />
<package id="System.Runtime.InteropServices.RuntimeInformation" version="4.3.0" targetFramework="net45" />
<package id="System.Runtime.Numerics" version="4.3.0" targetFramework="net45" />
<package id="System.Text.Encoding" version="4.3.0" targetFramework="net45" />
<package id="System.Text.Encoding.Extensions" version="4.3.0" targetFramework="net45" />
<package id="System.Text.RegularExpressions" version="4.3.0" targetFramework="net45" />
<package id="System.Threading" version="4.3.0" targetFramework="net45" />
<package id="System.Threading.Tasks" version="4.3.0" targetFramework="net45" />
<package id="System.Threading.Tasks.Parallel" version="4.3.0" targetFramework="net45" />
<package id="System.Xml.ReaderWriter" version="4.3.0" targetFramework="net45" />
<package id="System.Xml.XDocument" version="4.3.0" targetFramework="net45" />
</packages>