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