]> git.mxchange.org Git - simgear.git/blob - simgear/scene/material/mat.cxx
b8af8091faacf0a8f878a9466bb4909a4ab3918a
[simgear.git] / simgear / scene / material / mat.cxx
1 // mat.cxx -- class to handle material properties
2 //
3 // Written by Curtis Olson, started May 1998.
4 //
5 // Copyright (C) 1998 - 2000  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 #include <simgear/compiler.h>
29
30 #include <string.h>
31 #include <map>
32 SG_USING_STD(map);
33
34 #include <plib/ul.h>
35
36 #ifdef SG_MATH_EXCEPTION_CLASH
37 #  include <math.h>
38 #endif
39
40 #include <simgear/debug/logstream.hxx>
41 #include <simgear/misc/sg_path.hxx>
42 #include <simgear/misc/sgstream.hxx>
43
44 #include "mat.hxx"
45
46 static map<string, ssgTexture *> _tex_cache;
47 static map<string, ssgTexture *>::iterator _tex_cache_iter;
48
49 \f
50 ////////////////////////////////////////////////////////////////////////
51 // Constructors and destructor.
52 ////////////////////////////////////////////////////////////////////////
53
54
55 SGMaterial::SGMaterial( const string &fg_root, const SGPropertyNode *props, const char *season )
56 {
57     init();
58     read_properties( fg_root, props, season );
59     build_ssg_state( false );
60 }
61
62 SGMaterial::SGMaterial( const string &texpath )
63 {
64     init();
65
66     _internal_state st( NULL, texpath, false );
67     _status.push_back( st );
68
69     build_ssg_state( true );
70 }
71
72 SGMaterial::SGMaterial( ssgSimpleState *s )
73 {
74     init();
75     set_ssg_state( s );
76 }
77
78 SGMaterial::~SGMaterial (void)
79 {
80 }
81
82
83 \f
84 ////////////////////////////////////////////////////////////////////////
85 // Public methods.
86 ////////////////////////////////////////////////////////////////////////
87
88 void
89 SGMaterial::read_properties( const string &fg_root, const SGPropertyNode * props, const char *season )
90 {
91                                 // Gather the path(s) to the texture(s)
92   vector<SGPropertyNode_ptr> textures = props->getChildren("texture");
93   for (unsigned int i = 0; i < textures.size(); i++)
94   {
95     string tname = textures[i]->getStringValue();
96     string otname = tname;
97     if (season && strncmp(season, "summer", 6))
98     {
99         if (tname.substr(0,7) == "Terrain")
100             tname.insert(7,"."+string(season));
101     }
102
103     if (tname == "") {
104         tname = "unknown.rgb";
105     }
106
107     SGPath tpath( fg_root );
108     tpath.append("Textures.high");
109     tpath.append(tname);
110     if ( !ulFileExists(tpath.c_str()) ) {
111       tpath = SGPath( fg_root );
112       tpath.append("Textures");
113       tpath.append(tname);
114     }
115
116     if ( ulFileExists(tpath.c_str()) ) {
117       _internal_state st( NULL, tpath.str(), false );
118       _status.push_back( st );
119     }
120   }
121
122   if (textures.size() == 0) {
123     string tname = "unknown.rgb";
124     SGPath tpath( fg_root );
125     tpath.append("Textures");
126     tpath.append("Terrain");
127     tpath.append(tname);
128     _internal_state st( NULL, tpath.str(), true );
129     _status.push_back( st );
130   }
131
132   xsize = props->getDoubleValue("xsize", 0.0);
133   ysize = props->getDoubleValue("ysize", 0.0);
134   wrapu = props->getBoolValue("wrapu", true);
135   wrapv = props->getBoolValue("wrapv", true);
136   mipmap = props->getBoolValue("mipmap", true);
137   light_coverage = props->getDoubleValue("light-coverage", 0.0);
138
139   // surface values for use with ground reactions
140   solid = props->getBoolValue("solid", true);
141   friction_factor = props->getDoubleValue("friction-factor", 1.0);
142   rolling_friction = props->getDoubleValue("rolling-friction", 0.02);
143   bumpiness = props->getDoubleValue("bumpiness", 0.0);
144   load_resistence = props->getDoubleValue("load-resistence", 1e30);
145
146   // Taken from default values as used in ac3d
147   ambient[0] = props->getDoubleValue("ambient/r", 0.2);
148   ambient[1] = props->getDoubleValue("ambient/g", 0.2);
149   ambient[2] = props->getDoubleValue("ambient/b", 0.2);
150   ambient[3] = props->getDoubleValue("ambient/a", 1.0);
151
152   diffuse[0] = props->getDoubleValue("diffuse/r", 0.8);
153   diffuse[1] = props->getDoubleValue("diffuse/g", 0.8);
154   diffuse[2] = props->getDoubleValue("diffuse/b", 0.8);
155   diffuse[3] = props->getDoubleValue("diffuse/a", 1.0);
156
157   specular[0] = props->getDoubleValue("specular/r", 0.0);
158   specular[1] = props->getDoubleValue("specular/g", 0.0);
159   specular[2] = props->getDoubleValue("specular/b", 0.0);
160   specular[3] = props->getDoubleValue("specular/a", 1.0);
161
162   emission[0] = props->getDoubleValue("emissive/r", 0.0);
163   emission[1] = props->getDoubleValue("emissive/g", 0.0);
164   emission[2] = props->getDoubleValue("emissive/b", 0.0);
165   emission[3] = props->getDoubleValue("emissive/a", 1.0);
166
167   shininess = props->getDoubleValue("shininess", 1.0);
168
169   vector<SGPropertyNode_ptr> object_group_nodes =
170     ((SGPropertyNode *)props)->getChildren("object-group");
171   for (unsigned int i = 0; i < object_group_nodes.size(); i++)
172     object_groups.push_back(new SGMatModelGroup(object_group_nodes[i]));
173
174   // read glyph table for taxi-/runway-signs
175   vector<SGPropertyNode_ptr> glyph_nodes = props->getChildren("glyph");
176   for (unsigned int i = 0; i < glyph_nodes.size(); i++) {
177     const char *name = glyph_nodes[i]->getStringValue("name");
178     if (name)
179       glyphs[name] = new SGMaterialGlyph(glyph_nodes[i]);
180   }
181 }
182
183
184 \f
185 ////////////////////////////////////////////////////////////////////////
186 // Private methods.
187 ////////////////////////////////////////////////////////////////////////
188
189 void 
190 SGMaterial::init ()
191 {
192     _status.clear();
193     _current_ptr = 0;
194     xsize = 0;
195     ysize = 0;
196     wrapu = true;
197     wrapv = true;
198
199     mipmap = true;
200     light_coverage = 0.0;
201
202     solid = true;
203     friction_factor = 1;
204     rolling_friction = 0.02;
205     bumpiness = 0;
206     load_resistence = 1e30;
207
208     shininess = 1.0;
209     for (int i = 0; i < 4; i++) {
210         ambient[i]  = (i < 3) ? 0.2 : 1.0;
211         specular[i] = (i < 3) ? 0.0 : 1.0;
212         diffuse[i]  = (i < 3) ? 0.8 : 1.0;
213         emission[i] = (i < 3) ? 0.0 : 1.0;
214     }
215 }
216
217 bool
218 SGMaterial::load_texture ( int n )
219 {
220     int i   = (n >= 0) ? n   : 0 ;
221     int end = (n >= 0) ? n+1 : _status.size();
222
223     for (; i < end; i++)
224     {
225         if ( !_status[i].texture_loaded ) {
226             SG_LOG( SG_GENERAL, SG_INFO, "Loading deferred texture "
227                                           << _status[i].texture_path );
228             assignTexture(_status[i].state, _status[i].texture_path,
229                                          wrapu, wrapv, mipmap );
230             _status[i].texture_loaded = true;
231        }
232     }
233     return true;
234 }
235
236 ssgSimpleState *
237 SGMaterial::get_state (int n) const
238 {
239     if (_status.size() == 0) {
240         SG_LOG( SG_GENERAL, SG_WARN, "No state available.");
241         return NULL;
242     }
243
244     ssgSimpleState *st = (n >= 0) ? _status[n].state
245                                   : _status[_current_ptr].state;
246     ((SGMaterial *)this)->_current_ptr += 1;
247     if (_current_ptr >= _status.size())
248         ((SGMaterial *)this)->_current_ptr = 0;
249
250     return st;
251 }
252
253
254 void 
255 SGMaterial::build_ssg_state( bool defer_tex_load )
256 {
257     GLenum shade_model = GL_SMOOTH;
258     
259     for (unsigned int i = 0; i < _status.size(); i++)
260     {
261         ssgSimpleState *state = new ssgSimpleState();
262
263         // Set up the textured state
264         state->setShadeModel( shade_model );
265         state->enable( GL_LIGHTING );
266         state->enable ( GL_CULL_FACE ) ;
267         state->enable( GL_TEXTURE_2D );
268         state->disable( GL_BLEND );
269         state->disable( GL_ALPHA_TEST );
270
271         if ( !defer_tex_load ) {
272             SG_LOG(SG_INPUT, SG_INFO, "    " << _status[i].texture_path );
273             assignTexture( state, _status[i].texture_path, wrapu, wrapv );
274             _status[i].texture_loaded = true;
275         } else {
276             _status[i].texture_loaded = false;
277         }
278
279         state->enable( GL_COLOR_MATERIAL );
280         state->setMaterial ( GL_AMBIENT,
281                              ambient[0], ambient[1],
282                              ambient[2], ambient[3] ) ;
283         state->setMaterial ( GL_DIFFUSE,
284                             diffuse[0], diffuse[1],
285                             diffuse[2], diffuse[3] ) ;
286         state->setMaterial ( GL_SPECULAR,
287                             specular[0], specular[1],
288                             specular[2], specular[3] ) ;
289         state->setMaterial ( GL_EMISSION,
290                             emission[0], emission[1],
291                             emission[2], emission[3] ) ;
292         state->setShininess ( shininess );
293
294         _status[i].state = state;
295     }
296 }
297
298
299 void SGMaterial::set_ssg_state( ssgSimpleState *s )
300 {
301     _status.push_back( _internal_state( s, "", true ) );
302 }
303
304 void SGMaterial::assignTexture( ssgSimpleState *state, string &fname,
305                  int _wrapu, int _wrapv, int _mipmap )
306 {
307    _tex_cache_iter = _tex_cache.find(fname);
308    if (_tex_cache_iter == _tex_cache.end())
309    {
310       state->setTexture((char *)fname.c_str(), _wrapu, _wrapv, _mipmap);
311       _tex_cache[fname] = state->getTexture();
312    }
313    else
314    {
315       state->setTexture(_tex_cache_iter->second);
316       // cout << "Cache hit: " << fname << endl;
317    }
318 }
319
320 SGMaterialGlyph* SGMaterial::get_glyph (const string& name) const
321 {
322   map<string, SGSharedPtr<SGMaterialGlyph> >::const_iterator it;
323   it = glyphs.find(name);
324   if (it == glyphs.end())
325     return 0;
326
327   return it->second;
328 }
329
330 \f
331 ////////////////////////////////////////////////////////////////////////
332 // SGMaterialGlyph.
333 ////////////////////////////////////////////////////////////////////////
334
335 SGMaterialGlyph::SGMaterialGlyph(SGPropertyNode *p) :
336     _left(p->getDoubleValue("left", 0.0)),
337     _right(p->getDoubleValue("right", 1.0))
338 {
339 }
340
341
342 // end of mat.cxx