1 /+ 2 The MIT License (MIT) 3 4 Copyright (c) <2013> <Oleg Butko (deviator), Anton Akzhigitov (Akzwar)> 5 6 Permission is hereby granted, free of charge, to any person obtaining a copy 7 of this software and associated documentation files (the "Software"), to deal 8 in the Software without restriction, including without limitation the rights 9 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 copies of the Software, and to permit persons to whom the Software is 11 furnished to do so, subject to the following conditions: 12 13 The above copyright notice and this permission notice shall be included in 14 all copies or substantial portions of the Software. 15 16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 THE SOFTWARE. 23 +/ 24 25 module des.space.transform; 26 27 public import des.math.linear.vector; 28 public import des.math.linear.matrix; 29 30 /// 31 interface Transform 32 { 33 /// 34 mat4 matrix() @property const; 35 36 /// 37 protected final static mat4 getMatrix( const(Transform) tr ) 38 { 39 if( tr !is null ) 40 return tr.matrix; 41 return mat4.diag(1); 42 } 43 } 44 45 /// 46 class SimpleTransform : Transform 47 { 48 protected: 49 mat4 mtr; /// 50 51 public: 52 @property 53 { 54 /// 55 mat4 matrix() const { return mtr; } 56 /// 57 void matrix( in mat4 m ) { mtr = m; } 58 } 59 } 60 61 /// 62 class TransformList : Transform 63 { 64 Transform[] list; /// 65 enum Order { DIRECT, REVERSE } 66 Order order = Order.DIRECT; /// 67 68 /// 69 @property mat4 matrix() const 70 { 71 mat4 buf; 72 if( order == Order.DIRECT ) 73 foreach( tr; list ) 74 buf *= tr.matrix; 75 else 76 foreach_reverse( tr; list ) 77 buf *= tr.matrix; 78 return buf; 79 } 80 } 81 82 /// 83 class CachedTransform : Transform 84 { 85 protected: 86 mat4 mtr; /// 87 Transform transform_source; /// 88 89 public: 90 91 /// 92 this( Transform ntr ) { setTransform( ntr ); } 93 94 /// 95 void setTransform( Transform ntr ) 96 { 97 transform_source = ntr; 98 recalc(); 99 } 100 101 /// 102 void recalc() 103 { 104 if( transform_source !is null ) 105 mtr = transform_source.matrix; 106 else mtr = mat4.diag(1); 107 } 108 109 /// 110 @property mat4 matrix() const { return mtr; } 111 }