]> git.mxchange.org Git - simgear.git/blob - simgear/scene/material/matmodel.cxx
Reduce compiler.h to almost nothing (but it's worth keeping around I think, for
[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 SG_USING_STD(map);
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/sg_random.h>
41 #include <simgear/misc/sg_path.hxx>
42 #include <simgear/misc/sgstream.hxx>
43 #include <simgear/scene/model/modellib.hxx>
44
45 #include "matmodel.hxx"
46
47 using namespace simgear;
48
49 \f
50 ////////////////////////////////////////////////////////////////////////
51 // Local static functions.
52 ////////////////////////////////////////////////////////////////////////
53
54 /**
55  * Internal method to test whether a file exists.
56  *
57  * TODO: this should be moved to a SimGear library of local file
58  * functions.
59  */
60 static inline bool
61 local_file_exists( const string& path ) {
62     sg_gzifstream in( path );
63     if ( ! in.is_open() ) {
64         return false;
65     } else {
66         return true;
67     }
68 }
69
70
71 \f
72 ////////////////////////////////////////////////////////////////////////
73 // Implementation of SGMatModel.
74 ////////////////////////////////////////////////////////////////////////
75
76 SGMatModel::SGMatModel (const SGPropertyNode * node, double range_m)
77   : _models_loaded(false),
78     _coverage_m2(node->getDoubleValue("coverage-m2", 1000000)),
79     _range_m(range_m)
80 {
81                                 // Sanity check
82   if (_coverage_m2 < 1000) {
83     SG_LOG(SG_INPUT, SG_ALERT, "Random object coverage " << _coverage_m2
84            << " is too small, forcing, to 1000");
85     _coverage_m2 = 1000;
86   }
87
88                                 // Note all the model paths
89   vector <SGPropertyNode_ptr> path_nodes = node->getChildren("path");
90   for (unsigned int i = 0; i < path_nodes.size(); i++)
91     _paths.push_back(path_nodes[i]->getStringValue());
92
93                                 // Note the heading type
94   string hdg = node->getStringValue("heading-type", "fixed");
95   if (hdg == "fixed") {
96     _heading_type = HEADING_FIXED;
97   } else if (hdg == "billboard") {
98     _heading_type = HEADING_BILLBOARD;
99   } else if (hdg == "random") {
100     _heading_type = HEADING_RANDOM;
101   } else {
102     _heading_type = HEADING_FIXED;
103     SG_LOG(SG_INPUT, SG_ALERT, "Unknown heading type: " << hdg
104            << "; using 'fixed' instead.");
105   }
106
107   // uncomment to preload models
108   // load_models();
109 }
110
111 SGMatModel::~SGMatModel ()
112 {
113 }
114
115 int
116 SGMatModel::get_model_count( SGPropertyNode *prop_root )
117 {
118   load_models( prop_root );
119   return _models.size();
120 }
121
122 inline void
123 SGMatModel::load_models( SGPropertyNode *prop_root )
124 {
125                                 // Load model only on demand
126   if (!_models_loaded) {
127     for (unsigned int i = 0; i < _paths.size(); i++) {
128       osg::Node *entity = SGModelLib::loadModel(_paths[i], prop_root);
129       if (entity != 0) {
130         // FIXME: this stuff can be handled
131         // in the XML wrapper as well (at least,
132         // the billboarding should be handled
133         // there).
134         
135         if (_heading_type == HEADING_BILLBOARD) {
136           // if the model is a billboard, it is likely :
137           // 1. a branch with only leaves,
138           // 2. a tree or a non rectangular shape faked by transparency
139           // We add alpha clamp then
140           osg::StateSet* stateSet = entity->getOrCreateStateSet();
141           osg::AlphaFunc* alphaFunc =
142             new osg::AlphaFunc(osg::AlphaFunc::GREATER, 0.01f);
143           stateSet->setAttributeAndModes(alphaFunc,
144                                          osg::StateAttribute::OVERRIDE);
145           stateSet->setRenderingHint(osg::StateSet::TRANSPARENT_BIN);
146         } 
147         
148         _models.push_back(entity);
149         
150       } else {
151         SG_LOG(SG_INPUT, SG_ALERT, "Failed to load object " << _paths[i]);
152       }
153     }
154   }
155   _models_loaded = true;
156 }
157
158 osg::Node*
159 SGMatModel::get_model( int index,
160                        SGPropertyNode *prop_root )
161 {
162   load_models( prop_root ); // comment this out if preloading models
163   return _models[index].get();
164 }
165
166 osg::Node*
167 SGMatModel::get_random_model( SGPropertyNode *prop_root )
168 {
169   load_models( prop_root ); // comment this out if preloading models
170   int nModels = _models.size();
171   int index = int(sg_random() * nModels);
172   if (index >= nModels)
173     index = 0;
174   return _models[index].get();
175 }
176
177 double
178 SGMatModel::get_coverage_m2 () const
179 {
180   return _coverage_m2;
181 }
182
183 double SGMatModel::get_range_m() const
184 {
185   return _range_m;
186 }
187
188 double SGMatModel::get_randomized_range_m(mt* seed) const
189 {
190   double lrand = mt_rand(seed);
191   
192   // Note that the LoD is not completely randomized.
193   // 10% at 2   * range_m
194   // 30% at 1.5 * range_m
195   // 60% at 1   * range_m
196   if (lrand < 0.1) return 2   * _range_m;
197   if (lrand < 0.4) return 1.5 * _range_m;
198   else return _range_m;
199 }
200
201 SGMatModel::HeadingType
202 SGMatModel::get_heading_type () const
203 {
204   return _heading_type;
205 }
206
207
208 \f
209 ////////////////////////////////////////////////////////////////////////
210 // Implementation of SGMatModelGroup.
211 ////////////////////////////////////////////////////////////////////////
212
213 SGMatModelGroup::SGMatModelGroup (SGPropertyNode * node)
214   : _range_m(node->getDoubleValue("range-m", 2000))
215 {
216                                 // Load the object subnodes
217   vector<SGPropertyNode_ptr> object_nodes =
218     ((SGPropertyNode *)node)->getChildren("object");
219   for (unsigned int i = 0; i < object_nodes.size(); i++) {
220     const SGPropertyNode * object_node = object_nodes[i];
221     if (object_node->hasChild("path"))
222       _objects.push_back(new SGMatModel(object_node, _range_m));
223     else
224       SG_LOG(SG_INPUT, SG_ALERT, "No path supplied for object");
225   }
226 }
227
228 SGMatModelGroup::~SGMatModelGroup ()
229 {
230 }
231
232 double
233 SGMatModelGroup::get_range_m () const
234 {
235   return _range_m;
236 }
237
238 int
239 SGMatModelGroup::get_object_count () const
240 {
241   return _objects.size();
242 }
243
244 SGMatModel *
245 SGMatModelGroup::get_object (int index) const
246 {
247   return _objects[index];
248 }
249
250 // end of matmodel.cxx