1 // matmodel.cxx -- class to handle models tied to a material property
3 // Written by David Megginson, started May 1998.
5 // Copyright (C) 1998 - 2003 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., 675 Mass Ave, Cambridge, MA 02139, USA.
25 # include <simgear_config.h>
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>
43 #include <simgear/scene/model/modellib.hxx>
45 #include "matmodel.hxx"
48 ////////////////////////////////////////////////////////////////////////
49 // Local static functions.
50 ////////////////////////////////////////////////////////////////////////
53 * Internal method to test whether a file exists.
55 * TODO: this should be moved to a SimGear library of local file
59 local_file_exists( const string& path ) {
60 sg_gzifstream in( path );
61 if ( ! in.is_open() ) {
70 ////////////////////////////////////////////////////////////////////////
71 // Implementation of SGMatModel.
72 ////////////////////////////////////////////////////////////////////////
74 SGMatModel::SGMatModel (const SGPropertyNode * node, double range_m)
75 : _models_loaded(false),
76 _coverage_m2(node->getDoubleValue("coverage-m2", 1000000)),
80 if (_coverage_m2 < 1000) {
81 SG_LOG(SG_INPUT, SG_ALERT, "Random object coverage " << _coverage_m2
82 << " is too small, forcing, to 1000");
86 // Note all the model paths
87 vector <SGPropertyNode_ptr> path_nodes = node->getChildren("path");
88 for (unsigned int i = 0; i < path_nodes.size(); i++)
89 _paths.push_back(path_nodes[i]->getStringValue());
91 // Note the heading type
92 string hdg = node->getStringValue("heading-type", "fixed");
94 _heading_type = HEADING_FIXED;
95 } else if (hdg == "billboard") {
96 _heading_type = HEADING_BILLBOARD;
97 } else if (hdg == "random") {
98 _heading_type = HEADING_RANDOM;
100 _heading_type = HEADING_FIXED;
101 SG_LOG(SG_INPUT, SG_ALERT, "Unknown heading type: " << hdg
102 << "; using 'fixed' instead.");
105 // uncomment to preload models
109 SGMatModel::~SGMatModel ()
111 for (unsigned int i = 0; i < _models.size(); i++) {
112 if (_models[i] != 0) {
120 SGMatModel::get_model_count( SGModelLib *modellib,
121 const string &fg_root,
122 SGPropertyNode *prop_root,
123 double sim_time_sec )
125 load_models( modellib, fg_root, prop_root, sim_time_sec );
126 return _models.size();
130 setAlphaClampToBranch( ssgBranch *b, float clamp )
132 int nb = b->getNumKids();
133 for (int i = 0; i<nb; i++) {
134 ssgEntity *e = b->getKid(i);
135 if (e->isAKindOf(ssgTypeLeaf())) {
136 ssgSimpleState*s = (ssgSimpleState*)((ssgLeaf*)e)->getState();
137 s->enable( GL_ALPHA_TEST );
138 s->setAlphaClamp( clamp );
139 } else if (e->isAKindOf(ssgTypeBranch())) {
140 setAlphaClampToBranch( (ssgBranch*)e, clamp );
146 SGMatModel::load_models ( SGModelLib *modellib,
147 const string &fg_root,
148 SGPropertyNode *prop_root,
149 double sim_time_sec )
151 // Load model only on demand
152 if (!_models_loaded) {
153 for (unsigned int i = 0; i < _paths.size(); i++) {
154 ssgEntity *entity = modellib->load_model( fg_root, _paths[i],
155 prop_root, sim_time_sec );
157 // FIXME: this stuff can be handled
158 // in the XML wrapper as well (at least,
159 // the billboarding should be handled
161 float ranges[] = {0, _range_m};
162 ssgRangeSelector * lod = new ssgRangeSelector;
164 lod->setRanges(ranges, 2);
165 if (_heading_type == HEADING_BILLBOARD) {
166 // if the model is a billboard, it is likely :
167 // 1. a branch with only leaves,
168 // 2. a tree or a non rectangular shape faked by transparency
169 // We add alpha clamp then
170 if ( entity->isAKindOf(ssgTypeBranch()) ) {
171 ssgBranch *b = (ssgBranch *)entity;
172 setAlphaClampToBranch( b, 0.01f );
174 ssgCutout * cutout = new ssgCutout(false);
175 cutout->addKid(entity);
180 _models.push_back(lod);
182 SG_LOG(SG_INPUT, SG_ALERT, "Failed to load object " << _paths[i]);
186 _models_loaded = true;
190 SGMatModel::get_model( int index,
191 SGModelLib *modellib,
192 const string &fg_root,
193 SGPropertyNode *prop_root,
194 double sim_time_sec )
196 load_models( modellib, fg_root, prop_root, sim_time_sec ); // comment this out if preloading models
197 return _models[index];
201 SGMatModel::get_random_model( SGModelLib *modellib,
202 const string &fg_root,
203 SGPropertyNode *prop_root,
204 double sim_time_sec )
206 load_models( modellib, fg_root, prop_root, sim_time_sec ); // comment this out if preloading models
207 int nModels = _models.size();
208 int index = int(sg_random() * nModels);
209 if (index >= nModels)
211 return _models[index];
215 SGMatModel::get_coverage_m2 () const
220 SGMatModel::HeadingType
221 SGMatModel::get_heading_type () const
223 return _heading_type;
228 ////////////////////////////////////////////////////////////////////////
229 // Implementation of SGMatModelGroup.
230 ////////////////////////////////////////////////////////////////////////
232 SGMatModelGroup::SGMatModelGroup (SGPropertyNode * node)
233 : _range_m(node->getDoubleValue("range-m", 2000))
235 // Load the object subnodes
236 vector<SGPropertyNode_ptr> object_nodes =
237 ((SGPropertyNode *)node)->getChildren("object");
238 for (unsigned int i = 0; i < object_nodes.size(); i++) {
239 const SGPropertyNode * object_node = object_nodes[i];
240 if (object_node->hasChild("path"))
241 _objects.push_back(new SGMatModel(object_node, _range_m));
243 SG_LOG(SG_INPUT, SG_ALERT, "No path supplied for object");
247 SGMatModelGroup::~SGMatModelGroup ()
249 for (unsigned int i = 0; i < _objects.size(); i++) {
256 SGMatModelGroup::get_range_m () const
262 SGMatModelGroup::get_object_count () const
264 return _objects.size();
268 SGMatModelGroup::get_object (int index) const
270 return _objects[index];
274 // end of matmodel.cxx