]> git.mxchange.org Git - simgear.git/blob - simgear/scene/material/matlib.cxx
Merge branch 'timoore/mat-effect'
[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
44 #include "mat.hxx"
45
46 #include "Effect.hxx"
47 #include "Technique.hxx"
48 #include "matlib.hxx"
49
50 using std::string;
51
52 // Constructor
53 SGMaterialLib::SGMaterialLib ( void ) {
54 }
55
56 // Load a library of material properties
57 bool SGMaterialLib::load( const string &fg_root, const string& mpath,
58         SGPropertyNode *prop_root )
59 {
60     SGPropertyNode materials;
61
62     SG_LOG( SG_INPUT, SG_INFO, "Reading materials from " << mpath );
63     try {
64         readProperties( mpath, &materials );
65     } catch (const sg_exception &ex) {
66         SG_LOG( SG_INPUT, SG_ALERT, "Error reading materials: "
67                 << ex.getMessage() );
68         throw;
69     }
70     osg::ref_ptr<osgDB::ReaderWriter::Options> options
71         = new osgDB::ReaderWriter::Options;
72     options->setObjectCacheHint(osgDB::ReaderWriter::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             const SGPropertyNode *conditionNode = node->getChild("condition");
79             if (conditionNode) {
80                 SGSharedPtr<const SGCondition> condition = sgReadCondition(prop_root, conditionNode);
81                 if (!condition->test()) {
82                     SG_LOG(SG_INPUT, SG_DEBUG, "Skipping material entry #"
83                         << i << " (condition false)");
84                     continue;
85                 }
86             }
87
88             SGSharedPtr<SGMaterial> m = new SGMaterial(options.get(), node);
89
90             vector<SGPropertyNode_ptr>names = node->getChildren("name");
91             for ( unsigned int j = 0; j < names.size(); j++ ) {
92                 string name = names[j]->getStringValue();
93                 // cerr << "Material " << name << endl;
94                 matlib[name] = m;
95                 m->add_name(name);
96                 SG_LOG( SG_TERRAIN, SG_DEBUG, "  Loading material "
97                         << names[j]->getStringValue() );
98             }
99         } else {
100             SG_LOG(SG_INPUT, SG_WARN,
101                    "Skipping bad material entry " << node->getName());
102         }
103     }
104
105     return true;
106 }
107
108 // find a material record by material name
109 SGMaterial *SGMaterialLib::find( const string& material ) {
110     SGMaterial *result = NULL;
111     material_map_iterator it = matlib.find( material );
112     if ( it != end() ) {
113         result = it->second;
114         return result;
115     }
116
117     return NULL;
118 }
119
120 // Destructor
121 SGMaterialLib::~SGMaterialLib ( void ) {
122     SG_LOG( SG_GENERAL, SG_INFO, "SGMaterialLib::~SGMaterialLib() size=" << matlib.size());
123 }
124
125 const SGMaterial*
126 SGMaterialLib::findMaterial(const osg::Geode* geode)
127 {
128     if (!geode)
129         return 0;
130     const simgear::EffectGeode* effectGeode;
131     effectGeode = dynamic_cast<const simgear::EffectGeode*>(geode);
132     if (!effectGeode)
133         return 0;
134     const simgear::Effect* effect = effectGeode->getEffect();
135     if (!effect)
136         return 0;
137     const SGMaterialUserData* userData;
138     userData = dynamic_cast<const SGMaterialUserData*>(effect->getUserData());
139     if (!userData)
140         return 0;
141     return userData->getMaterial();
142 }