home *** CD-ROM | disk | FTP | other *** search
/ Resource Library: Graphics / graphics-16000.iso / msdos / raytrace / rayshade / src / noise.c < prev    next >
C/C++ Source or Header  |  1992-04-29  |  8KB  |  292 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. #define MAXSIZE    500
  43.  
  44. Float        RTable[MAXSIZE];
  45. static short    *hashTable;
  46. static int    R(), Crc16();
  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 *) RayMalloc(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. static int R(Vector *v);
  104.  
  105. void
  106. NoiseInit()
  107. {
  108.     int i;
  109.     Vector rp;
  110.  
  111.     InitTextureTable();
  112.  
  113.     for (i = 0; i < MAXSIZE; i++) {
  114.         rp.x = rp.y = rp.z = (Float)i;
  115.         RTable[i] = R(&rp)*REALSCALE - 1.0;
  116.     }
  117. }
  118.  
  119. static int Crc16(char *buf,int count);
  120.  
  121. static int
  122. R(v)
  123. Vector *v;
  124. {
  125.     v->x *= .12345;
  126.     v->y *= .12345;
  127.     v->z *= .12345;
  128.  
  129.     return Crc16(v, sizeof(Vector));
  130. }
  131.  
  132. /*
  133.  * Note that passing a Float to Crc16 and interpreting it as
  134.  * an array of chars means that machines with different floating-point
  135.  * representation schemes will evaluate Noise(point) differently.
  136.  */
  137. static int
  138. Crc16(buf, count)
  139. register char *buf;
  140. register int  count;
  141. {
  142.     register unsigned int crc = 0;
  143.  
  144.     while (count--)
  145.         crc = (crc >> 8) ^ xtab[ (unsigned char) (crc ^ *buf++) ];
  146.  
  147.     return crc;
  148. }
  149.  
  150.  
  151. /*
  152.  * Robert Skinner's Perlin-style "Noise" function
  153.  */
  154. Float
  155. Noise3(point)
  156. Vector *point;
  157. {
  158.     register int    ix, iy, iz, jx, jy, jz;
  159.     Float        x, y, z;
  160.     Float    sx, sy, sz, tx, ty, tz;
  161.     Float    sum;
  162.     short    m;
  163.  
  164.  
  165.     /* ensures the values are positive. */
  166.     x = point->x - MINX; y = point->y - MINY; z = point->z - MINZ;
  167.  
  168.     /* its equivalent integer lattice point. */
  169.     ix = (int)x; iy = (int)y; iz = (int)z;
  170.     jx = ix+1; jy = iy + 1; jz = iz + 1;
  171.  
  172.     sx = SCURVE(x - ix); sy = SCURVE(y - iy); sz = SCURVE(z - iz);
  173.  
  174.     /* the complement values of sx,sy,sz */
  175.     tx = 1.0 - sx; ty = 1.0 - sy; tz = 1.0 - sz;
  176.  
  177.     /*
  178.      *  interpolate!
  179.      */
  180.     m = Hash3d( ix, iy, iz ) & 0xFF;
  181.     sum = INCRSUM(m,(tx*ty*tz),(x-ix),(y-iy),(z-iz));
  182.  
  183.     m = Hash3d( jx, iy, iz ) & 0xFF;
  184.     sum += INCRSUM(m,(sx*ty*tz),(x-jx),(y-iy),(z-iz));
  185.  
  186.     m = Hash3d( ix, jy, iz ) & 0xFF;
  187.     sum += INCRSUM(m,(tx*sy*tz),(x-ix),(y-jy),(z-iz));
  188.  
  189.     m = Hash3d( jx, jy, iz ) & 0xFF;
  190.     sum += INCRSUM(m,(sx*sy*tz),(x-jx),(y-jy),(z-iz));
  191.  
  192.     m = Hash3d( ix, iy, jz ) & 0xFF;
  193.     sum += INCRSUM(m,(tx*ty*sz),(x-ix),(y-iy),(z-jz));
  194.  
  195.     m = Hash3d( jx, iy, jz ) & 0xFF;
  196.     sum += INCRSUM(m,(sx*ty*sz),(x-jx),(y-iy),(z-jz));
  197.  
  198.     m = Hash3d( ix, jy, jz ) & 0xFF;
  199.     sum += INCRSUM(m,(tx*sy*sz),(x-ix),(y-jy),(z-jz));
  200.  
  201.     m = Hash3d( jx, jy, jz ) & 0xFF;
  202.     sum += INCRSUM(m,(sx*sy*sz),(x-jx),(y-jy),(z-jz));
  203.  
  204.     return sum;
  205.  
  206. }
  207.  
  208. /*
  209.  * Vector-valued "Noise"
  210.  */
  211. void
  212. DNoise3(point, result)
  213. Vector *point, *result;
  214. {
  215.     register int    ix, iy, iz, jx, jy, jz;
  216.     Float        x, y, z;
  217.     Float px, py, pz, s;
  218.     Float    sx, sy, sz, tx, ty, tz;
  219.     short    m;
  220.  
  221.     /* ensures the values are positive. */
  222.     x = point->x - MINX; y = point->y - MINY; z = point->z - MINZ;
  223.  
  224.     /* its equivalent integer lattice point. */
  225.     ix = (int)x; iy = (int)y; iz = (int)z;
  226.     jx = ix+1; jy = iy + 1; jz = iz + 1;
  227.  
  228.     sx = SCURVE(x - ix); sy = SCURVE(y - iy); sz = SCURVE(z - iz);
  229.  
  230.     /* the complement values of sx,sy,sz */
  231.     tx = 1.0 - sx; ty = 1.0 - sy; tz = 1.0 - sz;
  232.  
  233.     /*
  234.      *  interpolate!
  235.      */
  236.     m = Hash3d( ix, iy, iz ) & 0xFF;
  237.     px = x-ix;  py = y-iy;  pz = z-iz;
  238.     s = tx*ty*tz;
  239.     result->x = INCRSUM(m,s,px,py,pz);
  240.     result->y = INCRSUM(m+4,s,px,py,pz);
  241.     result->z = INCRSUM(m+8,s,px,py,pz);
  242.  
  243.     m = Hash3d( jx, iy, iz ) & 0xFF;
  244.     px = x-jx;
  245.     s = sx*ty*tz;
  246.     result->x += INCRSUM(m,s,px,py,pz);
  247.     result->y += INCRSUM(m+4,s,px,py,pz);
  248.     result->z += INCRSUM(m+8,s,px,py,pz);
  249.  
  250.     m = Hash3d( jx, jy, iz ) & 0xFF;
  251.     py = y-jy;
  252.     s = sx*sy*tz;
  253.     result->x += INCRSUM(m,s,px,py,pz);
  254.     result->y += INCRSUM(m+4,s,px,py,pz);
  255.     result->z += INCRSUM(m+8,s,px,py,pz);
  256.  
  257.     m = Hash3d( ix, jy, iz ) & 0xFF;
  258.     px = x-ix;
  259.     s = tx*sy*tz;
  260.     result->x += INCRSUM(m,s,px,py,pz);
  261.     result->y += INCRSUM(m+4,s,px,py,pz);
  262.     result->z += INCRSUM(m+8,s,px,py,pz);
  263.  
  264.     m = Hash3d( ix, jy, jz ) & 0xFF;
  265.     pz = z-jz;
  266.     s = tx*sy*sz;
  267.     result->x += INCRSUM(m,s,px,py,pz);
  268.     result->y += INCRSUM(m+4,s,px,py,pz);
  269.     result->z += INCRSUM(m+8,s,px,py,pz);
  270.  
  271.     m = Hash3d( jx, jy, jz ) & 0xFF;
  272.     px = x-jx;
  273.     s = sx*sy*sz;
  274.     result->x += INCRSUM(m,s,px,py,pz);
  275.     result->y += INCRSUM(m+4,s,px,py,pz);
  276.     result->z += INCRSUM(m+8,s,px,py,pz);
  277.  
  278.     m = Hash3d( jx, iy, jz ) & 0xFF;
  279.     py = y-iy;
  280.     s = sx*ty*sz;
  281.     result->x += INCRSUM(m,s,px,py,pz);
  282.     result->y += INCRSUM(m+4,s,px,py,pz);
  283.     result->z += INCRSUM(m+8,s,px,py,pz);
  284.  
  285.     m = Hash3d( ix, iy, jz ) & 0xFF;
  286.     px = x-ix;
  287.     s = tx*ty*sz;
  288.     result->x += INCRSUM(m,s,px,py,pz);
  289.     result->y += INCRSUM(m+4,s,px,py,pz);
  290.     result->z += INCRSUM(m+8,s,px,py,pz);
  291. }
  292.