1 module sily.dlib.transform; 2 3 import dlib.math.matrix; 4 import dlib.math.vector; 5 import dlib.math.transformation; 6 import dlib.math.quaternion; 7 import dlib.math.utils; 8 9 class Transform { 10 private Matrix4f _transformMat; 11 12 private vec3 _position; 13 private vec3 _scaling; 14 private Quaternionf _rotation; 15 16 this() { 17 _transformMat = Matrix4f().identity(); 18 19 _position = vec3(0, 0, 0); 20 _scaling = vec3(1, 1, 1); 21 _rotation = Quaternionf().identity(); 22 } 23 24 public void parent(Transform tr) { 25 26 } 27 28 public void update() { 29 // always scale -> rotation -> translation 30 // _transformMat = _scaleMat * _rotationMat * _translationMat; 31 } 32 33 public void position(vec3 pos) { 34 _position = pos; 35 } 36 37 public vec3 position() { 38 return _position; 39 } 40 41 public void scaling(vec3 p_scale) { 42 _scaling = p_scale; 43 } 44 45 public vec3 scaling() { 46 return _scaling; 47 } 48 49 public void rotation(Quaternionf rot) { 50 _rotation = rot; 51 } 52 53 public Quaternionf rotation() { 54 return _rotation; 55 } 56 57 // public Matrix4f generateMat4() { 58 // // _translationMat = translationMatrix(_position); 59 // // _rotationMat = rotationMatrix(Axis.y, 0.0f); 60 // // _transformMat = _translationMat * _scaleMat * _rotationMat; 61 // // return _transformMat; 62 // } 63 64 void updateTransformation() { 65 // prevTransformation = transformation; 66 67 _transformMat = 68 translationMatrix(_position) * 69 _rotation.toMatrix4x4 * 70 scaleMatrix(_scaling); 71 72 // updateAbsoluteTransformation(); 73 } 74 75 vec3 direction() { 76 return forward(_transformMat); 77 } 78 79 // vec3 right() { 80 // return right(_transformMat); 81 // } 82 83 // vec3 up() { 84 // return right(_transformMat); 85 // } 86 87 public final void translate(vec3 offset) { 88 _position += offset; 89 } 90 91 public final void scale(vec3 offset) { 92 // transform.position = transform.position + offset; 93 } 94 95 /** 96 * Set rotation 97 * Params: 98 * v = Vector (degrees) 99 */ 100 void setRotation(vec3 v) { 101 _rotation = 102 rotationQuaternion!float(Axis.x, degtorad(v.x)) * 103 rotationQuaternion!float(Axis.y, degtorad(v.y)) * 104 rotationQuaternion!float(Axis.z, degtorad(v.z)); 105 } 106 107 /** 108 * Set rotation 109 * Params: 110 * x = x angle degrees 111 * y = y angle degrees 112 * z = z angle degrees 113 */ 114 void setRotation(float x, float y, float z) { 115 setRotation(vec3(x, y, z)); 116 } 117 118 /** 119 * Rotate by Vector 120 * Params: 121 * v = Vector (degrees) 122 */ 123 void rotate(vec3 v) { 124 auto r = 125 rotationQuaternion!float(Axis.x, degtorad(v.x)) * 126 rotationQuaternion!float(Axis.y, degtorad(v.y)) * 127 rotationQuaternion!float(Axis.z, degtorad(v.z)); 128 _rotation *= r; 129 } 130 131 /** 132 * Rotate by Vector 133 * Params: 134 * x = x angle degrees 135 * y = y angle degrees 136 * z = z angle degrees 137 */ 138 void rotate(float x, float y, float z) { 139 rotate(vec3(x, y, z)); 140 } 141 142 /** 143 * Look up/down 144 * Params: 145 * angle = angle in degrees 146 */ 147 void pitch(float angle) { 148 _rotation *= rotationQuaternion!float(Axis.x, degtorad(angle)); 149 } 150 151 /** 152 * Look left/right 153 * Params: 154 * angle = angle in degrees 155 */ 156 void turn(float angle) { 157 _rotation *= rotationQuaternion!float(Axis.y, degtorad(angle)); 158 } 159 160 /** 161 * Tilt left/right 162 * Params: 163 * angle = angle in degrees 164 */ 165 void roll(float angle) { 166 _rotation *= rotationQuaternion!float(Axis.z, degtorad(angle)); 167 } 168 169 // TODO do full transform class 170 // LINK https://github.com/gecko0307/dagon/blob/master/src/dagon/graphics/entity.d 171 // LINK https://learnopengl.com/Getting-started/Transformations 172 }