]> git.mxchange.org Git - simgear.git/blob - simgear/scene/material/matlib.cxx
Crash fix: use cache to find materials.
[simgear.git] / simgear / scene / material / matlib.cxx
1 // materialmgr.cxx -- class to handle material properties
2 //
3 // Written by Curtis Olson, started May 1998.
4 //
5 // Copyright (C) 1998  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 #ifdef HAVE_CONFIG_H
25 #  include <simgear_config.h>
26 #endif
27
28 #include <simgear/compiler.h>
29 #include <simgear/constants.h>
30 #include <simgear/structure/exception.hxx>
31
32 #include <string.h>
33 #include <string>
34
35 #include <osgDB/Registry>
36
37 #include <simgear/debug/logstream.hxx>
38 #include <simgear/misc/sg_path.hxx>
39 #include <simgear/misc/sgstream.hxx>
40 #include <simgear/props/props_io.hxx>
41 #include <simgear/props/condition.hxx>
42 #include <simgear/scene/tgdb/userdata.hxx>
43 #include <simgear/threads/SGThread.hxx>
44 #include <simgear/threads/SGGuard.hxx>
45
46 #include "mat.hxx"
47
48 #include "Effect.hxx"
49 #include "Technique.hxx"
50 #include "matlib.hxx"
51
52 using std::string;
53
54
55 class SGMaterialLib::MatLibPrivate
56 {
57 public:
58     SGMutex mutex;
59 };
60
61 // Constructor
62 SGMaterialLib::SGMaterialLib ( void ) :
63     d(new MatLibPrivate)
64 {
65 }
66
67 // Load a library of material properties
68 bool SGMaterialLib::load( const string &fg_root, const string& mpath,
69         SGPropertyNode *prop_root )
70 {
71     SGPropertyNode materials;
72
73     SG_LOG( SG_INPUT, SG_INFO, "Reading materials from " << mpath );
74     try {
75         readProperties( mpath, &materials );
76     } catch (const sg_exception &ex) {
77         SG_LOG( SG_INPUT, SG_ALERT, "Error reading materials: "
78                 << ex.getMessage() );
79         throw;
80     }
81     osg::ref_ptr<osgDB::Options> options
82         = new osgDB::Options;
83     options->setObjectCacheHint(osgDB::Options::CACHE_ALL);
84     options->setDatabasePath(fg_root);
85     int nMaterials = materials.nChildren();
86     for (int i = 0; i < nMaterials; i++) {
87         const SGPropertyNode *node = materials.getChild(i);
88         if (!strcmp(node->getName(), "material")) {
89             SGSharedPtr<SGMaterial> m = new SGMaterial(options.get(), node, prop_root);
90
91             std::vector<SGPropertyNode_ptr>names = node->getChildren("name");
92             for ( unsigned int j = 0; j < names.size(); j++ ) {
93                 string name = names[j]->getStringValue();
94                 // cerr << "Material " << name << endl;
95                 matlib[name].push_back(m);
96                 m->add_name(name);
97                 SG_LOG( SG_TERRAIN, SG_DEBUG, "  Loading material "
98                         << names[j]->getStringValue() );
99             }
100         } else {
101             SG_LOG(SG_INPUT, SG_WARN,
102                    "Skipping bad material entry " << node->getName());
103         }
104     }
105
106     return true;
107 }
108
109 // find a material record by material name
110 SGMaterial *SGMaterialLib::find( const string& material ) const
111 {
112     SGMaterial *result = NULL;
113     const_material_map_iterator it = matlib.find( material );
114     if ( it != end() ) {            
115         // We now have a list of materials that match this
116         // name. Find the first one that either doesn't have
117         // a condition, or has a condition that evaluates
118         // to true.
119         material_list::const_iterator iter = it->second.begin();
120         while (iter != it->second.end()) {            
121             result = *iter;
122             if (result->valid()) {
123                 return result;
124             }
125             iter++;
126         }
127     }
128
129     return NULL;
130 }
131
132 void SGMaterialLib::refreshActiveMaterials()
133 {
134     active_material_cache newCache;
135     material_map_iterator it = matlib.begin();
136     for (; it != matlib.end(); ++it) {
137         newCache[it->first] = find(it->first);
138     }
139     
140     // use this approach to minimise the time we're holding the lock
141     // lock on the mutex (and hence, would block findCached calls)
142     SGGuard<SGMutex> g(d->mutex);
143     active_cache = newCache;
144 }
145
146 SGMaterial *SGMaterialLib::findCached( const string& material ) const
147 {
148     SGGuard<SGMutex> g(d->mutex);
149     
150     active_material_cache::const_iterator it = active_cache.find(material);
151     if (it == active_cache.end())
152         return NULL;
153     
154     return it->second;
155 }
156
157 // Destructor
158 SGMaterialLib::~SGMaterialLib ( void ) {
159     SG_LOG( SG_GENERAL, SG_INFO, "SGMaterialLib::~SGMaterialLib() size=" << matlib.size());
160 }
161
162 const SGMaterial *SGMaterialLib::findMaterial(const osg::Geode* geode)
163 {
164     if (!geode)
165         return 0;
166     const simgear::EffectGeode* effectGeode;
167     effectGeode = dynamic_cast<const simgear::EffectGeode*>(geode);
168     if (!effectGeode)
169         return 0;
170     const simgear::Effect* effect = effectGeode->getEffect();
171     if (!effect)
172         return 0;
173     const SGMaterialUserData* userData;
174     userData = dynamic_cast<const SGMaterialUserData*>(effect->getUserData());
175     if (!userData)
176         return 0;
177     return userData->getMaterial();
178 }