budnhead/org.budnhead/graphics/GLWindow.cs

174 lines
4.0 KiB
C#

using System;
using System.Drawing;
using System.Collections.Generic;
using System.Threading;
using OpenTK.Graphics;
using OpenTK.Graphics.OpenGL;
using OpenTK;
using org.budnhead.core;
using org.budnhead.exceptions;
using org.budnhead.graphics.primitives;
namespace org.budnhead.graphics
{
public class GLWindow : GameWindow
{
private Point mouseCapturePosition;
private bool mouseCaptured;
private float arcUpDown,
arcLeftRight;
private Vector3 lookAt;
private float lookDistance;
GLCamera sceneCamera;
Scene scene;
public GLWindow()
:base(600, 600,
GraphicsMode.Default,
"nhEngine",
GameWindowFlags.Default,
DisplayDevice.Default,
3, 3,
GraphicsContextFlags.ForwardCompatible)
{
VSync = VSyncMode.On;
sceneCamera = new GLCamera();
scene = new Scene(sceneCamera);
}
public Scene Scene {
get { return this.scene; }
}
/*
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
GL.ClearColor(0.15f, 0.15f, 0.15f, 0.0f);
GL.Enable(EnableCap.DepthTest);
GL.Enable(EnableCap.Normalize);
GL.Enable(EnableCap.Blend);
GL.BlendFunc(BlendingFactorSrc.SrcAlpha,BlendingFactorDest.OneMinusSrcAlpha);
lookAt = new Vector3(0,0,SquaredMap.maxHeight);
lookDistance = 2048;
arcUpDown = MathHelper.PiOver6;
arcLeftRight = MathHelper.PiOver4;
sceneCamera.setFoV(MathHelper.PiOver2);
setupCamera();
}
protected override void OnResize(EventArgs e)
{
base.OnResize(e);
GL.Viewport(ClientRectangle.X, ClientRectangle.Y, ClientRectangle.Width, ClientRectangle.Height);
sceneCamera.setViewport(ClientRectangle.Width, ClientRectangle.Height);
}
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);
}
}
protected override void OnMouseUp(OpenTK.Input.MouseButtonEventArgs e)
{
}
protected override void OnMouseWheel(OpenTK.Input.MouseWheelEventArgs e)
{
}
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 / 180.0f);
break;
case OpenTK.Input.Key.Down:
rotateUpDown(MathHelper.Pi / 180.0f);
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:
walk(e.Shift ? 50.0f : 5.0f);
break;
case OpenTK.Input.Key.S:
walk(e.Shift ? -50.0f : -5.0f);
break;
case OpenTK.Input.Key.A:
strafe(e.Shift ? 50.0f : 5.0f);
break;
case OpenTK.Input.Key.D:
strafe(e.Shift ? -50.0f : -5.0f);
break;
case OpenTK.Input.Key.Space:
//fireBallistic();
break;
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;
}
}
private void fireBallistic(){
Console.WriteLine("Fire Ballistic...");
BallisticActor ba = new BallisticActor(0);
ba.fire();
ba.Position = BootStrap.instance().SquaredMap.ground(new Vector2(10,10));
ba.Velocity = new Vector3(0,1,1);// + (Matrix4.CreateRotationZ(arcLeftRight) * Vector4.UnitY).Xyz;
ba.Velocity.Normalize();
ba.Velocity *= 50;
}
*/
}
}