1 // matlib.hxx -- class to handle material properties
3 // Written by Curtis Olson, started May 1998.
5 // Copyright (C) 1998 - 2000 Curtis L. Olson - http://www.flightgear.org/~curt
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.
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.
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.
28 #include <simgear/compiler.h>
30 #include <simgear/structure/SGReferenced.hxx>
31 #include <simgear/structure/SGSharedPtr.hxx>
32 #include <simgear/math/SGMath.hxx>
35 #include <string> // Standard C++ string library
36 #include <map> // STL associative "array"
37 #include <vector> // STL "array"
42 namespace simgear { class Effect; }
43 namespace osg { class Geode; }
45 // Material cache class
46 class SGMaterialCache : public osg::Referenced
49 typedef std::map < std::string, SGSharedPtr<SGMaterial> > material_cache;
54 SGMaterialCache ( void );
57 void insert( const std::string& name, SGSharedPtr<SGMaterial> material );
60 SGMaterial *find( const std::string& material ) const;
63 ~SGMaterialCache ( void );
66 // Material management class
67 class SGMaterialLib : public SGReferenced
72 std::auto_ptr<MatLibPrivate> d;
74 // associative array of materials
75 typedef std::vector< SGSharedPtr<SGMaterial> > material_list;
76 typedef material_list::iterator material_list_iterator;
78 typedef std::map < std::string, material_list> material_map;
79 typedef material_map::iterator material_map_iterator;
80 typedef material_map::const_iterator const_material_map_iterator;
87 SGMaterialLib ( void );
89 // Load a library of material properties
90 bool load( const std::string &fg_root, const std::string& mpath,
91 SGPropertyNode *prop_root );
92 // find a material record by material name
93 SGMaterial *find( const std::string& material, SGVec2f center ) const;
94 SGMaterial *find( const std::string& material, const SGGeod& center ) const;
97 * Material lookup involves evaluation of position and SGConditions to
98 * determine which possible material (by season, region, etc) is valid.
99 * This involves property tree queries, so repeated calls to find() can cause
100 * race conditions when called from the osgDB pager thread. (especially
103 * To fix this, and also avoid repeated re-evaluation of the material
104 * conditions, we provide factory method to generate a material library
105 * cache of the valid materials based on the current state and a given position.
108 SGMaterialCache *generateMatCache( SGVec2f center);
109 SGMaterialCache *generateMatCache( SGGeod center);
111 material_map_iterator begin() { return matlib.begin(); }
112 const_material_map_iterator begin() const { return matlib.begin(); }
114 material_map_iterator end() { return matlib.end(); }
115 const_material_map_iterator end() const { return matlib.end(); }
117 static const SGMaterial *findMaterial(const osg::Geode* geode);
120 ~SGMaterialLib ( void );
124 typedef SGSharedPtr<SGMaterialLib> SGMaterialLibPtr;
126 #endif // _MATLIB_HXX