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 "userdata.hxx"
38 #include "SGReaderWriterBTG.hxx"
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.
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;
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
58 osgDB::RegisterReaderWriterProxy<SGReaderWriterBTG> g_readerWriter_BTG_Proxy;
60 void sgUserDataInit( SGModelLib *m, const string &r,
61 SGPropertyNode *p, double t ) {
70 static void random_pt_inside_tri( float *res,
71 float *n1, float *n2, float *n3 )
73 double a = sg_random();
74 double b = sg_random();
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;
88 * Fill in a triangle with randomly-placed objects.
90 * This method is invoked by a callback when the triangle is in range
91 * but not yet populated.
95 void SGTriUserData::fill_in_triangle ()
97 // generate a repeatable random seed
100 int nObjects = object_group->get_object_count();
102 for (int i = 0; i < nObjects; i++) {
103 SGMatModel * object = object_group->get_object(i);
104 double num = area / object->get_coverage_m2();
106 // place an object each unit of area
107 while ( num > 1.0 ) {
108 add_object_to_triangle(object);
111 // for partial units of area, use a zombie door method to
112 // create the proper random chance of an object being created
115 if ( sg_random() <= num ) {
116 // a zombie made it through our door
117 add_object_to_triangle(object);
123 void SGTriUserData::add_object_to_triangle (SGMatModel * object)
125 // Set up the random heading if required.
127 if (object->get_heading_type() == SGMatModel::HEADING_RANDOM)
128 hdg_deg = sg_random() * 360;
133 makeWorldMatrix(mat, hdg_deg);
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 )
147 void SGTriUserData::makeWorldMatrix (sgMat4 mat, double hdg_deg )
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;
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;
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;
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;
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;
179 // // translate to random point in triangle
181 // random_pt_inside_tri(result, p1, p2, p3);
182 // sgSubVec3(mat[3], result, center);
184 // mat[3][3] = SG_ONE ;
188 * SSG callback for an in-range triangle of randomly-placed objects.
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
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.
200 // tri_in_range_callback (ssgEntity * entity, int mask)
202 // SGTriUserData * data = (SGTriUserData *)entity->getUserData();
203 // if (!data->is_filled_in) {
204 // data->fill_in_triangle();
205 // data->is_filled_in = true;
212 * SSG callback for an out-of-range triangle of randomly-placed objects.
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.
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.
224 // tri_out_of_range_callback (ssgEntity * entity, int mask)
226 // SGTriUserData * data = (SGTriUserData *)entity->getUserData();
227 // if (data->is_filled_in) {
228 // data->branch->removeAllKids();
229 // data->is_filled_in = false;
236 * Calculate the bounding radius of a triangle from its center.
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.
244 // static inline float
245 // get_bounding_radius( sgVec3 center, float *p1, float *p2, float *p3)
247 // return sqrt( SG_MAX3( sgDistanceSquaredVec3(center, p1),
248 // sgDistanceSquaredVec3(center, p2),
249 // sgDistanceSquaredVec3(center, p3) ) );
254 * Set up a triangle for randomly-placed objects.
256 * No objects will be added unless the triangle comes into range.
260 void SGLeafUserData::setup_triangle (int i )
263 // leaf->getTriangle(i, &n1, &n2, &n3);
265 // float * p1 = leaf->getVertex(n1);
266 // float * p2 = leaf->getVertex(n2);
267 // float * p3 = leaf->getVertex(n3);
269 // // Set up a single center point for LOD
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);
277 // // maximum radius of an object from center.
278 // double bounding_radius = get_bounding_radius(center, p1, p2, p3);
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;
285 // sgMakeTransMat4(TRANS, center);
286 // location->setTransform(TRANS);
287 // branch->addKid(location);
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);
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,
302 // ssgRangeSelector * lod = new ssgRangeSelector;
303 // lod->setRanges(ranges, 3);
304 // location->addKid(lod);
306 // // Create the in-range and out-of-range
308 // ssgBranch * in_range = new ssgBranch;
309 // ssgBranch * out_of_range = new ssgBranch;
311 // // Set up the user data for if/when
312 // // the random objects in this triangle
314 // SGTriUserData * data = new SGTriUserData;
315 // data->is_filled_in = false;
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);
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);
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);