home *** CD-ROM | disk | FTP | other *** search
/ Altsys Virtuoso 2.0K / virtuoso_20k.iso / DemoApps / Graphics / Viewers / raytracers / ohta / Source / shape.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-10-08  |  1.0 KB  |  66 lines

  1. #include <math.h>
  2.  
  3. #include "ray.h"
  4.  
  5. void initsphere();
  6. struct intersect sphere();
  7.  
  8. initshape()
  9. {    nshapetab=1;
  10.     shapetab=nalloc(struct shapetab,nshapetab);
  11.     shapetab[0].shapeinitfunc=initsphere;
  12.     shapetab[0].shapefunc=sphere;
  13.     shapetab[0].nparams=4;
  14. }
  15.  
  16. void initsphere(o)
  17. struct object *o;
  18. {    o->center.x=o->spparams[0];
  19.     o->center.y=o->spparams[1];
  20.     o->center.z=o->spparams[2];
  21.     o->radius=o->spparams[3];
  22. }
  23.  
  24. struct intersect sphere(r,obj)
  25. struct ray r;
  26. register int obj;
  27. {struct vector v,n;
  28. register double *p,b,c,d,t,t0,t1;
  29. struct intersect i;
  30.     i.obj=0;
  31.     p=objects[obj].spparams;
  32.     v.x=r.a.x-p[0];
  33.     v.y=r.a.y-p[1];
  34.     v.z=r.a.z-p[2];
  35.     b=r.l.x*v.x+r.l.y*v.y+r.l.z*v.z;
  36.     c=v.x*v.x+v.y*v.y+v.z*v.z-p[3]*p[3];
  37.     d=b*b-c;
  38.     if(d<0)
  39.         return i;
  40.     d=sqrt(d);
  41.     if(b>=0)
  42.     {    t0= -b-d;
  43.         t1=c/(-b-d);
  44.     }
  45.     else
  46.     {    t0=c/(-b+d);
  47.         t1= -b+d;
  48.     }
  49.     if(t0>MINT)
  50.         t=t0;
  51.     else if(t1>MINT)
  52.         t=t1;
  53.     else
  54.         return i;
  55.     i.obj=obj;
  56.     i.t=t;
  57.     n.x=r.a.x+r.l.x*t-p[0];
  58.     n.y=r.a.y+r.l.y*t-p[1];
  59.     n.z=r.a.z+r.l.z*t-p[2];
  60.     t=sqrt(n.x*n.x+n.y*n.y+n.z*n.z);
  61.     i.n.x=n.x/t;
  62.     i.n.y=n.y/t;
  63.     i.n.z=n.z/t;
  64.     return i;
  65. }
  66.