1 /**
2  * Defines the Camera class, which controls the view matrix for the world.
3  */
4 module components.camera;
5 import core.properties, core.gameobject;
6 import components.component;
7 import graphics.shaders;
8 
9 import gl3n.linalg;
10 
11 import std.signals, std.conv;
12 
13 final class Camera : Component
14 {
15 public:	
16 	mixin Signal!( string, string );
17 
18 	this(  )
19 	{
20 		super( null );
21 		_viewMatrixIsDirty = true;
22 	}
23 
24 	override void update() { }
25 	override void shutdown() { }
26 
27 	final @property ref mat4 viewMatrix()
28 	{
29 		if( _viewMatrixIsDirty )
30 			updateViewMatrix();
31 
32 		return _viewMatrix;
33 	}
34 
35 private:
36 	mat4 _viewMatrix;
37 	bool _viewMatrixIsDirty;
38 	final void setMatrixDirty( string prop, string newVal )
39 	{
40 		_viewMatrixIsDirty = true;
41 	}
42 	final void updateViewMatrix()
43 	{
44 		//Assuming pitch & yaw are in radians
45 		float cosPitch = cos( owner.transform.rotation.pitch );
46 		float sinPitch = sin( owner.transform.rotation.pitch );
47 		float cosYaw = cos( owner.transform.rotation.yaw );
48 		float sinYaw = sin( owner.transform.rotation.yaw );
49 
50 		vec3 xaxis = vec3( cosYaw, 0.0f, -sinYaw );
51 		vec3 yaxis = vec3( sinYaw * sinPitch, cosPitch, cosYaw * sinPitch );
52 		vec3 zaxis = vec3( sinYaw * cosPitch, -sinPitch, cosPitch * cosYaw );
53 
54 		_viewMatrix.clear( 0.0f );
55 		_viewMatrix[ 0 ] = xaxis.vector ~ -( xaxis * owner.transform.position );
56 		_viewMatrix[ 1 ] = yaxis.vector ~ -( yaxis * owner.transform.position );
57 		_viewMatrix[ 2 ] = zaxis.vector ~ -( zaxis * owner.transform.position );
58 		_viewMatrix[ 3 ] = [ 0, 0, 0, 1 ];
59 
60 		_viewMatrixIsDirty = false;
61 	}
62 }