Wrote a very basic camera and unit tests.
This commit is contained in:
parent
e2c2b0adc0
commit
bb44e4fee3
68
RecrownedGTK.Tests/Graphics/CameraTest.cs
Normal file
68
RecrownedGTK.Tests/Graphics/CameraTest.cs
Normal file
@ -0,0 +1,68 @@
|
||||
using NUnit.Framework;
|
||||
using RecrownedGTK.Graphics;
|
||||
using OpenTK;
|
||||
using System;
|
||||
namespace RecrownedGTK.Tests.Graphics {
|
||||
[TestFixture]
|
||||
public class CameraTest {
|
||||
Camera camera;
|
||||
Vector4 position;
|
||||
|
||||
[SetUp]
|
||||
public void Setup() {
|
||||
camera = new Camera();
|
||||
position = new Vector4();
|
||||
position.X = 1;
|
||||
position.W = 1;
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void TestCameraTranslation() {
|
||||
Console.WriteLine("Camera Transform Matrix Before:");
|
||||
Console.WriteLine(camera.Transform);
|
||||
Console.WriteLine("Vector Before:");
|
||||
Console.WriteLine(position);
|
||||
camera.Translate = new Vector3(1, 1, 0);
|
||||
Console.WriteLine("Matrix After:");
|
||||
Console.WriteLine(camera.Transform);
|
||||
Vector4 result = camera.Transform * position;
|
||||
Console.WriteLine("Vector After:");
|
||||
Console.WriteLine(result);
|
||||
Vector4 expected = new Vector4(2, 1, 0, 1);
|
||||
Assert.AreEqual(expected, result);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void TestCameraRotation() {
|
||||
Console.WriteLine("Camera Transform Matrix Before:");
|
||||
Console.WriteLine(camera.Transform);
|
||||
Console.WriteLine("Vector Before:");
|
||||
Console.WriteLine(position);
|
||||
camera.Rotation = new Vector3(0, 0, MathHelper.DegreesToRadians(90f));
|
||||
Console.WriteLine("Matrix After:");
|
||||
Console.WriteLine(camera.Transform);
|
||||
Vector4 result = camera.Transform * position;
|
||||
result.X = (float) Math.Round((float) result.X, 4);
|
||||
Console.WriteLine("Vector After:");
|
||||
Console.WriteLine(result);
|
||||
Vector4 expected = new Vector4(0, 1, 0, 1);
|
||||
Assert.AreEqual(expected, result);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void TestCameraScale() {
|
||||
Console.WriteLine("Camera Transform Matrix Before:");
|
||||
Console.WriteLine(camera.Transform);
|
||||
Console.WriteLine("Vector Before:");
|
||||
Console.WriteLine(position);
|
||||
camera.Scale = new Vector3(2f);
|
||||
Console.WriteLine("Matrix After:");
|
||||
Console.WriteLine(camera.Transform);
|
||||
Vector4 result = camera.Transform * position;
|
||||
Console.WriteLine("Vector After:");
|
||||
Console.WriteLine(result);
|
||||
Vector4 expected = new Vector4(2, 0, 0, 1);
|
||||
Assert.AreEqual(expected, result);
|
||||
}
|
||||
}
|
||||
}
|
48
RecrownedGTK/Graphics/Camera.cs
Normal file
48
RecrownedGTK/Graphics/Camera.cs
Normal file
@ -0,0 +1,48 @@
|
||||
using OpenTK;
|
||||
|
||||
namespace RecrownedGTK.Graphics {
|
||||
public class Camera {
|
||||
private Vector3 scale = new Vector3(1f), translate, rotation;
|
||||
public Vector3 Scale {
|
||||
get {
|
||||
return scale;
|
||||
}
|
||||
set {
|
||||
updated = true;
|
||||
scale = value;
|
||||
}
|
||||
}
|
||||
public Vector3 Translate {
|
||||
set {
|
||||
updated = true;
|
||||
translate = value;
|
||||
}
|
||||
}
|
||||
public Vector3 Rotation {
|
||||
set {
|
||||
updated = true;
|
||||
rotation = value;
|
||||
}
|
||||
get {
|
||||
return rotation;
|
||||
}
|
||||
}
|
||||
private Matrix4 transform;
|
||||
|
||||
public Matrix4 Transform {
|
||||
get {
|
||||
if (!updated) return transform;
|
||||
updated = true;
|
||||
transform =
|
||||
Matrix4.CreateScale(scale) *
|
||||
Matrix4.CreateRotationX(rotation.X) *
|
||||
Matrix4.CreateRotationY(rotation.Y) *
|
||||
Matrix4.CreateRotationZ(rotation.Z) *
|
||||
Matrix4.CreateTranslation(translate);
|
||||
transform = Matrix4.Transpose(transform);
|
||||
return transform;
|
||||
}
|
||||
}
|
||||
private bool updated;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user