1 module polyplex.core.render.camera;
2 import polyplex.math;
3 import std.stdio;
4 
5 /**
6 	A basis of a camera.
7 */
8 public class Camera {
9 	protected Matrix4x4 matrix;
10 	protected float znear;
11 	protected float zfar;
12 
13 	public @property Matrix4x4 Matrix() { Update(); return this.matrix; }
14 	public @property void Matrix(Matrix4x4 value) { this.matrix = value; Update(); }
15 
16 	/**
17 		Update the camera rotation, scale and translation.
18 	*/
19 	public abstract void Update();
20 
21 	/**
22 		Creates a dimension dependant projection. (Recommended)
23 	*/
24 	public abstract Matrix4x4 Project(float width, float height);
25 
26 	/**
27 		Creates an perspective projection with the specified width and height.
28 	*/
29 	public Matrix4x4 ProjectPerspective(float width, float fov, float height) {
30 		return Matrix4x4.Perspective(width, height, fov, znear, zfar);
31 	}
32 
33 	/**
34 		Creates an othrographic projection with the specified width and height.
35 	*/
36 	public Matrix4x4 ProjectOrthographic(float width, float height) {
37 		return Matrix4x4.Identity.Orthographic(0f, width, height, 0f, znear, zfar);
38 	}
39 
40 	/**
41 		Creates an othrographic projection with the specified width and height.
42 	*/
43 	public Matrix4x4 ProjectOrthographicInv(float width, float height) {
44 		return Matrix4x4.Identity.Orthographic(0f, width, 0f, height, znear, zfar);
45 	}
46 }
47 
48 /**
49 	A basic 3D camera, optimized for use in 3D environments.
50 */
51 public class Camera3D : Camera {
52 	private float fov;
53 	public Vector3 Position;
54 	public Quaternion Rotation;
55 	public float Zoom;
56 
57 
58 	this(Vector3 position, Quaternion rotation = Quaternion.Identity, float zoom = 1f, float near = 0.1f, float far = 100f, float fov = 90f) {
59 		this.Position = position;
60 		this.Rotation = rotation;
61 		this.Zoom = zoom;
62 
63 		this.znear = near;
64 		this.zfar = far;
65 		this.fov = fov;
66 		Update();
67 	}
68 
69 	/**
70 		Update the camera rotation, scale and translation.
71 	*/
72 	public override void Update() {
73 		this.matrix = Matrix4x4.Identity;
74 		this.matrix
75 		.Translate(Position)
76 		.RotateX(this.Rotation.X)
77 		.RotateY(this.Rotation.Y)
78 		.RotateZ(this.Rotation.Z)
79 		.Scale(Vector3(Zoom, Zoom, Zoom));
80 	}
81 
82 	/**
83 		Creates an perspective projection with the specified width and height.
84 	*/
85 	public override Matrix4x4 Project(float width, float height) {
86 		return ProjectPerspective(width, this.fov, height);
87 	}
88 }
89 
90 public class Camera2D : Camera {
91 	public Vector2 Position;
92 	public float Rotation;
93 	public float RotationY;
94 	public float RotationX;
95 	public float Zoom;
96 	public float Depth;
97 	public Vector2 Origin;
98 
99 	this(Vector2 position, float depth = -5f, float rotation = 0f, float zoom = 1f, float near = 0.01f, float far = 100f) {
100 		this.Position = Vector2(position.X, position.Y);
101 		this.Rotation = rotation;
102 		this.RotationX = 0;
103 		this.RotationY = 0;
104 		this.Zoom = zoom;
105 		this.Depth = depth;
106 		this.Origin = Vector2(0, 0);
107 
108 		this.znear = near;
109 		this.zfar = far;
110 		this.matrix = Matrix4x4.Identity;
111 		Update();
112 	}
113 
114 	/**
115 		Update the camera rotation, scale and translation.
116 	*/
117 	public override void Update() {
118 		this.matrix = Matrix4x4.Identity
119 		.Translate(Vector3(-Position.X, -Position.Y, Depth))
120 		.RotateX(-this.RotationX)
121 		.RotateY(-this.RotationY)
122 		.RotateZ(-this.Rotation)
123 		.Scale(Vector3(Zoom, Zoom, Zoom))
124 		.Translate(Vector3(Origin.X, Origin.Y, Depth));
125 	}
126 
127 	/**
128 		Creates an othrographic projection with the specified width and height.
129 	*/
130 	public override Matrix4x4 Project(float width, float height) {
131 		return ProjectOrthographic(width, height);
132 	}
133 }