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