]> git.mxchange.org Git - simgear.git/blob - simgear/scene/tgdb/userdata.cxx
c1956edd3fdfb9251cc228555a642aa90683cc8c
[simgear.git] / simgear / scene / tgdb / userdata.cxx
1 // userdata.hxx -- two classes for populating ssg user data slots in association
2 //                 with our implimenation of random surface objects.
3 //
4 // Written by David Megginson, started December 2001.
5 //
6 // Copyright (C) 2001 - 2003  Curtis L. Olson  - http://www.flightgear.org/~curt
7 //
8 // This program is free software; you can redistribute it and/or
9 // modify it under the terms of the GNU General Public License as
10 // published by the Free Software Foundation; either version 2 of the
11 // License, or (at your option) any later version.
12 //
13 // This program is distributed in the hope that it will be useful, but
14 // WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16 // General Public License for more details.
17 //
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the Free Software
20 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
21 //
22 // $Id$
23
24 #ifdef HAVE_CONFIG_H
25 #  include <simgear_config.h>
26 #endif
27
28 #include <osgDB/Registry>
29
30 #include <simgear/sg_inlines.h>
31 #include <simgear/math/point3d.hxx>
32 #include <simgear/math/sg_geodesy.hxx>
33 #include <simgear/math/sg_random.h>
34 #include <simgear/scene/material/mat.hxx>
35 #include <simgear/scene/material/matmodel.hxx>
36
37 #include "SGModelBin.hxx"
38 #include "userdata.hxx"
39 #include "SGReaderWriterBTG.hxx"
40
41 // the following are static values needed by the runtime object
42 // loader.  However, the loading is done via a call back so these
43 // values cannot be passed as parameters.  The calling application
44 // needs to call sgUserDataInit() with the appropriate values before
45 // building / drawing any scenery.
46
47 static bool _inited = false;
48 static SGModelLib *modellib = NULL;
49 static string model_root = "";
50 static SGPropertyNode *root_props = NULL;
51 static double sim_time_sec = 0.0;
52
53 // Because BTG files are now loaded through the osgDB::Registry, there
54 // are no symbols referenced by FlightGear in this library other than
55 // sgUserDataInit. But the libraries are all statically linked, so
56 // none of the other object files in this library would be included in
57 // the executable! Sticking the static proxy here forces the BTG code
58 // to be sucked in.
59 osgDB::RegisterReaderWriterProxy<SGReaderWriterBTG> g_readerWriter_BTG_Proxy;
60
61 void sgUserDataInit( SGModelLib *m, const string &r,
62                      SGPropertyNode *p, double t ) {
63     _inited = true;
64     modellib = m;
65     model_root = r;
66     root_props = p;
67     sim_time_sec = t;
68 }
69
70  osg::Node* sgGetRandomModel(SGMatModel *obj) {
71    return obj->get_random_model(modellib, model_root, root_props, sim_time_sec);
72  }
73
74 osg::Node* sgGetModel(int i, SGMatModel *obj) {
75    return obj->get_model(i, modellib, model_root, root_props, sim_time_sec);
76  }
77
78 static void random_pt_inside_tri( float *res,
79                                   float *n1, float *n2, float *n3 )
80 {
81     double a = sg_random();
82     double b = sg_random();
83     if ( a + b > 1.0 ) {
84         a = 1.0 - a;
85         b = 1.0 - b;
86     }
87     double c = 1 - a - b;
88
89     res[0] = n1[0]*a + n2[0]*b + n3[0]*c;
90     res[1] = n1[1]*a + n2[1]*b + n3[1]*c;
91     res[2] = n1[2]*a + n2[2]*b + n3[2]*c;
92 }
93
94
95 /**
96  * Fill in a triangle with randomly-placed objects.
97  *
98  * This method is invoked by a callback when the triangle is in range
99  * but not yet populated.
100  *
101  */
102
103 void SGTriUserData::fill_in_triangle ()
104 {
105                                 // generate a repeatable random seed
106     sg_srandom(seed);
107
108     int nObjects = object_group->get_object_count();
109
110     for (int i = 0; i < nObjects; i++) {
111       SGMatModel * object = object_group->get_object(i);
112       double num = area / object->get_coverage_m2();
113
114       // place an object each unit of area
115       while ( num > 1.0 ) {
116           add_object_to_triangle(object);
117           num -= 1.0;
118       }
119       // for partial units of area, use a zombie door method to
120       // create the proper random chance of an object being created
121       // for this triangle
122       if ( num > 0.0 ) {
123         if ( sg_random() <= num ) {
124           // a zombie made it through our door
125                 add_object_to_triangle(object);
126         }
127       }
128     }
129 }
130
131 void SGTriUserData::add_object_to_triangle (SGMatModel * object)
132 {
133     // Set up the random heading if required.
134     double hdg_deg = 0;
135     if (object->get_heading_type() == SGMatModel::HEADING_RANDOM)
136         hdg_deg = sg_random() * 360;
137
138 #if 0
139     // OSGFIXME
140     sgMat4 mat;
141     makeWorldMatrix(mat, hdg_deg);
142
143     ssgTransform * pos = new ssgTransform;
144     pos->setTransform(mat);
145     // the parameters to get_random_model() are set in local static
146     // data via the ssgUserDataInit() function.  This function must be
147     // called before any scenery is drawn.
148     pos->addKid( object->get_random_model( modellib, model_root,
149                                            root_props, sim_time_sec )
150                  );
151     branch->addKid(pos);
152 #endif
153 }
154
155 void SGTriUserData::makeWorldMatrix (sgMat4 mat, double hdg_deg )
156 {
157   // OSGFIXME
158 //     if (hdg_deg == 0) {
159 //         mat[0][0] =  leafData->sin_lat * leafData->cos_lon;
160 //         mat[0][1] =  leafData->sin_lat * leafData->sin_lon;
161 //         mat[0][2] = -leafData->cos_lat;
162 //         mat[0][3] =  SG_ZERO;
163
164 //         mat[1][0] =  -leafData->sin_lon;
165 //         mat[1][1] =  leafData->cos_lon;
166 //         mat[1][2] =  SG_ZERO;
167 //         mat[1][3] =  SG_ZERO;
168 //     } else {
169 //         float sin_hdg = sin( hdg_deg * SGD_DEGREES_TO_RADIANS ) ;
170 //         float cos_hdg = cos( hdg_deg * SGD_DEGREES_TO_RADIANS ) ;
171 //         mat[0][0] =  cos_hdg * leafData->sin_lat * leafData->cos_lon - sin_hdg * leafData->sin_lon;
172 //         mat[0][1] =  cos_hdg * leafData->sin_lat * leafData->sin_lon + sin_hdg * leafData->cos_lon;
173 //         mat[0][2] = -cos_hdg * leafData->cos_lat;
174 //         mat[0][3] =  SG_ZERO;
175
176 //         mat[1][0] = -sin_hdg * leafData->sin_lat * leafData->cos_lon - cos_hdg * leafData->sin_lon;
177 //         mat[1][1] = -sin_hdg * leafData->sin_lat * leafData->sin_lon + cos_hdg * leafData->cos_lon;
178 //         mat[1][2] =  sin_hdg * leafData->cos_lat;
179 //         mat[1][3] =  SG_ZERO;
180 //     }
181
182 //     mat[2][0] = leafData->cos_lat * leafData->cos_lon;
183 //     mat[2][1] = leafData->cos_lat * leafData->sin_lon;
184 //     mat[2][2] = leafData->sin_lat;
185 //     mat[2][3] = SG_ZERO;
186
187 //     // translate to random point in triangle
188 //     sgVec3 result;
189 //     random_pt_inside_tri(result, p1, p2, p3);
190 //     sgSubVec3(mat[3], result, center);
191
192 //     mat[3][3] = SG_ONE ;
193 }
194
195 /**
196  * SSG callback for an in-range triangle of randomly-placed objects.
197  *
198  * This pretraversal callback is attached to a branch that is traversed
199  * only when a triangle is in range.  If the triangle is not currently
200  * populated with randomly-placed objects, this callback will populate
201  * it.
202  *
203  * @param entity The entity to which the callback is attached (not used).
204  * @param mask The entity's traversal mask (not used).
205  * @return Always 1, to allow traversal and culling to continue.
206  */
207 // static int
208 // tri_in_range_callback (ssgEntity * entity, int mask)
209 // {
210 //   SGTriUserData * data = (SGTriUserData *)entity->getUserData();
211 //   if (!data->is_filled_in) {
212 //         data->fill_in_triangle();
213 //     data->is_filled_in = true;
214 //   }
215 //   return 1;
216 // }
217
218
219 /**
220  * SSG callback for an out-of-range triangle of randomly-placed objects.
221  *
222  * This pretraversal callback is attached to a branch that is traversed
223  * only when a triangle is out of range.  If the triangle is currently
224  * populated with randomly-placed objects, the objects will be removed.
225  *
226  *
227  * @param entity The entity to which the callback is attached (not used).
228  * @param mask The entity's traversal mask (not used).
229  * @return Always 0, to prevent any further traversal or culling.
230  */
231 // static int
232 // tri_out_of_range_callback (ssgEntity * entity, int mask)
233 // {
234 //   SGTriUserData * data = (SGTriUserData *)entity->getUserData();
235 //   if (data->is_filled_in) {
236 //     data->branch->removeAllKids();
237 //     data->is_filled_in = false;
238 //   }
239 //   return 0;
240 // }
241
242
243 /**
244  * Calculate the bounding radius of a triangle from its center.
245  *
246  * @param center The triangle center.
247  * @param p1 The first point in the triangle.
248  * @param p2 The second point in the triangle.
249  * @param p3 The third point in the triangle.
250  * @return The greatest distance any point lies from the center.
251  */
252 // static inline float
253 // get_bounding_radius( sgVec3 center, float *p1, float *p2, float *p3)
254 // {
255 //    return sqrt( SG_MAX3( sgDistanceSquaredVec3(center, p1),
256 //                          sgDistanceSquaredVec3(center, p2),
257 //                          sgDistanceSquaredVec3(center, p3) ) );
258 // }
259
260
261 /**
262  * Set up a triangle for randomly-placed objects.
263  *
264  * No objects will be added unless the triangle comes into range.
265  *
266  */
267
268 void SGLeafUserData::setup_triangle (int i )
269 {
270 //     short n1, n2, n3;
271 //     leaf->getTriangle(i, &n1, &n2, &n3);
272
273 //     float * p1 = leaf->getVertex(n1);
274 //     float * p2 = leaf->getVertex(n2);
275 //     float * p3 = leaf->getVertex(n3);
276
277 //                                 // Set up a single center point for LOD
278 //     sgVec3 center;
279 //     sgSetVec3(center,
280 //               (p1[0] + p2[0] + p3[0]) / 3.0,
281 //               (p1[1] + p2[1] + p3[1]) / 3.0,
282 //               (p1[2] + p2[2] + p3[2]) / 3.0);
283 //     double area = sgTriArea(p1, p2, p3);
284       
285 //                                 // maximum radius of an object from center.
286 //     double bounding_radius = get_bounding_radius(center, p1, p2, p3);
287
288 //                                 // Set up a transformation to the center
289 //                                 // point, so that everything else can
290 //                                 // be specified relative to it.
291 //     ssgTransform * location = new ssgTransform;
292 //     sgMat4 TRANS;
293 //     sgMakeTransMat4(TRANS, center);
294 //     location->setTransform(TRANS);
295 //     branch->addKid(location);
296
297 //                                 // Iterate through all the object types.
298 //     int num_groups = mat->get_object_group_count();
299 //     for (int j = 0; j < num_groups; j++) {
300 //                                 // Look up the random object.
301 //         SGMatModelGroup * group = mat->get_object_group(j);
302
303 //                                 // Set up the range selector for the entire
304 //                                 // triangle; note that we use the object
305 //                                 // range plus the bounding radius here, to
306 //                                 // allow for objects far from the center.
307 //         float ranges[] = { 0,
308 //                           group->get_range_m() + bounding_radius,
309 //                 SG_MAX };
310 //         ssgRangeSelector * lod = new ssgRangeSelector;
311 //         lod->setRanges(ranges, 3);
312 //         location->addKid(lod);
313
314 //                                 // Create the in-range and out-of-range
315 //                                 // branches.
316 //         ssgBranch * in_range = new ssgBranch;
317 //         ssgBranch * out_of_range = new ssgBranch;
318
319 //                                 // Set up the user data for if/when
320 //                                 // the random objects in this triangle
321 //                                 // are filled in.
322 //         SGTriUserData * data = new SGTriUserData;
323 //         data->is_filled_in = false;
324 //         data->p1 = p1;
325 //         data->p2 = p2;
326 //         data->p3 = p3;
327 //         sgCopyVec3 (data->center, center);
328 //         data->area = area;
329 //         data->object_group = group;
330 //         data->branch = in_range;
331 //         data->leafData = this;
332 //         data->seed = (unsigned int)(p1[0] * j);
333
334 //                                 // Set up the in-range node.
335 //         in_range->setUserData(data);
336 //         in_range->setTravCallback(SSG_CALLBACK_PRETRAV,
337 //                                  tri_in_range_callback);
338 //         lod->addKid(in_range);
339
340 //                                 // Set up the out-of-range node.
341 //         out_of_range->setUserData(data);
342 //         out_of_range->setTravCallback(SSG_CALLBACK_PRETRAV,
343 //                                       tri_out_of_range_callback);
344 //         out_of_range->addKid(new SGDummyBSphereEntity(bounding_radius));
345 //         lod->addKid(out_of_range);
346 //     }
347 }