1 module prova.graphics.camera;
2 
3 import prova.math;
4 
5 ///
6 enum SortingMethod { Z, Distance }
7 ///
8 enum Projection { Perspective, Orthographic }
9 
10 ///
11 class Camera
12 {
13   ///
14   Projection projection = Projection.Perspective;
15   ///
16   SortingMethod sortingMethod = SortingMethod.Distance;
17   ///
18   Vector3 scale = Vector3(1, 1, 1);
19   ///
20   Vector3 position;
21   ///
22   Quaternion rotation;
23   ///
24   bool useDepthBuffer = true;
25   /// Width and height will be set to the screen resolution when true
26   bool resolutionDependent = false;
27   ///
28   float zNear = 0;
29   ///
30   float zFar = 1000;
31   /// For orthographic projection and UI
32   float width = 1;
33   /// For orthographic projection and UI
34   float height = 1;
35   /// For perspective projection
36   float FOV = 90;
37 
38   ///
39   @property float zRange()
40   {
41     return zFar - zNear;
42   }
43 
44   ///
45   Matrix getViewMatrix()
46   {
47     Matrix transform = Matrix.identity;
48     transform = transform.translate(-position);
49     transform = transform.scale(scale);
50     transform = transform.rotate(rotation);
51 
52     return transform;
53   }
54 
55   ///
56   Matrix getProjectionMatrix()
57   {
58     if(projection == Projection.Orthographic)
59       return Matrix.ortho(-width/2, width/2, height/2, -height/2, zNear, zFar);
60     return Matrix.perspective(width, height, zNear, zFar, FOV);
61   }
62 
63   /**
64    * Converts world position to screen position
65    * - This is equal to getProjectionMatrix() * getViewMatrix()
66    */
67   Matrix getScreenMatrix()
68   {
69     return getProjectionMatrix() * getViewMatrix();
70   }
71 
72   ///
73   Matrix getUIMatrix()
74   {
75     const float left = 0;
76     const float right = width / scale.x;
77     const float top = height / scale.y;
78     const float bottom = 0;
79 
80     return Matrix.ortho(left, right, top, bottom, -1, 1);
81   }
82 }