g4tools  5.4.0
state
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_state
5 #define tools_sg_state
6 
7 #include <string>
8 
9 #include "../lina/rotf" //m_camera_orientation
10 #include "../lina/mat4f"
11 #include "../lina/vec3f"
12 #include "../lina/vec4f"
13 #include "../colorf"
14 
15 #ifdef TOOLS_MEM
16 #include "../mem"
17 #include "../S_STRING"
18 #endif
19 
20 #include "enums"
21 
22 namespace tools {
23 namespace sg {
24 
25 class state {
26 #ifdef TOOLS_MEM
28 #endif
29 public:
31  //must be consistent with tools::sg::viewer::render.
32  :m_ww(0)
33  ,m_wh(0)
34 
35  ,m_GL_DEPTH_TEST(true)
36  ,m_GL_LIGHTING(false)
37  ,m_GL_CULL_FACE(true)
39  ,m_GL_TEXTURE_2D(false)
40  ,m_GL_POINT_SMOOTH(false)
41  ,m_GL_LINE_SMOOTH(false)
42  ,m_GL_BLEND(false)
43 
44  ,m_use_gsto(false)
45 
47  ,m_color(1,1,1,1)
48  ,m_normal(0,0,1)
49 
50  ,m_light(0)
51 
54 
55  ,m_line_width(1)
57  ,m_point_size(1)
58 
59  ,m_camera_ortho(true)
60  ,m_camera_znear(1)
61  ,m_camera_zfar(10)
62  ,m_camera_position(vec3f(0,0,1))
63  ,m_camera_orientation(rotf(vec3f(0,0,1),0))
64  //,m_camera_near_height(0)
65  ,m_camera_lrbt(0,0,0,0)
66  {
67 #ifdef TOOLS_MEM
68  mem::increment(s_class().c_str());
69 #endif
70  //m_proj.set_identity();
71  //m_model.set_identity();
72  }
73  virtual ~state(){
74 #ifdef TOOLS_MEM
75  mem::decrement(s_class().c_str());
76 #endif
77  }
78 public:
79  state(const state& a_from)
80  :m_ww(a_from.m_ww)
81  ,m_wh(a_from.m_wh)
82 
83  ,m_proj(a_from.m_proj)
84  ,m_model(a_from.m_model)
85 
93  ,m_GL_BLEND(a_from.m_GL_BLEND)
94 
95  ,m_use_gsto(a_from.m_use_gsto)
96 
97  ,m_winding(a_from.m_winding)
98  ,m_color(a_from.m_color)
99  ,m_normal(a_from.m_normal)
100 
101  ,m_light(a_from.m_light)
102 
103  ,m_draw_type(a_from.m_draw_type)
105 
106  ,m_line_width(a_from.m_line_width)
108  ,m_point_size(a_from.m_point_size)
109 
115  //,m_camera_near_height(a_from.m_camera_near_height)
117  {
118 #ifdef TOOLS_MEM
119  mem::increment(s_class().c_str());
120 #endif
121  }
122  state& operator=(const state& a_from){
123  m_ww = a_from.m_ww;
124  m_wh = a_from.m_wh;
125 
126  m_proj = a_from.m_proj;
127  m_model = a_from.m_model;
128 
130  m_GL_LIGHTING = a_from.m_GL_LIGHTING;
136  m_GL_BLEND = a_from.m_GL_BLEND;
137 
138  m_use_gsto = a_from.m_use_gsto;
139 
140  m_winding = a_from.m_winding;
141  m_color = a_from.m_color;
142  m_normal = a_from.m_normal;
143 
144  m_light = a_from.m_light;
145 
146  m_draw_type = a_from.m_draw_type;
147  m_shade_model = a_from.m_shade_model;
148 
149  m_line_width = a_from.m_line_width;
151  m_point_size = a_from.m_point_size;
152 
155  m_camera_zfar = a_from.m_camera_zfar;
158  //m_camera_near_height = a_from.m_camera_near_height;
159  m_camera_lrbt = a_from.m_camera_lrbt;
160 
161  return *this;
162  }
163 public:
164  bool project_point(float& a_x,float& a_y,float& a_z,float& a_w) const {
165  a_w = 1;
166  m_model.mul_4f(a_x,a_y,a_z,a_w);
167  m_proj.mul_4f(a_x,a_y,a_z,a_w);
168  if(a_w==0.0F) return false;
169  a_x /= a_w;
170  a_y /= a_w;
171  a_z /= a_w;
172  return true;
173  }
174 
175  void screen2ndc(int a_x,int a_y, //signed because of wall.
176  float& a_wcx,float& a_wcy,float& a_wcz,float& a_wcw) const {
177  // a proj point in near plane has (z,w) :
178  // ortho -1,1 -> z/w = -1 and xy in [-1,1][-1,1]
179  // persp -n,n -> z/w = -1 and xy in [-n,n][-n,n]
180 
181  a_wcx = 2*(float(a_x)/float(m_ww)-0.5f); //in [-1,1]
182  a_wcy = 2*(float(a_y)/float(m_wh)-0.5f);
183  a_wcz = -1;
184 
185  if(m_camera_ortho) {
186  a_wcw = 1;
187  } else {
188  float t = m_camera_znear;
189  a_wcx *= t;
190  a_wcy *= t;
191  a_wcz *= t;
192  a_wcw = t;
193  }
194  }
195 
196  bool screen2wc(int a_x,int a_y, //signed because of wall.
197  float& a_wcx,float& a_wcy,float& a_wcz) const {
198  mat4f mtx = m_proj;
199  mtx.mul_mtx(m_model);
200  mat4f inv;
201  if(!mtx.invert(inv)) {a_wcx = 0;a_wcy = 0;a_wcz = 0;return false;}
202  float w;
203  screen2ndc(a_x,a_y,a_wcx,a_wcy,a_wcz,w);
204  inv.mul_4f(a_wcx,a_wcy,a_wcz,w);
205  if(w==0.0F) return false;
206  a_wcx /= w;
207  a_wcy /= w;
208  a_wcz /= w;
209  return true;
210  }
211  bool screen2pwc(int a_x,int a_y, //signed because of wall.
212  float& a_wcx,float& a_wcy,float& a_wcz) const {
213  mat4f mtx = m_proj;
214  //mtx.mul_mtx(m_model);
215  mat4f inv;
216  if(!mtx.invert(inv)) {a_wcx = 0;a_wcy = 0;a_wcz = 0;return false;}
217  float w;
218  screen2ndc(a_x,a_y,a_wcx,a_wcy,a_wcz,w);
219  inv.mul_4f(a_wcx,a_wcy,a_wcz,w);
220  if(w==0.0F) return false;
221  a_wcx /= w;
222  a_wcy /= w;
223  a_wcz /= w;
224  return true;
225  }
226  void camera_proj_only(mat4f& a_mtx) const {
227  float l = m_camera_lrbt[0];
228  float r = m_camera_lrbt[1];
229  float b = m_camera_lrbt[2];
230  float t = m_camera_lrbt[3];
231  float n = m_camera_znear;
232  float f = m_camera_zfar;
233  if(m_camera_ortho) {
234  a_mtx.set_ortho(l,r,b,t,n,f);
235  } else {
236  a_mtx.set_frustum(l,r,b,t,n,f);
237  }
238  }
239 
240 public:
241  unsigned int m_ww; //window width
242  unsigned int m_wh; //window height
243 
246 
255 
257 
261 
262  unsigned int m_light;
263 
266 
268  unsigned short m_line_pattern;
270 
271  //camera (see base_camera::set_state()) :
276  rotf m_camera_orientation; //used by head_light
277  //float m_camera_near_height;
279 };
280 
281 }}
282 
283 #endif
tools::sg::state::camera_proj_only
void camera_proj_only(mat4f &a_mtx) const
Definition: state:226
tools::sg::state::state
state(const state &a_from)
Definition: state:79
tools::sg::state::screen2pwc
bool screen2pwc(int a_x, int a_y, float &a_wcx, float &a_wcy, float &a_wcz) const
Definition: state:211
tools::sg::state::~state
virtual ~state()
Definition: state:73
tools::sg::state
Definition: state:25
tools::sg::state::m_GL_LIGHTING
bool m_GL_LIGHTING
Definition: state:248
tools::sg::state::m_GL_DEPTH_TEST
bool m_GL_DEPTH_TEST
Definition: state:247
tools::rotf
Definition: rotf:16
tools::sg::winding_ccw
@ winding_ccw
Definition: enums:105
tools::sg::state::m_point_size
float m_point_size
Definition: state:269
tools::colorf
Definition: colorf:11
tools::sg::state::m_GL_POLYGON_OFFSET_FILL
bool m_GL_POLYGON_OFFSET_FILL
Definition: state:250
tools::sg::shade_flat
@ shade_flat
Definition: enums:198
tools::sg::state::m_camera_znear
float m_camera_znear
Definition: state:273
tools::sg::state::m_GL_CULL_FACE
bool m_GL_CULL_FACE
Definition: state:249
tools::sg::state::m_normal
vec3f m_normal
Definition: state:260
tools::mat4::set_frustum
void set_frustum(const T &a_l, const T &a_r, const T &a_b, const T &a_t, const T &a_n, const T &a_f)
Definition: mat4:97
tools::sg::shade_type
shade_type
Definition: enums:197
tools::sg::state::m_winding
winding_type m_winding
Definition: state:258
tools::sg::state::m_camera_lrbt
vec4f m_camera_lrbt
Definition: state:278
tools::sg::state::m_GL_TEXTURE_2D
bool m_GL_TEXTURE_2D
Definition: state:251
tools::sg::state::m_GL_POINT_SMOOTH
bool m_GL_POINT_SMOOTH
Definition: state:252
tools::mat4f::mul_4f
void mul_4f(float &a_x, float &a_y, float &a_z, float &a_w) const
Definition: mat4f:61
TOOLS_SCLASS
#define TOOLS_SCLASS(a_name)
Definition: S_STRING:41
tools::sg::state::m_camera_orientation
rotf m_camera_orientation
Definition: state:276
tools::sg::state::m_light
unsigned int m_light
Definition: state:262
tools::sg::state::m_wh
unsigned int m_wh
Definition: state:242
tools::sg::state::m_use_gsto
bool m_use_gsto
Definition: state:256
tools::sg::draw_filled
@ draw_filled
Definition: enums:193
tools::sg::state::m_camera_ortho
bool m_camera_ortho
Definition: state:272
tools::sg::state::m_GL_LINE_SMOOTH
bool m_GL_LINE_SMOOTH
Definition: state:253
tools::sg::state::operator=
state & operator=(const state &a_from)
Definition: state:122
tools::sg::draw_type
draw_type
Definition: enums:190
tools::sg::state::project_point
bool project_point(float &a_x, float &a_y, float &a_z, float &a_w) const
Definition: state:164
tools::mat4::set_ortho
void set_ortho(const T &a_l, const T &a_r, const T &a_b, const T &a_t, const T &a_n, const T &a_f)
Definition: mat4:64
tools::vec3f
Definition: vec3f:13
tools::sg::state::state
state()
Definition: state:30
tools::sg::state::m_camera_zfar
float m_camera_zfar
Definition: state:274
tools
inlined C code : ///////////////////////////////////
Definition: aida_ntuple:26
tools::sg::state::m_shade_model
shade_type m_shade_model
Definition: state:265
tools::sg::state::m_proj
mat4f m_proj
Definition: state:244
tools::sg::state::screen2ndc
void screen2ndc(int a_x, int a_y, float &a_wcx, float &a_wcy, float &a_wcz, float &a_wcw) const
Definition: state:175
tools::sg::winding_type
winding_type
Definition: enums:104
enums
tools::sg::state::m_model
mat4f m_model
Definition: state:245
tools::mat4f
Definition: mat4f:12
tools::sg::state::m_ww
unsigned int m_ww
Definition: state:241
tools::sg::line_solid
@ line_solid
Definition: enums:11
tools::sg::state::m_camera_position
vec3f m_camera_position
Definition: state:275
tools::sg::state::m_line_pattern
unsigned short m_line_pattern
Definition: state:268
tools::sg::state::m_line_width
float m_line_width
Definition: state:267
tools::sg::state::m_color
colorf m_color
Definition: state:259
tools::sg::state::screen2wc
bool screen2wc(int a_x, int a_y, float &a_wcx, float &a_wcy, float &a_wcz) const
Definition: state:196
tools::sg::state::m_GL_BLEND
bool m_GL_BLEND
Definition: state:254
tools::sg::state::m_draw_type
draw_type m_draw_type
Definition: state:264
tools::vec4f
Definition: vec4f:13