1 /* 2 Copyright © 2020, Luna Nielsen 3 Distributed under the 2-Clause BSD License, see LICENSE file. 4 5 Authors: Luna Nielsen 6 */ 7 module engine.math.camera; 8 import gl3n.linalg; 9 import engine; 10 11 /** 12 Camera for 3D space 13 */ 14 class Camera { 15 private: 16 17 mat4 g_matrix() { 18 return mat4.perspective(cameraTargetWidth, cameraTargetHeight, fov, 0.1, 50) * transform.matrix; 19 } 20 21 public: 22 23 this() { 24 25 } 26 27 float fov = 90; 28 29 /** 30 Transform of the position of the camera 31 */ 32 Transform transform; 33 34 /** 35 Create 3D camera 36 */ 37 this(Transform transform = null) { 38 this.transform = transform is null ? new Transform() : transform; 39 } 40 41 /** 42 The matrix for the camera 43 */ 44 mat4 matrix() { 45 return g_matrix(); 46 } 47 } 48 49 /** 50 Orthographic camera for rendering UI 51 52 TODO: extend camera as needed 53 */ 54 class Camera2D { 55 private: 56 mat4 projection; 57 58 public: 59 60 this() { 61 position = vec2(0, 0); 62 } 63 64 /** 65 Position of camera 66 */ 67 vec2 position; 68 69 /** 70 Matrix for this camera 71 */ 72 mat4 matrix() { 73 int largestSize = max(kmViewportWidth, kmViewportHeight); 74 return 75 mat4.orthographic(0f, cast(float)cameraTargetWidth, cast(float)cameraTargetHeight, 0, 0, largestSize) * 76 mat4.translation(position.x, position.y, -10); 77 } 78 } 79 80 private int cameraTargetWidth; 81 private int cameraTargetHeight; 82 83 void kmSetCameraTargetSize(int width, int height) { 84 cameraTargetWidth = width; 85 cameraTargetHeight = height; 86 } 87 88 int kmCameraViewWidth() { 89 return cameraTargetWidth; 90 } 91 92 int kmCameraViewHeight() { 93 return cameraTargetHeight; 94 }