1 /* 2 Inochi2D Camera 3 4 Copyright © 2020, Inochi2D Project 5 Distributed under the 2-Clause BSD License, see LICENSE file. 6 7 Authors: Luna Nielsen 8 */ 9 module inochi2d.math.camera; 10 import inochi2d.math; 11 import inochi2d; 12 import std.math : isFinite; 13 14 /** 15 An orthographic camera 16 */ 17 class Camera { 18 private: 19 mat4 projection; 20 21 public: 22 23 /** 24 Position of camera 25 */ 26 vec2 position = vec2(0, 0); 27 28 /** 29 Rotation of the camera 30 */ 31 float rotation = 0f; 32 33 /** 34 Size of the camera 35 */ 36 vec2 scale = vec2(1, 1); 37 38 vec2 getRealSize() { 39 int width, height; 40 inGetViewport(width, height); 41 42 return vec2(cast(float)width/scale.x, cast(float)height/scale.y); 43 } 44 45 vec2 getCenterOffset() { 46 vec2 realSize = getRealSize(); 47 return realSize/2; 48 } 49 50 /** 51 Matrix for this camera 52 53 width = width of camera area 54 height = height of camera area 55 */ 56 mat4 matrix() { 57 if(!position.isFinite) position = vec2(0); 58 if(!scale.isFinite) scale = vec2(1); 59 if(!rotation.isFinite) rotation = 0; 60 61 vec2 realSize = getRealSize(); 62 if(!realSize.isFinite) return mat4.identity; 63 64 vec2 origin = vec2(realSize.x/2, realSize.y/2); 65 vec3 pos = vec3(position.x, position.y, -(ushort.max/2)); 66 67 return 68 mat4.orthographic(0f, realSize.x, realSize.y, 0, 0, ushort.max) * 69 mat4.translation(origin.x, origin.y, 0) * 70 mat4.zRotation(rotation) * 71 mat4.translation(pos); 72 } 73 }