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>
72 SGMaterialLib::SGMaterialLib ( void ) {
75 // Load a library of material properties
76 bool SGMaterialLib::load( const string &fg_root, const string& mpath, const char *season,
77 SGPropertyNode *prop_root )
79 SGPropertyNode materials;
81 SG_LOG( SG_INPUT, SG_INFO, "Reading materials from " << mpath );
83 readProperties( mpath, &materials );
84 } catch (const sg_exception &ex) {
85 SG_LOG( SG_INPUT, SG_ALERT, "Error reading materials: "
90 int nMaterials = materials.nChildren();
91 for (int i = 0; i < nMaterials; i++) {
92 const SGPropertyNode *node = materials.getChild(i);
93 if (!strcmp(node->getName(), "material")) {
94 const SGPropertyNode *conditionNode = node->getChild("condition");
96 SGSharedPtr<const SGCondition> condition = sgReadCondition(prop_root, conditionNode);
97 if (!condition->test()) {
98 SG_LOG(SG_INPUT, SG_DEBUG, "Skipping material entry #"
99 << i << " (condition false)");
104 SGSharedPtr<SGMaterial> m = new SGMaterial(fg_root, node, season);
106 vector<SGPropertyNode_ptr>names = node->getChildren("name");
107 for ( unsigned int j = 0; j < names.size(); j++ ) {
108 string name = names[j]->getStringValue();
109 // cerr << "Material " << name << endl;
112 SG_LOG( SG_TERRAIN, SG_INFO, " Loading material "
113 << names[j]->getStringValue() );
116 SG_LOG(SG_INPUT, SG_WARN,
117 "Skipping bad material entry " << node->getName());
125 // Load a library of material properties
126 bool SGMaterialLib::add_item ( const string &tex_path )
128 string material_name = tex_path;
129 int pos = tex_path.rfind( "/" );
130 material_name = material_name.substr( pos + 1 );
132 return add_item( material_name, tex_path );
136 // Load a library of material properties
137 bool SGMaterialLib::add_item ( const string &mat_name, const string &full_path )
139 int pos = full_path.rfind( "/" );
140 string tex_name = full_path.substr( pos + 1 );
141 string tex_path = full_path.substr( 0, pos );
143 SG_LOG( SG_TERRAIN, SG_INFO, " Loading material "
144 << mat_name << " (" << full_path << ")");
146 matlib[mat_name] = new SGMaterial( full_path );
147 matlib[mat_name]->add_name(mat_name);
153 // Load a library of material properties
154 bool SGMaterialLib::add_item ( const string &mat_name, osg::StateSet *state )
156 matlib[mat_name] = new SGMaterial( state );
157 matlib[mat_name]->add_name(mat_name);
159 SG_LOG( SG_TERRAIN, SG_INFO, " Loading material given a premade "
160 << "osg::StateSet = " << mat_name );
166 // find a material record by material name
167 SGMaterial *SGMaterialLib::find( const string& material ) {
168 SGMaterial *result = NULL;
169 material_map_iterator it = matlib.find( material );
179 SGMaterialLib::~SGMaterialLib ( void ) {
180 SG_LOG( SG_GENERAL, SG_INFO, "SGMaterialLib::~SGMaterialLib() size=" << matlib.size());
184 SGMaterialLib::findMaterial(const osg::StateSet* stateSet) const
189 const osg::Referenced* base = stateSet->getUserData();
193 const SGMaterialUserData* matUserData
194 = dynamic_cast<const SGMaterialUserData*>(base);
198 return matUserData->getMaterial();