]> git.mxchange.org Git - simgear.git/blob - simgear/scene/material/matlib.hxx
Crash fix: use cache to find materials.
[simgear.git] / simgear / scene / material / matlib.hxx
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/SGSharedPtr.hxx>
31
32 #include <memory>
33 #include <string>               // Standard C++ string library
34 #include <map>                  // STL associative "array"
35 #include <vector>               // STL "array"
36
37 class SGMaterial;
38 class SGPropertyNode;
39
40 namespace simgear { class Effect; }
41 namespace osg { class Geode; }
42
43 // Material management class
44 class SGMaterialLib {
45
46 private:
47     class MatLibPrivate;
48     std::auto_ptr<MatLibPrivate> d;
49     
50     // associative array of materials
51     typedef std::vector< SGSharedPtr<SGMaterial> > material_list;    
52     typedef material_list::iterator material_list_iterator;
53     
54     typedef std::map < std::string,  material_list> material_map;
55     typedef material_map::iterator material_map_iterator;
56     typedef material_map::const_iterator const_material_map_iterator;
57
58     material_map matlib;
59     
60     typedef std::map < std::string, SGSharedPtr<SGMaterial> > active_material_cache;
61     active_material_cache active_cache;
62     
63 public:
64
65     // Constructor
66     SGMaterialLib ( void );
67
68     // Load a library of material properties
69     bool load( const std::string &fg_root, const std::string& mpath,
70             SGPropertyNode *prop_root );
71     // find a material record by material name
72     SGMaterial *find( const std::string& material ) const;
73
74     /**
75      * Material lookup involves evaluation of SGConditions to determine which
76      * possible material (by season, region, etc) is valid. This involves
77      * vproperty tree queries, so repeated calls to find() can cause
78      * race conditions when called from the osgDB pager thread. (especially
79      * during startup)
80      *
81      * To fix this, and also avoid repeated re-evaluation of the material
82      * conditions, we provide a version which uses a cached, threadsafe table
83      * of the currently valid materials. The main thread calls the refresh
84      * method below to evaluate the valid materials, and findCached can be
85      * safely called from other threads with no access to unprotected state.
86      */
87     SGMaterial *findCached( const std::string& material ) const;
88     void refreshActiveMaterials();
89     
90     material_map_iterator begin() { return matlib.begin(); }
91     const_material_map_iterator begin() const { return matlib.begin(); }
92
93     material_map_iterator end() { return matlib.end(); }
94     const_material_map_iterator end() const { return matlib.end(); }
95
96     static const SGMaterial *findMaterial(const osg::Geode* geode);
97
98     // Destructor
99     ~SGMaterialLib ( void );
100 };
101
102
103 #endif // _MATLIB_HXX