]> git.mxchange.org Git - flightgear.git/blobdiff - src/FDM/groundcache.cxx
- fix two bugs
[flightgear.git] / src / FDM / groundcache.cxx
index b241a0f1c1e081a5c4c90d9d9b0b985b6947e78f..d4cfb80a9612a8b4a8de9b9b4b0f959dcea5ba70 100644 (file)
 //
 // You should have received a copy of the GNU General Public License
 // along with this program; if not, write to the Free Software
-// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 //
 // $Id$
 
+#ifdef HAVE_CONFIG_H
+#  include "config.h"
+#endif
 
 #include <float.h>
 
@@ -29,6 +32,8 @@
 #include <simgear/constants.h>
 #include <simgear/debug/logstream.hxx>
 #include <simgear/math/sg_geodesy.hxx>
+#include <simgear/scene/material/mat.hxx>
+#include <simgear/scene/material/matlib.hxx>
 
 #include <Main/globals.hxx>
 #include <Scenery/scenery.hxx>
@@ -202,7 +207,7 @@ FGGroundCache::extractGroundProperty( ssgLeaf* l )
 {
   // FIXME: Do more ...
   // Idea: have a get_globals() function which knows about that stuff.
-  // Or most propably read that from a configuration file,
+  // Or most probably read that from a configuration file,
   // from property tree or whatever ...
   
   // Get ground dependent data.
@@ -230,32 +235,15 @@ FGGroundCache::extractGroundProperty( ssgLeaf* l )
   }
 
   else {
-
     // Initialize velocity field.
     sgdSetVec3( gp.vel, 0.0, 0.0, 0.0 );
     sgdSetVec3( gp.rot, 0.0, 0.0, 0.0 );
     sgdSetVec3( gp.pivot, 0.0, 0.0, 0.0 );
-  }
-  
-  // Get the texture name and decide what ground type we have.
-  ssgState *st = l->getState();
-  if (st != NULL && st->isAKindOf(ssgTypeSimpleState())) {
-    ssgSimpleState *ss = (ssgSimpleState*)st;
-    SGPath fullPath( ss->getTextureFilename() ? ss->getTextureFilename(): "" );
-    string file = fullPath.file();
-    SGPath dirPath(fullPath.dir());
-    string category = dirPath.file();
-    
-    if (category == "Runway")
-      gp.type = FGInterface::Solid;
-    else {
-      if (file == "asphault.rgb" || file == "airport.rgb")
-        gp.type = FGInterface::Solid;
-      else if (file == "water.rgb" || file == "water-lake.rgb")
-        gp.type = FGInterface::Water;
-      else if (file == "forest.rgb" || file == "cropwood.rgb")
-        gp.type = FGInterface::Forest;
-    }
+
+    // get some material information for use in the gear model
+    gp.material = globals->get_matlib()->findMaterial(l);
+    if (gp.material)
+      gp.type = gp.material->get_solid() ? FGInterface::Solid : FGInterface::Water;
   }
   
   return gp;
