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 #include <simgear/debug/logstream.hxx>
40 #include <simgear/math/SGMath.hxx>
41 #include <simgear/math/sg_random.h>
42 #include <simgear/misc/sg_path.hxx>
43 #include <simgear/misc/sgstream.hxx>
44 #include <simgear/scene/model/modellib.hxx>
46 #include "matmodel.hxx"
48 using namespace simgear;
51 ////////////////////////////////////////////////////////////////////////
52 // Implementation of SGMatModel.
53 ////////////////////////////////////////////////////////////////////////
55 SGMatModel::SGMatModel (const SGPropertyNode * node, double range_m)
56 : _models_loaded(false),
57 _coverage_m2(node->getDoubleValue("coverage-m2", 1000000)),
61 if (_coverage_m2 < 1000) {
62 SG_LOG(SG_INPUT, SG_ALERT, "Random object coverage " << _coverage_m2
63 << " is too small, forcing, to 1000");
67 // Note all the model paths
68 std::vector <SGPropertyNode_ptr> path_nodes = node->getChildren("path");
69 for (unsigned int i = 0; i < path_nodes.size(); i++)
70 _paths.push_back(path_nodes[i]->getStringValue());
72 // Note the heading type
73 string hdg = node->getStringValue("heading-type", "fixed");
75 _heading_type = HEADING_FIXED;
76 } else if (hdg == "billboard") {
77 _heading_type = HEADING_BILLBOARD;
78 } else if (hdg == "random") {
79 _heading_type = HEADING_RANDOM;
81 _heading_type = HEADING_FIXED;
82 SG_LOG(SG_INPUT, SG_ALERT, "Unknown heading type: " << hdg
83 << "; using 'fixed' instead.");
86 // uncomment to preload models
90 SGMatModel::~SGMatModel ()
95 SGMatModel::get_model_count( SGPropertyNode *prop_root )
97 load_models( prop_root );
98 return _models.size();
102 SGMatModel::load_models( SGPropertyNode *prop_root )
104 // Load model only on demand
105 if (!_models_loaded) {
106 for (unsigned int i = 0; i < _paths.size(); i++) {
107 osg::Node *entity = SGModelLib::loadModel(_paths[i], prop_root);
109 // FIXME: this stuff can be handled
110 // in the XML wrapper as well (at least,
111 // the billboarding should be handled
114 if (_heading_type == HEADING_BILLBOARD) {
115 // if the model is a billboard, it is likely :
116 // 1. a branch with only leaves,
117 // 2. a tree or a non rectangular shape faked by transparency
118 // We add alpha clamp then
119 osg::StateSet* stateSet = entity->getOrCreateStateSet();
120 osg::AlphaFunc* alphaFunc =
121 new osg::AlphaFunc(osg::AlphaFunc::GREATER, 0.01f);
122 stateSet->setAttributeAndModes(alphaFunc,
123 osg::StateAttribute::OVERRIDE);
124 stateSet->setRenderingHint(osg::StateSet::TRANSPARENT_BIN);
127 _models.push_back(entity);
130 SG_LOG(SG_INPUT, SG_ALERT, "Failed to load object " << _paths[i]);
134 _models_loaded = true;
138 SGMatModel::get_random_model( SGPropertyNode *prop_root, mt seed )
140 load_models( prop_root ); // comment this out if preloading models
141 int nModels = _models.size();
142 return _models[mt_rand(&seed) * nModels].get();
146 SGMatModel::get_coverage_m2 () const
151 double SGMatModel::get_range_m() const
156 double SGMatModel::get_randomized_range_m(mt* seed) const
158 double lrand = mt_rand(seed);
160 // Note that the LoD is not completely randomized.
161 // 10% at 2 * range_m
162 // 30% at 1.5 * range_m
163 // 60% at 1 * range_m
164 if (lrand < 0.1) return 2 * _range_m;
165 if (lrand < 0.4) return 1.5 * _range_m;
166 else return _range_m;
169 SGMatModel::HeadingType
170 SGMatModel::get_heading_type () const
172 return _heading_type;
177 ////////////////////////////////////////////////////////////////////////
178 // Implementation of SGMatModelGroup.
179 ////////////////////////////////////////////////////////////////////////
181 SGMatModelGroup::SGMatModelGroup (SGPropertyNode * node)
182 : _range_m(node->getDoubleValue("range-m", 2000))
184 // Load the object subnodes
185 std::vector<SGPropertyNode_ptr> object_nodes =
186 ((SGPropertyNode *)node)->getChildren("object");
187 for (unsigned int i = 0; i < object_nodes.size(); i++) {
188 const SGPropertyNode * object_node = object_nodes[i];
189 if (object_node->hasChild("path"))
190 _objects.push_back(new SGMatModel(object_node, _range_m));
192 SG_LOG(SG_INPUT, SG_ALERT, "No path supplied for object");
196 SGMatModelGroup::~SGMatModelGroup ()
201 SGMatModelGroup::get_range_m () const
207 SGMatModelGroup::get_object_count () const
209 return _objects.size();
213 SGMatModelGroup::get_object (int index) const
215 return _objects[index];
218 // end of matmodel.cxx