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>
50 ////////////////////////////////////////////////////////////////////////
51 // Local static variables.
52 // FIXME: write a proper manager.
53 ////////////////////////////////////////////////////////////////////////
55 // Objects already loaded (that can be reused).
56 map<string,ssgEntity *> object_map;
60 ////////////////////////////////////////////////////////////////////////
61 // Local static functions.
62 ////////////////////////////////////////////////////////////////////////
64 // FIXME: this is totally evil and non-robust: it assumes that
65 // entities will never be refcounted to 0 (which is safe for now).
67 load_object (char * path)
69 ssgEntity * object = object_map[path];
71 object = ssgLoad(path);
72 object_map[path] = object;
78 * Internal method to test whether a file exists.
80 * TODO: this should be moved to a SimGear library of local file
84 local_file_exists( const string& path ) {
85 sg_gzifstream in( path );
86 if ( ! in.is_open() ) {
95 ////////////////////////////////////////////////////////////////////////
96 // Implementation of FGNewMat::Object.
97 ////////////////////////////////////////////////////////////////////////
99 FGNewMat::Object::Object (const SGPropertyNode * node, double range_m)
100 : _models_loaded(false),
101 _coverage_m2(node->getDoubleValue("coverage-m2", 1000000)),
105 if (_coverage_m2 < 1000) {
106 SG_LOG(SG_INPUT, SG_ALERT, "Random object coverage " << _coverage_m2
107 << " is too small, forcing, to 1000");
111 // Note all the model paths
112 vector <SGPropertyNode_ptr> path_nodes = node->getChildren("path");
113 for (unsigned int i = 0; i < path_nodes.size(); i++)
114 _paths.push_back(path_nodes[i]->getStringValue());
116 // Note the heading type
117 string hdg = node->getStringValue("heading-type", "fixed");
118 if (hdg == "fixed") {
119 _heading_type = HEADING_FIXED;
120 } else if (hdg == "billboard") {
121 _heading_type = HEADING_BILLBOARD;
122 } else if (hdg == "random") {
123 _heading_type = HEADING_RANDOM;
125 _heading_type = HEADING_FIXED;
126 SG_LOG(SG_INPUT, SG_ALERT, "Unknown heading type: " << hdg
127 << "; using 'fixed' instead.");
130 // uncomment to preload models
134 FGNewMat::Object::~Object ()
136 for (unsigned int i = 0; i < _models.size(); i++) {
137 if (_models[i] != 0) {
145 FGNewMat::Object::get_model_count () const
147 return _models.size();
151 FGNewMat::Object::load_models () const
153 cout << "loading ground cover objects:" << endl;
155 // Load model only on demand
156 if (!_models_loaded) {
157 for (unsigned int i = 0; i < _paths.size(); i++) {
158 SGPath path = globals->get_fg_root();
159 path.append(_paths[i]);
160 ssgTexturePath((char *)path.dir().c_str());
161 ssgEntity * entity = load_object((char *)path.c_str());
162 cout << " " << path.str() << endl;
164 float ranges[] = {0, _range_m};
165 ssgRangeSelector * lod = new ssgRangeSelector;
166 lod->setRanges(ranges, 2);
167 if (_heading_type == HEADING_BILLBOARD) {
168 ssgCutout * cutout = new ssgCutout(false);
169 cutout->addKid(entity);
175 _models.push_back(lod);
177 SG_LOG(SG_INPUT, SG_ALERT, "Failed to load object " << path.str());
181 _models_loaded = true;
185 FGNewMat::Object::get_model (int index) const
187 load_models(); // comment this out if preloading models
188 return _models[index];
192 FGNewMat::Object::get_random_model () const
194 load_models(); // comment this out if preloading models
195 int nModels = _models.size();
196 int index = int(sg_random() * nModels);
197 if (index >= nModels)
199 return _models[index];
203 FGNewMat::Object::get_coverage_m2 () const
208 FGNewMat::Object::HeadingType
209 FGNewMat::Object::get_heading_type () const
211 return _heading_type;
216 ////////////////////////////////////////////////////////////////////////
217 // Implementation of FGNewMat::ObjectGroup.
218 ////////////////////////////////////////////////////////////////////////
220 FGNewMat::ObjectGroup::ObjectGroup (SGPropertyNode * node)
221 : _range_m(node->getDoubleValue("range-m", 2000))
223 // Load the object subnodes
224 vector<SGPropertyNode_ptr> object_nodes =
225 ((SGPropertyNode *)node)->getChildren("object");
226 for (unsigned int i = 0; i < object_nodes.size(); i++) {
227 const SGPropertyNode * object_node = object_nodes[i];
228 if (object_node->hasChild("path"))
229 _objects.push_back(new Object(object_node, _range_m));
231 SG_LOG(SG_INPUT, SG_ALERT, "No path supplied for object");
235 FGNewMat::ObjectGroup::~ObjectGroup ()
237 for (unsigned int i = 0; i < _objects.size(); i++) {
244 FGNewMat::ObjectGroup::get_range_m () const
250 FGNewMat::ObjectGroup::get_object_count () const
252 return _objects.size();
256 FGNewMat::ObjectGroup::get_object (int index) const
258 return _objects[index];
263 ////////////////////////////////////////////////////////////////////////
264 // Constructors and destructor.
265 ////////////////////////////////////////////////////////////////////////
268 FGNewMat::FGNewMat (const SGPropertyNode * props)
271 read_properties(props);
272 build_ssg_state(false);
275 FGNewMat::FGNewMat (const string &texpath)
278 texture_path = texpath;
279 build_ssg_state(true);
282 FGNewMat::FGNewMat (ssgSimpleState * s)
288 FGNewMat::~FGNewMat (void)
290 for (unsigned int i = 0; i < object_groups.size(); i++) {
291 delete object_groups[i];
292 object_groups[i] = 0;
298 ////////////////////////////////////////////////////////////////////////
300 ////////////////////////////////////////////////////////////////////////
303 FGNewMat::read_properties (const SGPropertyNode * props)
305 // Get the path to the texture
306 string tname = props->getStringValue("texture", "unknown.rgb");
307 SGPath tpath(globals->get_fg_root());
308 tpath.append("Textures.high");
310 if (!local_file_exists(tpath.str())) {
311 tpath = SGPath(globals->get_fg_root());
312 tpath.append("Textures");
315 texture_path = tpath.str();
317 xsize = props->getDoubleValue("xsize", 0.0);
318 ysize = props->getDoubleValue("ysize", 0.0);
319 wrapu = props->getBoolValue("wrapu", true);
320 wrapv = props->getBoolValue("wrapv", true);
321 mipmap = props->getBoolValue("mipmap", true);
322 light_coverage = props->getDoubleValue("light-coverage", 0.0);
324 ambient[0] = props->getDoubleValue("ambient/r", 0.0);
325 ambient[1] = props->getDoubleValue("ambient/g", 0.0);
326 ambient[2] = props->getDoubleValue("ambient/b", 0.0);
327 ambient[3] = props->getDoubleValue("ambient/a", 0.0);
329 diffuse[0] = props->getDoubleValue("diffuse/r", 0.0);
330 diffuse[1] = props->getDoubleValue("diffuse/g", 0.0);
331 diffuse[2] = props->getDoubleValue("diffuse/b", 0.0);
332 diffuse[3] = props->getDoubleValue("diffuse/a", 0.0);
334 specular[0] = props->getDoubleValue("specular/r", 0.0);
335 specular[1] = props->getDoubleValue("specular/g", 0.0);
336 specular[2] = props->getDoubleValue("specular/b", 0.0);
337 specular[3] = props->getDoubleValue("specular/a", 0.0);
339 emission[0] = props->getDoubleValue("emissive/r", 0.0);
340 emission[1] = props->getDoubleValue("emissive/g", 0.0);
341 emission[2] = props->getDoubleValue("emissive/b", 0.0);
342 emission[3] = props->getDoubleValue("emissive/a", 0.0);
344 vector<SGPropertyNode_ptr> object_group_nodes =
345 ((SGPropertyNode *)props)->getChildren("object-group");
346 for (unsigned int i = 0; i < object_group_nodes.size(); i++)
347 object_groups.push_back(new ObjectGroup(object_group_nodes[i]));
352 ////////////////////////////////////////////////////////////////////////
354 ////////////////////////////////////////////////////////////////////////
368 light_coverage = 0.0;
369 texture_loaded = false;
371 for (int i = 0; i < 4; i++)
372 ambient[i] = diffuse[i] = specular[i] = emission[i] = 0.0;
376 FGNewMat::load_texture ()
378 if (texture_loaded) {
381 SG_LOG( SG_GENERAL, SG_INFO, "Loading deferred texture " << texture_path );
382 textured->setTexture((char *)texture_path.c_str(), wrapu, wrapv, mipmap );
383 texture_loaded = true;
390 FGNewMat::build_ssg_state (bool defer_tex_load)
393 (fgGetBool("/sim/rendering/shading") ? GL_SMOOTH : GL_FLAT);
394 bool texture_default = fgGetBool("/sim/rendering/textures");
396 state = new ssgStateSelector(2);
399 textured = new ssgSimpleState();
402 nontextured = new ssgSimpleState();
405 // Set up the textured state
406 textured->setShadeModel( shade_model );
407 textured->enable( GL_LIGHTING );
408 textured->enable ( GL_CULL_FACE ) ;
409 textured->enable( GL_TEXTURE_2D );
410 textured->disable( GL_BLEND );
411 textured->disable( GL_ALPHA_TEST );
413 # ifdef GL_EXT_texture_filter_anisotropic
414 float max_anisotropy;
415 glGetFloatv( GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT, &max_anisotropy );
416 glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT,
418 cout << "Max anisotropy = " << max_anisotropy << endl;
421 if ( !defer_tex_load ) {
422 textured->setTexture( (char *)texture_path.c_str(), wrapu, wrapv );
423 texture_loaded = true;
425 texture_loaded = false;
427 textured->enable( GL_COLOR_MATERIAL );
428 textured->setColourMaterial( GL_AMBIENT_AND_DIFFUSE );
429 textured->setMaterial( GL_EMISSION, 0, 0, 0, 1 );
430 textured->setMaterial( GL_SPECULAR, 0, 0, 0, 1 );
432 // Set up the coloured state
433 nontextured->enable( GL_LIGHTING );
434 nontextured->setShadeModel( shade_model );
435 nontextured->enable ( GL_CULL_FACE ) ;
436 nontextured->disable( GL_TEXTURE_2D );
437 nontextured->disable( GL_BLEND );
438 nontextured->disable( GL_ALPHA_TEST );
439 nontextured->disable( GL_COLOR_MATERIAL );
441 nontextured->setMaterial ( GL_AMBIENT,
442 ambient[0], ambient[1],
443 ambient[2], ambient[3] ) ;
444 nontextured->setMaterial ( GL_DIFFUSE,
445 diffuse[0], diffuse[1],
446 diffuse[2], diffuse[3] ) ;
447 nontextured->setMaterial ( GL_SPECULAR,
448 specular[0], specular[1],
449 specular[2], specular[3] ) ;
450 nontextured->setMaterial ( GL_EMISSION,
451 emission[0], emission[1],
452 emission[2], emission[3] ) ;
454 state->setStep( 0, textured ); // textured
455 state->setStep( 1, nontextured ); // untextured
457 // Choose the appropriate starting state.
458 if ( texture_default ) {
459 state->selectStep(0);
461 state->selectStep(1);
466 void FGNewMat::set_ssg_state( ssgSimpleState *s )
468 state = new ssgStateSelector(2);
473 nontextured = new ssgSimpleState();
476 // Set up the coloured state
477 nontextured->enable( GL_LIGHTING );
478 nontextured->setShadeModel( GL_FLAT );
479 nontextured->enable ( GL_CULL_FACE ) ;
480 nontextured->disable( GL_TEXTURE_2D );
481 nontextured->disable( GL_BLEND );
482 nontextured->disable( GL_ALPHA_TEST );
483 nontextured->disable( GL_COLOR_MATERIAL );
485 /* cout << "ambient = " << ambient[0] << "," << ambient[1]
486 << "," << ambient[2] << endl; */
487 nontextured->setMaterial ( GL_AMBIENT,
488 ambient[0], ambient[1],
489 ambient[2], ambient[3] ) ;
490 nontextured->setMaterial ( GL_DIFFUSE,
491 diffuse[0], diffuse[1],
492 diffuse[2], diffuse[3] ) ;
493 nontextured->setMaterial ( GL_SPECULAR,
494 specular[0], specular[1],
495 specular[2], specular[3] ) ;
496 nontextured->setMaterial ( GL_EMISSION,
497 emission[0], emission[1],
498 emission[2], emission[3] ) ;
500 state->setStep( 0, textured ); // textured
501 state->setStep( 1, nontextured ); // untextured
503 // Choose the appropriate starting state.
504 state->selectStep(0);