]> git.mxchange.org Git - simgear.git/blob - simgear/scene/material/matmodel.cxx
Oops, ALUT 1.0 requires a little more work than expected.
[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., 675 Mass Ave, Cambridge, MA 02139, 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 <simgear/compiler.h>
34
35 #ifdef SG_MATH_EXCEPTION_CLASH
36 #  include <math.h>
37 #endif
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 \f
48 ////////////////////////////////////////////////////////////////////////
49 // Local static functions.
50 ////////////////////////////////////////////////////////////////////////
51
52 /**
53  * Internal method to test whether a file exists.
54  *
55  * TODO: this should be moved to a SimGear library of local file
56  * functions.
57  */
58 static inline bool
59 local_file_exists( const string& path ) {
60     sg_gzifstream in( path );
61     if ( ! in.is_open() ) {
62         return false;
63     } else {
64         return true;
65     }
66 }
67
68
69 \f
70 ////////////////////////////////////////////////////////////////////////
71 // Implementation of SGMatModel.
72 ////////////////////////////////////////////////////////////////////////
73
74 SGMatModel::SGMatModel (const SGPropertyNode * node, double range_m)
75   : _models_loaded(false),
76     _coverage_m2(node->getDoubleValue("coverage-m2", 1000000)),
77     _range_m(range_m)
78 {
79                                 // Sanity check
80   if (_coverage_m2 < 1000) {
81     SG_LOG(SG_INPUT, SG_ALERT, "Random object coverage " << _coverage_m2
82            << " is too small, forcing, to 1000");
83     _coverage_m2 = 1000;
84   }
85
86                                 // Note all the model paths
87   vector <SGPropertyNode_ptr> path_nodes = node->getChildren("path");
88   for (unsigned int i = 0; i < path_nodes.size(); i++)
89     _paths.push_back(path_nodes[i]->getStringValue());
90
91                                 // Note the heading type
92   string hdg = node->getStringValue("heading-type", "fixed");
93   if (hdg == "fixed") {
94     _heading_type = HEADING_FIXED;
95   } else if (hdg == "billboard") {
96     _heading_type = HEADING_BILLBOARD;
97   } else if (hdg == "random") {
98     _heading_type = HEADING_RANDOM;
99   } else {
100     _heading_type = HEADING_FIXED;
101     SG_LOG(SG_INPUT, SG_ALERT, "Unknown heading type: " << hdg
102            << "; using 'fixed' instead.");
103   }
104
105   // uncomment to preload models
106   // load_models();
107 }
108
109 SGMatModel::~SGMatModel ()
110 {
111   for (unsigned int i = 0; i < _models.size(); i++) {
112     if (_models[i] != 0) {
113       _models[i]->deRef();
114       _models[i] = 0;
115     }
116   }
117 }
118
119 int
120 SGMatModel::get_model_count( SGModelLib *modellib,
121                              const string &fg_root,
122                              SGPropertyNode *prop_root,
123                              double sim_time_sec )
124 {
125   load_models( modellib, fg_root, prop_root, sim_time_sec );
126   return _models.size();
127 }
128
129 static void
130 setAlphaClampToBranch( ssgBranch *b, float clamp )
131 {
132   int nb = b->getNumKids();
133   for (int i = 0; i<nb; i++) {
134     ssgEntity *e = b->getKid(i);
135     if (e->isAKindOf(ssgTypeLeaf())) {
136       ssgSimpleState*s = (ssgSimpleState*)((ssgLeaf*)e)->getState();
137       s->enable( GL_ALPHA_TEST );
138       s->setAlphaClamp( clamp );
139     } else if (e->isAKindOf(ssgTypeBranch())) {
140       setAlphaClampToBranch( (ssgBranch*)e, clamp );
141     }
142   }
143 }
144
145 inline void
146 SGMatModel::load_models ( SGModelLib *modellib,
147                           const string &fg_root,
148                           SGPropertyNode *prop_root,
149                           double sim_time_sec )
150 {
151                                 // Load model only on demand
152   if (!_models_loaded) {
153     for (unsigned int i = 0; i < _paths.size(); i++) {
154       ssgEntity *entity = modellib->load_model( fg_root, _paths[i],
155                                                 prop_root, sim_time_sec );
156       if (entity != 0) {
157                                 // FIXME: this stuff can be handled
158                                 // in the XML wrapper as well (at least,
159                                 // the billboarding should be handled
160                                 // there).
161         float ranges[] = {0, _range_m};
162         ssgRangeSelector * lod = new ssgRangeSelector;
163         lod->ref();
164         lod->setRanges(ranges, 2);
165         if (_heading_type == HEADING_BILLBOARD) {
166           // if the model is a billboard, it is likely :
167           // 1. a branch with only leaves,
168           // 2. a tree or a non rectangular shape faked by transparency
169           // We add alpha clamp then
170           if ( entity->isAKindOf(ssgTypeBranch()) ) {
171             ssgBranch *b = (ssgBranch *)entity;
172             setAlphaClampToBranch( b, 0.01f );
173           }
174           ssgCutout * cutout = new ssgCutout(false);
175           cutout->addKid(entity);
176           lod->addKid(cutout);
177         } else {
178           lod->addKid(entity);
179         }
180         _models.push_back(lod);
181       } else {
182         SG_LOG(SG_INPUT, SG_ALERT, "Failed to load object " << _paths[i]);
183       }
184     }
185   }
186   _models_loaded = true;
187 }
188
189 ssgEntity *
190 SGMatModel::get_model( int index,
191                        SGModelLib *modellib,
192                        const string &fg_root,
193                        SGPropertyNode *prop_root,
194                        double sim_time_sec )
195 {
196   load_models( modellib, fg_root, prop_root, sim_time_sec ); // comment this out if preloading models
197   return _models[index];
198 }
199
200 ssgEntity *
201 SGMatModel::get_random_model( SGModelLib *modellib,
202                               const string &fg_root,
203                               SGPropertyNode *prop_root,
204                               double sim_time_sec )
205 {
206   load_models( modellib, fg_root, prop_root, sim_time_sec ); // comment this out if preloading models
207   int nModels = _models.size();
208   int index = int(sg_random() * nModels);
209   if (index >= nModels)
210     index = 0;
211   return _models[index];
212 }
213
214 double
215 SGMatModel::get_coverage_m2 () const
216 {
217   return _coverage_m2;
218 }
219
220 SGMatModel::HeadingType
221 SGMatModel::get_heading_type () const
222 {
223   return _heading_type;
224 }
225
226
227 \f
228 ////////////////////////////////////////////////////////////////////////
229 // Implementation of SGMatModelGroup.
230 ////////////////////////////////////////////////////////////////////////
231
232 SGMatModelGroup::SGMatModelGroup (SGPropertyNode * node)
233   : _range_m(node->getDoubleValue("range-m", 2000))
234 {
235                                 // Load the object subnodes
236   vector<SGPropertyNode_ptr> object_nodes =
237     ((SGPropertyNode *)node)->getChildren("object");
238   for (unsigned int i = 0; i < object_nodes.size(); i++) {
239     const SGPropertyNode * object_node = object_nodes[i];
240     if (object_node->hasChild("path"))
241       _objects.push_back(new SGMatModel(object_node, _range_m));
242     else
243       SG_LOG(SG_INPUT, SG_ALERT, "No path supplied for object");
244   }
245 }
246
247 SGMatModelGroup::~SGMatModelGroup ()
248 {
249   for (unsigned int i = 0; i < _objects.size(); i++) {
250     delete _objects[i];
251     _objects[i] = 0;
252   }
253 }
254
255 double
256 SGMatModelGroup::get_range_m () const
257 {
258   return _range_m;
259 }
260
261 int
262 SGMatModelGroup::get_object_count () const
263 {
264   return _objects.size();
265 }
266
267 SGMatModel *
268 SGMatModelGroup::get_object (int index) const
269 {
270   return _objects[index];
271 }
272
273
274 // end of matmodel.cxx