200 lines
4.4 KiB
C#
200 lines
4.4 KiB
C#
using System;
|
|
using System.Drawing;
|
|
using System.Collections.Generic;
|
|
|
|
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 Point mouseCapturePosition;
|
|
private bool mouseCaptured;
|
|
|
|
private float arcUpDown,
|
|
arcLeftRight;
|
|
|
|
private Cube cube,
|
|
c1, c2, c3;
|
|
|
|
GraphicsMode gmode = new GraphicsMode();
|
|
|
|
GLCamera scene;
|
|
|
|
List<GLObject> glObjects;
|
|
|
|
public OpenGLWindow()
|
|
:base(800, 600,
|
|
GraphicsMode.Default,
|
|
"nhEngine",
|
|
GameWindowFlags.Default,
|
|
DisplayDevice.Default,
|
|
3, 3,
|
|
GraphicsContextFlags.ForwardCompatible)
|
|
{
|
|
glObjects = new List<GLObject>();
|
|
VSync = VSyncMode.On;
|
|
scene = new GLCamera();
|
|
}
|
|
|
|
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);
|
|
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);
|
|
|
|
}
|
|
|
|
protected override void OnResize(EventArgs e)
|
|
{
|
|
base.OnResize(e);
|
|
|
|
GL.Viewport(ClientRectangle.X, ClientRectangle.Y, ClientRectangle.Width, ClientRectangle.Height);
|
|
scene.setViewport(ClientRectangle.Width, ClientRectangle.Height);
|
|
}
|
|
|
|
protected override void OnRenderFrame(FrameEventArgs e)
|
|
{
|
|
base.OnRenderFrame(e);
|
|
|
|
GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
|
|
|
|
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;
|
|
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)
|
|
{
|
|
walk(e.ValuePrecise);
|
|
}
|
|
|
|
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;
|
|
case OpenTK.Input.Key.W:
|
|
walk(100.0f);
|
|
break;
|
|
case OpenTK.Input.Key.S:
|
|
walk(-100.0f);
|
|
break;
|
|
|
|
}
|
|
}
|
|
|
|
public void rotateUpDown(float arc)
|
|
{
|
|
arcUpDown += arc;
|
|
applyRotation();
|
|
}
|
|
public void rotateLeftRight(float arc)
|
|
{
|
|
arcLeftRight += arc;
|
|
applyRotation();
|
|
}
|
|
|
|
public void walk(float distance){
|
|
scene.Position += scene.View * distance;
|
|
}
|
|
|
|
private void applyRotation(){
|
|
Console.WriteLine("Rotation: {0} {1}", arcLeftRight, arcUpDown);
|
|
}
|
|
|
|
protected override void OnKeyUp(OpenTK.Input.KeyboardKeyEventArgs e)
|
|
{
|
|
}
|
|
|
|
}
|
|
}
|