Geant4 Cross Reference |
1 // Copyright (C) 2010, Guy Barrand. All rights reserved. 2 // See the file tools.license for terms. 3 4 #ifndef tools_sg_gstos 5 #define tools_sg_gstos 6 7 #include "render_manager" 8 #include <vector> 9 #include <ostream> 10 11 //#define TOOLS_SG_GSTOS_DEBUG 12 13 namespace tools { 14 namespace sg { 15 16 class gstos { 17 protected: 18 gstos() {} 19 virtual ~gstos() {clean_gstos();} 20 protected: 21 gstos(const gstos&) {} 22 gstos& operator=(const gstos& a_from) { 23 if(&a_from==this) return *this; 24 clean_gstos(); 25 return *this; 26 } 27 public: 28 size_t num_gstos() const {return m_gstos.size();} 29 protected: 30 unsigned int get_tex_id(std::ostream& a_out,render_manager& a_mgr,const img_byte& a_img,bool a_NEAREST) { 31 unsigned int _id = _find(&a_mgr); 32 if(_id && !a_mgr.is_gsto_id_valid(_id)){ 33 #ifdef TOOLS_SG_GSTOS_DEBUG 34 a_out << "debug : tools::sg::gstos::get_tex_id : " << _id << " not valid." << std::endl; 35 #endif 36 clean_gstos(&a_mgr); 37 _id = 0; 38 } 39 if(!_id) { 40 #ifdef TOOLS_SG_GSTOS_DEBUG 41 a_out << "debug : tools::sg::gstos::get_tex_id : create_texture ... " << std::endl; 42 #endif 43 _id = a_mgr.create_texture(a_img,a_NEAREST); 44 #ifdef TOOLS_SG_GSTOS_DEBUG 45 a_out << "debug : tools::sg::gstos::get_tex_id : create_texture : _id " << _id << "." << std::endl; 46 #endif 47 if(!_id) { 48 a_out << "tools::sg::gstos::get_tex_id :" 49 << " render_manager.create_texture() failed." 50 << std::endl; 51 } else { 52 m_gstos.push_back(std::pair<unsigned int,render_manager*>(_id,&a_mgr)); 53 } 54 } 55 return _id; 56 } 57 protected: 58 virtual unsigned int create_gsto(std::ostream&,render_manager&) {return 0;} 59 60 unsigned int get_gsto_id(std::ostream& a_out,render_manager& a_mgr){ 61 unsigned int _id = _find(&a_mgr); 62 if(_id && !a_mgr.is_gsto_id_valid(_id)){ 63 #ifdef TOOLS_SG_GSTOS_DEBUG 64 a_out << "debug : tools::sg::gstos::get_gsto_id : " << _id << " not valid." << std::endl; 65 #endif 66 clean_gstos(&a_mgr); 67 _id = 0; 68 } 69 if(!_id) { 70 #ifdef TOOLS_SG_GSTOS_DEBUG 71 a_out << "debug : tools::sg::gstos::get_gsto_id : create_gsto ..." << std::endl; 72 #endif 73 _id = create_gsto(a_out,a_mgr); 74 #ifdef TOOLS_SG_GSTOS_DEBUG 75 a_out << "debug : tools::sg::gstos::get_gsto_id : create_gsto : _id " << _id << "." << std::endl; 76 #endif 77 if(!_id) { 78 // could be ok if there is no graphical data to load. 79 //a_out << "tools::sg::gstos::get_gsto_id :" 80 // << " create_gsto() failed." 81 // << std::endl; 82 } else { 83 m_gstos.push_back(std::pair<unsigned int,render_manager*>(_id,&a_mgr)); 84 } 85 } 86 return _id; 87 } 88 protected: 89 void clean_gstos() { 90 std::vector< std::pair<unsigned int,render_manager*> >::iterator it; 91 for(it=m_gstos.begin();it!=m_gstos.end();){ 92 (*it).second->delete_gsto((*it).first); 93 it = m_gstos.erase(it); 94 } 95 } 96 void clean_gstos(render_manager* a_mgr) { 97 std::vector< std::pair<unsigned int,render_manager*> >::iterator it; 98 for(it=m_gstos.begin();it!=m_gstos.end();){ 99 if((*it).second==a_mgr) { 100 (*it).second->delete_gsto((*it).first); 101 it = m_gstos.erase(it); 102 } else { 103 it++; 104 } 105 } 106 } 107 protected: 108 unsigned int _find(render_manager* a_mgr) { 109 std::vector< std::pair<unsigned int,render_manager*> >::iterator it; 110 for(it=m_gstos.begin();it!=m_gstos.end();++it){ 111 if((*it).second==a_mgr) return (*it).first; 112 } 113 return 0; 114 } 115 116 protected: 117 std::vector< std::pair<unsigned int,render_manager*> > m_gstos; 118 }; 119 120 }} 121 122 #endif