]> git.mxchange.org Git - simgear.git/blob - simgear/scene/material/matlib.cxx
de8e6241c7fd780da74c550fa740074037db7d13
[simgear.git] / simgear / scene / material / matlib.cxx
1 // materialmgr.cxx -- class to handle material properties
2 //
3 // Written by Curtis Olson, started May 1998.
4 //
5 // Copyright (C) 1998  Curtis L. Olson  - http://www.flightgear.org/~curt
6 //
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.
11 //
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.
16 //
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.
20 //
21 // $Id$
22
23
24 #ifdef HAVE_CONFIG_H
25 #  include <simgear_config.h>
26 #endif
27
28 #if defined ( __CYGWIN__ )
29 #include <ieeefp.h>
30 #endif
31
32 #include <simgear/compiler.h>
33 #include <simgear/constants.h>
34 #include <simgear/structure/exception.hxx>
35
36 #include <string.h>
37 #include <string>
38
39 #include <osg/AlphaFunc>
40 #include <osg/BlendFunc>
41 #include <osg/CullFace>
42 #include <osg/Material>
43 #include <osg/Point>
44 #include <osg/PointSprite>
45 #include <osg/PolygonMode>
46 #include <osg/PolygonOffset>
47 #include <osg/StateSet>
48 #include <osg/TexEnv>
49 #include <osg/TexGen>
50 #include <osg/Texture2D>
51
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>
58
59 #include "mat.hxx"
60
61 #include "matlib.hxx"
62
63 SG_USING_STD(string);
64
65 // Constructor
66 SGMaterialLib::SGMaterialLib ( void ) {
67 }
68
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 )
72 {
73     SGPropertyNode materials;
74
75     SG_LOG( SG_INPUT, SG_INFO, "Reading materials from " << mpath );
76     try {
77         readProperties( mpath, &materials );
78     } catch (const sg_exception &ex) {
79         SG_LOG( SG_INPUT, SG_ALERT, "Error reading materials: "
80                 << ex.getMessage() );
81         throw;
82     }
83
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");
89             if (conditionNode) {
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)");
94                     continue;
95                 }
96             }
97
98             SGSharedPtr<SGMaterial> m = new SGMaterial(fg_root, node, season);
99
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;
104                 matlib[name] = m;
105                 m->add_name(name);
106                 SG_LOG( SG_TERRAIN, SG_INFO, "  Loading material "
107                         << names[j]->getStringValue() );
108             }
109         } else {
110             SG_LOG(SG_INPUT, SG_WARN,
111                    "Skipping bad material entry " << node->getName());
112         }
113     }
114
115     return true;
116 }
117
118
119 // Load a library of material properties
120 bool SGMaterialLib::add_item ( const string &tex_path )
121 {
122     string material_name = tex_path;
123     int pos = tex_path.rfind( "/" );
124     material_name = material_name.substr( pos + 1 );
125
126     return add_item( material_name, tex_path );
127 }
128
129
130 // Load a library of material properties
131 bool SGMaterialLib::add_item ( const string &mat_name, const string &full_path )
132 {
133     int pos = full_path.rfind( "/" );
134     string tex_name = full_path.substr( pos + 1 );
135     string tex_path = full_path.substr( 0, pos );
136
137     SG_LOG( SG_TERRAIN, SG_INFO, "  Loading material " 
138             << mat_name << " (" << full_path << ")");
139
140     matlib[mat_name] = new SGMaterial( full_path );
141     matlib[mat_name]->add_name(mat_name);
142
143     return true;
144 }
145
146
147 // Load a library of material properties
148 bool SGMaterialLib::add_item ( const string &mat_name, osg::StateSet *state )
149 {
150     matlib[mat_name] = new SGMaterial( state );
151     matlib[mat_name]->add_name(mat_name);
152
153     SG_LOG( SG_TERRAIN, SG_INFO, "  Loading material given a premade "
154             << "osg::StateSet = " << mat_name );
155
156     return true;
157 }
158
159
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 );
164     if ( it != end() ) {
165         result = it->second;
166         return result;
167     }
168
169     return NULL;
170 }
171
172 // Destructor
173 SGMaterialLib::~SGMaterialLib ( void ) {
174     SG_LOG( SG_GENERAL, SG_INFO, "SGMaterialLib::~SGMaterialLib() size=" << matlib.size());
175 }
176
177 const SGMaterial*
178 SGMaterialLib::findMaterial(const osg::StateSet* stateSet) const
179 {
180   if (!stateSet)
181     return 0;
182   
183   const osg::Referenced* base = stateSet->getUserData();
184   if (!base)
185     return 0;
186
187   const SGMaterialUserData* matUserData
188     = dynamic_cast<const SGMaterialUserData*>(base);
189   if (!matUserData)
190     return 0;
191
192   return matUserData->getMaterial();
193 }