HWO-base
Harald Christian Joachim Wolff 2017-04-25 22:06:21 +02:00
parent fa784a83f3
commit c6aafb8cc9
4 changed files with 196 additions and 4 deletions

10
GLObject.cs 100644
View File

@ -0,0 +1,10 @@
using System;
namespace nhengine
{
public class GLObject
{
public GLObject()
{
}
}
}

View File

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

View File

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

View File

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