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