Subversion Repositories gelsvn

Rev

Rev 428 | Rev 430 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 428 Rev 429
Line 394... Line 394...
394
  }
394
  }
395
 
395
 
396
  bool BSPTree::intersect_node(Ray &ray, const BSPNode &node, double t_min, double t_max) const 
396
  bool BSPTree::intersect_node(Ray &ray, const BSPNode &node, double t_min, double t_max) const 
397
  {
397
  {
398
//    cout << node.plane << endl;
398
//    cout << node.plane << endl;
399
    node_calls++;
399
    node_calls++;    
400
    static bool found;
-
 
401
    static int i;
-
 
402
    
-
 
403
    if (node.axis_leaf==4) 
400
    if (node.axis_leaf==4) 
404
    {
401
    {
405
      found = false; 
402
      bool found = false; 
406
      for(i=0;i<node.count;i++) 
403
      for(int i=0; i < node.count; ++i) 
407
      {
404
      {
408
//        const TriAccel* tri2 = all_triaccel[node.id+i];
405
//        const TriAccel* tri2 = all_triaccel[node.id+i];
409
        const ISectTri* tri = all_objects[node.id+i];
406
        const ISectTri* tri = all_objects[node.id+i];
410
//        if (intersect2(ray, *tri2, t_max))  
407
//        if (intersect2(ray, *tri2, t_max))  
411
//          found=true;
408
//          found=true;
412
        if (intersect(ray, *tri, t_max))  
409
        if (intersect(ray, *tri, t_max))  
413
          found=true;
410
          found=true;
414
      }
411
      }
415
      if (found)
412
      if (found)
416
    return true;
413
        return true;
417
      else 
414
      else 
418
    return false;
415
        return false;
419
    } 
416
    } 
420
    else 
417
    else 
421
    {
418
    {
422
      BSPNode *near_node;
419
      BSPNode *near_node;
423
      BSPNode *far_node;
420
      BSPNode *far_node;
Line 613... Line 610...
613
  }
610
  }
614
 
611
 
615
  bool BSPTree::intersect(Ray &ray, const ISectTri &isecttri, double t_max) const 
612
  bool BSPTree::intersect(Ray &ray, const ISectTri &isecttri, double t_max) const 
616
  {
613
  {
617
    tri_calls++;
614
    tri_calls++;
-
 
615
 
618
    // This is the Möller-Trumbore method
616
    // This is the Möller-Trumbore method
619
    static Vec3d direction; // = Vec3d(ray.direction);
617
    Vec3d direction(ray.direction);
620
    static Vec3d origin; // = Vec3d(ray.direction);
-
 
621
    static Vec3d edge1; // = Vec3d(ray.direction);
618
    Vec3d edge0(isecttri.edge0);
622
    static Vec3d edge0; // = Vec3d(ray.direction);
619
    Vec3d edge1(isecttri.edge1);
623
    static Vec3d point0; // = Vec3d(ray.direction);
-
 
624
    static Vec3d p;
-
 
625
    static Vec3d q;
-
 
626
    static Vec3d s;
-
 
627
    static Vec3f point;
-
 
628
    static double a, f, u, v, t;
-
 
629
    static double ray_dist_sq;
-
 
630
    
-
 
631
    static double dist_sq;
-
 
632
    
-
 
633
    static Vec3f found_point;
-
 
634
    
-
 
635
    direction.set((double)ray.direction[0], (double)ray.direction[1], (double)ray.direction[2]);
-
 
636
    edge0.set((double)isecttri.edge0[0], (double)isecttri.edge0[1], (double)isecttri.edge0[2]);
-
 
637
    edge1.set((double)isecttri.edge1[0], (double)isecttri.edge1[1], (double)isecttri.edge1[2]);
-
 
638
 
620
 
639
// Ray-tri intersection
621
    // Ray-triangle intersection
640
//      const double eps = 0.001f;
-
 
641
/********* Complain!!!!!!!!!!!!!!!! *****************/      
-
 
642
    //p = cross(direction, edge1);
622
    Vec3d p = cross(direction, edge1);
643
    // Why the &%¤/ is this so much faster????? - Because of MS Compiler - the intel compiler does it right!!
-
 
644
    p.set(direction[1] * edge1[2] - direction[2] * edge1[1], 
-
 
645
          direction[2] * edge1[0] - direction[0] * edge1[2], 
-
 
646
          direction[0] * edge1[1] - direction[1] * edge1[0]);
-
 
647
/****************************************************/
-
 
648
    a = dot(edge0,p);
623
    double a = dot(edge0, p);
649
    if (a>-d_eps && a<d_eps)
624
    if(a > -d_eps && a < d_eps)
650
      return false;
-
 
651
  // Just delay these 
-
 
652
    origin.set((double)ray.origin[0], (double)ray.origin[1], (double)ray.origin[2]);
-
 
653
    point0.set((double)isecttri.point0[0], (double)isecttri.point0[1], (double)isecttri.point0[2]);
-
 
654
    
-
 
655
    f = 1.0/a;
-
 
656
    s = origin - point0;
-
 
657
    u = f*dot(s,p);
-
 
658
    if (u<0.0 || u>1.0)
-
 
659
      return false;
625
      return false;
660
/********* Complain!!!!!!!!!!!!!!!! *****************/      
-
 
-
 
626
 
661
  //q = cross(s, edge0);
627
    // Just delay these 
662
  // Why the &%¤/ is this so much faster?????
628
    Vec3d origin(ray.origin);
663
    q.set(s[1] * edge0[2] - s[2] * edge0[1], 
629
    Vec3d point0(isecttri.point0);    
664
      s[2] * edge0[0] - s[0] * edge0[2], 
630
    double f = 1.0/a;
665
      s[0] * edge0[1] - s[1] * edge0[0]);
631
    Vec3d s = origin - point0;
666
/****************************************************/
-
 
667
    
-
 
668
    v = f * dot(direction, q);  
632
    double u = f*dot(s,p);
669
    if (v<0.0 || u+v>1.0)
633
    if(u < 0.0 || u > 1.0)
670
      return false;
634
      return false;
-
 
635
 
-
 
636
    Vec3d q = cross(s, edge0);
671
    t = f*dot(edge1, q);
637
    double v = f*dot(direction, q);  
672
    if (t<0)
638
    if(v < 0.0 || u + v > 1.0)
673
      return false;
639
      return false;
-
 
640
 
-
 
641
    double t = f*dot(edge1, q);
674
    if (fabs(t)<d_eps)
642
    if(t < 0.0)
675
      return false;
643
      return false;
676
    if (t_max<t)
644
    if(fabs(t) < f_eps)
677
      return false;
645
      return false;
678
    point = ray.origin + ray.direction*t;
-
 
679
    
-
 
680
    dist_sq = dot(point-ray.origin, point-ray.origin);
-
 
681
    ray_dist_sq = ray.dist * ray.dist;
-
 
682
    
-
 
683
    if (dist_sq<f_eps)
646
    if(t_max < t)
684
      return false;
647
      return false;
685
    if (dist_sq>ray_dist_sq)
648
    if(t > ray.dist)
686
      return false;
649
      return false;
687
  
650
  
688
    ray.dist = sqrt(dist_sq);
651
    ray.dist = t;
689
    ray.u = u;
652
    ray.u = u;
690
    ray.v = v;
653
    ray.v = v;
691
    ray.hit_object = (TriMesh*)isecttri.mesh_id;
654
    ray.hit_object = (TriMesh*)isecttri.mesh_id;
692
    ray.hit_face_id = isecttri.tri_id;
655
    ray.hit_face_id = isecttri.tri_id;
693
    ray.has_hit=true;
656
    ray.has_hit=true;