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>
32 #if defined ( __CYGWIN__ )
36 #include <simgear/compiler.h>
37 #include <simgear/constants.h>
38 #include <simgear/structure/exception.hxx>
45 #include <osg/AlphaFunc>
46 #include <osg/BlendFunc>
47 #include <osg/CullFace>
48 #include <osg/Material>
50 #include <osg/PointSprite>
51 #include <osg/PolygonMode>
52 #include <osg/PolygonOffset>
53 #include <osg/StateSet>
56 #include <osg/Texture2D>
58 #include <simgear/debug/logstream.hxx>
59 #include <simgear/misc/sg_path.hxx>
60 #include <simgear/misc/sgstream.hxx>
61 #include <simgear/props/props_io.hxx>
62 #include <simgear/props/condition.hxx>
63 #include <simgear/scene/tgdb/userdata.hxx>
69 SG_USING_NAMESPACE(std);
73 SGMaterialLib::SGMaterialLib ( void ) {
76 // Load a library of material properties
77 bool SGMaterialLib::load( const string &fg_root, const string& mpath, const char *season,
78 SGPropertyNode *prop_root )
80 SGPropertyNode materials;
82 SG_LOG( SG_INPUT, SG_INFO, "Reading materials from " << mpath );
84 readProperties( mpath, &materials );
85 } catch (const sg_exception &ex) {
86 SG_LOG( SG_INPUT, SG_ALERT, "Error reading materials: "
91 int nMaterials = materials.nChildren();
92 for (int i = 0; i < nMaterials; i++) {
93 const SGPropertyNode *node = materials.getChild(i);
94 if (!strcmp(node->getName(), "material")) {
95 const SGPropertyNode *conditionNode = node->getChild("condition");
97 SGSharedPtr<const SGCondition> condition = sgReadCondition(prop_root, conditionNode);
98 if (!condition->test()) {
99 SG_LOG(SG_INPUT, SG_DEBUG, "Skipping material entry #"
100 << i << " (condition false)");
105 SGSharedPtr<SGMaterial> m = new SGMaterial(fg_root, node, season);
107 vector<SGPropertyNode_ptr>names = node->getChildren("name");
108 for ( unsigned int j = 0; j < names.size(); j++ ) {
109 string name = names[j]->getStringValue();
110 // cerr << "Material " << name << endl;
113 SG_LOG( SG_TERRAIN, SG_INFO, " Loading material "
114 << names[j]->getStringValue() );
117 SG_LOG(SG_INPUT, SG_WARN,
118 "Skipping bad material entry " << node->getName());
126 // Load a library of material properties
127 bool SGMaterialLib::add_item ( const string &tex_path )
129 string material_name = tex_path;
130 int pos = tex_path.rfind( "/" );
131 material_name = material_name.substr( pos + 1 );
133 return add_item( material_name, tex_path );
137 // Load a library of material properties
138 bool SGMaterialLib::add_item ( const string &mat_name, const string &full_path )
140 int pos = full_path.rfind( "/" );
141 string tex_name = full_path.substr( pos + 1 );
142 string tex_path = full_path.substr( 0, pos );
144 SG_LOG( SG_TERRAIN, SG_INFO, " Loading material "
145 << mat_name << " (" << full_path << ")");
147 matlib[mat_name] = new SGMaterial( full_path );
148 matlib[mat_name]->add_name(mat_name);
154 // Load a library of material properties
155 bool SGMaterialLib::add_item ( const string &mat_name, osg::StateSet *state )
157 matlib[mat_name] = new SGMaterial( state );
158 matlib[mat_name]->add_name(mat_name);
160 SG_LOG( SG_TERRAIN, SG_INFO, " Loading material given a premade "
161 << "osg::StateSet = " << mat_name );
167 // find a material record by material name
168 SGMaterial *SGMaterialLib::find( const string& material ) {
169 SGMaterial *result = NULL;
170 material_map_iterator it = matlib.find( material );
180 SGMaterialLib::~SGMaterialLib ( void ) {
181 SG_LOG( SG_GENERAL, SG_INFO, "SGMaterialLib::~SGMaterialLib() size=" << matlib.size());
185 SGMaterialLib::findMaterial(const osg::StateSet* stateSet) const
190 const osg::Referenced* base = stateSet->getUserData();
194 const SGMaterialUserData* matUserData
195 = dynamic_cast<const SGMaterialUserData*>(base);
199 return matUserData->getMaterial();