budnhead/org.budnhead/graphics/GLWindow.cs

271 lines
6.4 KiB
C#
Raw Normal View History

2017-04-20 16:49:43 +02:00
using System;
using System.Drawing;
using System.Collections.Generic;
using System.Threading;
2017-04-20 16:49:43 +02:00
using OpenTK.Graphics;
using OpenTK.Graphics.OpenGL;
using OpenTK;
2017-05-09 23:41:50 +02:00
using org.budnhead.core;
using org.budnhead.exceptions;
using org.budnhead.graphics.primitives;
2017-04-25 22:01:18 +02:00
namespace org.budnhead.graphics
2017-04-20 16:49:43 +02:00
{
public class GLWindow : GameWindow
2017-04-20 16:49:43 +02:00
{
private Point mouseCapturePosition;
private bool mouseCaptured;
2017-04-25 22:01:18 +02:00
private float arcUpDown,
arcLeftRight;
2017-05-01 01:33:33 +02:00
private Vector3 lookAt;
private float lookDistance;
2017-04-25 22:01:18 +02:00
2017-05-01 01:33:33 +02:00
GLCamera sceneCamera;
Scene scene;
2017-05-01 01:33:33 +02:00
public GLWindow()
2017-05-08 14:30:41 +02:00
:base(600, 600,
2017-04-20 16:49:43 +02:00
GraphicsMode.Default,
"nhEngine",
GameWindowFlags.Default,
DisplayDevice.Default,
3, 3,
GraphicsContextFlags.ForwardCompatible)
{
VSync = VSyncMode.On;
2017-05-01 01:33:33 +02:00
sceneCamera = new GLCamera();
scene = new Scene(sceneCamera);
}
public Scene Scene {
get { return this.scene; }
2017-04-20 16:49:43 +02:00
}
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
GL.ClearColor(0.15f, 0.15f, 0.15f, 0.0f);
2017-04-20 16:49:43 +02:00
GL.Enable(EnableCap.DepthTest);
2017-04-25 22:01:18 +02:00
GL.Enable(EnableCap.Normalize);
GL.Enable(EnableCap.Blend);
GL.BlendFunc(BlendingFactorSrc.SrcAlpha,BlendingFactorDest.OneMinusSrcAlpha);
2017-04-25 22:01:18 +02:00
2017-05-08 14:30:41 +02:00
lookAt = new Vector3(0,0,SquaredMap.maxHeight);
2017-05-01 01:33:33 +02:00
lookDistance = 2048;
arcUpDown = MathHelper.PiOver6;
arcLeftRight = MathHelper.PiOver4;
sceneCamera.setFoV(MathHelper.PiOver2);
2017-05-01 01:33:33 +02:00
setupCamera();
2017-04-20 16:49:43 +02:00
}
protected override void OnResize(EventArgs e)
{
base.OnResize(e);
GL.Viewport(ClientRectangle.X, ClientRectangle.Y, ClientRectangle.Width, ClientRectangle.Height);
2017-05-01 01:33:33 +02:00
sceneCamera.setViewport(ClientRectangle.Width, ClientRectangle.Height);
2017-04-20 16:49:43 +02:00
}
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;
2017-05-08 14:30:41 +02:00
} else {
2017-04-20 16:49:43 +02:00
}
}
protected virtual void onMouseCapturedMove(Point delta){
2017-05-01 01:33:33 +02:00
rotateLeftRight(((float)delta.X) / 20.0f);
rotateUpDown(((float)delta.Y) / 20.0f);
2017-04-20 16:49:43 +02:00
}
protected override void OnMouseDown(OpenTK.Input.MouseButtonEventArgs e)
{
switch (e.Button){
case OpenTK.Input.MouseButton.Left:
//markMouse(e.X,e.Y);
2017-04-20 16:49:43 +02:00
break;
case OpenTK.Input.MouseButton.Right:
captureMouse();
break;
}
}
2017-05-08 14:30:41 +02:00
protected void markMouse(float x,float y){
try {
Vector3 P = sceneCamera.Position;
Vector3 V = sceneCamera.pickRay(x,y);
Vector3 isect = SquaredMap.activeMap.intersect(P,V);
Vector2 tile = SquaredMap.activeMap.toTile(isect.Xy);
SquaredMap.activeMap.highlight(tile);
} catch (OutOfWorldException owe){
Console.WriteLine(owe);
}
}
2017-04-20 16:49:43 +02:00
protected override void OnMouseUp(OpenTK.Input.MouseButtonEventArgs e)
{
releaseMouse();
}
protected override void OnMouseWheel(OpenTK.Input.MouseWheelEventArgs e)
{
2017-05-01 01:33:33 +02:00
lookDistance *= 1.0f - ( e.DeltaPrecise / 16 );
2017-05-08 14:30:41 +02:00
lookDistance = MathHelper.Clamp( lookDistance, 4, 8192 );
2017-05-01 01:33:33 +02:00
setupCamera();
2017-04-25 22:01:18 +02:00
}
protected override void OnKeyPress(KeyPressEventArgs e)
{
}
protected override void OnKeyDown(OpenTK.Input.KeyboardKeyEventArgs e)
{
switch (e.Key){
case OpenTK.Input.Key.Up:
2017-05-01 01:33:33 +02:00
rotateUpDown(-MathHelper.Pi / 180.0f);
2017-04-25 22:01:18 +02:00
break;
case OpenTK.Input.Key.Down:
2017-05-01 01:33:33 +02:00
rotateUpDown(MathHelper.Pi / 180.0f);
2017-04-25 22:01:18 +02:00
break;
case OpenTK.Input.Key.Left:
rotateLeftRight(MathHelper.Pi / 90.0f);
break;
case OpenTK.Input.Key.Right:
rotateLeftRight(-MathHelper.Pi / 90.0f);
break;
case OpenTK.Input.Key.W:
2017-05-04 23:57:00 +02:00
walk(e.Shift ? 50.0f : 5.0f);
break;
case OpenTK.Input.Key.S:
2017-05-04 23:57:00 +02:00
walk(e.Shift ? -50.0f : -5.0f);
break;
case OpenTK.Input.Key.A:
2017-05-08 14:30:41 +02:00
strafe(e.Shift ? 50.0f : 5.0f);
2017-05-04 23:57:00 +02:00
break;
case OpenTK.Input.Key.D:
2017-05-08 14:30:41 +02:00
strafe(e.Shift ? -50.0f : -5.0f);
2017-05-04 23:57:00 +02:00
break;
case OpenTK.Input.Key.Space:
//fireBallistic();
break;
2017-05-08 14:30:41 +02:00
case OpenTK.Input.Key.Number1:
lookAt = new Vector3(0,0,SquaredMap.maxHeight);
arcLeftRight = 0;
arcUpDown = MathHelper.PiOver2;
setupCamera();
break;
case OpenTK.Input.Key.Number2:
lookAt = new Vector3(0,0,SquaredMap.maxHeight);
arcLeftRight = MathHelper.PiOver2;
arcUpDown = MathHelper.PiOver2;
setupCamera();
break;
case OpenTK.Input.Key.Number3:
lookAt = new Vector3(0,0,SquaredMap.maxHeight);
sceneCamera.View = new Vector3(1,1,-1);
sceneCamera.Top = new Vector3(0,0,1);
break;
2017-04-25 22:01:18 +02:00
}
2017-04-20 16:49:43 +02:00
}
/*
2017-05-04 23:57:00 +02:00
private void fireBallistic(){
Console.WriteLine("Fire Ballistic...");
BallisticActor ba = new BallisticActor(0);
ba.fire();
2017-05-04 23:57:00 +02:00
ba.Position = BootStrap.instance().SquaredMap.ground(new Vector2(10,10));
2017-05-08 14:30:41 +02:00
ba.Velocity = new Vector3(0,1,1);// + (Matrix4.CreateRotationZ(arcLeftRight) * Vector4.UnitY).Xyz;
2017-05-04 23:57:00 +02:00
ba.Velocity.Normalize();
ba.Velocity *= 50;
}
*/
2017-05-04 23:57:00 +02:00
2017-04-25 22:01:18 +02:00
public void rotateUpDown(float arc)
{
arcUpDown += arc;
2017-05-08 14:30:41 +02:00
arcUpDown = MathHelper.Clamp(arcUpDown,MathHelper.Pi / 16,MathHelper.PiOver2);
2017-05-01 01:33:33 +02:00
setupCamera();
2017-04-25 22:01:18 +02:00
}
public void rotateLeftRight(float arc)
{
arcLeftRight += arc;
2017-05-01 01:33:33 +02:00
arcLeftRight %= MathHelper.TwoPi;
setupCamera();
2017-04-25 22:01:18 +02:00
}
public void walk(float distance){
2017-05-01 01:33:33 +02:00
lookAt += (new Vector3(sceneCamera.View.Xy)).Normalized() * distance;
setupCamera();
2017-04-25 22:01:18 +02:00
}
2017-05-04 23:57:00 +02:00
public void strafe(float distance){
lookAt += Vector3.Cross(new Vector3(sceneCamera.View.Xy),Vector3.UnitZ).Normalized() * distance;
setupCamera();
}
2017-04-25 22:01:18 +02:00
protected override void OnKeyUp(OpenTK.Input.KeyboardKeyEventArgs e)
{
}
2017-05-01 01:33:33 +02:00
private void setupCamera(){
2017-05-08 14:30:41 +02:00
Matrix4 mr = (Matrix4.CreateRotationZ(arcLeftRight) * Matrix4.CreateRotationX(arcUpDown));
Vector3 view = (mr * Vector4.UnitY).Xyz;
Vector3 top = (mr * Vector4.UnitZ).Xyz;
2017-05-01 01:33:33 +02:00
view.Normalize();
2017-05-08 14:30:41 +02:00
top.Normalize();
2017-05-01 01:33:33 +02:00
Vector3 pos = lookAt - (view * lookDistance);
sceneCamera.View = view;
2017-05-08 14:30:41 +02:00
sceneCamera.Top = top;
2017-05-01 01:33:33 +02:00
sceneCamera.Position = pos;
}
/* private void startTimers(){
timerGraphics.Change(0,50);
timerUpdates.Change(0,50);
}
private void stopTimers(){
timerGraphics.Change(Timeout.Infinite,Timeout.Infinite);
timerUpdates.Change(Timeout.Infinite,Timeout.Infinite);
}
*/
2017-04-20 16:49:43 +02:00
}
}