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>
34 #include <string> // Standard C++ string library
35 #include <map> // STL associative "array"
36 #include <vector> // STL "array"
41 namespace simgear { class Effect; }
42 namespace osg { class Geode; }
44 // Material management class
45 class SGMaterialLib : public SGReferenced
50 std::auto_ptr<MatLibPrivate> d;
52 // associative array of materials
53 typedef std::vector< SGSharedPtr<SGMaterial> > material_list;
54 typedef material_list::iterator material_list_iterator;
56 typedef std::map < std::string, material_list> material_map;
57 typedef material_map::iterator material_map_iterator;
58 typedef material_map::const_iterator const_material_map_iterator;
62 typedef std::map < std::string, SGSharedPtr<SGMaterial> > active_material_cache;
63 active_material_cache active_cache;
68 SGMaterialLib ( void );
70 // Load a library of material properties
71 bool load( const std::string &fg_root, const std::string& mpath,
72 SGPropertyNode *prop_root );
73 // find a material record by material name
74 SGMaterial *find( const std::string& material ) const;
77 * Material lookup involves evaluation of SGConditions to determine which
78 * possible material (by season, region, etc) is valid. This involves
79 * vproperty tree queries, so repeated calls to find() can cause
80 * race conditions when called from the osgDB pager thread. (especially
83 * To fix this, and also avoid repeated re-evaluation of the material
84 * conditions, we provide a version which uses a cached, threadsafe table
85 * of the currently valid materials. The main thread calls the refresh
86 * method below to evaluate the valid materials, and findCached can be
87 * safely called from other threads with no access to unprotected state.
89 SGMaterial *findCached( const std::string& material ) const;
90 void refreshActiveMaterials();
92 material_map_iterator begin() { return matlib.begin(); }
93 const_material_map_iterator begin() const { return matlib.begin(); }
95 material_map_iterator end() { return matlib.end(); }
96 const_material_map_iterator end() const { return matlib.end(); }
98 static const SGMaterial *findMaterial(const osg::Geode* geode);
101 ~SGMaterialLib ( void );
104 typedef SGSharedPtr<SGMaterialLib> SGMaterialLibPtr;
106 #endif // _MATLIB_HXX