+
+ void computeRandomForest(SGMaterialLib* matlib)
+ {
+ SGMaterialTriangleMap::iterator i;
+
+ // generate a repeatable random seed
+ mt seed;
+ mt_init(&seed, unsigned(586));
+
+ for (i = materialTriangleMap.begin(); i != materialTriangleMap.end(); ++i) {
+ SGMaterial *mat = matlib->find(i->first);
+ if (!mat)
+ continue;
+
+ float coverage = mat->get_tree_coverage();
+ if (coverage <= 0)
+ continue;
+
+ // Attributes that don't vary by tree
+ randomForest.texture = mat->get_tree_texture();
+ randomForest.range = mat->get_tree_range();
+ randomForest.width = mat->get_tree_width();
+ randomForest.height = mat->get_tree_height();
+ randomForest.texture_varieties = mat->get_tree_varieties();
+
+ std::vector<SGVec3f> randomPoints;
+ i->second.addRandomSurfacePoints(coverage, 0, randomPoints);
+ std::vector<SGVec3f>::iterator j;
+ for (j = randomPoints.begin(); j != randomPoints.end(); ++j) {
+
+ // Apply a random scaling factor and texture index.
+ float scale = (mt_rand(&seed) + mt_rand(&seed)) / 2.0f + 0.5f;
+ int v = (int) (mt_rand(&seed) * mat->get_tree_varieties());
+ if (v == mat->get_tree_varieties()) v--;
+ randomForest.insert(*j, v, scale);
+ }
+ }
+ }
+