1 // userdata.hxx -- two classes for populating ssg user data slots in association
2 // with our implimenation of random surface objects.
4 // Written by David Megginson, started December 2001.
6 // Copyright (C) 2001 - 2003 Curtis L. Olson - http://www.flightgear.org/~curt
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.
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.
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.
25 # include <simgear_config.h>
28 #include <osgDB/Registry>
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>
37 #include "SGModelBin.hxx"
38 #include "userdata.hxx"
39 #include "SGReaderWriterBTG.hxx"
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.
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;
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
59 osgDB::RegisterReaderWriterProxy<SGReaderWriterBTG> g_readerWriter_BTG_Proxy;
61 void sgUserDataInit( SGModelLib *m, const string &r,
62 SGPropertyNode *p, double t ) {
70 osg::Node* sgGetRandomModel(SGMatModel *obj) {
71 return obj->get_random_model(modellib, model_root, root_props, sim_time_sec);
74 osg::Node* sgGetModel(int i, SGMatModel *obj) {
75 return obj->get_model(i, modellib, model_root, root_props, sim_time_sec);
78 static void random_pt_inside_tri( float *res,
79 float *n1, float *n2, float *n3 )
81 double a = sg_random();
82 double b = sg_random();
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;
96 * Fill in a triangle with randomly-placed objects.
98 * This method is invoked by a callback when the triangle is in range
99 * but not yet populated.
103 void SGTriUserData::fill_in_triangle ()
105 // generate a repeatable random seed
108 int nObjects = object_group->get_object_count();
110 for (int i = 0; i < nObjects; i++) {
111 SGMatModel * object = object_group->get_object(i);
112 double num = area / object->get_coverage_m2();
114 // place an object each unit of area
115 while ( num > 1.0 ) {
116 add_object_to_triangle(object);
119 // for partial units of area, use a zombie door method to
120 // create the proper random chance of an object being created
123 if ( sg_random() <= num ) {
124 // a zombie made it through our door
125 add_object_to_triangle(object);
131 void SGTriUserData::add_object_to_triangle (SGMatModel * object)
133 // Set up the random heading if required.
135 if (object->get_heading_type() == SGMatModel::HEADING_RANDOM)
136 hdg_deg = sg_random() * 360;
141 makeWorldMatrix(mat, hdg_deg);
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 )
155 void SGTriUserData::makeWorldMatrix (sgMat4 mat, double hdg_deg )
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;
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;
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;
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;
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;
187 // // translate to random point in triangle
189 // random_pt_inside_tri(result, p1, p2, p3);
190 // sgSubVec3(mat[3], result, center);
192 // mat[3][3] = SG_ONE ;
196 * SSG callback for an in-range triangle of randomly-placed objects.
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
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.
208 // tri_in_range_callback (ssgEntity * entity, int mask)
210 // SGTriUserData * data = (SGTriUserData *)entity->getUserData();
211 // if (!data->is_filled_in) {
212 // data->fill_in_triangle();
213 // data->is_filled_in = true;
220 * SSG callback for an out-of-range triangle of randomly-placed objects.
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.
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.
232 // tri_out_of_range_callback (ssgEntity * entity, int mask)
234 // SGTriUserData * data = (SGTriUserData *)entity->getUserData();
235 // if (data->is_filled_in) {
236 // data->branch->removeAllKids();
237 // data->is_filled_in = false;
244 * Calculate the bounding radius of a triangle from its center.
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.
252 // static inline float
253 // get_bounding_radius( sgVec3 center, float *p1, float *p2, float *p3)
255 // return sqrt( SG_MAX3( sgDistanceSquaredVec3(center, p1),
256 // sgDistanceSquaredVec3(center, p2),
257 // sgDistanceSquaredVec3(center, p3) ) );
262 * Set up a triangle for randomly-placed objects.
264 * No objects will be added unless the triangle comes into range.
268 void SGLeafUserData::setup_triangle (int i )
271 // leaf->getTriangle(i, &n1, &n2, &n3);
273 // float * p1 = leaf->getVertex(n1);
274 // float * p2 = leaf->getVertex(n2);
275 // float * p3 = leaf->getVertex(n3);
277 // // Set up a single center point for LOD
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);
285 // // maximum radius of an object from center.
286 // double bounding_radius = get_bounding_radius(center, p1, p2, p3);
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;
293 // sgMakeTransMat4(TRANS, center);
294 // location->setTransform(TRANS);
295 // branch->addKid(location);
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);
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,
310 // ssgRangeSelector * lod = new ssgRangeSelector;
311 // lod->setRanges(ranges, 3);
312 // location->addKid(lod);
314 // // Create the in-range and out-of-range
316 // ssgBranch * in_range = new ssgBranch;
317 // ssgBranch * out_of_range = new ssgBranch;
319 // // Set up the user data for if/when
320 // // the random objects in this triangle
322 // SGTriUserData * data = new SGTriUserData;
323 // data->is_filled_in = false;
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);
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);
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);