]> git.mxchange.org Git - simgear.git/blob - simgear/scene/material/matlib.hxx
Canvas: fix element mouse hit detection with OSG 3.3.2.
[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/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
42 namespace simgear { class Effect; }
43 namespace osg { class Geode; }
44
45 // Material cache class
46 class SGMaterialCache : public osg::Referenced
47 {
48 private:
49     typedef std::map < std::string, SGSharedPtr<SGMaterial> > material_cache;
50     material_cache cache;
51
52 public:
53     // Constructor
54     SGMaterialCache ( void );
55
56     // Insertion
57     void insert( const std::string& name, SGSharedPtr<SGMaterial> material );
58
59     // Lookup
60     SGMaterial *find( const std::string& material ) const;
61
62     // Destructor
63     ~SGMaterialCache ( void );
64 };
65
66 // Material management class
67 class SGMaterialLib : public SGReferenced
68 {
69
70 private:
71     class MatLibPrivate;
72     std::auto_ptr<MatLibPrivate> d;
73     
74     // associative array of materials
75     typedef std::vector< SGSharedPtr<SGMaterial> > material_list;    
76     typedef material_list::iterator material_list_iterator;
77     
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;
81
82     material_map matlib;
83     
84 public:
85
86     // Constructor
87     SGMaterialLib ( void );
88
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;
95
96     /**
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
101      * during startup)
102      *
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.
106      */
107
108     SGMaterialCache *generateMatCache( SGVec2f center);
109     SGMaterialCache *generateMatCache( SGGeod center);
110
111     material_map_iterator begin() { return matlib.begin(); }
112     const_material_map_iterator begin() const { return matlib.begin(); }
113
114     material_map_iterator end() { return matlib.end(); }
115     const_material_map_iterator end() const { return matlib.end(); }
116
117     static const SGMaterial *findMaterial(const osg::Geode* geode);
118
119     // Destructor
120     ~SGMaterialLib ( void );
121
122 };
123
124 typedef SGSharedPtr<SGMaterialLib> SGMaterialLibPtr;
125
126 #endif // _MATLIB_HXX