Geant4 Cross Reference |
1 // Copyright (C) 2010, Guy Barrand. All rights reserved. 2 // See the file tools.license for terms. 3 4 #ifndef tools_sg_matrix 5 #define tools_sg_matrix 6 7 #include "node" 8 #include "sf_mat4f" 9 #include "../lina/mat4f" 10 #include "render_action" 11 #include "pick_action" 12 #include "bbox_action" 13 #include "event_action" 14 #include "visible_action" 15 16 namespace tools { 17 namespace sg { 18 19 class matrix : public node { 20 TOOLS_NODE(matrix,tools::sg::matrix,node) 21 public: 22 sf_mat4f mtx; 23 public: 24 virtual const desc_fields& node_desc_fields() const { 25 TOOLS_FIELD_DESC_NODE_CLASS(tools::sg::matrix) 26 static const desc_fields s_v(parent::node_desc_fields(),1, //WARNING : take care of count. 27 TOOLS_ARG_FIELD_DESC(mtx) 28 ); 29 return s_v; 30 } 31 private: 32 void add_fields(){ 33 add_field(&mtx); 34 } 35 public: 36 virtual void render(render_action& a_action) { 37 a_action.model_matrix().mul_mtx(mtx.value(),m_tmp); 38 a_action.state().m_model = a_action.model_matrix(); 39 a_action.load_model_matrix(a_action.model_matrix()); 40 } 41 virtual void pick(pick_action& a_action) { 42 a_action.model_matrix().mul_mtx(mtx.value(),m_tmp); 43 a_action.state().m_model = a_action.model_matrix(); 44 } 45 virtual void bbox(bbox_action& a_action) { 46 a_action.model_matrix().mul_mtx(mtx.value(),m_tmp); 47 a_action.state().m_model = a_action.model_matrix(); 48 } 49 virtual void event(event_action& a_action) { 50 a_action.model_matrix().mul_mtx(mtx.value(),m_tmp); 51 a_action.state().m_model = a_action.model_matrix(); 52 } 53 virtual void get_matrix(get_matrix_action& a_action) { 54 a_action.model_matrix().mul_mtx(mtx.value(),m_tmp); 55 a_action.state().m_model = a_action.model_matrix(); 56 } 57 virtual void is_visible(visible_action& a_action) { 58 a_action.model_matrix().mul_mtx(mtx.value(),m_tmp); 59 a_action.state().m_model = a_action.model_matrix(); 60 } 61 public: 62 matrix():parent(),mtx(mat4f()) { 63 #ifdef TOOLS_MEM 64 mem::increment(s_class().c_str()); 65 #endif 66 add_fields(); 67 mtx.set_identity(); 68 } 69 virtual ~matrix(){ 70 #ifdef TOOLS_MEM 71 mem::decrement(s_class().c_str()); 72 #endif 73 } 74 public: 75 matrix(const matrix& a_from):parent(a_from),mtx(a_from.mtx) { 76 #ifdef TOOLS_MEM 77 mem::increment(s_class().c_str()); 78 #endif 79 add_fields(); 80 } 81 matrix& operator=(const matrix& a_from){ 82 parent::operator=(a_from); 83 mtx = a_from.mtx; 84 return *this; 85 } 86 public: 87 // shortcuts : 88 void set_identity() {mtx.set_identity();} 89 90 void set_translate(float a_x,float a_y,float a_z) {mtx.set_translate(a_x,a_y,a_z);} 91 void set_translate(const vec3f& a_v) {mtx.set_translate(a_v);} 92 93 void set_scale(float a_x,float a_y,float a_z) {mtx.set_scale(a_x,a_y,a_z);} 94 void set_scale(float a_s) {mtx.set_scale(a_s);} 95 96 void set_rotate(float a_x,float a_y,float a_z,float a_angle) {mtx.set_rotate(a_x,a_y,a_z,a_angle);} 97 void set_rotate(const vec3f& a_v,float a_angle) {mtx.set_rotate(a_v,a_angle);} 98 bool set_rotate(const vec3f& a_from,const vec3f& a_to) {return mtx.set_rotate(a_from,a_to,m_tmp);} 99 100 void mul_mtx(const mat4f& a_m) {mtx.mul_mtx(a_m,m_tmp);} 101 102 void mul_translate(float a_x,float a_y,float a_z) {mtx.mul_translate(a_x,a_y,a_z);} 103 void mul_translate(const vec3f& a_v) {mtx.mul_translate(a_v);} 104 void mul_scale(float a_x,float a_y,float a_z) {mtx.mul_scale(a_x,a_y,a_z);} 105 void mul_scale(float a_s) {mtx.mul_scale(a_s);} 106 107 void mul_rotate(float a_x,float a_y,float a_z,float a_angle) {mtx.mul_rotate(a_x,a_y,a_z,a_angle);} 108 109 void mul_rotate(const vec3f& a_v,float a_angle) {mtx.mul_rotate(a_v,a_angle);} 110 void mul_rotate(const vec4f& a_v) {mtx.mul_rotate(a_v.v0(),a_v.v1(),a_v.v2(),a_v.v3());} 111 112 void left_mul_rotate(float a_x,float a_y,float a_z,float a_angle) {mtx.left_mul_rotate(a_x,a_y,a_z,a_angle);} 113 114 void left_mul_scale(float a_x,float a_y,float a_z) {mtx.left_mul_scale(a_x,a_y,a_z);} 115 116 void left_mul_translate(float a_x,float a_y,float a_z) {mtx.left_mul_translate(a_x,a_y,a_z);} 117 118 void left_mul_translate(const vec3f& a_v) {mtx.left_mul_translate(a_v);} 119 120 bool mul_rotate(const vec3f& a_from,const vec3f& a_to) {return mtx.mul_rotate(a_from,a_to,m_tmp);} 121 protected: 122 float m_tmp[16]; //OPTIMIZATION 123 }; 124 125 }} 126 127 #endif