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 #if defined ( __CYGWIN__ )
32 #include <simgear/compiler.h>
33 #include <simgear/constants.h>
34 #include <simgear/structure/exception.hxx>
39 #include <osg/AlphaFunc>
40 #include <osg/BlendFunc>
41 #include <osg/CullFace>
42 #include <osg/Material>
44 #include <osg/PointSprite>
45 #include <osg/PolygonMode>
46 #include <osg/PolygonOffset>
47 #include <osg/StateSet>
50 #include <osg/Texture2D>
52 #include <simgear/debug/logstream.hxx>
53 #include <simgear/misc/sg_path.hxx>
54 #include <simgear/misc/sgstream.hxx>
55 #include <simgear/props/props_io.hxx>
56 #include <simgear/props/condition.hxx>
57 #include <simgear/scene/tgdb/userdata.hxx>
66 SGMaterialLib::SGMaterialLib ( void ) {
69 // Load a library of material properties
70 bool SGMaterialLib::load( const string &fg_root, const string& mpath, const char *season,
71 SGPropertyNode *prop_root )
73 SGPropertyNode materials;
75 SG_LOG( SG_INPUT, SG_INFO, "Reading materials from " << mpath );
77 readProperties( mpath, &materials );
78 } catch (const sg_exception &ex) {
79 SG_LOG( SG_INPUT, SG_ALERT, "Error reading materials: "
84 int nMaterials = materials.nChildren();
85 for (int i = 0; i < nMaterials; i++) {
86 const SGPropertyNode *node = materials.getChild(i);
87 if (!strcmp(node->getName(), "material")) {
88 const SGPropertyNode *conditionNode = node->getChild("condition");
90 SGSharedPtr<const SGCondition> condition = sgReadCondition(prop_root, conditionNode);
91 if (!condition->test()) {
92 SG_LOG(SG_INPUT, SG_DEBUG, "Skipping material entry #"
93 << i << " (condition false)");
98 SGSharedPtr<SGMaterial> m = new SGMaterial(fg_root, node, season);
100 vector<SGPropertyNode_ptr>names = node->getChildren("name");
101 for ( unsigned int j = 0; j < names.size(); j++ ) {
102 string name = names[j]->getStringValue();
103 // cerr << "Material " << name << endl;
106 SG_LOG( SG_TERRAIN, SG_INFO, " Loading material "
107 << names[j]->getStringValue() );
110 SG_LOG(SG_INPUT, SG_WARN,
111 "Skipping bad material entry " << node->getName());
119 // Load a library of material properties
120 bool SGMaterialLib::add_item ( const string &tex_path )
122 string material_name = tex_path;
123 int pos = tex_path.rfind( "/" );
124 material_name = material_name.substr( pos + 1 );
126 return add_item( material_name, tex_path );
130 // Load a library of material properties
131 bool SGMaterialLib::add_item ( const string &mat_name, const string &full_path )
133 int pos = full_path.rfind( "/" );
134 string tex_name = full_path.substr( pos + 1 );
135 string tex_path = full_path.substr( 0, pos );
137 SG_LOG( SG_TERRAIN, SG_INFO, " Loading material "
138 << mat_name << " (" << full_path << ")");
140 matlib[mat_name] = new SGMaterial( full_path );
141 matlib[mat_name]->add_name(mat_name);
147 // Load a library of material properties
148 bool SGMaterialLib::add_item ( const string &mat_name, osg::StateSet *state )
150 matlib[mat_name] = new SGMaterial( state );
151 matlib[mat_name]->add_name(mat_name);
153 SG_LOG( SG_TERRAIN, SG_INFO, " Loading material given a premade "
154 << "osg::StateSet = " << mat_name );
160 // find a material record by material name
161 SGMaterial *SGMaterialLib::find( const string& material ) {
162 SGMaterial *result = NULL;
163 material_map_iterator it = matlib.find( material );
173 SGMaterialLib::~SGMaterialLib ( void ) {
174 SG_LOG( SG_GENERAL, SG_INFO, "SGMaterialLib::~SGMaterialLib() size=" << matlib.size());
178 SGMaterialLib::findMaterial(const osg::StateSet* stateSet) const
183 const osg::Referenced* base = stateSet->getUserData();
187 const SGMaterialUserData* matUserData
188 = dynamic_cast<const SGMaterialUserData*>(base);
192 return matUserData->getMaterial();