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 /** 42 A basic 3D camera, optimized for use in 3D environments. 43 */ 44 public class Camera3D : Camera { 45 private float fov; 46 public Vector3 Position; 47 public Quaternion Rotation; 48 public float Zoom; 49 50 51 this(Vector3 position, Quaternion rotation = Quaternion.Identity, float zoom = 1f, float near = 0.1f, float far = 100f, float fov = 90f) { 52 this.Position = position; 53 this.Rotation = rotation; 54 this.Zoom = zoom; 55 56 this.znear = near; 57 this.zfar = far; 58 this.fov = fov; 59 Update(); 60 } 61 62 /** 63 Update the camera rotation, scale and translation. 64 */ 65 public override void Update() { 66 this.matrix = Matrix4x4.Identity; 67 this.matrix 68 .Translate(Position) 69 .RotateX(this.Rotation.X) 70 .RotateY(this.Rotation.Y) 71 .RotateZ(this.Rotation.Z) 72 .Scale(Vector3(Zoom, Zoom, Zoom)); 73 } 74 75 /** 76 Creates an perspective projection with the specified width and height. 77 */ 78 public override Matrix4x4 Project(float width, float height) { 79 return ProjectPerspective(width, this.fov, height); 80 } 81 } 82 83 public class Camera2D : Camera { 84 public Vector3 Position; 85 public float Rotation; 86 public float RotationY; 87 public float RotationX; 88 public float Zoom; 89 public Vector3 Origin; 90 91 this(Vector2 position, float rotation = 0f, float zoom = 1f, float near = 0.01f, float far = 100f) { 92 this.Position = Vector3(position.X, position.Y, -5); 93 this.Rotation = rotation; 94 this.RotationX = 0; 95 this.RotationY = 0; 96 this.Zoom = zoom; 97 this.Origin = Vector3(0, 0, -5); 98 99 this.znear = near; 100 this.zfar = far; 101 this.matrix = Matrix4x4.Identity; 102 Update(); 103 } 104 105 /** 106 Update the camera rotation, scale and translation. 107 */ 108 public override void Update() { 109 this.matrix = Matrix4x4.Identity 110 .Translate(Vector3(-Position.X, -Position.Y, 0)) 111 .RotateX(this.RotationX) 112 .RotateY(this.RotationY) 113 .RotateZ(this.Rotation) 114 .Scale(Vector3(Zoom, Zoom, Zoom)) 115 .Translate(Vector3(Origin.X, Origin.Y, Origin.Z)); 116 } 117 118 /** 119 Creates an othrographic projection with the specified width and height. 120 */ 121 public override Matrix4x4 Project(float width, float height) { 122 return ProjectOrthographic(width, height); 123 } 124 }