home *** CD-ROM | disk | FTP | other *** search
/ Otherware / Otherware_1_SB_Development.iso / mac / developm / source / macraysh.sit / Code / Source / noise.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-11-11  |  7.7 KB  |  288 lines

  1. /*
  2.  * noise.c
  3.  *
  4.  * Copyright (C) 1989, 1991, Robert Skinner, 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: noise.c,v 4.0 91/07/17 14:43:38 kolb Exp Locker: kolb $
  17.  *
  18.  * $Log:    noise.c,v $
  19.  * Revision 4.0  91/07/17  14:43:38  kolb
  20.  * Initial version.
  21.  * 
  22.  */
  23. #include "common.h"
  24.  
  25. #define MINX        -1000000
  26. #define MINY        MINX
  27. #define MINZ        MINX
  28.  
  29. #define SCURVE(a) ((a)*(a)*(3.0-2.0*(a)))
  30. #define REALSCALE ( 2.0 / 65536.0 )
  31. #define NREALSCALE ( 2.0 / 4096.0 )
  32. #define Hash3d(a,b,c) hashTable[hashTable[hashTable[(a) & 0xfff] ^ ((b) & 0xfff)] ^ ((c) & 0xfff)]
  33. #define Hash(a,b,c) (xtab[(xtab[(xtab[(a) & 0xff] ^ (b)) & 0xff] ^ (c)) & 0xff] & 0xff)
  34.  
  35. #define INCRSUM(m,s,x,y,z)    ((s)*(RTable[m]*0.5        \
  36.                     + RTable[m+1]*(x)    \
  37.                     + RTable[m+2]*(y)    \
  38.                     + RTable[m+3]*(z)))    \
  39.  
  40.  
  41. #define MAXSIZE 267
  42.  
  43. Float        RTable[MAXSIZE];
  44. static short *hashTable ;
  45. static int    R() ;
  46. static int Crc16(register char *buf,register int  count);
  47.  
  48. static unsigned short xtab[256] =
  49. {
  50.    0x0000, 0xc0c1, 0xc181, 0x0140, 0xc301, 0x03c0, 0x0280, 0xc241,
  51.    0xc601, 0x06c0, 0x0780, 0xc741, 0x0500, 0xc5c1, 0xc481, 0x0440,
  52.    0xcc01, 0x0cc0, 0x0d80, 0xcd41, 0x0f00, 0xcfc1, 0xce81, 0x0e40,
  53.    0x0a00, 0xcac1, 0xcb81, 0x0b40, 0xc901, 0x09c0, 0x0880, 0xc841,
  54.    0xd801, 0x18c0, 0x1980, 0xd941, 0x1b00, 0xdbc1, 0xda81, 0x1a40,
  55.    0x1e00, 0xdec1, 0xdf81, 0x1f40, 0xdd01, 0x1dc0, 0x1c80, 0xdc41,
  56.    0x1400, 0xd4c1, 0xd581, 0x1540, 0xd701, 0x17c0, 0x1680, 0xd641,
  57.    0xd201, 0x12c0, 0x1380, 0xd341, 0x1100, 0xd1c1, 0xd081, 0x1040,
  58.    0xf001, 0x30c0, 0x3180, 0xf141, 0x3300, 0xf3c1, 0xf281, 0x3240,
  59.    0x3600, 0xf6c1, 0xf781, 0x3740, 0xf501, 0x35c0, 0x3480, 0xf441,
  60.    0x3c00, 0xfcc1, 0xfd81, 0x3d40, 0xff01, 0x3fc0, 0x3e80, 0xfe41,
  61.    0xfa01, 0x3ac0, 0x3b80, 0xfb41, 0x3900, 0xf9c1, 0xf881, 0x3840,
  62.    0x2800, 0xe8c1, 0xe981, 0x2940, 0xeb01, 0x2bc0, 0x2a80, 0xea41,
  63.    0xee01, 0x2ec0, 0x2f80, 0xef41, 0x2d00, 0xedc1, 0xec81, 0x2c40,
  64.    0xe401, 0x24c0, 0x2580, 0xe541, 0x2700, 0xe7c1, 0xe681, 0x2640,
  65.    0x2200, 0xe2c1, 0xe381, 0x2340, 0xe101, 0x21c0, 0x2080, 0xe041,
  66.    0xa001, 0x60c0, 0x6180, 0xa141, 0x6300, 0xa3c1, 0xa281, 0x6240,
  67.    0x6600, 0xa6c1, 0xa781, 0x6740, 0xa501, 0x65c0, 0x6480, 0xa441,
  68.    0x6c00, 0xacc1, 0xad81, 0x6d40, 0xaf01, 0x6fc0, 0x6e80, 0xae41,
  69.    0xaa01, 0x6ac0, 0x6b80, 0xab41, 0x6900, 0xa9c1, 0xa881, 0x6840,
  70.    0x7800, 0xb8c1, 0xb981, 0x7940, 0xbb01, 0x7bc0, 0x7a80, 0xba41,
  71.    0xbe01, 0x7ec0, 0x7f80, 0xbf41, 0x7d00, 0xbdc1, 0xbc81, 0x7c40,
  72.    0xb401, 0x74c0, 0x7580, 0xb541, 0x7700, 0xb7c1, 0xb681, 0x7640,
  73.    0x7200, 0xb2c1, 0xb381, 0x7340, 0xb101, 0x71c0, 0x7080, 0xb041,
  74.    0x5000, 0x90c1, 0x9181, 0x5140, 0x9301, 0x53c0, 0x5280, 0x9241,
  75.    0x9601, 0x56c0, 0x5780, 0x9741, 0x5500, 0x95c1, 0x9481, 0x5440,
  76.    0x9c01, 0x5cc0, 0x5d80, 0x9d41, 0x5f00, 0x9fc1, 0x9e81, 0x5e40,
  77.    0x5a00, 0x9ac1, 0x9b81, 0x5b40, 0x9901, 0x59c0, 0x5880, 0x9841,
  78.    0x8801, 0x48c0, 0x4980, 0x8941, 0x4b00, 0x8bc1, 0x8a81, 0x4a40,
  79.    0x4e00, 0x8ec1, 0x8f81, 0x4f40, 0x8d01, 0x4dc0, 0x4c80, 0x8c41,
  80.    0x4400, 0x84c1, 0x8581, 0x4540, 0x8701, 0x47c0, 0x4680, 0x8641,
  81.    0x8201, 0x42c0, 0x4380, 0x8341, 0x4100, 0x81c1, 0x8081, 0x4040
  82. };
  83.  
  84. Float Chaos(), Marble();
  85.  
  86. void
  87. InitTextureTable()
  88. {
  89.     int i, j, temp;
  90.  
  91.     seednrand(1);
  92.     hashTable = (short *) Malloc(4096*sizeof(short int)) ;
  93.     for (i = 0; i < 4096; i++)
  94.         hashTable[i] = i;
  95.     for (i = 4095; i > 0; i--) {
  96.         j = (int)(nrand() * 4096);
  97.         temp = hashTable[i];
  98.         hashTable[i] = hashTable[j];
  99.         hashTable[j] = temp;
  100.     }
  101. }
  102.  
  103. void
  104. NoiseInit()
  105. {
  106.     int i;
  107.     Vector rp;
  108.  
  109.     InitTextureTable();
  110.  
  111.     for (i = 0; i < MAXSIZE; i++) {
  112.            rp.x = rp.y = rp.z = (Float)i;
  113.            RTable[i] = R(&rp)*REALSCALE - 1.0;
  114.      }
  115. }
  116.  
  117. static int
  118. R(v)
  119. Vector *v;
  120. {
  121.     v->x *= .12345;
  122.     v->y *= .12345;
  123.     v->z *= .12345;
  124.  
  125.     return Crc16((char *)v, sizeof(Vector));
  126. }
  127.  
  128. /*
  129.  * Note that passing a Float to Crc16 and interpreting it as
  130.  * an array of chars means that machines with different floating-point
  131.  * representation schemes will evaluate Noise(point) differently.
  132.  */
  133. static int
  134. Crc16(buf, count)
  135. register char *buf;
  136. register int  count;
  137. {
  138.     register unsigned int crc = 0;
  139.  
  140.     while (count--)
  141.         crc = (crc >> 8) ^ xtab[ (unsigned char) (crc ^ *buf++) ];
  142.  
  143.     return crc;
  144. }
  145.  
  146.  
  147. /*
  148.  * Robert Skinner's Perlin-style "Noise" function
  149.  */
  150. Float
  151. Noise3(point)
  152. Vector *point;
  153. {
  154.     register int    ix, iy, iz, jx, jy, jz;
  155.     Float        x, y, z;
  156.     Float    sx, sy, sz, tx, ty, tz;
  157.     Float    sum;
  158.     short    m;
  159.  
  160.  
  161.     /* ensures the values are positive. */
  162.     x = point->x - MINX; y = point->y - MINY; z = point->z - MINZ;
  163.  
  164.     /* its equivalent integer lattice point. */
  165.     ix = (int)x; iy = (int)y; iz = (int)z;
  166.     jx = ix+1; jy = iy + 1; jz = iz + 1;
  167.  
  168.     sx = SCURVE(x - ix); sy = SCURVE(y - iy); sz = SCURVE(z - iz);
  169.  
  170.     /* the complement values of sx,sy,sz */
  171.     tx = 1.0 - sx; ty = 1.0 - sy; tz = 1.0 - sz;
  172.  
  173.     /*
  174.      *  interpolate!
  175.      */
  176.     m = Hash3d( ix, iy, iz ) & 0xFF;
  177.     sum = INCRSUM(m,(tx*ty*tz),(x-ix),(y-iy),(z-iz));
  178.  
  179.     m = Hash3d( jx, iy, iz ) & 0xFF;
  180.     sum += INCRSUM(m,(sx*ty*tz),(x-jx),(y-iy),(z-iz));
  181.  
  182.     m = Hash3d( ix, jy, iz ) & 0xFF;
  183.     sum += INCRSUM(m,(tx*sy*tz),(x-ix),(y-jy),(z-iz));
  184.  
  185.     m = Hash3d( jx, jy, iz ) & 0xFF;
  186.     sum += INCRSUM(m,(sx*sy*tz),(x-jx),(y-jy),(z-iz));
  187.  
  188.     m = Hash3d( ix, iy, jz ) & 0xFF;
  189.     sum += INCRSUM(m,(tx*ty*sz),(x-ix),(y-iy),(z-jz));
  190.  
  191.     m = Hash3d( jx, iy, jz ) & 0xFF;
  192.     sum += INCRSUM(m,(sx*ty*sz),(x-jx),(y-iy),(z-jz));
  193.  
  194.     m = Hash3d( ix, jy, jz ) & 0xFF;
  195.     sum += INCRSUM(m,(tx*sy*sz),(x-ix),(y-jy),(z-jz));
  196.  
  197.     m = Hash3d( jx, jy, jz ) & 0xFF;
  198.     sum += INCRSUM(m,(sx*sy*sz),(x-jx),(y-jy),(z-jz));
  199.  
  200.     return sum;
  201.  
  202. }
  203.  
  204. /*
  205.  * Vector-valued "Noise"
  206.  */
  207. void
  208. DNoise3(point, result)
  209. Vector *point, *result;
  210. {
  211.     register int    ix, iy, iz, jx, jy, jz;
  212.     Float        x, y, z;
  213.     Float px, py, pz, s;
  214.     Float    sx, sy, sz, tx, ty, tz;
  215.     short    m;
  216.  
  217.     /* ensures the values are positive. */
  218.     x = point->x - MINX; y = point->y - MINY; z = point->z - MINZ;
  219.  
  220.     /* its equivalent integer lattice point. */
  221.     ix = (int)x; iy = (int)y; iz = (int)z;
  222.     jx = ix+1; jy = iy + 1; jz = iz + 1;
  223.  
  224.     sx = SCURVE(x - ix); sy = SCURVE(y - iy); sz = SCURVE(z - iz);
  225.  
  226.     /* the complement values of sx,sy,sz */
  227.     tx = 1.0 - sx; ty = 1.0 - sy; tz = 1.0 - sz;
  228.  
  229.     /*
  230.      *  interpolate!
  231.      */
  232.     m = Hash3d( ix, iy, iz ) & 0xFF;
  233.     px = x-ix;  py = y-iy;  pz = z-iz;
  234.     s = tx*ty*tz;
  235.     result->x = INCRSUM(m,s,px,py,pz);
  236.     result->y = INCRSUM(m+4,s,px,py,pz);
  237.     result->z = INCRSUM(m+8,s,px,py,pz);
  238.  
  239.     m = Hash3d( jx, iy, iz ) & 0xFF;
  240.     px = x-jx;
  241.     s = sx*ty*tz;
  242.     result->x += INCRSUM(m,s,px,py,pz);
  243.     result->y += INCRSUM(m+4,s,px,py,pz);
  244.     result->z += INCRSUM(m+8,s,px,py,pz);
  245.  
  246.     m = Hash3d( jx, jy, iz ) & 0xFF;
  247.     py = y-jy;
  248.     s = sx*sy*tz;
  249.     result->x += INCRSUM(m,s,px,py,pz);
  250.     result->y += INCRSUM(m+4,s,px,py,pz);
  251.     result->z += INCRSUM(m+8,s,px,py,pz);
  252.  
  253.     m = Hash3d( ix, jy, iz ) & 0xFF;
  254.     px = x-ix;
  255.     s = tx*sy*tz;
  256.     result->x += INCRSUM(m,s,px,py,pz);
  257.     result->y += INCRSUM(m+4,s,px,py,pz);
  258.     result->z += INCRSUM(m+8,s,px,py,pz);
  259.  
  260.     m = Hash3d( ix, jy, jz ) & 0xFF;
  261.     pz = z-jz;
  262.     s = tx*sy*sz;
  263.     result->x += INCRSUM(m,s,px,py,pz);
  264.     result->y += INCRSUM(m+4,s,px,py,pz);
  265.     result->z += INCRSUM(m+8,s,px,py,pz);
  266.  
  267.     m = Hash3d( jx, jy, jz ) & 0xFF;
  268.     px = x-jx;
  269.     s = sx*sy*sz;
  270.     result->x += INCRSUM(m,s,px,py,pz);
  271.     result->y += INCRSUM(m+4,s,px,py,pz);
  272.     result->z += INCRSUM(m+8,s,px,py,pz);
  273.  
  274.     m = Hash3d( jx, iy, jz ) & 0xFF;
  275.     py = y-iy;
  276.     s = sx*ty*sz;
  277.     result->x += INCRSUM(m,s,px,py,pz);
  278.     result->y += INCRSUM(m+4,s,px,py,pz);
  279.     result->z += INCRSUM(m+8,s,px,py,pz);
  280.  
  281.     m = Hash3d( ix, iy, jz ) & 0xFF;
  282.     px = x-ix;
  283.     s = tx*ty*sz;
  284.     result->x += INCRSUM(m,s,px,py,pz);
  285.     result->y += INCRSUM(m+4,s,px,py,pz);
  286.     result->z += INCRSUM(m+8,s,px,py,pz);
  287. }
  288.