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 #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/scene/tgdb/userdata.hxx>
68 SG_USING_NAMESPACE(std);
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 ) {
78 SGPropertyNode materials;
80 SG_LOG( SG_INPUT, SG_INFO, "Reading materials from " << mpath );
82 readProperties( mpath, &materials );
83 } catch (const sg_exception &ex) {
84 SG_LOG( SG_INPUT, SG_ALERT, "Error reading materials: "
89 int nMaterials = materials.nChildren();
90 for (int i = 0; i < nMaterials; i++) {
91 const SGPropertyNode * node = materials.getChild(i);
92 if (!strcmp(node->getName(), "material")) {
93 SGSharedPtr<SGMaterial> m = new SGMaterial(fg_root, node, season);
95 vector<SGPropertyNode_ptr>names = node->getChildren("name");
96 for ( unsigned int j = 0; j < names.size(); j++ ) {
97 string name = names[j]->getStringValue();
98 // cerr << "Material " << name << endl;
101 SG_LOG( SG_TERRAIN, SG_INFO, " Loading material "
102 << names[j]->getStringValue() );
105 SG_LOG(SG_INPUT, SG_WARN,
106 "Skipping bad material entry " << node->getName());
114 // Load a library of material properties
115 bool SGMaterialLib::add_item ( const string &tex_path )
117 string material_name = tex_path;
118 int pos = tex_path.rfind( "/" );
119 material_name = material_name.substr( pos + 1 );
121 return add_item( material_name, tex_path );
125 // Load a library of material properties
126 bool SGMaterialLib::add_item ( const string &mat_name, const string &full_path )
128 int pos = full_path.rfind( "/" );
129 string tex_name = full_path.substr( pos + 1 );
130 string tex_path = full_path.substr( 0, pos );
132 SG_LOG( SG_TERRAIN, SG_INFO, " Loading material "
133 << mat_name << " (" << full_path << ")");
135 matlib[mat_name] = new SGMaterial( full_path );
136 matlib[mat_name]->add_name(mat_name);
142 // Load a library of material properties
143 bool SGMaterialLib::add_item ( const string &mat_name, osg::StateSet *state )
145 matlib[mat_name] = new SGMaterial( state );
146 matlib[mat_name]->add_name(mat_name);
148 SG_LOG( SG_TERRAIN, SG_INFO, " Loading material given a premade "
149 << "osg::StateSet = " << mat_name );
155 // find a material record by material name
156 SGMaterial *SGMaterialLib::find( const string& material ) {
157 SGMaterial *result = NULL;
158 material_map_iterator it = matlib.find( material );
169 SGMaterialLib::~SGMaterialLib ( void ) {
173 // Load one pending "deferred" texture. Return true if a texture
174 // loaded successfully, false if no pending, or error.
175 void SGMaterialLib::load_next_deferred() {
176 // container::iterator it = begin();
177 for ( material_map_iterator it = begin(); it != end(); it++ ) {
178 /* we don't need the key, but here's how we'd get it if we wanted it. */
179 // const string &key = it->first;
180 SGMaterial *slot = it->second;
181 if (slot->load_texture())
187 SGMaterialLib::findMaterial(const osg::StateSet* stateSet) const
192 const osg::Referenced* base = stateSet->getUserData();
196 const SGMaterialUserData* matUserData
197 = dynamic_cast<const SGMaterialUserData*>(base);
201 return matUserData->getMaterial();