299 lines
7.3 KiB
C#
299 lines
7.3 KiB
C#
using System;
|
|
using System.Drawing;
|
|
using System.Collections.Generic;
|
|
|
|
using OpenTK.Graphics;
|
|
using OpenTK.Graphics.OpenGL;
|
|
using OpenTK;
|
|
|
|
using org.niclasundharald.engine;
|
|
using org.niclasundharald.engine.graphics;
|
|
using org.niclasundharald.engine.graphics.primitives;
|
|
|
|
namespace nhengine
|
|
{
|
|
|
|
public class OpenGLWindow : GameWindow
|
|
{
|
|
private Point mouseCapturePosition;
|
|
private bool mouseCaptured;
|
|
|
|
private float arcUpDown,
|
|
arcLeftRight;
|
|
|
|
private Vector3 lookAt;
|
|
private float lookDistance;
|
|
|
|
private Cube cube,
|
|
c1, c2, c3;
|
|
|
|
private string titleMsg = "";
|
|
|
|
GLCamera sceneCamera;
|
|
GLScene scene;
|
|
|
|
public OpenGLWindow()
|
|
:base(600, 600,
|
|
GraphicsMode.Default,
|
|
"nhEngine",
|
|
GameWindowFlags.Default,
|
|
DisplayDevice.Default,
|
|
3, 3,
|
|
GraphicsContextFlags.ForwardCompatible)
|
|
{
|
|
VSync = VSyncMode.On;
|
|
sceneCamera = new GLCamera();
|
|
scene = new GLScene(sceneCamera);
|
|
}
|
|
|
|
protected override void OnLoad(EventArgs e)
|
|
{
|
|
base.OnLoad(e);
|
|
GL.ClearColor(0.05f, 0.05f, 0.05f, 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,150), 16.0f);
|
|
c1 = new Cube(new Vector3(16,0,150), 8.0f);
|
|
c2 = new Cube(new Vector3(0,150,150), 8.0f);
|
|
c3 = new Cube(new Vector3(175,175,150), 8.0f);
|
|
|
|
lookAt = new Vector3(0,0,SquaredMap.maxHeight);
|
|
lookDistance = 2048;
|
|
arcUpDown = MathHelper.PiOver6;
|
|
arcLeftRight = MathHelper.PiOver4;
|
|
|
|
sceneCamera.setFoV(MathHelper.PiOver4);
|
|
|
|
setupCamera();
|
|
|
|
scene.RootObject.addChild( cube );
|
|
scene.RootObject.addChild( c1 );
|
|
scene.RootObject.addChild( c2 );
|
|
scene.RootObject.addChild( c3 );
|
|
|
|
}
|
|
|
|
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 override void OnRenderFrame(FrameEventArgs e)
|
|
{
|
|
base.OnRenderFrame(e);
|
|
GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
|
|
|
|
scene.draw();
|
|
|
|
SwapBuffers();
|
|
|
|
Title = string.Format("{0:F}/s {1:F}/s [{2:F}s] {3}", RenderFrequency,UpdateFrequency,UpdatePeriod,titleMsg);
|
|
}
|
|
|
|
protected override void OnUpdateFrame(FrameEventArgs e)
|
|
{
|
|
base.OnUpdateFrame(e);
|
|
Matrix4 m4 = Matrix4.CreateRotationZ(MathHelper.Pi / 90.0f);
|
|
|
|
cube.Rotation = m4 * cube.Rotation;
|
|
c1.Position = (m4.Inverted() * new Vector4(c1.Position)).Xyz;
|
|
c2.Rotation = (m4 * c2.Rotation);
|
|
c3.Position = (m4 * new Vector4(c3.Position)).Xyz;
|
|
|
|
|
|
Actor.updateAll( (float)UpdatePeriod );
|
|
}
|
|
|
|
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;
|
|
} else {
|
|
markMouse(e.X,e.Y);
|
|
}
|
|
}
|
|
|
|
protected virtual void onMouseCapturedMove(Point delta){
|
|
rotateLeftRight(((float)delta.X) / 20.0f);
|
|
rotateUpDown(((float)delta.Y) / 20.0f);
|
|
}
|
|
|
|
protected override void OnMouseDown(OpenTK.Input.MouseButtonEventArgs e)
|
|
{
|
|
switch (e.Button){
|
|
case OpenTK.Input.MouseButton.Left:
|
|
markMouse(e.X,e.Y);
|
|
break;
|
|
case OpenTK.Input.MouseButton.Right:
|
|
captureMouse();
|
|
break;
|
|
}
|
|
}
|
|
|
|
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)
|
|
{
|
|
releaseMouse();
|
|
}
|
|
|
|
protected override void OnMouseWheel(OpenTK.Input.MouseWheelEventArgs e)
|
|
{
|
|
lookDistance *= 1.0f - ( e.DeltaPrecise / 16 );
|
|
lookDistance = MathHelper.Clamp( lookDistance, 4, 8192 );
|
|
setupCamera();
|
|
}
|
|
|
|
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;
|
|
}
|
|
|
|
|
|
public void rotateUpDown(float arc)
|
|
{
|
|
arcUpDown += arc;
|
|
arcUpDown = MathHelper.Clamp(arcUpDown,MathHelper.Pi / 16,MathHelper.PiOver2);
|
|
setupCamera();
|
|
}
|
|
public void rotateLeftRight(float arc)
|
|
{
|
|
arcLeftRight += arc;
|
|
arcLeftRight %= MathHelper.TwoPi;
|
|
setupCamera();
|
|
}
|
|
|
|
public void walk(float distance){
|
|
lookAt += (new Vector3(sceneCamera.View.Xy)).Normalized() * distance;
|
|
setupCamera();
|
|
}
|
|
|
|
public void strafe(float distance){
|
|
lookAt += Vector3.Cross(new Vector3(sceneCamera.View.Xy),Vector3.UnitZ).Normalized() * distance;
|
|
setupCamera();
|
|
}
|
|
|
|
protected override void OnKeyUp(OpenTK.Input.KeyboardKeyEventArgs e)
|
|
{
|
|
}
|
|
|
|
private void setupCamera(){
|
|
Matrix4 mr = (Matrix4.CreateRotationZ(arcLeftRight) * Matrix4.CreateRotationX(arcUpDown));
|
|
Vector3 view = (mr * Vector4.UnitY).Xyz;
|
|
Vector3 top = (mr * Vector4.UnitZ).Xyz;
|
|
|
|
view.Normalize();
|
|
top.Normalize();
|
|
|
|
Vector3 pos = lookAt - (view * lookDistance);
|
|
|
|
sceneCamera.View = view;
|
|
sceneCamera.Top = top;
|
|
sceneCamera.Position = pos;
|
|
}
|
|
|
|
public GLScene Scene {
|
|
get { return this.scene; }
|
|
}
|
|
|
|
|
|
}
|
|
}
|