home *** CD-ROM | disk | FTP | other *** search
/ RISC DISC 3 / RISC_DISC_3.iso / resources / etexts / gems / gemsv / ch5_5 / finish.cxx < prev    next >
C/C++ Source or Header  |  1995-03-04  |  2KB  |  52 lines

  1. #include <iostream.h>
  2. #include <math.h>
  3. #include "global.h"
  4.  
  5. ostream& operator<<(ostream& o, finish& f) {
  6.         o<<"finish {\n";
  7.         o<<"diffuse "<<f.kd<<"  "<<"brilliance "<<f.kb<<"\n";
  8.         o<<"crand "<<f.kc<<"  "<<"ambient "<<f.ka<<"\n";
  9.         o<<"reflection "<<f.kr<<"\n";
  10.         o<<"phong "<<f.ks<<"  "<<"phong_size "<<f.kp<<"\n";
  11.         o<<"specular "<<f.kh<<"  "<<"roughness "<<f.kg<<"\n";
  12.         if(f.km) o<<"metallic\n";
  13.         o<<"refraction "<<f.kt<<"  "<<"ior "<<f.ki<<"\n";
  14.         o<<"}\n";
  15.         return o;
  16. }
  17.  
  18. extern vector norm(vector& v);
  19.  
  20. intensity finish::surfoptics(color& c, vector& n, ray& r, intersect& i){
  21.         intensity I(this->ka, this->ka, this->ka);
  22.         for(lightsource *ls=lightsources.first(); ls; ls=lightsources.next()) {
  23.                 list<light*>* L=ls->illum(i);
  24.                 for(light *l=L->first(); l; l=L->next()) {
  25.                         double f=n%l->v;
  26.                         vector h=norm(l->v+(-r.d));
  27.                         double j=n%h;
  28.                         double g=pow(j,this->kp);
  29.                         if(f>0.)
  30.                                 I+=c*l->i*(this->kd*f+ this->ks*g);
  31.                         delete l;
  32.                 }
  33.                 delete L;
  34.         }
  35.         if(this->kr) {
  36.                 vector d=norm(r.d-n*((n%r.d)*2));
  37.                 ray rr(i.p,d,r.l+1,2*r.c+1);
  38.                 I+=trace(rr)*this->kr ;
  39.         }
  40.         if(this->kt) {
  41.                 vector l=-r.d;
  42.                 double ci=l%n;
  43.                 double ct=1+(this->ki*this->ki)*(ci*ci-1);
  44.                 if(ct>=0.) {
  45.                         vector d=norm(r.d*this->ki+n*(this->ki*ci-sqrt(ct)));
  46.                         ray rt(i.p,d,r.l+1,2*r.c+2);
  47.                         I+=trace(rt)*this->kt;
  48.                 }
  49.         }
  50.         return I;
  51. }
  52.