]> git.mxchange.org Git - simgear.git/blob - simgear/scene/material/matmodel.cxx
Random buildings - initial commit.
[simgear.git] / simgear / scene / material / matmodel.cxx
1 // matmodel.cxx -- class to handle models tied to a material property
2 //
3 // Written by David Megginson, started May 1998.
4 //
5 // Copyright (C) 1998 - 2003  Curtis L. Olson  - http://www.flightgear.org/~curt
6 //
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.
11 //
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.
16 //
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.
20 //
21 // $Id$
22
23
24 #ifdef HAVE_CONFIG_H
25 #  include <simgear_config.h>
26 #endif
27
28 #include <simgear/compiler.h>
29
30 #include <map>
31
32
33 #include <osg/AlphaFunc>
34 #include <osg/Group>
35 #include <osg/LOD>
36 #include <osg/StateSet>
37 #include <osg/Transform>
38
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>
45
46 #include "matmodel.hxx"
47
48 using namespace simgear;
49 using std::string;
50 using std::map;\f
51 ////////////////////////////////////////////////////////////////////////
52 // Implementation of SGMatModel.
53 ////////////////////////////////////////////////////////////////////////
54
55 SGMatModel::SGMatModel (const SGPropertyNode * node, double range_m)
56   : _models_loaded(false),
57     _coverage_m2(node->getDoubleValue("coverage-m2", 1000000)),
58     _spacing_m(node->getDoubleValue("spacing-m", 20)),
59     _range_m(range_m)
60 {
61                                 // Sanity check
62   if (_coverage_m2 < 1000) {
63     SG_LOG(SG_INPUT, SG_ALERT, "Random object coverage " << _coverage_m2
64            << " is too small, forcing, to 1000");
65     _coverage_m2 = 1000;
66   }
67
68                                 // Note all the model paths
69   std::vector <SGPropertyNode_ptr> path_nodes = node->getChildren("path");
70   for (unsigned int i = 0; i < path_nodes.size(); i++)
71     _paths.push_back(path_nodes[i]->getStringValue());
72
73                                 // Note the heading type
74   string hdg = node->getStringValue("heading-type", "fixed");
75   if (hdg == "fixed") {
76     _heading_type = HEADING_FIXED;
77   } else if (hdg == "billboard") {
78     _heading_type = HEADING_BILLBOARD;
79   } else if (hdg == "random") {
80     _heading_type = HEADING_RANDOM;
81   } else if (hdg == "mask") {
82     _heading_type = HEADING_MASK;
83   } else {
84     _heading_type = HEADING_FIXED;
85     SG_LOG(SG_INPUT, SG_ALERT, "Unknown heading type: " << hdg
86            << "; using 'fixed' instead.");
87   }
88
89   // uncomment to preload models
90   // load_models();
91 }
92
93 SGMatModel::~SGMatModel ()
94 {
95 }
96
97 int
98 SGMatModel::get_model_count( SGPropertyNode *prop_root )
99 {
100   load_models( prop_root );
101   return _models.size();
102 }
103
104 inline void
105 SGMatModel::load_models( SGPropertyNode *prop_root )
106 {
107                                 // Load model only on demand
108   if (!_models_loaded) {
109     for (unsigned int i = 0; i < _paths.size(); i++) {
110       osg::Node *entity = SGModelLib::loadModel(_paths[i], prop_root);
111       if (entity != 0) {
112         // FIXME: this stuff can be handled
113         // in the XML wrapper as well (at least,
114         // the billboarding should be handled
115         // there).
116         
117         if (_heading_type == HEADING_BILLBOARD) {
118           // if the model is a billboard, it is likely :
119           // 1. a branch with only leaves,
120           // 2. a tree or a non rectangular shape faked by transparency
121           // We add alpha clamp then
122           osg::StateSet* stateSet = entity->getOrCreateStateSet();
123           osg::AlphaFunc* alphaFunc =
124             new osg::AlphaFunc(osg::AlphaFunc::GREATER, 0.01f);
125           stateSet->setAttributeAndModes(alphaFunc,
126                                          osg::StateAttribute::OVERRIDE);
127           stateSet->setRenderingHint(osg::StateSet::TRANSPARENT_BIN);
128         } 
129         
130         _models.push_back(entity);
131         
132       } else {
133         SG_LOG(SG_INPUT, SG_ALERT, "Failed to load object " << _paths[i]);
134       }
135     }
136   }
137   _models_loaded = true;
138 }
139
140 osg::Node*
141 SGMatModel::get_random_model( SGPropertyNode *prop_root, mt* seed )
142 {
143   load_models( prop_root ); // comment this out if preloading models
144   int nModels = _models.size();
145   return _models[mt_rand(seed) * nModels].get();
146 }
147
148 double
149 SGMatModel::get_coverage_m2 () const
150 {
151   return _coverage_m2;
152 }
153
154 double SGMatModel::get_range_m() const
155 {
156   return _range_m;
157 }
158
159 double SGMatModel::get_spacing_m() const
160 {
161   return _spacing_m;
162 }
163
164 double SGMatModel::get_randomized_range_m(mt* seed) const
165 {
166   double lrand = mt_rand(seed);
167   
168   // Note that the LoD is not completely randomized.
169   // 10% at 2   * range_m
170   // 30% at 1.5 * range_m
171   // 60% at 1   * range_m
172   if (lrand < 0.1) return 2   * _range_m;
173   if (lrand < 0.4) return 1.5 * _range_m;
174   else return _range_m;
175 }
176
177 SGMatModel::HeadingType
178 SGMatModel::get_heading_type () const
179 {
180   return _heading_type;
181 }
182
183
184 \f
185 ////////////////////////////////////////////////////////////////////////
186 // Implementation of SGMatModelGroup.
187 ////////////////////////////////////////////////////////////////////////
188
189 SGMatModelGroup::SGMatModelGroup (SGPropertyNode * node)
190   : _range_m(node->getDoubleValue("range-m", 2000))
191 {
192                                 // Load the object subnodes
193   std::vector<SGPropertyNode_ptr> object_nodes =
194     ((SGPropertyNode *)node)->getChildren("object");
195   for (unsigned int i = 0; i < object_nodes.size(); i++) {
196     const SGPropertyNode * object_node = object_nodes[i];
197     if (object_node->hasChild("path"))
198       _objects.push_back(new SGMatModel(object_node, _range_m));
199     else
200       SG_LOG(SG_INPUT, SG_ALERT, "No path supplied for object");
201   }
202 }
203
204 SGMatModelGroup::~SGMatModelGroup ()
205 {
206 }
207
208 double
209 SGMatModelGroup::get_range_m () const
210 {
211   return _range_m;
212 }
213
214 int
215 SGMatModelGroup::get_object_count () const
216 {
217   return _objects.size();
218 }
219
220 SGMatModel *
221 SGMatModelGroup::get_object (int index) const
222 {
223   return _objects[index];
224 }
225
226 // end of matmodel.cxx