]> git.mxchange.org Git - simgear.git/blob - simgear/scene/material/matlib.cxx
Fix problem with ocean files not recognized as water.
[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 #if defined ( __CYGWIN__ )
29 #include <ieeefp.h>
30 #endif
31
32 #include <simgear/compiler.h>
33 #include <simgear/constants.h>
34 #include <simgear/structure/exception.hxx>
35
36 #include <string.h>
37 #include <string>
38
39 #include <osgDB/Registry>
40
41 #include <simgear/debug/logstream.hxx>
42 #include <simgear/misc/sg_path.hxx>
43 #include <simgear/misc/sgstream.hxx>
44 #include <simgear/props/props_io.hxx>
45 #include <simgear/props/condition.hxx>
46 #include <simgear/scene/tgdb/userdata.hxx>
47
48 #include "mat.hxx"
49
50 #include "Effect.hxx"
51 #include "Technique.hxx"
52 #include "matlib.hxx"
53
54 using std::string;
55
56 // Constructor
57 SGMaterialLib::SGMaterialLib ( void ) {
58 }
59
60 // Load a library of material properties
61 bool SGMaterialLib::load( const string &fg_root, const string& mpath,
62         SGPropertyNode *prop_root )
63 {
64     SGPropertyNode materials;
65
66     SG_LOG( SG_INPUT, SG_INFO, "Reading materials from " << mpath );
67     try {
68         readProperties( mpath, &materials );
69     } catch (const sg_exception &ex) {
70         SG_LOG( SG_INPUT, SG_ALERT, "Error reading materials: "
71                 << ex.getMessage() );
72         throw;
73     }
74     osg::ref_ptr<osgDB::ReaderWriter::Options> options
75         = new osgDB::ReaderWriter::Options;
76     options->setObjectCacheHint(osgDB::ReaderWriter::Options::CACHE_ALL);
77     options->setDatabasePath(fg_root);
78     int nMaterials = materials.nChildren();
79     for (int i = 0; i < nMaterials; i++) {
80         const SGPropertyNode *node = materials.getChild(i);
81         if (!strcmp(node->getName(), "material")) {
82             const SGPropertyNode *conditionNode = node->getChild("condition");
83             if (conditionNode) {
84                 SGSharedPtr<const SGCondition> condition = sgReadCondition(prop_root, conditionNode);
85                 if (!condition->test()) {
86                     SG_LOG(SG_INPUT, SG_DEBUG, "Skipping material entry #"
87                         << i << " (condition false)");
88                     continue;
89                 }
90             }
91
92             SGSharedPtr<SGMaterial> m = new SGMaterial(options.get(), node);
93
94             vector<SGPropertyNode_ptr>names = node->getChildren("name");
95             for ( unsigned int j = 0; j < names.size(); j++ ) {
96                 string name = names[j]->getStringValue();
97                 // cerr << "Material " << name << endl;
98                 matlib[name] = m;
99                 m->add_name(name);
100                 SG_LOG( SG_TERRAIN, SG_INFO, "  Loading material "
101                         << names[j]->getStringValue() );
102             }
103         } else {
104             SG_LOG(SG_INPUT, SG_WARN,
105                    "Skipping bad material entry " << node->getName());
106         }
107     }
108
109     return true;
110 }
111
112 // find a material record by material name
113 SGMaterial *SGMaterialLib::find( const string& material ) {
114     SGMaterial *result = NULL;
115     material_map_iterator it = matlib.find( material );
116     if ( it != end() ) {
117         result = it->second;
118         return result;
119     }
120
121     return NULL;
122 }
123
124 // Destructor
125 SGMaterialLib::~SGMaterialLib ( void ) {
126     SG_LOG( SG_GENERAL, SG_INFO, "SGMaterialLib::~SGMaterialLib() size=" << matlib.size());
127 }
128
129 const SGMaterial*
130 SGMaterialLib::findMaterial(const osg::Geode* geode)
131 {
132     if (!geode)
133         return 0;
134     const simgear::EffectGeode* effectGeode;
135     effectGeode = dynamic_cast<const simgear::EffectGeode*>(geode);
136     if (!effectGeode)
137         return 0;
138     const simgear::Effect* effect = effectGeode->getEffect();
139     if (!effect)
140         return 0;
141     const SGMaterialUserData* userData;
142     userData = dynamic_cast<const SGMaterialUserData*>(effect->getUserData());
143     if (!userData)
144         return 0;
145     return userData->getMaterial();
146 }