Warning, /include/Geant4/tools/sg/tex_rect is written in an unsupported language. File is not indexed.
0001 // Copyright (C) 2010, Guy Barrand. All rights reserved.
0002 // See the file tools.license for terms.
0003
0004 #ifndef tools_sg_tex_rect
0005 #define tools_sg_tex_rect
0006
0007 // node to render an RGB tools::img_byte with ::glTexImage2D.
0008
0009 #include "node"
0010 #include "render_action"
0011 #include "pick_action"
0012 #include "bbox_action"
0013 #include "event_action"
0014 #include "render_manager"
0015 #include "gstos"
0016 #include "base_tex"
0017
0018 #include "../num2s"
0019
0020 namespace tools {
0021 namespace sg {
0022
0023 class tex_rect : public node, public gstos, public base_tex {
0024 TOOLS_NODE_NO_CAST(tex_rect,tools::sg::tex_rect,node)
0025 public:
0026 virtual void* cast(const std::string& a_class) const {
0027 {if(void* p = cmp_cast<tex_rect>(this,a_class)) return p;}
0028 {if(void* p = base_tex::cast(a_class)) return p;}
0029 return parent::cast(a_class);
0030 }
0031 public:
0032 sf<bool> show_border;
0033 sf<float> height;
0034 public:
0035 virtual const desc_fields& node_desc_fields() const {
0036 TOOLS_FIELD_DESC_NODE_CLASS(tools::sg::tex_rect)
0037 static const desc_fields s_v(parent::node_desc_fields(),6, //WARNING : take care of count.
0038 TOOLS_ARG_FIELD_DESC(img),
0039 TOOLS_ARG_FIELD_DESC(back_color),
0040 TOOLS_ARG_FIELD_DESC(expand),
0041 TOOLS_ARG_FIELD_DESC(limit),
0042 TOOLS_ARG_FIELD_DESC(show_border),
0043 TOOLS_ARG_FIELD_DESC(height)
0044 );
0045 return s_v;
0046 }
0047 private:
0048 void add_fields(){
0049 add_field(&img);
0050 add_field(&back_color);
0051 add_field(&expand);
0052 add_field(&limit);
0053 add_field(&show_border);
0054 add_field(&height);
0055 }
0056 public:
0057 virtual void render(render_action& a_action) {
0058 //NOTE : we draw border (show_border is true) and background even if
0059 // gen_texture() failed.
0060
0061 if(touched()) {
0062 update_sg(a_action.out());
0063 reset_touched();
0064 }
0065 if(m_img.is_empty()) {
0066 return;
0067 }
0068
0069 unsigned int _id = get_tex_id(a_action.out(),a_action.render_manager(),m_img,nearest.value());
0070
0071 const state& state = a_action.state();
0072
0073 //image must be 2^n,2^m in size !
0074 // exa : 128x64
0075
0076 f12 xyzs;
0077
0078 if(show_border.value()) {
0079 f12 nms;
0080 _front(xyzs,nms,0.01f);
0081
0082 a_action.color4f(1,0,0,1);
0083 a_action.line_width(4);
0084
0085 a_action.draw_vertex_array(gl::line_loop(),12,xyzs);
0086
0087 //pushes back the filled polygons to avoid z-fighting with lines
0088 a_action.set_polygon_offset(true);
0089
0090 a_action.color4f(state.m_color);
0091 a_action.line_width(state.m_line_width);
0092 }
0093
0094 //draw a back face pointing toward negative z :
0095 {a_action.color4f(back_color.value());
0096 f18 tris,nms;
0097 _tris(tris,nms);
0098 a_action.draw_vertex_normal_array(gl::triangles(),18,tris,nms);
0099 a_action.color4f(state.m_color);}
0100
0101 if(_id) {
0102 f12 nms;
0103 _front(xyzs,nms);
0104 float tcs[8];
0105 set_tcs(tcs);
0106 a_action.draw_vertex_normal_array_texture(gl::triangle_fan(),12,xyzs,nms,_id,tcs);
0107 }
0108 a_action.set_polygon_offset(state.m_GL_POLYGON_OFFSET_FILL);
0109 }
0110 virtual void pick(pick_action& a_action) {
0111 if(touched()) {
0112 update_sg(a_action.out());
0113 reset_touched();
0114 }
0115 if(m_img.is_empty()) return;
0116 f12 xyzs,nms;
0117 _front(xyzs,nms);
0118 a_action.add__primitive(*this,gl::triangle_fan(),12,xyzs,true);
0119 }
0120
0121 virtual void bbox(bbox_action& a_action) {
0122 if(touched()) {
0123 update_sg(a_action.out());
0124 reset_touched();
0125 }
0126 if(m_img.is_empty()) return;
0127 f12 xyzs,nms;
0128 _front(xyzs,nms);
0129 a_action.add_points(12,xyzs);
0130 }
0131 public:
0132 virtual bool intersect_value(std::ostream&,intersect_type,const line<vec3f>& a_line,std::string& a_s) const {
0133 // a_line is in local world coordinate.
0134
0135 const img_byte& _img = img.value();
0136 if(_img.is_empty()) {a_s.clear();return false;}
0137
0138 float aspect = float(_img.width())/float(_img.height());
0139 float h2 = height.value()*0.5f;
0140 float w2 = aspect*h2;
0141
0142 plane<vec3f> plane(vec3f(w2,h2,0),vec3f(-w2,h2,0),vec3f(-w2,-h2,0));
0143 vec3f p;
0144 if(!plane.intersect(a_line,p)) {a_s.clear();return false;}
0145
0146 float imw = (float)_img.width();
0147 float imh = (float)_img.height();
0148
0149 //image coordinates :
0150 int ix = int((imw*p.x()/w2+imw)*0.5f);
0151 int iy = int((imh*p.y()/h2+imh)*0.5f);
0152
0153 //rgb of pixel :
0154 std::vector<unsigned char> pixel;
0155 if((ix<0)||(iy<0)||!_img.pixel(ix,iy,pixel)) {a_s.clear();return false;}
0156
0157 a_s.clear();
0158 for(unsigned int ipix=0;ipix<pixel.size();ipix++) {
0159 if(ipix) a_s += " ";
0160 if(!numas<float>(float(pixel[ipix])/255.0f,a_s)){}
0161 }
0162
0163 return true;
0164 }
0165 public:
0166 tex_rect()
0167 :parent()
0168 ,base_tex()
0169 ,show_border(false)
0170 ,height(1)
0171 {
0172 add_fields();
0173 }
0174 virtual ~tex_rect(){}
0175 public:
0176 tex_rect(const tex_rect& a_from)
0177 :parent(a_from)
0178 ,gstos(a_from)
0179 ,base_tex(a_from)
0180 ,show_border(a_from.show_border)
0181 ,height(a_from.height)
0182 {
0183 add_fields();
0184 }
0185 tex_rect& operator=(const tex_rect& a_from){
0186 parent::operator=(a_from);
0187 gstos::operator=(a_from);
0188 base_tex::operator=(a_from);
0189 if(&a_from==this) return *this;
0190 show_border = a_from.show_border;
0191 height = a_from.height;
0192 return *this;
0193 }
0194 public:
0195
0196 //const img_byte& rendered_img() const {return m_img;}
0197
0198 void rendered_size(std::ostream& a_out,unsigned int& a_w,unsigned int& a_h) {
0199 update_sg(a_out);
0200 reset_touched();
0201 a_w = m_img.width();
0202 a_h = m_img.height();
0203 }
0204
0205 protected:
0206 //virtual //NOTE : virtual for diaporama node. (but warning with clang -g4flags).
0207 void update_sg(std::ostream& a_out) {
0208 clean_gstos(); //must reset for all render_manager.
0209 if(height.value()<=0) {
0210 m_img.make_empty();
0211 return;
0212 }
0213 base_tex::_update_sg_(a_out);
0214 }
0215 protected:
0216
0217 typedef float f12[12];
0218 void _front(f12& front,f12& nms,float a_epsil = 0.0f) { //[12]
0219 float aspect = float(img.value().width())/float(img.value().height());
0220 float h2 = height*0.5f;
0221 float w2 = aspect*h2;
0222
0223 h2 += a_epsil;
0224 w2 += a_epsil;
0225
0226 front[0] = -w2;
0227 front[1] = -h2;
0228 front[2] = 0;
0229
0230 front[3] = w2;
0231 front[4] = -h2;
0232 front[5] = 0;
0233
0234 front[6] = w2;
0235 front[7] = h2;
0236 front[8] = 0;
0237
0238 front[ 9] = -w2;
0239 front[10] = h2;
0240 front[11] = 0;
0241
0242 nms[0] = 0;
0243 nms[1] = 0;
0244 nms[2] = 1;
0245
0246 nms[3] = 0;
0247 nms[4] = 0;
0248 nms[5] = 1;
0249
0250 nms[6] = 0;
0251 nms[7] = 0;
0252 nms[8] = 1;
0253
0254 nms[9] = 0;
0255 nms[10] = 0;
0256 nms[11] = 1;
0257 }
0258
0259 void _back(f12& back) { //[12]
0260 float aspect = float(img.value().width())/float(img.value().height());
0261 float h2 = height*0.5f;
0262 float w2 = aspect*h2;
0263 float d2 = 0;
0264
0265 back[0] = w2;back[ 1] = -h2;back[ 2] = d2;
0266 back[3] = -w2;back[ 4] = -h2;back[ 5] = d2;
0267 back[6] = -w2;back[ 7] = h2;back[ 8] = d2;
0268 back[9] = w2;back[10] = h2;back[11] = d2;
0269 }
0270
0271 typedef float f18[18];
0272 void _tris(f18& tris,f18& nms){
0273 f12 back;
0274 _back(back);
0275
0276 tris[0] = back[0];
0277 tris[1] = back[1];
0278 tris[2] = back[2];
0279
0280 tris[3] = back[3];
0281 tris[4] = back[4];
0282 tris[5] = back[5];
0283
0284 tris[6] = back[6];
0285 tris[7] = back[7];
0286 tris[8] = back[8];
0287 //
0288 tris[9] = back[6];
0289 tris[10] = back[7];
0290 tris[11] = back[8];
0291
0292 tris[12] = back[9];
0293 tris[13] = back[10];
0294 tris[14] = back[11];
0295
0296 tris[15] = back[0];
0297 tris[16] = back[1];
0298 tris[17] = back[2];
0299
0300 ///////////////////// back
0301 nms[0] = 0;
0302 nms[1] = 0;
0303 nms[2] = -1;
0304
0305 nms[3] = 0;
0306 nms[4] = 0;
0307 nms[5] = -1;
0308
0309 nms[6] = 0;
0310 nms[7] = 0;
0311 nms[8] = -1;
0312 //
0313 nms[9] = 0;
0314 nms[10] = 0;
0315 nms[11] = -1;
0316
0317 nms[12] = 0;
0318 nms[13] = 0;
0319 nms[14] = -1;
0320
0321 nms[15] = 0;
0322 nms[16] = 0;
0323 nms[17] = -1;
0324 }
0325 };
0326
0327 }}
0328
0329 #endif