home *** CD-ROM | disk | FTP | other *** search
/ Hackers Magazine 57 / CdHackersMagazineNr57.iso / Software / Multimedia / k3d-setup-0.7.11.0.exe / share / k3d / shaders / surface / k3d_saturnring.sl < prev    next >
Encoding:
Text File  |  2008-01-23  |  2.7 KB  |  106 lines

  1. /*
  2.  * TLRing.sl -- surface for a saturn like ring to be used on a disk
  3.  *
  4.  * DESCRIPTION:
  5.  *    When put on a disk will give a "saturn-like ringed" apearence with
  6.  * varing colors and transparency.
  7.  *
  8.  * PARAMETERS:
  9.  *    Ka, Kd - the usual
  10.  *    cutoff - what point to start rings (radius of transparency)
  11.  *    ringrad - radius of ring
  12.  *    opacity - the opacity of the rings (may not be used anymore)
  13.  *
  14.  * HINTS:
  15.  *    The default values assume that the disk has a radius of one.  If it is
  16.  *  otherwise then they will neeb to be changed.
  17.  *
  18.  * AUTHOR: Tal Lancaster
  19.  *    tal@SpamSucks_cs.caltech.edu
  20.  *
  21.  * History:
  22.  *    Created: 5/15/95
  23.  */
  24.  
  25. #define RING1 0.83    /* Relative spacing for outermost ring */
  26. #define RING2 0.77    /* Relative spacing for next outermost ring */    
  27. #define RING3 0.62    /* Relative spacing for   "  outermost ring */
  28. #define RING4 0.58    /* Relative spacing for   "  outermost ring */
  29. #define RING5 0.55    /* Relative spacing for   "  outermost ring */
  30.  
  31. #define snoise25(x) (2.5 * (noise(x) - 1)) /* why this variation of snoise? */
  32.  
  33.  
  34. surface k3d_saturnring (float Ka = 1.0, Kd = 1.0,
  35.     cutoff = 0.55, ringrad = 1.0, opacity = 0.5;)
  36. {
  37.     point PP;       /* Transformed point */
  38.     point Nf;       /* Forward facing Normalized vector of incident light */
  39.     float val;      /* length of PP */
  40.     float relpos;   /* relative position of PP on disk 
  41.                                    (a percentage distance) */
  42.     float oi = 0.0; /* Opacity holder */
  43.     color cs;        /* color holder */
  44.     color dgrey = color (.266, .266, .266);      /* A shade of dark grey */
  45.     color dyellow = color (.73, .664, .398);     /* A shade of dark yellow */
  46.     color dpink = color (.664, .465,  .465);     /* A shade of dark pink */
  47.     color mutedgreen = color (.531, .531, .398); /* A shade of muted green */
  48.     
  49.     PP = transform ("shader", P);
  50.     val= length (PP);
  51.      
  52. #define DEBUG 0
  53. #if DEBUG    
  54.     printf ("val %f ringrad %f \n",
  55.      val,  ringrad);
  56. #endif
  57.      
  58.     if (val < cutoff * ringrad) {
  59.         /* Creating an inner disk that is transparent to place the planet */
  60.         Oi = 0.0;    
  61.     }
  62.     else {
  63.         /* Create rings of varing transparency */
  64.         relpos =  val / ringrad;
  65.         oi =  (relpos + snoise25 (40* relpos) - floor(relpos) );
  66.         
  67. #define DEBUG2 0
  68. #if DEBUG2
  69.         printf ("oi = %f\n", oi);
  70. #endif
  71.  
  72.         /* Create some gaps of completely transparent rings */
  73.         if (oi > 1.0)
  74.             oi = 0.0;
  75.     }
  76.     
  77.     
  78.     if (oi == 0.0)
  79.         Oi = 0.0;
  80.         
  81.     else {
  82.  
  83.         if (relpos >  RING1) 
  84.             cs = dgrey;
  85.         else if (relpos >  RING2) {
  86.             /* Want transparent section */
  87.             oi = 0.0;
  88.             Oi = 0.0;
  89.         }
  90.         else if (relpos > RING3) {
  91.             cs = dyellow;    
  92.         }
  93.         else if (relpos > RING4)
  94.             cs = dpink;    
  95.         else 
  96.             cs = mutedgreen;    
  97.  
  98.         if (oi != 0.0) {
  99.             Oi = oi;
  100.             /* A Matte model */
  101.             Nf = faceforward (normalize(N), I);
  102.             Ci = Oi * (cs * (Ka*ambient() + Kd*diffuse(Nf) ));
  103.         }
  104.     }
  105. }
  106.