1 // materialmgr.cxx -- class to handle material properties
3 // Written by Curtis Olson, started May 1998.
5 // Copyright (C) 1998 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.
25 # include <simgear_config.h>
28 #include <simgear/compiler.h>
29 #include <simgear/constants.h>
30 #include <simgear/structure/exception.hxx>
35 #include <osgDB/Registry>
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>
47 #include "Technique.hxx"
53 SGMaterialLib::SGMaterialLib ( void ) {
56 // Load a library of material properties
57 bool SGMaterialLib::load( const string &fg_root, const string& mpath,
58 SGPropertyNode *prop_root )
60 SGPropertyNode materials;
62 SG_LOG( SG_INPUT, SG_INFO, "Reading materials from " << mpath );
64 readProperties( mpath, &materials );
65 } catch (const sg_exception &ex) {
66 SG_LOG( SG_INPUT, SG_ALERT, "Error reading materials: "
70 osg::ref_ptr<osgDB::Options> options
72 options->setObjectCacheHint(osgDB::Options::CACHE_ALL);
73 options->setDatabasePath(fg_root);
74 int nMaterials = materials.nChildren();
75 for (int i = 0; i < nMaterials; i++) {
76 const SGPropertyNode *node = materials.getChild(i);
77 if (!strcmp(node->getName(), "material")) {
78 SGSharedPtr<SGMaterial> m = new SGMaterial(options.get(), node, prop_root);
80 std::vector<SGPropertyNode_ptr>names = node->getChildren("name");
81 for ( unsigned int j = 0; j < names.size(); j++ ) {
82 string name = names[j]->getStringValue();
83 // cerr << "Material " << name << endl;
84 matlib[name].push_back(m);
86 SG_LOG( SG_TERRAIN, SG_DEBUG, " Loading material "
87 << names[j]->getStringValue() );
90 SG_LOG(SG_INPUT, SG_WARN,
91 "Skipping bad material entry " << node->getName());
98 // find a material record by material name
99 SGMaterial *SGMaterialLib::find( const string& material ) {
100 SGMaterial *result = NULL;
101 material_map_iterator it = matlib.find( material );
103 // We now have a list of materials that match this
104 // name. Find the first one that either doesn't have
105 // a condition, or has a condition that evaluates
107 material_list_iterator iter = it->second.begin();
108 while (iter != it->second.end()) {
110 if (result->valid()) {
121 SGMaterialLib::~SGMaterialLib ( void ) {
122 SG_LOG( SG_GENERAL, SG_INFO, "SGMaterialLib::~SGMaterialLib() size=" << matlib.size());
125 const SGMaterial *SGMaterialLib::findMaterial(const osg::Geode* geode)
129 const simgear::EffectGeode* effectGeode;
130 effectGeode = dynamic_cast<const simgear::EffectGeode*>(geode);
133 const simgear::Effect* effect = effectGeode->getEffect();
136 const SGMaterialUserData* userData;
137 userData = dynamic_cast<const SGMaterialUserData*>(effect->getUserData());
140 return userData->getMaterial();