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/scene/tgdb/userdata.hxx>
72 SG_USING_NAMESPACE(std);
76 SGMaterialLib::SGMaterialLib ( void ) {
79 // Load a library of material properties
80 bool SGMaterialLib::load( const string &fg_root, const string& mpath, const char *season ) {
82 SGPropertyNode materials;
84 SG_LOG( SG_INPUT, SG_INFO, "Reading materials from " << mpath );
86 readProperties( mpath, &materials );
87 } catch (const sg_exception &ex) {
88 SG_LOG( SG_INPUT, SG_ALERT, "Error reading materials: "
93 int nMaterials = materials.nChildren();
94 for (int i = 0; i < nMaterials; i++) {
95 const SGPropertyNode * node = materials.getChild(i);
96 if (!strcmp(node->getName(), "material")) {
97 SGSharedPtr<SGMaterial> m = new SGMaterial(fg_root, node, season);
99 vector<SGPropertyNode_ptr>names = node->getChildren("name");
100 for ( unsigned int j = 0; j < names.size(); j++ ) {
101 string name = names[j]->getStringValue();
102 // cerr << "Material " << name << endl;
105 SG_LOG( SG_TERRAIN, SG_INFO, " Loading material "
106 << names[j]->getStringValue() );
109 SG_LOG(SG_INPUT, SG_WARN,
110 "Skipping bad material entry " << node->getName());
118 // Load a library of material properties
119 bool SGMaterialLib::add_item ( const string &tex_path )
121 string material_name = tex_path;
122 int pos = tex_path.rfind( "/" );
123 material_name = material_name.substr( pos + 1 );
125 return add_item( material_name, tex_path );
129 // Load a library of material properties
130 bool SGMaterialLib::add_item ( const string &mat_name, const string &full_path )
132 int pos = full_path.rfind( "/" );
133 string tex_name = full_path.substr( pos + 1 );
134 string tex_path = full_path.substr( 0, pos );
136 SG_LOG( SG_TERRAIN, SG_INFO, " Loading material "
137 << mat_name << " (" << full_path << ")");
139 matlib[mat_name] = new SGMaterial( full_path );
140 matlib[mat_name]->add_name(mat_name);
146 // Load a library of material properties
147 bool SGMaterialLib::add_item ( const string &mat_name, osg::StateSet *state )
149 matlib[mat_name] = new SGMaterial( state );
150 matlib[mat_name]->add_name(mat_name);
152 SG_LOG( SG_TERRAIN, SG_INFO, " Loading material given a premade "
153 << "osg::StateSet = " << mat_name );
159 // find a material record by material name
160 SGMaterial *SGMaterialLib::find( const string& material ) {
161 SGMaterial *result = NULL;
162 material_map_iterator it = matlib.find( material );
172 SGMaterialLib::~SGMaterialLib ( void ) {
173 SG_LOG( SG_GENERAL, SG_INFO, "SGMaterialLib::~SGMaterialLib() size=" << matlib.size());
177 SGMaterialLib::findMaterial(const osg::StateSet* stateSet) const
182 const osg::Referenced* base = stateSet->getUserData();
186 const SGMaterialUserData* matUserData
187 = dynamic_cast<const SGMaterialUserData*>(base);
191 return matUserData->getMaterial();