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 #ifdef SG_MATH_EXCEPTION_CLASH
36 #if defined ( __CYGWIN__ )
40 #include <simgear/compiler.h>
41 #include <simgear/constants.h>
42 #include <simgear/structure/exception.hxx>
49 #include <osg/AlphaFunc>
50 #include <osg/BlendFunc>
51 #include <osg/CullFace>
52 #include <osg/Material>
54 #include <osg/PointSprite>
55 #include <osg/PolygonMode>
56 #include <osg/PolygonOffset>
57 #include <osg/StateSet>
60 #include <osg/Texture2D>
62 #include <simgear/debug/logstream.hxx>
63 #include <simgear/misc/sg_path.hxx>
64 #include <simgear/misc/sgstream.hxx>
65 #include <simgear/props/props_io.hxx>
66 #include <simgear/props/condition.hxx>
67 #include <simgear/scene/tgdb/userdata.hxx>
73 SG_USING_NAMESPACE(std);
77 SGMaterialLib::SGMaterialLib ( void ) {
80 // Load a library of material properties
81 bool SGMaterialLib::load( const string &fg_root, const string& mpath, const char *season,
82 SGPropertyNode *prop_root )
84 SGPropertyNode materials;
86 SG_LOG( SG_INPUT, SG_INFO, "Reading materials from " << mpath );
88 readProperties( mpath, &materials );
89 } catch (const sg_exception &ex) {
90 SG_LOG( SG_INPUT, SG_ALERT, "Error reading materials: "
95 int nMaterials = materials.nChildren();
96 for (int i = 0; i < nMaterials; i++) {
97 const SGPropertyNode *node = materials.getChild(i);
98 if (!strcmp(node->getName(), "material")) {
99 const SGPropertyNode *conditionNode = node->getChild("condition");
101 SGSharedPtr<const SGCondition> condition = sgReadCondition(prop_root, conditionNode);
102 if (!condition->test()) {
103 SG_LOG(SG_INPUT, SG_DEBUG, "Skipping material entry #"
104 << i << " (condition false)");
109 SGSharedPtr<SGMaterial> m = new SGMaterial(fg_root, node, season);
111 vector<SGPropertyNode_ptr>names = node->getChildren("name");
112 for ( unsigned int j = 0; j < names.size(); j++ ) {
113 string name = names[j]->getStringValue();
114 // cerr << "Material " << name << endl;
117 SG_LOG( SG_TERRAIN, SG_INFO, " Loading material "
118 << names[j]->getStringValue() );
121 SG_LOG(SG_INPUT, SG_WARN,
122 "Skipping bad material entry " << node->getName());
130 // Load a library of material properties
131 bool SGMaterialLib::add_item ( const string &tex_path )
133 string material_name = tex_path;
134 int pos = tex_path.rfind( "/" );
135 material_name = material_name.substr( pos + 1 );
137 return add_item( material_name, tex_path );
141 // Load a library of material properties
142 bool SGMaterialLib::add_item ( const string &mat_name, const string &full_path )
144 int pos = full_path.rfind( "/" );
145 string tex_name = full_path.substr( pos + 1 );
146 string tex_path = full_path.substr( 0, pos );
148 SG_LOG( SG_TERRAIN, SG_INFO, " Loading material "
149 << mat_name << " (" << full_path << ")");
151 matlib[mat_name] = new SGMaterial( full_path );
152 matlib[mat_name]->add_name(mat_name);
158 // Load a library of material properties
159 bool SGMaterialLib::add_item ( const string &mat_name, osg::StateSet *state )
161 matlib[mat_name] = new SGMaterial( state );
162 matlib[mat_name]->add_name(mat_name);
164 SG_LOG( SG_TERRAIN, SG_INFO, " Loading material given a premade "
165 << "osg::StateSet = " << mat_name );
171 // find a material record by material name
172 SGMaterial *SGMaterialLib::find( const string& material ) {
173 SGMaterial *result = NULL;
174 material_map_iterator it = matlib.find( material );
184 SGMaterialLib::~SGMaterialLib ( void ) {
185 SG_LOG( SG_GENERAL, SG_INFO, "SGMaterialLib::~SGMaterialLib() size=" << matlib.size());
189 SGMaterialLib::findMaterial(const osg::StateSet* stateSet) const
194 const osg::Referenced* base = stateSet->getUserData();
198 const SGMaterialUserData* matUserData
199 = dynamic_cast<const SGMaterialUserData*>(base);
203 return matUserData->getMaterial();