X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=simgear%2Fscene%2Ftgdb%2FSGTexturedTriangleBin.hxx;h=bbeb6244c518e207fe19b616f0004693b1c90cf1;hb=7e7ce2f38e87d6244e05730fa4382da088bb25f1;hp=dcf78e1cad79fa322cd005fe90ad8ada536e3b8e;hpb=49473845cf23e40c76f56ae4e041af342d57f13d;p=simgear.git diff --git a/simgear/scene/tgdb/SGTexturedTriangleBin.hxx b/simgear/scene/tgdb/SGTexturedTriangleBin.hxx index dcf78e1c..bbeb6244 100644 --- a/simgear/scene/tgdb/SGTexturedTriangleBin.hxx +++ b/simgear/scene/tgdb/SGTexturedTriangleBin.hxx @@ -30,6 +30,8 @@ #include #include "SGTriangleBin.hxx" + + struct SGVertNormTex { SGVertNormTex() { } @@ -94,13 +96,17 @@ protected: class SGTexturedTriangleBin : public SGTriangleBin { public: + SGTexturedTriangleBin() + { + mt_init(&seed, 123); + } // Computes and adds random surface points to the points list. // The random points are computed with a density of (coverage points)/1 // The points are offsetted away from the triangles in // offset * positive normal direction. void addRandomSurfacePoints(float coverage, float offset, - std::vector& points) const + std::vector& points) { unsigned num = getNumTriangles(); for (unsigned i = 0; i < num; ++i) { @@ -118,13 +124,13 @@ public: // For partial units of area, use a zombie door method to // create the proper random chance of a light being created // for this triangle - float unit = area + sg_random()*coverage; + float unit = area + mt_rand(&seed)*coverage; SGVec3f offsetVector = offset*normalize(normal); // generate a light point for each unit of area while ( coverage < unit ) { - float a = sg_random(); - float b = sg_random(); + float a = mt_rand(&seed); + float b = mt_rand(&seed); if ( a + b > 1 ) { a = 1 - a; b = 1 - b; @@ -136,6 +142,43 @@ public: } } } + + void addRandomPoints(float coverage, + std::vector& points) + { + unsigned num = getNumTriangles(); + for (unsigned i = 0; i < num; ++i) { + triangle_ref triangleRef = getTriangleRef(i); + SGVec3f v0 = getVertex(triangleRef[0]).vertex; + SGVec3f v1 = getVertex(triangleRef[1]).vertex; + SGVec3f v2 = getVertex(triangleRef[2]).vertex; + SGVec3f normal = cross(v1 - v0, v2 - v0); + + // Compute the area + float area = 0.5f*length(normal); + if (area <= SGLimitsf::min()) + continue; + + // for partial units of area, use a zombie door method to + // create the proper random chance of an object being created + // for this triangle. + double num = area / coverage + mt_rand(&seed); + + // place an object each unit of area + while ( num > 1.0 ) { + float a = mt_rand(&seed); + float b = mt_rand(&seed); + if ( a + b > 1 ) { + a = 1 - a; + b = 1 - b; + } + float c = 1 - a - b; + SGVec3f randomPoint = a*v0 + b*v1 + c*v2; + points.push_back(randomPoint); + num -= 1.0; + } + } + } osg::Geometry* buildGeometry(const TriangleVector& triangles) const { @@ -196,6 +239,10 @@ public: osg::Geometry* buildGeometry() const { return buildGeometry(getTriangles()); } + +private: + // Random seed for the triangle. + mt seed; }; #endif