g4tools  5.4.0
matrix_action
Go to the documentation of this file.
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 {
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:
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  }
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  }
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;
130 private:
131  bool m_landscape; //false not yet ready.
132 };
133 
134 }}
135 
136 #endif
tools::sg::matrix_action::m_models
std::vector< mat4f > m_models
Definition: matrix_action:127
tools::sg::matrix_action::model_point
void model_point(float &a_x, float &a_y, float &a_z, float &a_w)
Definition: matrix_action:95
tools::sg::state
Definition: state:25
tools::sg::matrix_action::pop_matrices
void pop_matrices()
Definition: matrix_action:66
tools::sg::matrix_action::cur
int cur() const
Definition: matrix_action:76
tools::sg::matrix_action::model_matrix
mat4f & model_matrix()
Definition: matrix_action:73
tools::sg::matrix_action::reset
void reset()
Definition: matrix_action:110
tools::sg::matrix_action::m_projs
std::vector< mat4f > m_projs
Definition: matrix_action:126
tools::sg::win_action
Definition: win_action:12
tools::sg::matrix_action
Definition: matrix_action:15
tools::sg::matrix_action::operator=
matrix_action & operator=(const matrix_action &a_from)
Definition: matrix_action:41
tools::sg::matrix_action::end
bool end() const
Definition: matrix_action:75
win_action
tools::sg::states
Definition: states:14
tools::mat4f::mul_4f
void mul_4f(float &a_x, float &a_y, float &a_z, float &a_w) const
Definition: mat4f:61
tools::sg::matrix_action::push_matrices
void push_matrices()
Definition: matrix_action:51
TOOLS_ACTION
#define TOOLS_ACTION(a__class, a__sclass, a__parent)
Definition: action:60
tools::sg::matrix_action::matrix_action
matrix_action(const matrix_action &a_from)
Definition: matrix_action:31
tools::sg::matrix_action::~matrix_action
virtual ~matrix_action()
Definition: matrix_action:29
states
tools::sg::matrix_action::project_point
bool project_point(float &a_x, float &a_y, float &a_z, float &a_w)
Definition: matrix_action:78
tools
inlined C code : ///////////////////////////////////
Definition: aida_ntuple:26
tools::sg::state::m_proj
mat4f m_proj
Definition: state:244
tools::sg::matrix_action::m_identity
mat4f m_identity
Definition: matrix_action:129
tools::sg::matrix_action::projected_origin
void projected_origin(float &a_x, float &a_y, float &a_z)
Definition: matrix_action:101
tools::sg::state::m_model
mat4f m_model
Definition: state:245
tools::mat4f
Definition: mat4f:12
tools::sg::matrix_action::m_cur
int m_cur
Definition: matrix_action:128
tools::sg::matrix_action::matrix_action
matrix_action(std::ostream &a_out, unsigned int a_ww, unsigned int a_wh)
Definition: matrix_action:18
tools::sg::states::state
const sg::state & state() const
Definition: states:76
tools::sg::matrix_action::projection_matrix
mat4f & projection_matrix()
Definition: matrix_action:72
tools::sg::states::operator=
states & operator=(const states &a_from)
Definition: states:53