budnhead/NHEngine/OpenGLWindow.cs

200 lines
4.4 KiB
C#
Raw Normal View History

2017-04-20 16:49:43 +02:00
using System;
using System.Drawing;
using System.Collections.Generic;
2017-04-20 16:49:43 +02:00
using OpenTK.Graphics;
using OpenTK.Graphics.OpenGL;
using OpenTK;
2017-04-25 22:01:18 +02:00
using org.nhengine.graphics;
using org.nhengine.graphics.primitives;
2017-04-20 16:49:43 +02:00
namespace nhengine
{
public class OpenGLWindow : GameWindow
{
private Point mouseCapturePosition;
private bool mouseCaptured;
2017-04-25 22:01:18 +02:00
private float arcUpDown,
arcLeftRight;
private Cube cube,
c1, c2, c3;
GraphicsMode gmode = new GraphicsMode();
GLCamera scene;
List<GLObject> glObjects;
2017-04-20 16:49:43 +02:00
public OpenGLWindow()
:base(800, 600,
GraphicsMode.Default,
"nhEngine",
GameWindowFlags.Default,
DisplayDevice.Default,
3, 3,
GraphicsContextFlags.ForwardCompatible)
{
glObjects = new List<GLObject>();
2017-04-20 16:49:43 +02:00
VSync = VSyncMode.On;
scene = new GLCamera();
2017-04-20 16:49:43 +02:00
}
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
GL.ClearColor(0.1f, 0.2f, 0.5f, 0.0f);
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
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);
2017-04-25 22:01:18 +02:00
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);
scene.setViewport(ClientRectangle.Width, ClientRectangle.Height);
2017-04-20 16:49:43 +02:00
}
protected override void OnRenderFrame(FrameEventArgs e)
{
base.OnRenderFrame(e);
GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
foreach (GLObject glo in glObjects){
glo.paint(scene);
}
2017-04-20 16:49:43 +02:00
SwapBuffers();
}
public void addGLObject(GLObject glObject){
this.glObjects.Add( glObject );
}
public void removeGLObject(GLObject glObject){
this.glObjects.Remove( glObject );
}
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;
}
}
protected virtual void onMouseCapturedMove(Point delta){
2017-04-25 22:01:18 +02:00
rotateLeftRight(((float)delta.X) / 10.0f);
rotateUpDown(((float)delta.Y) / 10.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:
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);
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:
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;
2017-04-25 22:01:18 +02:00
}
2017-04-20 16:49:43 +02:00
}
2017-04-25 22:01:18 +02:00
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;
}
2017-04-25 22:01:18 +02:00
private void applyRotation(){
Console.WriteLine("Rotation: {0} {1}", arcLeftRight, arcUpDown);
}
protected override void OnKeyUp(OpenTK.Input.KeyboardKeyEventArgs e)
{
}
2017-04-20 16:49:43 +02:00
}
}