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,
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);
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());
118 // find a material record by material name
119 SGMaterial *SGMaterialLib::find( const string& material ) {
120 SGMaterial *result = NULL;
121 material_map_iterator it = matlib.find( material );
131 SGMaterialLib::~SGMaterialLib ( void ) {
132 SG_LOG( SG_GENERAL, SG_INFO, "SGMaterialLib::~SGMaterialLib() size=" << matlib.size());
136 SGMaterialLib::findMaterial(const osg::StateSet* stateSet)
141 const osg::Referenced* base = stateSet->getUserData();
145 const SGMaterialUserData* matUserData
146 = dynamic_cast<const SGMaterialUserData*>(base);
150 return matUserData->getMaterial();