home *** CD-ROM | disk | FTP | other *** search
/ AP Professional Graphics CD-ROM Library / AP Professional Graphics CD-ROM Library.iso / pc / unix / appendix / gemsiii / luminair / trngllum.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-03-20  |  1.4 KB  |  52 lines

  1. // ******************************************************************
  2. //
  3. // Physically Correct Direct Lighting For Distribution Ray Tracing
  4. //             by Changyaw Wang
  5. //
  6. // triangle_luminaire.c
  7. //
  8. // ******************************************************************
  9.  
  10. #include "utility.h"
  11.  
  12. // Selects a point visible from x given (r1,r2).
  13. // Here, visible means not SELF-shadowed.
  14.  
  15. void triangle::select_visible_point(
  16.                const point& x,   // viewpoint
  17.                const double r1,  // random number
  18.                const double r2,  // random number
  19.                point& on_light,  // point corresponding to (r1,r2)
  20.                double& prob)     // probability of selecting on_light
  21. {
  22.    point pt, pt1, pt2, pt3;
  23.    vector v1, v2, v3, psi, temp1, temp2;
  24.    double u, v, area;
  25.  
  26.    v1 = p1 - x;
  27.    v1.normalize();
  28.    pt1 = x + v1;
  29.    v2 = p2 - x;
  30.    v2.normalize();
  31.    pt2 = x + v2;
  32.    v3 = p3 - x;
  33.    v3.normalize();
  34.    pt3 = x + v3;
  35.    u = 1.0 - sqrt(1.0 - r1);
  36.    v = r2 * sqrt(1.0 - r1);
  37.   
  38.    pt = pt1 + u*(pt2 - pt1) + v*(pt3 - pt1);
  39.    psi = pt - x;
  40.    psi.normalize();
  41.    hit(x, psi, on_light);    
  42.    temp1 = pt2 - pt1;
  43.    temp2 = pt3 - pt1; 
  44.    temp1 = cross(temp1,temp2);
  45.    // area is the area of pt1,pt2,pt3
  46.    area = 0.5 * sqrt(dot(temp1, temp1)); 
  47.    temp1.normalize();
  48.    prob =  distance_squared(x,pt)*dot(-1.0*psi,normal) /
  49.           (distance_squared(x,on_light)*dot(-1.0*psi,temp1)*area); 
  50. }
  51.  
  52.