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_action 5 #define tools_sg_matrix_action 6 7 #include "win_action" 8 9 #include "../lina/mat4f" 10 #include "states" 11 12 namespace tools { 13 namespace sg { 14 15 class matrix_action : public win_action,public states { 16 TOOLS_ACTION(matrix_action,tools::sg::matrix_action,win_action) 17 public: 18 matrix_action(std::ostream& a_out,unsigned int a_ww,unsigned int a_wh) 19 :parent(a_out,a_ww,a_wh) 20 ,states(a_ww,a_wh) 21 ,m_cur(0) 22 ,m_landscape(true) 23 { 24 m_projs.resize(5); 25 m_models.resize(5); 26 reset(); 27 m_identity.set_identity(); 28 } 29 virtual ~matrix_action(){} 30 public: 31 matrix_action(const matrix_action& a_from) 32 :parent(a_from) 33 ,states(a_from) 34 ,m_projs(a_from.m_projs) 35 ,m_models(a_from.m_models) 36 ,m_cur(a_from.m_cur) 37 ,m_landscape(a_from.m_landscape) 38 { 39 m_identity.set_identity(); 40 } 41 matrix_action& operator=(const matrix_action& a_from){ 42 parent::operator=(a_from); 43 states::operator=(a_from); 44 m_projs = a_from.m_projs; 45 m_models = a_from.m_models; 46 m_cur = a_from.m_cur; 47 m_landscape = a_from.m_landscape; 48 return *this; 49 } 50 public: 51 void push_matrices() { 52 if((m_cur+1)>=(int)m_projs.size()) { 53 m_projs.resize(m_projs.size()+5); 54 m_models.resize(m_models.size()+5); 55 } 56 m_cur++; 57 m_projs[m_cur].set_matrix(m_projs[m_cur-1]); 58 m_models[m_cur].set_matrix(m_models[m_cur-1]); 59 //OPTIMIZATION : in fact the two lines below are not needed ! 60 // m_state.m_proj = m_projs[m_cur]; 61 // m_state.m_model = m_models[m_cur]; 62 } 63 64 //WARNING : in the three below methods, there is no 65 // protection against m_cur<0 being zero here. 66 void pop_matrices() { 67 m_cur--; 68 //OPTIMIZATION : in fact the two lines below are not needed ! 69 // m_state.m_proj = m_projs[m_cur]; 70 // m_state.m_model = m_models[m_cur]; 71 } 72 mat4f& projection_matrix() {return m_projs[m_cur];} 73 mat4f& model_matrix() {return m_models[m_cur];} 74 75 bool end() const {return m_cur==0?true:false;} 76 int cur() const {return m_cur;} 77 78 bool project_point(float& a_x,float& a_y,float& a_z,float& a_w) { 79 a_w = 1; 80 model_matrix().mul_4f(a_x,a_y,a_z,a_w); 81 projection_matrix().mul_4f(a_x,a_y,a_z,a_w); 82 if(a_w==0.0F) return false; 83 a_x /= a_w; 84 a_y /= a_w; 85 a_z /= a_w; 86 return true; 87 } 88 89 // bool project_normal(float& a_x,float& a_y,float& a_z) { 90 // model_matrix().mul_dir_3f(a_x,a_y,a_z); 91 // projection_matrix().mul_dir_3f(a_x,a_y,a_z); 92 // return true; 93 // } 94 95 void model_point(float& a_x,float& a_y,float& a_z,float& a_w) { 96 a_w = 1; 97 model_matrix().mul_4f(a_x,a_y,a_z,a_w); 98 } 99 100 // for marker rendering : 101 void projected_origin(float& a_x,float& a_y,float& a_z) { 102 a_x = 0; 103 a_y = 0; 104 a_z = 0; 105 float w; 106 project_point(a_x,a_y,a_z,w); //if render : in [-1,1][-1,1] 107 } 108 109 protected: 110 void reset() { 111 m_cur = 0; 112 if(m_landscape) { 113 m_projs[m_cur].set_identity(); 114 } else { 115 m_projs[m_cur].set_matrix(0,-1,0,0, 116 1, 0,0,0, 117 0, 0,1,0, 118 0, 0,0,1); 119 } 120 m_models[m_cur].set_identity(); 121 sg::state& _state = state(); 122 _state.m_proj = m_projs[m_cur]; 123 _state.m_model = m_models[m_cur]; 124 } 125 protected: 126 std::vector<mat4f> m_projs; 127 std::vector<mat4f> m_models; 128 int m_cur; 129 mat4f m_identity; 130 private: 131 bool m_landscape; //false not yet ready. 132 }; 133 134 }} 135 136 #endif