]> git.mxchange.org Git - simgear.git/commitdiff
Stop objects from overlapping the edge of the triangle in which they are placed....
authorStuart Buchanan <stuart_d_buchanan@yahoo.co.uk>
Thu, 23 Feb 2012 20:10:02 +0000 (20:10 +0000)
committerStuart Buchanan <stuart_d_buchanan@yahoo.co.uk>
Thu, 23 Feb 2012 20:10:02 +0000 (20:10 +0000)
simgear/scene/tgdb/SGTexturedTriangleBin.hxx
simgear/scene/tgdb/obj.cxx

index 3e722d9893f5758ce4860e75028050a6f0ba2277..9c6d6ab72148b792b2d24ba5def4428396e9e6d6 100644 (file)
@@ -235,7 +235,8 @@ public:
     }
   }
   
-   void addRandomPoints(float coverage, 
+   void addRandomPoints(double coverage, 
+                        double spacing,
                         osg::Texture2D* object_mask,
                         std::vector<std::pair<SGVec3f, float> >& points)
   {
@@ -271,22 +272,30 @@ public:
         float c = 1 - a - b;
         SGVec3f randomPoint = a*v0 + b*v1 + c*v2;
         
-        if (object_mask != NULL) {
-          SGVec2f texCoord = a*t0 + b*t1 + c*t2;
-          
-          // Check this random point against the object mask
-          // blue (for buildings) channel. 
-          osg::Image* img = object_mask->getImage();            
-          unsigned int x = (int) (img->s() * texCoord.x()) % img->s();
-          unsigned int y = (int) (img->t() * texCoord.y()) % img->t();
-          
-          if (mt_rand(&seed) < img->getColor(x, y).b()) {  
-            // The red channel contains the rotation for this object                                  
-            points.push_back(std::make_pair(randomPoint, img->getColor(x,y).r()));
-          }
-        } else {
-          points.push_back(std::make_pair(randomPoint, mt_rand(&seed)));
-        }        
+        // Check that the point is sufficiently far from
+        // the edge of the triangle by measuring the distance
+        // from the three lines that make up the triangle.        
+        if (((length(cross(randomPoint - v0, randomPoint - v1)) / length(v1 - v0)) > spacing) &&
+            ((length(cross(randomPoint - v1, randomPoint - v2)) / length(v2 - v1)) > spacing) &&
+            ((length(cross(randomPoint - v2, randomPoint - v0)) / length(v0 - v2)) > spacing)   )
+        {
+          if (object_mask != NULL) {
+            SGVec2f texCoord = a*t0 + b*t1 + c*t2;
+            
+            // Check this random point against the object mask
+            // blue (for buildings) channel. 
+            osg::Image* img = object_mask->getImage();            
+            unsigned int x = (int) (img->s() * texCoord.x()) % img->s();
+            unsigned int y = (int) (img->t() * texCoord.y()) % img->t();
+            
+            if (mt_rand(&seed) < img->getColor(x, y).b()) {  
+              // The red channel contains the rotation for this object                                  
+              points.push_back(std::make_pair(randomPoint, img->getColor(x,y).r()));
+            }
+          } else {
+            points.push_back(std::make_pair(randomPoint, mt_rand(&seed)));
+          }        
+        }
         num -= 1.0;
       }
     }
index 8de319f58a6935194b4d8ead89d422441e4bf57e..235b8f6234cfd5f8bd68f1d4b6527b7a1a801402 100644 (file)
@@ -536,6 +536,7 @@ struct SGTileGeometryBin {
               std::vector<std::pair<SGVec3f, float> > randomPoints;
 
               i->second.addRandomPoints(object->get_coverage_m2(), 
+                                        object->get_spacing_m(),
                                         mat->get_object_mask(i->second), 
                                         randomPoints);
               std::vector<std::pair<SGVec3f, float> >::iterator l;