budnhead/org.niclasundharald.engine/graphics/GLCamera.cs

93 lines
1.7 KiB
C#

using System;
using OpenTK.Graphics.OpenGL;
using OpenTK.Graphics;
using OpenTK;
namespace org.niclasundharald.engine.graphics
{
public class GLCamera : GLSceneOrientation
{
float fov;
float width, height;
Vector3 vPosition,
vView,
vTop;
public GLCamera()
{
fov = MathHelper.PiOver2;
width = 100;
height = 100;
vPosition = new Vector3(0,0,1000);
vView = new Vector3(0,0,-1);
vTop = new Vector3(0,1,0);
buildProjection();
buildMatrices();
}
public void setViewport(int width, int height)
{
this.width = (float)width;
this.height = (float)height;
buildProjection();
}
public void setFoV(float fov){
this.fov = fov;
buildProjection();
}
private void buildProjection(){
_mProjection = Matrix4.CreatePerspectiveFieldOfView(
fov,
width / height,
1.0f,
100000.0f
);
}
public Vector3 Position
{
get { return new Vector3(vPosition); }
set { this.vPosition = new Vector3(value); buildMatrices(); }
}
public Vector3 View
{
get { return new Vector3(vView); }
set { this.vView = new Vector3(value).Normalized(); buildMatrices(); }
}
public Vector3 Top
{
get { return new Vector3(vTop); }
set { this.vTop = new Vector3(value).Normalized(); buildMatrices(); }
}
private void buildMatrices(){
Matrix4 mTranslation = Matrix4.CreateTranslation( -this.vPosition );
Vector3 x,y,z;
z = -vView;
x = Vector3.Cross( vTop, z ).Normalized();
y = Vector3.Cross( z, x );
Matrix4 mRotation = new Matrix4(
new Vector4(x,0),
new Vector4(y,0),
new Vector4(z,0),
new Vector4(0,0,0,1)
);
mRotation.Transpose();
_mCamera = mTranslation * mRotation;
}
}
}