1 // matlib.hxx -- class to handle material properties 2 // 3 // Written by Curtis Olson, started May 1998. 4 // 5 // Copyright (C) 1998 - 2000 Curtis L. Olson - http://www.flightgear.org/~curt 6 // 7 // This program is free software; you can redistribute it and/or 8 // modify it under the terms of the GNU General Public License as 9 // published by the Free Software Foundation; either version 2 of the 10 // License, or (at your option) any later version. 11 // 12 // This program is distributed in the hope that it will be useful, but 13 // WITHOUT ANY WARRANTY; without even the implied warranty of 14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 15 // General Public License for more details. 16 // 17 // You should have received a copy of the GNU General Public License 18 // along with this program; if not, write to the Free Software 19 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 20 // 21 // $Id$ 22 23 24 #ifndef _MATLIB_HXX 25 #define _MATLIB_HXX 26 27 28 #include <simgear/compiler.h> 29 30 #include <simgear/structure/SGReferenced.hxx> 31 #include <simgear/structure/SGSharedPtr.hxx> 32 #include <simgear/math/SGMath.hxx> 33 34 #include <memory> 35 #include <string> // Standard C++ string library 36 #include <map> // STL associative "array" 37 #include <vector> // STL "array" 38 39 class SGMaterial; 40 class SGPropertyNode; 41 class SGPath; 42 43 namespace simgear { class Effect; } 44 namespace osg { class Geode; } 45 46 // Material cache class 47 class SGMaterialCache : public osg::Referenced 48 { 49 private: 50 typedef std::map < std::string, SGSharedPtr<SGMaterial> > material_cache; 51 material_cache cache; 52 53 public: 54 // Constructor 55 SGMaterialCache ( void ); 56 57 // Insertion 58 void insert( const std::string& name, SGSharedPtr<SGMaterial> material ); 59 60 // Lookup 61 SGMaterial *find( const std::string& material ) const; 62 63 // Destructor 64 ~SGMaterialCache ( void ); 65 }; 66 67 // Material management class 68 class SGMaterialLib : public SGReferenced 69 { 70 71 private: 72 class MatLibPrivate; 73 std::unique_ptr<MatLibPrivate> d; 74 75 // associative array of materials 76 typedef std::vector< SGSharedPtr<SGMaterial> > material_list; 77 typedef material_list::iterator material_list_iterator; 78 79 typedef std::map < std::string, material_list> material_map; 80 typedef material_map::iterator material_map_iterator; 81 typedef material_map::const_iterator const_material_map_iterator; 82 83 material_map matlib; 84 85 public: 86 87 // Constructor 88 SGMaterialLib ( void ); 89 90 // Load a library of material properties 91 bool load( const SGPath &fg_root, const SGPath& mpath, 92 SGPropertyNode *prop_root ); 93 // find a material record by material name 94 SGMaterial *find( const std::string& material, SGVec2f center ) const; 95 SGMaterial *find( const std::string& material, const SGGeod& center ) const; 96 97 /** 98 * Material lookup involves evaluation of position and SGConditions to 99 * determine which possible material (by season, region, etc) is valid. 100 * This involves property tree queries, so repeated calls to find() can cause 101 * race conditions when called from the osgDB pager thread. (especially 102 * during startup) 103 * 104 * To fix this, and also avoid repeated re-evaluation of the material 105 * conditions, we provide factory method to generate a material library 106 * cache of the valid materials based on the current state and a given position. 107 */ 108 109 SGMaterialCache *generateMatCache( SGVec2f center); 110 SGMaterialCache *generateMatCache( SGGeod center); 111 begin()112 material_map_iterator begin() { return matlib.begin(); } begin() const113 const_material_map_iterator begin() const { return matlib.begin(); } 114 end()115 material_map_iterator end() { return matlib.end(); } end() const116 const_material_map_iterator end() const { return matlib.end(); } 117 118 static const SGMaterial *findMaterial(const osg::Geode* geode); 119 120 // Destructor 121 ~SGMaterialLib ( void ); 122 123 }; 124 125 typedef SGSharedPtr<SGMaterialLib> SGMaterialLibPtr; 126 127 #endif // _MATLIB_HXX 128