Material Based Effects - Detection - shapeBase Derivatives

From TDN

shapebase.h

#include "materials/materialList.h"
#include "materials/matInstance.h"
#include "materials/material.h"
bool castRay( const Point3F& start, const Point3F& end, TriRayInfo* rayInfo );

shapebase.cc

#include "materials/materialList.h"
#include "materials/matInstance.h"
bool ShapeBase::castRay( const Point3F& start, const Point3F& end, TriRayInfo* rayInfo )
{
   TSShapeInstance* si = this->getShapeInstance();
   if ( !si || !si->getShape() || si->getShape()->details.empty() )
      return false;
   
   TSShape* tsShape = si->getShape();

   Point3F tstart, tend;

   MatrixF mat = this->getWorldTransform();
   mat.mulP(start,&tstart);
   mat.mulP(end,&tend);

   si->setStatics(0);

   rayInfo->distance = F32_MAX;
   
   const TSDetail& detail = tsShape->details[0];
   S32 ss = detail.subShapeNum;
   S32 od = detail.objectDetailNum;

   S32 first = tsShape->subShapeFirstObject[ss];
   S32 last = tsShape->subShapeFirstObject[ss] + tsShape->subShapeNumObjects[ss];

   for ( S32 m=first; m < last; m++ )
   {
      TSMesh* mesh = tsShape->meshes[ m ];
      if ( !mesh || mesh->getMeshType() != TSMesh::StandardMeshType )
         continue;

      TriRayInfo info;
      if ( mesh->castRayTri( 0, tstart, tend, &info ) && info.distance < rayInfo->distance )
      {
         *rayInfo = info;

         // Ok... we got a hit... cast back into world 
         // space and return it.
         MatrixF invmat( mat );
         invmat.inverse();
         invmat.mulP(rayInfo->point);

         // Find the material for this surface.
         MatInstance* matInst = tsShape->materialList->getMaterialInst( rayInfo->material );
         if ( matInst && matInst->getMaterial() ) {
            rayInfo->material = matInst->getMaterial()->getId();
         } else {
            rayInfo->material = 0;
         }
      }
   }

   si->clearStatics();
   return rayInfo->distance < F32_MAX;
}


(we'll be revisiting this file in a bit, but for now, let's just get to the point of grabbing faces)

Backtrace: Material Based Effects Projects