@@ -319,6 +307,7 @@ FGGroundCache::putSurfaceLeafIntoCache(const sgdSphere *sp,
   for (int i = 0; i < nt; ++i) {
     Triangle t;
     t.sphere.empty();
+    t.material = gp.material;
     short v[3];
     l->getTriangle(i, &v[0], &v[1], &v[2]);
     for (int k = 0; k < 3; ++k) {
@@ -356,11 +345,18 @@ FGGroundCache::putSurfaceLeafIntoCache(const sgdSphere *sp,
       sgdVec3 isectpoint;
       if ( sgdIsectInfLinePlane( isectpoint, tmp, down, t.plane ) &&
            fgdPointInTriangle( isectpoint, t.vertices ) ) {
-        found_ground = true;
-        sgdAddVec3(isectpoint, cache_center);
-        double this_radius = sgdLengthVec3(isectpoint);
-        if (ground_radius < this_radius)
-          ground_radius = this_radius;
+        // Compute the offset to the ground cache midpoint
+        sgdVec3 off;
+        sgdSubVec3(off, isectpoint, tmp);
+        // Only accept the altitude if the intersection point is below the
+        // ground cache midpoint
+        if (0 < sgdScalarProductVec3( off, down )) {
+          found_ground = true;
+          sgdAddVec3(isectpoint, cache_center);
+          double this_radius = sgdLengthVec3(isectpoint);
+          if (ground_radius < this_radius)
+            ground_radius = this_radius;
+        }
       }
     }
   }
@@ -385,6 +381,7 @@ FGGroundCache::velocityTransformTriangle(double dt,
   sgdCopyVec3(dst.rotation_pivot, src.rotation_pivot);
 
   dst.type = src.type;
+  dst.material = src.material;
 
   if (dt*sgdLengthSquaredVec3(src.velocity) != 0) {
     sgdVec3 pivotoff, vel;
@@ -434,7 +431,7 @@ FGGroundCache::cache_fill(ssgBranch *branch, sgdMat4 xform,
     if ( e->isAKindOf( ssgTypeBranch() ) ) {
       ssgBranch *b = (ssgBranch *)e;
       if ( b->isAKindOf( ssgTypeTransform() ) ) {
-        // Collect up the transfors required to reach that part of
+        // Collect up the transforms required to reach that part of
         // the branch.
         sgMat4 xform2;
         sgMakeIdentMat4( xform2 );
@@ -451,9 +448,9 @@ FGGroundCache::cache_fill(ssgBranch *branch, sgdMat4 xform,
     // This will minimize the number of vertices/triangles in the cache.
     else if (e->isAKindOf(ssgTypeLeaf())) {
       // Since we reach that leaf if we have an intersection with the
-      // most propably bigger wire/catapult cache sphere, we need to check
+      // most probably bigger wire/catapult cache sphere, we need to check
       // that here, if the smaller cache for the surface has a chance for hits.
-      // Also, if the spheres do not intersect compute a croase agl value
+      // Also, if the spheres do not intersect compute a coarse agl value
       // by following the line downwards originating at the aircraft.
       bool spIsec = sp->intersects(&esphere);
       putSurfaceLeafIntoCache(sp, xform, spIsec, down, (ssgLeaf *)e);
@@ -486,7 +483,7 @@ FGGroundCache::prepare_ground_cache(double ref_time, const double pt[3],
   // Decide where we put the scenery center.
   Point3D old_cntr = globals->get_scenery()->get_center();
   Point3D cntr(pt[0], pt[1], pt[2]);
-  // Only move the cache center if it is unaccaptable far away.
+  // Only move the cache center if it is unacceptable far away.
   if (40*40 < old_cntr.distance3Dsquared(cntr))
     globals->get_scenery()->set_center(cntr);
   else
@@ -516,7 +513,7 @@ FGGroundCache::prepare_ground_cache(double ref_time, const double pt[3],
   sgdSetVec3(down, -pt[0], -pt[1], -pt[2]);
   sgdNormalizeVec3(down);
 
-  // We collaps all transforms we need to reach a particular leaf.
+  // We collapse all transforms we need to reach a particular leaf.
   // The leafs itself will be then transformed later.
   // So our cache is just flat.
   // For leafs which are moving (carriers surface, etc ...)
@@ -532,7 +529,7 @@ FGGroundCache::prepare_ground_cache(double ref_time, const double pt[3],
   cache_fill(terrain, xform, &acSphere, down, &wireSphere);
 
   // some stats
-  SG_LOG(SG_FLIGHT,SG_INFO, "prepare_ground_cache(): ac radius = " << rad
+  SG_LOG(SG_FLIGHT,SG_DEBUG, "prepare_ground_cache(): ac radius = " << rad
          << ", # triangles = " << triangles.size()
          << ", # wires = " << wires.size()
          << ", # catapults = " << catapults.size()
@@ -611,15 +608,14 @@ FGGroundCache::get_cat(double t, const double dpt[3],
 bool
 FGGroundCache::get_agl(double t, const double dpt[3], double max_altoff,
                        double contact[3], double normal[3], double vel[3],
-                       int *type, double *loadCapacity,
-                       double *frictionFactor, double *agl)
+                       int *type, const SGMaterial** material, double *agl)
 {
   bool ret = false;
 
   *type = FGInterface::Unknown;
 //   *agl = 0.0;
-  *loadCapacity = DBL_MAX;
-  *frictionFactor = 1.0;
+  if (material)
+    *material = 0;
   sgdSetVec3( vel, 0.0, 0.0, 0.0 );
   sgdSetVec3( contact, 0.0, 0.0, 0.0 );
   sgdSetVec3( normal, 0.0, 0.0, 0.0 );
@@ -650,16 +646,16 @@ FGGroundCache::get_agl(double t, const double dpt[3], double max_altoff,
     sgdVec3 isecpoint;
     if ( sgdIsectInfLinePlane( isecpoint, pt, dir, triangle.plane ) &&
          sgdPointInTriangle( isecpoint, triangle.vertices ) ) {
-      // Transform to the wgs system
-      sgdAddVec3( isecpoint, cache_center );
-      // compute the radius, good enough approximation to take the geocentric radius
-      SGDfloat radius = sgdLengthSquaredVec3(isecpoint);
-      if (current_radius < radius) {
-        // Compute the vector from pt to the intersection point ...
-        sgdVec3 off;
-        sgdSubVec3(off, pt, isecpoint);
-        // ... and check if it is too high or not
-        if (-max_altoff < sgdScalarProductVec3( off, dir )) {
+      // Compute the vector from pt to the intersection point ...
+      sgdVec3 off;
+      sgdSubVec3(off, isecpoint, pt);
+      // ... and check if it is too high or not
+      if (-max_altoff < sgdScalarProductVec3( off, dir )) {
+        // Transform to the wgs system
+        sgdAddVec3( isecpoint, cache_center );
+        // compute the radius, good enough approximation to take the geocentric radius
+        SGDfloat radius = sgdLengthSquaredVec3(isecpoint);
+        if (current_radius < radius) {
           current_radius = radius;
           ret = true;
           // Save the new potential intersection point.
@@ -673,11 +669,11 @@ FGGroundCache::get_agl(double t, const double dpt[3], double max_altoff,
           sgdAddVec3(vel, triangle.velocity);
           // Save the ground type.
           *type = triangle.type;
-          // FIXME: figure out how to get that sign ...
-//           *agl = sqrt(sqdist);
-          *agl = sgdLengthVec3( dpt ) - sgdLengthVec3( contact );
-//           *loadCapacity = DBL_MAX;
-//           *frictionFactor = 1.0;
+          sgdVec3 dstToContact;
+          sgdSubVec3(dstToContact, contact, dpt);
+          *agl = sgdScalarProductVec3(dir, dstToContact);
+          if (material)
+            *material = triangle.material;
         }
       }
     }
@@ -698,10 +694,10 @@ FGGroundCache::get_agl(double t, const double dpt[3], double max_altoff,
   
   // The altitude is the distance of the requested point from the
   // contact point.
-  *agl = sgdLengthVec3( dpt ) - sgdLengthVec3( contact );
+  sgdVec3 dstToContact;
+  sgdSubVec3(dstToContact, contact, dpt);
+  *agl = sgdScalarProductVec3(dir, dstToContact);
   *type = FGInterface::Unknown;
-  *loadCapacity = DBL_MAX;
-  *frictionFactor = 1.0;
 
   return ret;
 }
@@ -729,7 +725,7 @@ bool FGGroundCache::caught_wire(double t, const double pt[4][3])
   sgdCopyVec3( tri[1][2], pt[3] );
 
   // Intersect the wire lines with each of these triangles.
-  // You have cautght a wire if they intersect.
+  // You have caught a wire if they intersect.
   for (size_t i = 0; i < sz; ++i) {
     sgdVec3 le[2];
     for (int k = 0; k < 2; ++k) {