]> git.mxchange.org Git - simgear.git/blob - simgear/scene/material/matlib.cxx
b20ccb447abdf34689fff536485f61cda4543cf1
[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 <osg/AlphaFunc>
40 #include <osg/BlendFunc>
41 #include <osg/CullFace>
42 #include <osg/Material>
43 #include <osg/Point>
44 #include <osg/PointSprite>
45 #include <osg/PolygonMode>
46 #include <osg/PolygonOffset>
47 #include <osg/StateSet>
48 #include <osg/TexEnv>
49 #include <osg/TexGen>
50 #include <osg/Texture2D>
51
52 #include <simgear/debug/logstream.hxx>
53 #include <simgear/misc/sg_path.hxx>
54 #include <simgear/misc/sgstream.hxx>
55 #include <simgear/props/props_io.hxx>
56 #include <simgear/props/condition.hxx>
57 #include <simgear/scene/tgdb/userdata.hxx>
58
59 #include "mat.hxx"
60
61 #include "matlib.hxx"
62
63 using std::string;
64
65 // Constructor
66 SGMaterialLib::SGMaterialLib ( void ) {
67 }
68
69 // Load a library of material properties
70 bool SGMaterialLib::load( const string &fg_root, const string& mpath,
71         SGPropertyNode *prop_root )
72 {
73     SGPropertyNode materials;
74
75     SG_LOG( SG_INPUT, SG_INFO, "Reading materials from " << mpath );
76     try {
77         readProperties( mpath, &materials );
78     } catch (const sg_exception &ex) {
79         SG_LOG( SG_INPUT, SG_ALERT, "Error reading materials: "
80                 << ex.getMessage() );
81         throw;
82     }
83
84     int nMaterials = materials.nChildren();
85     for (int i = 0; i < nMaterials; i++) {
86         const SGPropertyNode *node = materials.getChild(i);
87         if (!strcmp(node->getName(), "material")) {
88             const SGPropertyNode *conditionNode = node->getChild("condition");
89             if (conditionNode) {
90                 SGSharedPtr<const SGCondition> condition = sgReadCondition(prop_root, conditionNode);
91                 if (!condition->test()) {
92                     SG_LOG(SG_INPUT, SG_DEBUG, "Skipping material entry #"
93                         << i << " (condition false)");
94                     continue;
95                 }
96             }
97
98             SGSharedPtr<SGMaterial> m = new SGMaterial(fg_root, node);
99
100             vector<SGPropertyNode_ptr>names = node->getChildren("name");
101             for ( unsigned int j = 0; j < names.size(); j++ ) {
102                 string name = names[j]->getStringValue();
103                 // cerr << "Material " << name << endl;
104                 matlib[name] = m;
105                 m->add_name(name);
106                 SG_LOG( SG_TERRAIN, SG_INFO, "  Loading material "
107                         << names[j]->getStringValue() );
108             }
109         } else {
110             SG_LOG(SG_INPUT, SG_WARN,
111                    "Skipping bad material entry " << node->getName());
112         }
113     }
114
115     return true;
116 }
117
118 // find a material record by material name
119 SGMaterial *SGMaterialLib::find( const string& material ) {
120     SGMaterial *result = NULL;
121     material_map_iterator it = matlib.find( material );
122     if ( it != end() ) {
123         result = it->second;
124         return result;
125     }
126
127     return NULL;
128 }
129
130 // Destructor
131 SGMaterialLib::~SGMaterialLib ( void ) {
132     SG_LOG( SG_GENERAL, SG_INFO, "SGMaterialLib::~SGMaterialLib() size=" << matlib.size());
133 }
134
135 const SGMaterial*
136 SGMaterialLib::findMaterial(const osg::StateSet* stateSet)
137 {
138   if (!stateSet)
139     return 0;
140   
141   const osg::Referenced* base = stateSet->getUserData();
142   if (!base)
143     return 0;
144
145   const SGMaterialUserData* matUserData
146     = dynamic_cast<const SGMaterialUserData*>(base);
147   if (!matUserData)
148     return 0;
149
150   return matUserData->getMaterial();
151 }