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