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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
25 # include <simgear_config.h>
28 #include <simgear/compiler.h>
33 #include <osg/AlphaFunc>
36 #include <osg/StateSet>
37 #include <osg/Transform>
39 #ifdef SG_MATH_EXCEPTION_CLASH
43 #include <simgear/debug/logstream.hxx>
44 #include <simgear/math/sg_random.h>
45 #include <simgear/misc/sg_path.hxx>
46 #include <simgear/misc/sgstream.hxx>
47 #include <simgear/scene/model/modellib.hxx>
49 #include "matmodel.hxx"
52 ////////////////////////////////////////////////////////////////////////
53 // Local static functions.
54 ////////////////////////////////////////////////////////////////////////
57 * Internal method to test whether a file exists.
59 * TODO: this should be moved to a SimGear library of local file
63 local_file_exists( const string& path ) {
64 sg_gzifstream in( path );
65 if ( ! in.is_open() ) {
74 ////////////////////////////////////////////////////////////////////////
75 // Implementation of SGMatModel.
76 ////////////////////////////////////////////////////////////////////////
78 SGMatModel::SGMatModel (const SGPropertyNode * node, double range_m)
79 : _models_loaded(false),
80 _coverage_m2(node->getDoubleValue("coverage-m2", 1000000)),
84 if (_coverage_m2 < 1000) {
85 SG_LOG(SG_INPUT, SG_ALERT, "Random object coverage " << _coverage_m2
86 << " is too small, forcing, to 1000");
90 // Note all the model paths
91 vector <SGPropertyNode_ptr> path_nodes = node->getChildren("path");
92 for (unsigned int i = 0; i < path_nodes.size(); i++)
93 _paths.push_back(path_nodes[i]->getStringValue());
95 // Note the heading type
96 string hdg = node->getStringValue("heading-type", "fixed");
98 _heading_type = HEADING_FIXED;
99 } else if (hdg == "billboard") {
100 _heading_type = HEADING_BILLBOARD;
101 } else if (hdg == "random") {
102 _heading_type = HEADING_RANDOM;
104 _heading_type = HEADING_FIXED;
105 SG_LOG(SG_INPUT, SG_ALERT, "Unknown heading type: " << hdg
106 << "; using 'fixed' instead.");
109 // uncomment to preload models
113 SGMatModel::~SGMatModel ()
118 SGMatModel::get_model_count( SGModelLib *modellib,
119 const string &fg_root,
120 SGPropertyNode *prop_root,
121 double sim_time_sec )
123 load_models( modellib, fg_root, prop_root, sim_time_sec );
124 return _models.size();
128 SGMatModel::load_models ( SGModelLib *modellib,
129 const string &fg_root,
130 SGPropertyNode *prop_root,
131 double sim_time_sec )
133 // Load model only on demand
134 if (!_models_loaded) {
135 for (unsigned int i = 0; i < _paths.size(); i++) {
136 osg::Node *entity = modellib->load_model( fg_root, _paths[i],
137 prop_root, sim_time_sec,
138 /*cache_object*/ true );
140 // FIXME: this stuff can be handled
141 // in the XML wrapper as well (at least,
142 // the billboarding should be handled
145 if (_heading_type == HEADING_BILLBOARD) {
146 // if the model is a billboard, it is likely :
147 // 1. a branch with only leaves,
148 // 2. a tree or a non rectangular shape faked by transparency
149 // We add alpha clamp then
150 osg::StateSet* stateSet = entity->getOrCreateStateSet();
151 osg::AlphaFunc* alphaFunc =
152 new osg::AlphaFunc(osg::AlphaFunc::GREATER, 0.01f);
153 stateSet->setAttributeAndModes(alphaFunc,
154 osg::StateAttribute::OVERRIDE);
155 stateSet->setRenderingHint(osg::StateSet::TRANSPARENT_BIN);
158 _models.push_back(entity);
161 SG_LOG(SG_INPUT, SG_ALERT, "Failed to load object " << _paths[i]);
165 _models_loaded = true;
169 SGMatModel::get_model( int index,
170 SGModelLib *modellib,
171 const string &fg_root,
172 SGPropertyNode *prop_root,
173 double sim_time_sec )
175 load_models( modellib, fg_root, prop_root, sim_time_sec ); // comment this out if preloading models
176 return _models[index].get();
180 SGMatModel::get_random_model( SGModelLib *modellib,
181 const string &fg_root,
182 SGPropertyNode *prop_root,
183 double sim_time_sec )
185 load_models( modellib, fg_root, prop_root, sim_time_sec ); // comment this out if preloading models
186 int nModels = _models.size();
187 int index = int(sg_random() * nModels);
188 if (index >= nModels)
190 return _models[index].get();
194 SGMatModel::get_coverage_m2 () const
199 double SGMatModel::get_range_m() const
204 double SGMatModel::get_randomized_range_m(mt* seed) const
206 double lrand = mt_rand(seed);
208 // Note that the LoD is not completely randomized.
209 // 10% at 2 * range_m
210 // 30% at 1.5 * range_m
211 // 60% at 1 * range_m
212 if (lrand < 0.1) return 2 * _range_m;
213 if (lrand < 0.4) return 1.5 * _range_m;
214 else return _range_m;
217 SGMatModel::HeadingType
218 SGMatModel::get_heading_type () const
220 return _heading_type;
225 ////////////////////////////////////////////////////////////////////////
226 // Implementation of SGMatModelGroup.
227 ////////////////////////////////////////////////////////////////////////
229 SGMatModelGroup::SGMatModelGroup (SGPropertyNode * node)
230 : _range_m(node->getDoubleValue("range-m", 2000))
232 // Load the object subnodes
233 vector<SGPropertyNode_ptr> object_nodes =
234 ((SGPropertyNode *)node)->getChildren("object");
235 for (unsigned int i = 0; i < object_nodes.size(); i++) {
236 const SGPropertyNode * object_node = object_nodes[i];
237 if (object_node->hasChild("path"))
238 _objects.push_back(new SGMatModel(object_node, _range_m));
240 SG_LOG(SG_INPUT, SG_ALERT, "No path supplied for object");
244 SGMatModelGroup::~SGMatModelGroup ()
249 SGMatModelGroup::get_range_m () const
255 SGMatModelGroup::get_object_count () const
257 return _objects.size();
261 SGMatModelGroup::get_object (int index) const
263 return _objects[index];
266 // end of matmodel.cxx