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 13 /** 14 An orthographic camera 15 */ 16 class Camera { 17 private: 18 mat4 projection; 19 20 public: 21 22 /** 23 Position of camera 24 */ 25 vec2 position = vec2(0, 0); 26 27 /** 28 Size of the camera 29 */ 30 vec2 scale = vec2(1, 1); 31 32 vec2 getRealSize() { 33 int width, height; 34 inGetViewport(width, height); 35 36 return vec2(cast(float)width/scale.x, cast(float)height/scale.y); 37 } 38 39 vec2 getCenterOffset() { 40 vec2 realSize = getRealSize(); 41 return realSize/2; 42 } 43 44 /** 45 Matrix for this camera 46 47 width = width of camera area 48 height = height of camera area 49 */ 50 mat4 matrix() { 51 vec2 realSize = getRealSize(); 52 if(!position.isFinite) position = vec2(0); 53 if(!scale.isFinite) scale = vec2(1); 54 if(!realSize.isFinite) return mat4.identity; 55 56 return 57 mat4.orthographic(0f, realSize.x, realSize.y, 0, 0, ushort.max) * 58 mat4.translation(position.x+(realSize.x/2), position.y+(realSize.y/2), -(ushort.max/2)); 59 } 60 }