]> git.mxchange.org Git - simgear.git/blob - simgear/scene/material/matlib.cxx
9c02cb96c2f18e062b64021585fe9ee9ccbac847
[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 #ifdef SG_MATH_EXCEPTION_CLASH
29 #  include <math.h>
30 #endif
31
32 #ifdef HAVE_WINDOWS_H
33 #  include <windows.h>
34 #endif
35
36 #include <simgear/compiler.h>
37 #include <simgear/constants.h>
38 #include <simgear/structure/exception.hxx>
39
40 #include SG_GL_H
41
42 #include <string.h>
43 #include STL_STRING
44
45 #include <osg/AlphaFunc>
46 #include <osg/BlendFunc>
47 #include <osg/CullFace>
48 #include <osg/Material>
49 #include <osg/Point>
50 #include <osg/PointSprite>
51 #include <osg/PolygonMode>
52 #include <osg/PolygonOffset>
53 #include <osg/StateSet>
54 #include <osg/TexEnv>
55 #include <osg/TexGen>
56 #include <osg/Texture2D>
57
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>
63
64 #include "mat.hxx"
65
66 #include "matlib.hxx"
67
68 SG_USING_NAMESPACE(std);
69 SG_USING_STD(string);
70
71 // Constructor
72 SGMaterialLib::SGMaterialLib ( void ) {
73 }
74
75 // Load a library of material properties
76 bool SGMaterialLib::load( const string &fg_root, const string& mpath, const char *season ) {
77
78     SGPropertyNode materials;
79
80     SG_LOG( SG_INPUT, SG_INFO, "Reading materials from " << mpath );
81     try {
82         readProperties( mpath, &materials );
83     } catch (const sg_exception &ex) {
84         SG_LOG( SG_INPUT, SG_ALERT, "Error reading materials: "
85                 << ex.getMessage() );
86         throw;
87     }
88
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);
94
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;
99                 matlib[name] = m;
100                 m->add_name(name);
101                 SG_LOG( SG_TERRAIN, SG_INFO, "  Loading material "
102                         << names[j]->getStringValue() );
103             }
104         } else {
105             SG_LOG(SG_INPUT, SG_WARN,
106                    "Skipping bad material entry " << node->getName());
107         }
108     }
109
110     return true;
111 }
112
113
114 // Load a library of material properties
115 bool SGMaterialLib::add_item ( const string &tex_path )
116 {
117     string material_name = tex_path;
118     int pos = tex_path.rfind( "/" );
119     material_name = material_name.substr( pos + 1 );
120
121     return add_item( material_name, tex_path );
122 }
123
124
125 // Load a library of material properties
126 bool SGMaterialLib::add_item ( const string &mat_name, const string &full_path )
127 {
128     int pos = full_path.rfind( "/" );
129     string tex_name = full_path.substr( pos + 1 );
130     string tex_path = full_path.substr( 0, pos );
131
132     SG_LOG( SG_TERRAIN, SG_INFO, "  Loading material " 
133             << mat_name << " (" << full_path << ")");
134
135     matlib[mat_name] = new SGMaterial( full_path );
136     matlib[mat_name]->add_name(mat_name);
137
138     return true;
139 }
140
141
142 // Load a library of material properties
143 bool SGMaterialLib::add_item ( const string &mat_name, osg::StateSet *state )
144 {
145     matlib[mat_name] = new SGMaterial( state );
146     matlib[mat_name]->add_name(mat_name);
147
148     SG_LOG( SG_TERRAIN, SG_INFO, "  Loading material given a premade "
149             << "osg::StateSet = " << mat_name );
150
151     return true;
152 }
153
154
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 );
159     if ( it != end() ) {
160         result = it->second;
161         return result;
162     }
163
164     return NULL;
165 }
166
167
168 // Destructor
169 SGMaterialLib::~SGMaterialLib ( void ) {
170 }
171
172
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())
182           return;
183     }
184 }
185
186 const SGMaterial*
187 SGMaterialLib::findMaterial(const osg::StateSet* stateSet) const
188 {
189   if (!stateSet)
190     return 0;
191   
192   const osg::Referenced* base = stateSet->getUserData();
193   if (!base)
194     return 0;
195
196   const SGMaterialUserData* matUserData
197     = dynamic_cast<const SGMaterialUserData*>(base);
198   if (!matUserData)
199     return 0;
200
201   return matUserData->getMaterial();
202 }