home *** CD-ROM | disk | FTP | other *** search
/ Amiga Tools 3 / Amiga Tools 3.iso / grafik / raytracing / rayshade-4.0.6.3 / libray / libtext / stripe.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-09  |  3.0 KB  |  136 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.  * stripe.c,v 4.1 1994/08/09 08:03:20 explorer Exp
  17.  *
  18.  * stripe.c,v
  19.  * Revision 4.1  1994/08/09  08:03:20  explorer
  20.  * Bump version to 4.1
  21.  *
  22.  * Revision 1.1.1.1  1994/08/08  04:52:15  explorer
  23.  * Initial import.  This is a prerelease of 4.0.6enh3, or 4.1 possibly.
  24.  *
  25.  * Revision 4.0  91/07/17  14:43:52  kolb
  26.  * Initial version.
  27.  * 
  28.  */
  29. #include "texture.h"
  30. #include "stripe.h"
  31.  
  32. Stripe *
  33. StripeCreate(surf, width, bump, mapping)
  34. Surface *surf;
  35. Float width, bump;
  36. Mapping *mapping;
  37. {
  38.     Stripe *stripe;
  39.  
  40.     stripe = (Stripe *)Malloc(sizeof(Stripe));
  41.     stripe->surf = surf;
  42.     stripe->mapping = mapping;
  43.     stripe->width = width;
  44.     stripe->bump = bump;
  45.     return stripe;
  46. }
  47.  
  48. void
  49. StripeApply(stripe, prim, ray, pos, norm, gnorm, surf)
  50. Stripe *stripe;
  51. Geom *prim;
  52. Vector *ray, *pos, *norm, *gnorm;
  53. Surface *surf;
  54. {
  55.     Vector dpdu, dpdv;
  56.     Float fu, fv, u, v;
  57.  
  58.     TextToUV(stripe->mapping, prim, pos, gnorm, &u, &v, &dpdu, &dpdv);
  59.  
  60.     u -= floor(u);
  61.     v -= floor(v);
  62.  
  63.     /*
  64.      *    s s          s
  65.      *   | | |        | |
  66.      * 1 +-+------------+
  67.      *   |X|\^^^^^^^^^^/| } s
  68.      *   |X|<+--------+>|
  69.      *   |X|<|        |>|
  70.      *   |X|<|        |>|
  71.      *   |X|<|        |>|
  72.      * v |X|<|        |>|
  73.      *   |X|<|        |>|
  74.      *   |X|<|        |>|
  75.      *   |X|<+--------+>|
  76.      *   |X|/vvvvvvvvvv\| } s
  77.      *   |X+------------+
  78.      *   |XXXXXXXXXXXXXX| } s
  79.      * 0 +--------------+
  80.      *   0              1
  81.      *        u
  82.      *
  83.      * where ^ == positive fv, 0 fu, original surf.
  84.      *     v == negative fv, 0 fu, original surf.
  85.      *     > == positive fu, 0 fv, original surf.
  86.      *     < == negative fu, 0 fv, original surf.
  87.      *   blank == 0 fu, 0 fv, original surf.
  88.      *       X == 0 fu, 0 fv, alternate surf.
  89.      * for stripe->bump > 0.  For stripe->bump < 0., change signs.
  90.      */
  91.      
  92.     if (u > 2*stripe->width && v > 2*stripe->width &&
  93.         u <= 1. - stripe->width && v <= 1. - stripe->width)
  94.         /* flat surface */
  95.         return;
  96.     else if (u < stripe->width || v < stripe->width) {
  97.         /* on the bottom of the bump. */
  98.         *surf = *stripe->surf;
  99.         return;
  100.     }
  101.  
  102.     /*
  103.      * Lower u & v edges are the 'flat' part of the bump --
  104.      * make our lives simpler below by 'removing' this area
  105.      * from u & v.
  106.      */
  107.     u = (u - stripe->width) / (1. - stripe->width);
  108.     v = (v - stripe->width) / (1. - stripe->width);
  109.     /*
  110.      * Now the hard part -- where's the bump?
  111.      */
  112.     if (v < u) { 
  113.         if (v < 1. - u) {
  114.             /* bottom */
  115.             fu = 0.;
  116.             fv = -stripe->bump;
  117.         } else {
  118.             /* right */
  119.             fu = stripe->bump;
  120.             fv = 0.;
  121.         }
  122.     } else {
  123.         if (v < 1. - u) {
  124.             /* left */
  125.             fu = -stripe->bump;
  126.             fv = 0.;
  127.         } else {
  128.             /* top */
  129.             fu = 0.;
  130.             fv = stripe->bump;
  131.         }
  132.     }
  133.  
  134.     MakeBump(norm, &dpdu, &dpdv, fu, fv);
  135. }
  136.