1 // mat.cxx -- class to handle material properties
3 // Written by Curtis Olson, started May 1998.
5 // Copyright (C) 1998 - 2000 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 #include <simgear/compiler.h>
36 #ifdef SG_MATH_EXCEPTION_CLASH
40 #include <simgear/debug/logstream.hxx>
41 #include <simgear/misc/sg_path.hxx>
42 #include <simgear/misc/sgstream.hxx>
46 static map<string, ssgTexture *> _tex_cache;
47 static map<string, ssgTexture *>::iterator _tex_cache_iter;
50 ////////////////////////////////////////////////////////////////////////
51 // Constructors and destructor.
52 ////////////////////////////////////////////////////////////////////////
55 SGMaterial::SGMaterial( const string &fg_root, const SGPropertyNode *props, const char *season )
58 read_properties( fg_root, props, season );
59 build_ssg_state( false );
62 SGMaterial::SGMaterial( const string &texpath )
66 _internal_state st( NULL, texpath, false );
67 _status.push_back( st );
69 build_ssg_state( true );
72 SGMaterial::SGMaterial( ssgSimpleState *s )
78 SGMaterial::~SGMaterial (void)
84 ////////////////////////////////////////////////////////////////////////
86 ////////////////////////////////////////////////////////////////////////
89 SGMaterial::read_properties( const string &fg_root, const SGPropertyNode * props, const char *season )
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++)
95 string tname = textures[i]->getStringValue();
96 string otname = tname;
97 if (season && strncmp(season, "summer", 6))
99 if (tname.substr(0,7) == "Terrain")
100 tname.insert(7,"."+string(season));
104 tname = "unknown.rgb";
107 SGPath tpath( fg_root );
108 tpath.append("Textures.high");
110 if ( !ulFileExists(tpath.c_str()) ) {
111 tpath = SGPath( fg_root );
112 tpath.append("Textures");
116 if ( ulFileExists(tpath.c_str()) ) {
117 _internal_state st( NULL, tpath.str(), false );
118 _status.push_back( st );
122 if (textures.size() == 0) {
123 string tname = "unknown.rgb";
124 SGPath tpath( fg_root );
125 tpath.append("Textures");
126 tpath.append("Terrain");
128 _internal_state st( NULL, tpath.str(), true );
129 _status.push_back( st );
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);
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);
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);
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);
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);
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);
167 shininess = props->getDoubleValue("shininess", 1.0);
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]));
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");
179 glyphs[name] = new SGMaterialGlyph(glyph_nodes[i]);
185 ////////////////////////////////////////////////////////////////////////
187 ////////////////////////////////////////////////////////////////////////
200 light_coverage = 0.0;
204 rolling_friction = 0.02;
206 load_resistence = 1e30;
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;
218 SGMaterial::load_texture ( int n )
220 int i = (n >= 0) ? n : 0 ;
221 int end = (n >= 0) ? n+1 : _status.size();
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;
237 SGMaterial::get_state (int n) const
239 if (_status.size() == 0) {
240 SG_LOG( SG_GENERAL, SG_WARN, "No state available.");
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;
255 SGMaterial::build_ssg_state( bool defer_tex_load )
257 GLenum shade_model = GL_SMOOTH;
259 for (unsigned int i = 0; i < _status.size(); i++)
261 ssgSimpleState *state = new ssgSimpleState();
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 );
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;
276 _status[i].texture_loaded = false;
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 );
294 _status[i].state = state;
299 void SGMaterial::set_ssg_state( ssgSimpleState *s )
301 _status.push_back( _internal_state( s, "", true ) );
304 void SGMaterial::assignTexture( ssgSimpleState *state, string &fname,
305 int _wrapu, int _wrapv, int _mipmap )
307 _tex_cache_iter = _tex_cache.find(fname);
308 if (_tex_cache_iter == _tex_cache.end())
310 state->setTexture((char *)fname.c_str(), _wrapu, _wrapv, _mipmap);
311 _tex_cache[fname] = state->getTexture();
315 state->setTexture(_tex_cache_iter->second);
316 // cout << "Cache hit: " << fname << endl;
322 ////////////////////////////////////////////////////////////////////////
324 ////////////////////////////////////////////////////////////////////////
326 SGMaterialGlyph::SGMaterialGlyph(SGPropertyNode *p) :
327 _left(p->getDoubleValue("left", 0.0)),
328 _right(p->getDoubleValue("right", 1.0))