Back to home page

EIC code displayed by LXR

 
 

    


Warning, /include/Geant4/tools/sg/base_tex 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_base_tex
0005 #define tools_sg_base_tex
0006 
0007 #include "../platform"
0008 #include "../img"
0009 #include "../lina/line"
0010 #include "../lina/vec3f"
0011 
0012 #include "sf_vec"
0013 #include "sf_img"
0014 #include "../colorfs"
0015 
0016 namespace tools {
0017 namespace sg {
0018 
0019 class base_tex {
0020 public:
0021   TOOLS_SCLASS(tools::sg::base_tex)
0022 public:
0023   virtual void* cast(const std::string& a_class) const {
0024     if(void* p = cmp_cast<base_tex>(this,a_class)) return p;
0025     return 0;
0026   }
0027 public:
0028   sf_img<byte> img;
0029   sf_vec<colorf,float> back_color;
0030   sf<bool> expand;
0031   sf<unsigned int> limit;
0032   sf<bool> nearest; //for glTexParameteri. See exlib/sg/GL_manager. default=true for astro images.
0033 public:
0034   enum intersect_type {
0035     intersect_down,
0036     intersect_move,
0037     intersect_up
0038   };
0039   virtual bool intersect_value(std::ostream&,intersect_type a_type,const line<vec3f>& a_line,std::string& a_s) const = 0;
0040 public:
0041   base_tex()
0042   :img(img_byte())
0043   ,back_color(colorf_white())
0044   ,expand(false)
0045   ,limit(device::tex_mem_limit()) //OpenGL-ES glTex limitation.
0046   ,nearest(true)
0047   ,m_img()
0048   {}
0049   virtual ~base_tex(){}
0050 public:
0051   base_tex(const base_tex& a_from)
0052   :img(a_from.img)
0053   ,back_color(a_from.back_color)
0054   ,expand(a_from.expand)
0055   ,limit(a_from.limit)
0056   ,nearest(a_from.nearest)
0057   ,m_img()
0058   {}
0059   base_tex& operator=(const base_tex& a_from){
0060     img = a_from.img;
0061     back_color = a_from.back_color;
0062     expand = a_from.expand;
0063     limit = a_from.limit;
0064     nearest = a_from.nearest;
0065     m_img.make_empty();
0066     return *this;
0067   }
0068 protected:
0069   void _update_sg_(std::ostream& a_out) {
0070     //clean_texs(); //must reset for all render_manager.
0071 
0072     const img_byte& _img = img.value();
0073 
0074    //::printf("debug : base_tex::_update_sg : size = %d, w = %d, h = %d, bpp %d\n",
0075    //    _img.size(),_img.width(),_img.height(),_img.bpp());
0076 
0077 
0078     if(_img.is_empty()) {
0079       m_img.make_empty();
0080       return;
0081     }
0082 
0083     unsigned int bpp = _img.bpp();
0084     if((bpp!=1)&&(bpp!=3)&&(bpp!=4)) {
0085       a_out << "tools::sg::tex_rect::update_sg :"
0086             << " bpp " << bpp << " not handled."
0087             << std::endl;
0088       m_img.make_empty();
0089       return;
0090     }
0091 
0092     //a_out << "debug : tools::sg::tex_rect::update_sg :"
0093     //      << " this " << tools::p2s(this)
0094     //      << std::endl;
0095 
0096     // image must be power of two in width and height.
0097 
0098     const colorf& bc = back_color.value();
0099     //::printf("debug : back_color %g %g %g %g\n",bc.r(),bc.g(),bc.b(),bc.a());
0100 
0101     byte pixel[4];
0102     pixel[0] = bc.ruchar();
0103     pixel[1] = bc.guchar();
0104     pixel[2] = bc.buchar();
0105     pixel[3] = bc.auchar();
0106 
0107     //dump(a_out,"debug : 0000 :",_img);
0108 
0109     if((back_color.value().a()!=1)&&(bpp!=4)) {
0110       //transparent background.
0111 
0112       //NOTE : the node must be rendered after the "behind nodes" so that
0113       //       transparency be taken into account for the "behind nodes".
0114 
0115       img_byte img4;
0116       if(!_img.rgb2rgba(img4,255)){
0117         a_out << "tools::sg::tex_rect::update_sg :"
0118               << " rgb2rgba failed."
0119               << std::endl;
0120         m_img.make_empty();
0121         return;
0122       }
0123 
0124       if(!img4.to_texture(expand.value(),pixel,m_img)){
0125         a_out << "tools::sg::tex_rect::update_sg :"
0126               << " problem with tools::tex_rect::to_texture."
0127               << std::endl;
0128         m_img.make_empty();
0129         return;
0130       }
0131 
0132     } else {
0133       if(!_img.to_texture(expand.value(),pixel,m_img)){
0134         a_out << "tools::sg::tex_rect::update_sg :"
0135               << " problem with tools::tex_rect::to_texture."
0136               << std::endl;
0137         m_img.make_empty();
0138         return;
0139       }
0140     }
0141 
0142   //a_out << "debug : limit : 000 : " << limit.value() << std::endl;
0143     if(limit.value()) {
0144       unsigned int tw = m_img.width();
0145       unsigned int th = m_img.height();
0146       if((tw*th*m_img.bpp())>limit.value()) {
0147         //a_out << "debug : trunc " << (tw*th) << std::endl;
0148         unsigned int fac = 2;
0149         while(true) {
0150           unsigned int pw = tw/fac;
0151           unsigned int ph = th/fac;
0152           if((pw*ph)<limit.value()) {
0153             unsigned int sx = (tw-pw)/2;
0154             unsigned int sy = (th-ph)/2;
0155 
0156             img_byte part;
0157             if(!m_img.get_part(sx,sy,pw,ph,part)) {
0158               m_img.make_empty();
0159               return;
0160             }
0161           //a_out << "debug : base_tex : img.get_part due to limit (" << limit.value() << ")." << std::endl;
0162             m_img = part;
0163             break;
0164           }
0165           fac *= 2;
0166         }
0167       }
0168     }
0169     //dump_not_null(a_out,"debug : base_tex::_update_sg_ :",m_img);
0170 
0171   }
0172 
0173   static void dump(std::ostream& a_out,const std::string& a_cmt,const img_byte& a_img) {
0174     if(a_cmt.size()) a_out << a_cmt << std::endl;
0175     a_out << " width " << a_img.width()
0176           << " height " << a_img.height()
0177           << " bpp " << a_img.bpp()
0178           << std::endl;
0179   }
0180 
0181   static void dump_not_null(std::ostream& a_out,const std::string& a_cmt,const img_byte& a_img) {
0182     if(a_cmt.size()) a_out << a_cmt << std::endl;
0183     unsigned int w = a_img.width();
0184     unsigned int h = a_img.height();
0185     unsigned int n = a_img.bpp();
0186     a_out << "img_byte : width " << w << " height " << h << " bpp " << n << std::endl;
0187     byte* pos = (byte*)a_img.buffer();
0188     if(n==3) {
0189       byte r,g,b;
0190       for(unsigned int j=0;j<h;j++) {
0191         for(unsigned int i=0;i<w;i++) {
0192           r = *pos;pos++;
0193           g = *pos;pos++;
0194           b = *pos;pos++;
0195           if(r||g||b)
0196             a_out << " " << i << " " << j
0197                   << " : " << (unsigned int)r << " " << (unsigned int)g << " " << (unsigned int)b
0198                   << std::endl;
0199         }
0200       }
0201     }
0202   }
0203 
0204   void set_tcs(float a_tcs[8]) {
0205     const img_byte& _img = img.value();
0206 
0207     a_tcs[0] = 0;a_tcs[1] = 0;
0208     a_tcs[2] = 1;a_tcs[3] = 0;
0209     a_tcs[4] = 1;a_tcs[5] = 1;
0210     a_tcs[6] = 0;a_tcs[7] = 1;
0211 
0212     float ax = 1;
0213     float bx = 0;
0214     float ay = 1;
0215     float by = 0;
0216    {unsigned int iw = _img.width();
0217     unsigned int ih = _img.height();
0218     unsigned int rw = m_img.width();
0219     unsigned int rh = m_img.height();
0220     if(rw>iw) {
0221       float part = float(iw)/float(rw);
0222       ax = part;
0223       bx = 0.5f*(1-part);
0224     }
0225     if(rh>ih) {
0226       float part = float(ih)/float(rh);
0227       ay = part;
0228       by = 0.5f*(1-part);
0229     }}
0230 
0231    {unsigned int num = 12/3;
0232     for(unsigned int index=0;index<num;index++) {
0233       a_tcs[2*index]   = ax*a_tcs[2*index]  +bx;
0234       a_tcs[2*index+1] = ay*a_tcs[2*index+1]+by;
0235     }}
0236 
0237   }
0238 protected:
0239   img_byte m_img;
0240 };
0241 
0242 }}
0243 
0244 #endif