]> git.mxchange.org Git - simgear.git/blob - simgear/scene/material/matlib.cxx
Construct effects from property lists
[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/Options>
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::Options> options = new osgDB::Options;
75     options->setObjectCacheHint(osgDB::Options::CACHE_ALL);
76     options->setDatabasePath(fg_root);
77     int nMaterials = materials.nChildren();
78     for (int i = 0; i < nMaterials; i++) {
79         const SGPropertyNode *node = materials.getChild(i);
80         if (!strcmp(node->getName(), "material")) {
81             const SGPropertyNode *conditionNode = node->getChild("condition");
82             if (conditionNode) {
83                 SGSharedPtr<const SGCondition> condition = sgReadCondition(prop_root, conditionNode);
84                 if (!condition->test()) {
85                     SG_LOG(SG_INPUT, SG_DEBUG, "Skipping material entry #"
86                         << i << " (condition false)");
87                     continue;
88                 }
89             }
90
91             SGSharedPtr<SGMaterial> m = new SGMaterial(options.get(), node);
92
93             vector<SGPropertyNode_ptr>names = node->getChildren("name");
94             for ( unsigned int j = 0; j < names.size(); j++ ) {
95                 string name = names[j]->getStringValue();
96                 // cerr << "Material " << name << endl;
97                 matlib[name] = m;
98                 m->add_name(name);
99                 SG_LOG( SG_TERRAIN, SG_INFO, "  Loading material "
100                         << names[j]->getStringValue() );
101             }
102         } else {
103             SG_LOG(SG_INPUT, SG_WARN,
104                    "Skipping bad material entry " << node->getName());
105         }
106     }
107
108     return true;
109 }
110
111 // find a material record by material name
112 SGMaterial *SGMaterialLib::find( const string& material ) {
113     SGMaterial *result = NULL;
114     material_map_iterator it = matlib.find( material );
115     if ( it != end() ) {
116         result = it->second;
117         return result;
118     }
119
120     return NULL;
121 }
122
123 // Destructor
124 SGMaterialLib::~SGMaterialLib ( void ) {
125     SG_LOG( SG_GENERAL, SG_INFO, "SGMaterialLib::~SGMaterialLib() size=" << matlib.size());
126 }
127
128 const SGMaterial*
129 SGMaterialLib::findMaterial(const simgear::Effect* effect)
130 {
131   if (!effect)
132     return 0;
133
134   const SGMaterialUserData* matUserData
135     = dynamic_cast<const SGMaterialUserData*>(effect->getUserData());
136   if (!matUserData)
137     return 0;
138   else
139     return matUserData->getMaterial();
140 }