]> git.mxchange.org Git - simgear.git/blob - simgear/scene/tgdb/userdata.cxx
Modified Files:
[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 <simgear/sg_inlines.h>
29 #include <simgear/math/point3d.hxx>
30 #include <simgear/math/sg_geodesy.hxx>
31 #include <simgear/math/sg_random.h>
32 #include <simgear/scene/material/mat.hxx>
33 #include <simgear/scene/material/matmodel.hxx>
34
35 #include "userdata.hxx"
36
37
38 // the following are static values needed by the runtime object
39 // loader.  However, the loading is done via a call back so these
40 // values cannot be passed as parameters.  The calling application
41 // needs to call sgUserDataInit() with the appropriate values before
42 // building / drawing any scenery.
43
44 static bool _inited = false;
45 static SGModelLib *modellib = NULL;
46 static string model_root = "";
47 static SGPropertyNode *root_props = NULL;
48 static double sim_time_sec = 0.0;
49
50 void sgUserDataInit( SGModelLib *m, const string &r,
51                      SGPropertyNode *p, double t ) {
52     _inited = true;
53     modellib = m;
54     model_root = r;
55     root_props = p;
56     sim_time_sec = t;
57 }
58
59
60 static void random_pt_inside_tri( float *res,
61                                   float *n1, float *n2, float *n3 )
62 {
63     double a = sg_random();
64     double b = sg_random();
65     if ( a + b > 1.0 ) {
66         a = 1.0 - a;
67         b = 1.0 - b;
68     }
69     double c = 1 - a - b;
70
71     res[0] = n1[0]*a + n2[0]*b + n3[0]*c;
72     res[1] = n1[1]*a + n2[1]*b + n3[1]*c;
73     res[2] = n1[2]*a + n2[2]*b + n3[2]*c;
74 }
75
76
77 /**
78  * Fill in a triangle with randomly-placed objects.
79  *
80  * This method is invoked by a callback when the triangle is in range
81  * but not yet populated.
82  *
83  */
84
85 void SGTriUserData::fill_in_triangle ()
86 {
87                                 // generate a repeatable random seed
88     sg_srandom(seed);
89
90     int nObjects = object_group->get_object_count();
91
92     for (int i = 0; i < nObjects; i++) {
93       SGMatModel * object = object_group->get_object(i);
94       double num = area / object->get_coverage_m2();
95
96       // place an object each unit of area
97       while ( num > 1.0 ) {
98           add_object_to_triangle(object);
99           num -= 1.0;
100       }
101       // for partial units of area, use a zombie door method to
102       // create the proper random chance of an object being created
103       // for this triangle
104       if ( num > 0.0 ) {
105         if ( sg_random() <= num ) {
106           // a zombie made it through our door
107                 add_object_to_triangle(object);
108         }
109       }
110     }
111 }
112
113 void SGTriUserData::add_object_to_triangle (SGMatModel * object)
114 {
115     // Set up the random heading if required.
116     double hdg_deg = 0;
117     if (object->get_heading_type() == SGMatModel::HEADING_RANDOM)
118         hdg_deg = sg_random() * 360;
119
120 #if 0
121     // OSGFIXME
122     sgMat4 mat;
123     makeWorldMatrix(mat, hdg_deg);
124
125     ssgTransform * pos = new ssgTransform;
126     pos->setTransform(mat);
127     // the parameters to get_random_model() are set in local static
128     // data via the ssgUserDataInit() function.  This function must be
129     // called before any scenery is drawn.
130     pos->addKid( object->get_random_model( modellib, model_root,
131                                            root_props, sim_time_sec )
132                  );
133     branch->addKid(pos);
134 #endif
135 }
136
137 void SGTriUserData::makeWorldMatrix (sgMat4 mat, double hdg_deg )
138 {
139   // OSGFIXME
140 //     if (hdg_deg == 0) {
141 //         mat[0][0] =  leafData->sin_lat * leafData->cos_lon;
142 //         mat[0][1] =  leafData->sin_lat * leafData->sin_lon;
143 //         mat[0][2] = -leafData->cos_lat;
144 //         mat[0][3] =  SG_ZERO;
145
146 //         mat[1][0] =  -leafData->sin_lon;
147 //         mat[1][1] =  leafData->cos_lon;
148 //         mat[1][2] =  SG_ZERO;
149 //         mat[1][3] =  SG_ZERO;
150 //     } else {
151 //         float sin_hdg = sin( hdg_deg * SGD_DEGREES_TO_RADIANS ) ;
152 //         float cos_hdg = cos( hdg_deg * SGD_DEGREES_TO_RADIANS ) ;
153 //         mat[0][0] =  cos_hdg * leafData->sin_lat * leafData->cos_lon - sin_hdg * leafData->sin_lon;
154 //         mat[0][1] =  cos_hdg * leafData->sin_lat * leafData->sin_lon + sin_hdg * leafData->cos_lon;
155 //         mat[0][2] = -cos_hdg * leafData->cos_lat;
156 //         mat[0][3] =  SG_ZERO;
157
158 //         mat[1][0] = -sin_hdg * leafData->sin_lat * leafData->cos_lon - cos_hdg * leafData->sin_lon;
159 //         mat[1][1] = -sin_hdg * leafData->sin_lat * leafData->sin_lon + cos_hdg * leafData->cos_lon;
160 //         mat[1][2] =  sin_hdg * leafData->cos_lat;
161 //         mat[1][3] =  SG_ZERO;
162 //     }
163
164 //     mat[2][0] = leafData->cos_lat * leafData->cos_lon;
165 //     mat[2][1] = leafData->cos_lat * leafData->sin_lon;
166 //     mat[2][2] = leafData->sin_lat;
167 //     mat[2][3] = SG_ZERO;
168
169 //     // translate to random point in triangle
170 //     sgVec3 result;
171 //     random_pt_inside_tri(result, p1, p2, p3);
172 //     sgSubVec3(mat[3], result, center);
173
174 //     mat[3][3] = SG_ONE ;
175 }
176
177 /**
178  * SSG callback for an in-range triangle of randomly-placed objects.
179  *
180  * This pretraversal callback is attached to a branch that is traversed
181  * only when a triangle is in range.  If the triangle is not currently
182  * populated with randomly-placed objects, this callback will populate
183  * it.
184  *
185  * @param entity The entity to which the callback is attached (not used).
186  * @param mask The entity's traversal mask (not used).
187  * @return Always 1, to allow traversal and culling to continue.
188  */
189 // static int
190 // tri_in_range_callback (ssgEntity * entity, int mask)
191 // {
192 //   SGTriUserData * data = (SGTriUserData *)entity->getUserData();
193 //   if (!data->is_filled_in) {
194 //         data->fill_in_triangle();
195 //     data->is_filled_in = true;
196 //   }
197 //   return 1;
198 // }
199
200
201 /**
202  * SSG callback for an out-of-range triangle of randomly-placed objects.
203  *
204  * This pretraversal callback is attached to a branch that is traversed
205  * only when a triangle is out of range.  If the triangle is currently
206  * populated with randomly-placed objects, the objects will be removed.
207  *
208  *
209  * @param entity The entity to which the callback is attached (not used).
210  * @param mask The entity's traversal mask (not used).
211  * @return Always 0, to prevent any further traversal or culling.
212  */
213 // static int
214 // tri_out_of_range_callback (ssgEntity * entity, int mask)
215 // {
216 //   SGTriUserData * data = (SGTriUserData *)entity->getUserData();
217 //   if (data->is_filled_in) {
218 //     data->branch->removeAllKids();
219 //     data->is_filled_in = false;
220 //   }
221 //   return 0;
222 // }
223
224
225 /**
226  * Calculate the bounding radius of a triangle from its center.
227  *
228  * @param center The triangle center.
229  * @param p1 The first point in the triangle.
230  * @param p2 The second point in the triangle.
231  * @param p3 The third point in the triangle.
232  * @return The greatest distance any point lies from the center.
233  */
234 // static inline float
235 // get_bounding_radius( sgVec3 center, float *p1, float *p2, float *p3)
236 // {
237 //    return sqrt( SG_MAX3( sgDistanceSquaredVec3(center, p1),
238 //                          sgDistanceSquaredVec3(center, p2),
239 //                          sgDistanceSquaredVec3(center, p3) ) );
240 // }
241
242
243 /**
244  * Set up a triangle for randomly-placed objects.
245  *
246  * No objects will be added unless the triangle comes into range.
247  *
248  */
249
250 void SGLeafUserData::setup_triangle (int i )
251 {
252 //     short n1, n2, n3;
253 //     leaf->getTriangle(i, &n1, &n2, &n3);
254
255 //     float * p1 = leaf->getVertex(n1);
256 //     float * p2 = leaf->getVertex(n2);
257 //     float * p3 = leaf->getVertex(n3);
258
259 //                                 // Set up a single center point for LOD
260 //     sgVec3 center;
261 //     sgSetVec3(center,
262 //               (p1[0] + p2[0] + p3[0]) / 3.0,
263 //               (p1[1] + p2[1] + p3[1]) / 3.0,
264 //               (p1[2] + p2[2] + p3[2]) / 3.0);
265 //     double area = sgTriArea(p1, p2, p3);
266       
267 //                                 // maximum radius of an object from center.
268 //     double bounding_radius = get_bounding_radius(center, p1, p2, p3);
269
270 //                                 // Set up a transformation to the center
271 //                                 // point, so that everything else can
272 //                                 // be specified relative to it.
273 //     ssgTransform * location = new ssgTransform;
274 //     sgMat4 TRANS;
275 //     sgMakeTransMat4(TRANS, center);
276 //     location->setTransform(TRANS);
277 //     branch->addKid(location);
278
279 //                                 // Iterate through all the object types.
280 //     int num_groups = mat->get_object_group_count();
281 //     for (int j = 0; j < num_groups; j++) {
282 //                                 // Look up the random object.
283 //         SGMatModelGroup * group = mat->get_object_group(j);
284
285 //                                 // Set up the range selector for the entire
286 //                                 // triangle; note that we use the object
287 //                                 // range plus the bounding radius here, to
288 //                                 // allow for objects far from the center.
289 //         float ranges[] = { 0,
290 //                           group->get_range_m() + bounding_radius,
291 //                 SG_MAX };
292 //         ssgRangeSelector * lod = new ssgRangeSelector;
293 //         lod->setRanges(ranges, 3);
294 //         location->addKid(lod);
295
296 //                                 // Create the in-range and out-of-range
297 //                                 // branches.
298 //         ssgBranch * in_range = new ssgBranch;
299 //         ssgBranch * out_of_range = new ssgBranch;
300
301 //                                 // Set up the user data for if/when
302 //                                 // the random objects in this triangle
303 //                                 // are filled in.
304 //         SGTriUserData * data = new SGTriUserData;
305 //         data->is_filled_in = false;
306 //         data->p1 = p1;
307 //         data->p2 = p2;
308 //         data->p3 = p3;
309 //         sgCopyVec3 (data->center, center);
310 //         data->area = area;
311 //         data->object_group = group;
312 //         data->branch = in_range;
313 //         data->leafData = this;
314 //         data->seed = (unsigned int)(p1[0] * j);
315
316 //                                 // Set up the in-range node.
317 //         in_range->setUserData(data);
318 //         in_range->setTravCallback(SSG_CALLBACK_PRETRAV,
319 //                                  tri_in_range_callback);
320 //         lod->addKid(in_range);
321
322 //                                 // Set up the out-of-range node.
323 //         out_of_range->setUserData(data);
324 //         out_of_range->setTravCallback(SSG_CALLBACK_PRETRAV,
325 //                                       tri_out_of_range_callback);
326 //         out_of_range->addKid(new SGDummyBSphereEntity(bounding_radius));
327 //         lod->addKid(out_of_range);
328 //     }
329 }