+
+ sgdVec4 plane;
+ sgdMakePlane( plane, tri[0], tri[1], tri[2] );
+
+ sgdVec3 point;
+ if( fgdIsectInfLinePlane( point, orig, dir, plane ) ) {
+ if( fgdPointInTriangle( point, tri ) ) {
+ // transform point into passed into desired coordinate frame
+ sgdXformPnt3( point, point, m );
+ sgdXformPnt4(plane,plane,m);
+ add(leaf,i,point,plane);
+ num_hits++;
+ }
+ }
+#endif
+ }
+ return num_hits;
+}
+
+
+// Short circuit/slightly optimized version of the full IntersectLeaf()
+int FGHitList::IntersectLeaf( ssgLeaf *leaf, sgdMat4 m,
+ sgdVec3 orig, sgdVec3 dir,
+ GLenum primType )
+{
+ double tmp_dist;
+
+ // number of hits but there could be more that
+ // were not found because of short circut switch !
+ // so you may want to use the unspecialized IntersectLeaf()
+ int n, num_hits = 0;
+
+ int ntri = leaf->getNumTriangles();
+ for ( n = 0; n < ntri; ++n )
+ {
+ sgdVec3 tri[3];
+
+ switch ( primType )
+ {
+ case GL_POLYGON :
+ SG_LOG( SG_TERRAIN, SG_ALERT,
+ "WARNING: dubiously handled GL_POLYGON" );
+ case GL_TRIANGLE_FAN :
+ /* SG_LOG( SG_TERRAIN, SG_ALERT,
+ "IntersectLeaf: GL_TRIANGLE_FAN" ); */
+ if ( !n ) {
+ sgdSetVec3( tri[0], leaf->getVertex( short(0) ) );
+ sgdSetVec3( tri[1], leaf->getVertex( short(1) ) );
+ sgdSetVec3( tri[2], leaf->getVertex( short(2) ) );
+ } else {
+ sgdCopyVec3( tri[1], tri[2] );
+ sgdSetVec3( tri[2], leaf->getVertex( short(n+2) ) );
+ }
+ break;
+ case GL_TRIANGLES :
+ /* SG_LOG( SG_TERRAIN, SG_DEBUG,
+ "IntersectLeaf: GL_TRIANGLES" ); */
+ sgdSetVec3( tri[0], leaf->getVertex( short(n*3) ) );
+ sgdSetVec3( tri[1], leaf->getVertex( short(n*3+1) ) );
+ sgdSetVec3( tri[2], leaf->getVertex( short(n*3+2) ) );
+ break;
+ case GL_QUAD_STRIP :
+ SG_LOG( SG_TERRAIN, SG_ALERT,
+ "WARNING: dubiously handled GL_QUAD_STRIP" );
+ case GL_TRIANGLE_STRIP :
+ /* SG_LOG( SG_TERRAIN, SG_ALERT,
+ "IntersectLeaf: GL_TRIANGLE_STRIP" ); */
+ if ( !n ) {
+ sgdSetVec3( tri[0], leaf->getVertex( short(0) ) );
+ sgdSetVec3( tri[1], leaf->getVertex( short(1) ) );
+ sgdSetVec3( tri[2], leaf->getVertex( short(2) ) );
+ } else {
+ if ( n & 1 ) {
+ sgdCopyVec3( tri[0], tri[2] );
+ sgdSetVec3( tri[2], leaf->getVertex( short(n+2) ) );
+ } else {
+ sgdCopyVec3( tri[1], tri[2] );
+ sgdSetVec3( tri[2], leaf->getVertex( short(n+2) ) );
+ }
+ }
+ break;
+ case GL_QUADS :
+ SG_LOG( SG_TERRAIN, SG_ALERT,
+ "WARNING: dubiously handled GL_QUADS" );
+ sgdSetVec3( tri[0], leaf->getVertex( short(n*2) ) );
+ sgdSetVec3( tri[1], leaf->getVertex( short(n*2+1) ) );
+ sgdSetVec3( tri[2], leaf->getVertex( short(n*2 + 2 - (n&1)*4) ) );
+ break;
+ default:
+ SG_LOG( SG_TERRAIN, SG_ALERT,
+ "WARNING: not-handled structure: " << primType );
+ return IntersectLeaf( leaf, m, orig, dir);