home *** CD-ROM | disk | FTP | other *** search
/ Resource Library: Graphics / graphics-16000.iso / msdos / raytrace / rayshade / src / stripe.c < prev    next >
C/C++ Source or Header  |  1992-04-28  |  3KB  |  130 lines

  1. /*
  2.  * stripe.c
  3.  *
  4.  * Copyright (C) 1989, 1991, Craig E. Kolb
  5.  * All rights reserved.
  6.  *
  7.  * This software may be freely copied, modified, and redistributed
  8.  * provided that this copyright notice is preserved on all copies.
  9.  *
  10.  * You may not distribute this software, in whole or in part, as part of
  11.  * any commercial product without the express consent of the authors.
  12.  *
  13.  * There is no warranty or other guarantee of fitness of this software
  14.  * for any purpose.  It is provided solely "as is".
  15.  *
  16.  * $Id: stripe.c,v 4.0 91/07/17 14:43:52 kolb Exp Locker: kolb $
  17.  *
  18.  * $Log:    stripe.c,v $
  19.  * Revision 4.0  91/07/17  14:43:52  kolb
  20.  * Initial version.
  21.  * 
  22.  */
  23. #include "texture.h"
  24. #include "stripe.h"
  25.  
  26. Stripe *
  27. StripeCreate(surf, width, bump, mapping)
  28. Surface *surf;
  29. Float width, bump;
  30. Mapping *mapping;
  31. {
  32.     Stripe *stripe;
  33.  
  34.     stripe = (Stripe *)RayMalloc(sizeof(Stripe));
  35.     stripe->surf = surf;
  36.     stripe->mapping = mapping;
  37.     stripe->width = width;
  38.     stripe->bump = bump;
  39.     return stripe;
  40. }
  41.  
  42. void
  43. StripeApply(stripe, prim, ray, pos, norm, gnorm, surf)
  44. Stripe *stripe;
  45. Geom *prim;
  46. Vector *ray, *pos, *norm, *gnorm;
  47. Surface *surf;
  48. {
  49.     Vector dpdu, dpdv;
  50.     Float fu, fv, u, v;
  51.  
  52.     TextToUV(stripe->mapping, prim, pos, gnorm, &u, &v, &dpdu, &dpdv);
  53.  
  54.     u -= floor(u);
  55.     v -= floor(v);
  56.  
  57.     /*
  58.      *    s s          s
  59.      *   | | |        | |
  60.      * 1 +-+------------+
  61.      *   |X|\^^^^^^^^^^/| } s
  62.      *   |X|<+--------+>|
  63.      *   |X|<|        |>|
  64.      *   |X|<|        |>|
  65.      *   |X|<|        |>|
  66.      * v |X|<|        |>|
  67.      *   |X|<|        |>|
  68.      *   |X|<|        |>|
  69.      *   |X|<+--------+>|
  70.      *   |X|/vvvvvvvvvv\| } s
  71.      *   |X+------------+
  72.      *   |XXXXXXXXXXXXXX| } s
  73.      * 0 +--------------+
  74.      *   0              1
  75.      *        u
  76.      *
  77.      * where ^ == positive fv, 0 fu, original surf.
  78.      *     v == negative fv, 0 fu, original surf.
  79.      *     > == positive fu, 0 fv, original surf.
  80.      *     < == negative fu, 0 fv, original surf.
  81.      *   blank == 0 fu, 0 fv, original surf.
  82.      *       X == 0 fu, 0 fv, alternate surf.
  83.      * for stripe->bump > 0.  For stripe->bump < 0., change signs.
  84.      */
  85.      
  86.     if (u > 2*stripe->width && v > 2*stripe->width &&
  87.         u <= 1. - stripe->width && v <= 1. - stripe->width)
  88.         /* flat surface */
  89.         return;
  90.     else if (u < stripe->width || v < stripe->width) {
  91.         /* on the bottom of the bump. */
  92.         *surf = *stripe->surf;
  93.         return;
  94.     }
  95.  
  96.     /*
  97.      * Lower u & v edges are the 'flat' part of the bump --
  98.      * make our lives simpler below by 'removing' this area
  99.      * from u & v.
  100.      */
  101.     u = (u - stripe->width) / (1. - stripe->width);
  102.     v = (v - stripe->width) / (1. - stripe->width);
  103.     /*
  104.      * Now the hard part -- where's the bump?
  105.      */
  106.     if (v < u) { 
  107.         if (v < 1. - u) {
  108.             /* bottom */
  109.             fu = 0.;
  110.             fv = -stripe->bump;
  111.         } else {
  112.             /* right */
  113.             fu = stripe->bump;
  114.             fv = 0.;
  115.         }
  116.     } else {
  117.         if (v < 1. - u) {
  118.             /* left */
  119.             fu = -stripe->bump;
  120.             fv = 0.;
  121.         } else {
  122.             /* top */
  123.             fu = 0.;
  124.             fv = stripe->bump;
  125.         }
  126.     }
  127.  
  128.     MakeBump(norm, &dpdu, &dpdv, fu, fv);
  129. }
  130.