-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCamera.cs
More file actions
63 lines (51 loc) · 1.98 KB
/
Camera.cs
File metadata and controls
63 lines (51 loc) · 1.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
using System;
using System.Numerics;
namespace SoftwareRenderer
{
public class Camera
{
public Vector3 Position = Vector3.Zero;
public Quaternion Rotation = Quaternion.Identity;
public float Sensitivity = 0.1f;
public Matrix4x4 GetViewMatrix()
{
Vector3 forward = GetFront();
Vector3 up = GetUp();
return Matrix4x4.CreateLookAt(Position, Position + forward, up);
}
public Vector3 GetFront() =>
Vector3.Transform(-Vector3.UnitZ, Rotation);
public Vector3 GetRight() =>
Vector3.Transform(Vector3.UnitX, Rotation);
public Vector3 GetUp() =>
Vector3.Transform(Vector3.UnitY, Rotation);
private static float ToDegrees(float radians)
{
return radians * (180f / MathF.PI);
}
public Vector3 GetEulerAngles()
{
// Extract Euler angles from quaternion
Vector3 angles = new Vector3();
// Roll (Z)
float sinr_cosp = 2 * (Rotation.W * Rotation.Z + Rotation.X * Rotation.Y);
float cosr_cosp = 1 - 2 * (Rotation.Z * Rotation.Z + Rotation.X * Rotation.X);
angles.Z = MathF.Atan2(sinr_cosp, cosr_cosp);
// Pitch (X)
float sinp = 2 * (Rotation.W * Rotation.X - Rotation.Y * Rotation.Z);
if (MathF.Abs(sinp) >= 1)
angles.X = MathF.CopySign(MathF.PI / 2, sinp);
else
angles.X = MathF.Asin(sinp);
// Yaw (Y)
float siny_cosp = 2 * (Rotation.W * Rotation.Y + Rotation.Z * Rotation.X);
float cosy_cosp = 1 - 2 * (Rotation.X * Rotation.X + Rotation.Y * Rotation.Y);
angles.Y = MathF.Atan2(siny_cosp, cosy_cosp);
// Convert to degrees
angles.X *= (180f / MathF.PI);
angles.Y *= (180f / MathF.PI);
angles.Z *= (180f / MathF.PI);
return angles;
}
}
}