1 // newmat.cxx -- class to handle material properties
3 // Written by Curtis Olson, started May 1998.
5 // Copyright (C) 1998 - 2000 Curtis L. Olson - curt@flightgear.org
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., 675 Mass Ave, Cambridge, MA 02139, USA.
28 #include <simgear/compiler.h>
33 #include <simgear/compiler.h>
35 #ifdef SG_MATH_EXCEPTION_CLASH
39 #include <simgear/debug/logstream.hxx>
40 #include <simgear/math/sg_random.h>
41 #include <simgear/misc/sg_path.hxx>
42 #include <simgear/misc/sgstream.hxx>
44 #include <Main/globals.hxx>
45 #include <Main/fg_props.hxx>
46 #include <Model/loader.hxx>
51 ////////////////////////////////////////////////////////////////////////
52 // Local static functions.
53 ////////////////////////////////////////////////////////////////////////
56 * Internal method to test whether a file exists.
58 * TODO: this should be moved to a SimGear library of local file
62 local_file_exists( const string& path ) {
63 sg_gzifstream in( path );
64 if ( ! in.is_open() ) {
73 ////////////////////////////////////////////////////////////////////////
74 // Implementation of FGNewMat::Object.
75 ////////////////////////////////////////////////////////////////////////
77 FGNewMat::Object::Object (const SGPropertyNode * node, double range_m)
78 : _models_loaded(false),
79 _coverage_m2(node->getDoubleValue("coverage-m2", 1000000)),
83 if (_coverage_m2 < 1000) {
84 SG_LOG(SG_INPUT, SG_ALERT, "Random object coverage " << _coverage_m2
85 << " is too small, forcing, to 1000");
89 // Note all the model paths
90 vector <SGPropertyNode_ptr> path_nodes = node->getChildren("path");
91 for (unsigned int i = 0; i < path_nodes.size(); i++)
92 _paths.push_back(path_nodes[i]->getStringValue());
94 // Note the heading type
95 string hdg = node->getStringValue("heading-type", "fixed");
97 _heading_type = HEADING_FIXED;
98 } else if (hdg == "billboard") {
99 _heading_type = HEADING_BILLBOARD;
100 } else if (hdg == "random") {
101 _heading_type = HEADING_RANDOM;
103 _heading_type = HEADING_FIXED;
104 SG_LOG(SG_INPUT, SG_ALERT, "Unknown heading type: " << hdg
105 << "; using 'fixed' instead.");
108 // uncomment to preload models
112 FGNewMat::Object::~Object ()
114 for (unsigned int i = 0; i < _models.size(); i++) {
115 if (_models[i] != 0) {
123 FGNewMat::Object::get_model_count () const
126 return _models.size();
130 FGNewMat::Object::load_models () const
132 // Load model only on demand
133 if (!_models_loaded) {
134 for (int i = 0; i < _paths.size(); i++) {
135 ssgEntity * entity = globals->get_model_loader()->load_model(_paths[i]);
137 // FIXME: this stuff can be handled
138 // in the XML wrapper as well (at least,
139 // the billboarding should be handled
141 float ranges[] = {0, _range_m};
142 ssgRangeSelector * lod = new ssgRangeSelector;
144 lod->setRanges(ranges, 2);
145 if (_heading_type == HEADING_BILLBOARD) {
146 ssgCutout * cutout = new ssgCutout(false);
147 cutout->addKid(entity);
152 _models.push_back(lod);
154 SG_LOG(SG_INPUT, SG_ALERT, "Failed to load object " << _paths[i]);
158 _models_loaded = true;
162 FGNewMat::Object::get_model (int index) const
164 load_models(); // comment this out if preloading models
165 return _models[index];
169 FGNewMat::Object::get_random_model () const
171 load_models(); // comment this out if preloading models
172 int nModels = _models.size();
173 int index = int(sg_random() * nModels);
174 if (index >= nModels)
176 return _models[index];
180 FGNewMat::Object::get_coverage_m2 () const
185 FGNewMat::Object::HeadingType
186 FGNewMat::Object::get_heading_type () const
188 return _heading_type;
193 ////////////////////////////////////////////////////////////////////////
194 // Implementation of FGNewMat::ObjectGroup.
195 ////////////////////////////////////////////////////////////////////////
197 FGNewMat::ObjectGroup::ObjectGroup (SGPropertyNode * node)
198 : _range_m(node->getDoubleValue("range-m", 2000))
200 // Load the object subnodes
201 vector<SGPropertyNode_ptr> object_nodes =
202 ((SGPropertyNode *)node)->getChildren("object");
203 for (unsigned int i = 0; i < object_nodes.size(); i++) {
204 const SGPropertyNode * object_node = object_nodes[i];
205 if (object_node->hasChild("path"))
206 _objects.push_back(new Object(object_node, _range_m));
208 SG_LOG(SG_INPUT, SG_ALERT, "No path supplied for object");
212 FGNewMat::ObjectGroup::~ObjectGroup ()
214 for (unsigned int i = 0; i < _objects.size(); i++) {
221 FGNewMat::ObjectGroup::get_range_m () const
227 FGNewMat::ObjectGroup::get_object_count () const
229 return _objects.size();
233 FGNewMat::ObjectGroup::get_object (int index) const
235 return _objects[index];
240 ////////////////////////////////////////////////////////////////////////
241 // Constructors and destructor.
242 ////////////////////////////////////////////////////////////////////////
245 FGNewMat::FGNewMat (const SGPropertyNode * props)
248 read_properties(props);
249 build_ssg_state(false);
252 FGNewMat::FGNewMat (const string &texpath)
255 texture_path = texpath;
256 build_ssg_state(true);
259 FGNewMat::FGNewMat (ssgSimpleState * s)
265 FGNewMat::~FGNewMat (void)
267 for (unsigned int i = 0; i < object_groups.size(); i++) {
268 delete object_groups[i];
269 object_groups[i] = 0;
275 ////////////////////////////////////////////////////////////////////////
277 ////////////////////////////////////////////////////////////////////////
280 FGNewMat::read_properties (const SGPropertyNode * props)
282 // Get the path to the texture
283 string tname = props->getStringValue("texture", "unknown.rgb");
284 SGPath tpath(globals->get_fg_root());
285 tpath.append("Textures.high");
287 if (!local_file_exists(tpath.str())) {
288 tpath = SGPath(globals->get_fg_root());
289 tpath.append("Textures");
292 texture_path = tpath.str();
294 xsize = props->getDoubleValue("xsize", 0.0);
295 ysize = props->getDoubleValue("ysize", 0.0);
296 wrapu = props->getBoolValue("wrapu", true);
297 wrapv = props->getBoolValue("wrapv", true);
298 mipmap = props->getBoolValue("mipmap", true);
299 light_coverage = props->getDoubleValue("light-coverage", 0.0);
301 ambient[0] = props->getDoubleValue("ambient/r", 0.0);
302 ambient[1] = props->getDoubleValue("ambient/g", 0.0);
303 ambient[2] = props->getDoubleValue("ambient/b", 0.0);
304 ambient[3] = props->getDoubleValue("ambient/a", 0.0);
306 diffuse[0] = props->getDoubleValue("diffuse/r", 0.0);
307 diffuse[1] = props->getDoubleValue("diffuse/g", 0.0);
308 diffuse[2] = props->getDoubleValue("diffuse/b", 0.0);
309 diffuse[3] = props->getDoubleValue("diffuse/a", 0.0);
311 specular[0] = props->getDoubleValue("specular/r", 0.0);
312 specular[1] = props->getDoubleValue("specular/g", 0.0);
313 specular[2] = props->getDoubleValue("specular/b", 0.0);
314 specular[3] = props->getDoubleValue("specular/a", 0.0);
316 emission[0] = props->getDoubleValue("emissive/r", 0.0);
317 emission[1] = props->getDoubleValue("emissive/g", 0.0);
318 emission[2] = props->getDoubleValue("emissive/b", 0.0);
319 emission[3] = props->getDoubleValue("emissive/a", 0.0);
321 vector<SGPropertyNode_ptr> object_group_nodes =
322 ((SGPropertyNode *)props)->getChildren("object-group");
323 for (unsigned int i = 0; i < object_group_nodes.size(); i++)
324 object_groups.push_back(new ObjectGroup(object_group_nodes[i]));
329 ////////////////////////////////////////////////////////////////////////
331 ////////////////////////////////////////////////////////////////////////
345 light_coverage = 0.0;
346 texture_loaded = false;
348 for (int i = 0; i < 4; i++)
349 ambient[i] = diffuse[i] = specular[i] = emission[i] = 0.0;
353 FGNewMat::load_texture ()
355 if (texture_loaded) {
358 SG_LOG( SG_GENERAL, SG_INFO, "Loading deferred texture " << texture_path );
359 textured->setTexture((char *)texture_path.c_str(), wrapu, wrapv, mipmap );
360 texture_loaded = true;
367 FGNewMat::build_ssg_state (bool defer_tex_load)
370 (fgGetBool("/sim/rendering/shading") ? GL_SMOOTH : GL_FLAT);
371 bool texture_default = fgGetBool("/sim/rendering/textures");
373 state = new ssgStateSelector(2);
376 textured = new ssgSimpleState();
379 nontextured = new ssgSimpleState();
382 // Set up the textured state
383 textured->setShadeModel( shade_model );
384 textured->enable( GL_LIGHTING );
385 textured->enable ( GL_CULL_FACE ) ;
386 textured->enable( GL_TEXTURE_2D );
387 textured->disable( GL_BLEND );
388 textured->disable( GL_ALPHA_TEST );
390 # ifdef GL_EXT_texture_filter_anisotropic
391 float max_anisotropy;
392 glGetFloatv( GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT, &max_anisotropy );
393 glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT,
395 cout << "Max anisotropy = " << max_anisotropy << endl;
398 if ( !defer_tex_load ) {
399 textured->setTexture( (char *)texture_path.c_str(), wrapu, wrapv );
400 texture_loaded = true;
402 texture_loaded = false;
404 textured->enable( GL_COLOR_MATERIAL );
406 textured->setColourMaterial( GL_AMBIENT_AND_DIFFUSE );
407 textured->setMaterial( GL_EMISSION, 0, 0, 0, 1 );
408 textured->setMaterial( GL_SPECULAR, 0, 0, 0, 1 );
410 textured->setMaterial ( GL_AMBIENT,
411 ambient[0], ambient[1],
412 ambient[2], ambient[3] ) ;
413 textured->setMaterial ( GL_DIFFUSE,
414 diffuse[0], diffuse[1],
415 diffuse[2], diffuse[3] ) ;
416 textured->setMaterial ( GL_SPECULAR,
417 specular[0], specular[1],
418 specular[2], specular[3] ) ;
419 textured->setMaterial ( GL_EMISSION,
420 emission[0], emission[1],
421 emission[2], emission[3] ) ;
424 // Set up the coloured state
425 nontextured->enable( GL_LIGHTING );
426 nontextured->setShadeModel( shade_model );
427 nontextured->enable ( GL_CULL_FACE ) ;
428 nontextured->disable( GL_TEXTURE_2D );
429 nontextured->disable( GL_BLEND );
430 nontextured->disable( GL_ALPHA_TEST );
431 nontextured->disable( GL_COLOR_MATERIAL );
433 nontextured->setMaterial ( GL_AMBIENT,
434 ambient[0], ambient[1],
435 ambient[2], ambient[3] ) ;
436 nontextured->setMaterial ( GL_DIFFUSE,
437 diffuse[0], diffuse[1],
438 diffuse[2], diffuse[3] ) ;
439 nontextured->setMaterial ( GL_SPECULAR,
440 specular[0], specular[1],
441 specular[2], specular[3] ) ;
442 nontextured->setMaterial ( GL_EMISSION,
443 emission[0], emission[1],
444 emission[2], emission[3] ) ;
446 state->setStep( 0, textured ); // textured
447 state->setStep( 1, nontextured ); // untextured
449 // Choose the appropriate starting state.
450 if ( texture_default ) {
451 state->selectStep(0);
453 state->selectStep(1);
458 void FGNewMat::set_ssg_state( ssgSimpleState *s )
460 state = new ssgStateSelector(2);
465 nontextured = new ssgSimpleState();
468 // Set up the coloured state
469 nontextured->enable( GL_LIGHTING );
470 nontextured->setShadeModel( GL_FLAT );
471 nontextured->enable ( GL_CULL_FACE ) ;
472 nontextured->disable( GL_TEXTURE_2D );
473 nontextured->disable( GL_BLEND );
474 nontextured->disable( GL_ALPHA_TEST );
475 nontextured->disable( GL_COLOR_MATERIAL );
477 /* cout << "ambient = " << ambient[0] << "," << ambient[1]
478 << "," << ambient[2] << endl; */
479 nontextured->setMaterial ( GL_AMBIENT,
480 ambient[0], ambient[1],
481 ambient[2], ambient[3] ) ;
482 nontextured->setMaterial ( GL_DIFFUSE,
483 diffuse[0], diffuse[1],
484 diffuse[2], diffuse[3] ) ;
485 nontextured->setMaterial ( GL_SPECULAR,
486 specular[0], specular[1],
487 specular[2], specular[3] ) ;
488 nontextured->setMaterial ( GL_EMISSION,
489 emission[0], emission[1],
490 emission[2], emission[3] ) ;
492 state->setStep( 0, textured ); // textured
493 state->setStep( 1, nontextured ); // untextured
495 // Choose the appropriate starting state.
496 state->selectStep(0);