[comp.sources.amiga] v90i256: DKBTrace 2.01 - DKBtrace Ray-Tracer, Part08/10

amiga-request@abcfd20.larc.nasa.gov (Amiga Sources/Binaries Moderator) (09/04/90)

Submitted-by: David Schanen <mtv@milton.u.washington.edu>
Posting-number: Volume 90, Issue 256
Archive-name: applications/dkbtrace-2.01/part08

#!/bin/sh
# This is a shell archive.  Remove anything before this line, then unpack
# it by saving it into a file and typing "sh file".  To overwrite existing
# files, type "sh file -c".  You can also feed this as standard input via
# unshar, or by typing "sh <file", e.g..  If this archive is complete, you
# will see the following message at the end:
#		"End of archive 8 (of 10)."
# Contents:  src/texture.c
# Wrapped by tadguy@abcfd20 on Mon Sep  3 19:21:21 1990
PATH=/bin:/usr/bin:/usr/ucb ; export PATH
if test -f 'src/texture.c' -a "${1}" != "-c" ; then 
  echo shar: Will not clobber existing file \"'src/texture.c'\"
else
echo shar: Extracting \"'src/texture.c'\" \(28884 characters\)
sed "s/^X//" >'src/texture.c' <<'END_OF_FILE'
X/*****************************************************************************
X*
X*                                   texture.c
X*
X*   from DKBTrace (c) 1990  David Buck
X*
X*  This module implements solid texturing functions such as wood, marble, and
X*  bozo.  The noise function used here is the one described by Ken Perlin in
X*  "Hypertexture", SIGGRAPH '89 Conference Proceedings page 253.
X*
X* This software is freely distributable. The source and/or object code may be
X* copied or uploaded to communications services so long as this notice remains
X* at the top of each file.  If any changes are made to the program, you must
X* clearly indicate in the documentation and in the programs startup message
X* who it was who made the changes. The documentation should also describe what
X* those changes were. This software may not be included in whole or in
X* part into any commercial package without the express written consent of the
X* author.  It may, however, be included in other public domain or freely
X* distributed software so long as the proper credit for the software is given.
X*
X* This software is provided as is without any guarantees or warranty. Although
X* the author has attempted to find and correct any bugs in the software, he
X* is not responsible for any damage caused by the use of the software.  The
X* author is under no obligation to provide service, corrections, or upgrades
X* to this package.
X*
X* Despite all the legal stuff above, if you do find bugs, I would like to hear
X* about them.  Also, if you have any comments or questions, you may contact me
X* at the following address:
X*
X*     David Buck
X*     22C Sonnet Cres.
X*     Nepean Ontario
X*     Canada, K2H 8W7
X*
X*  I can also be reached on the following bulleton boards:
X*
X*     ATX              (613) 526-4141
X*     OMX              (613) 731-3419
X*     Mystic           (613) 731-0088 or (613) 731-6698
X*
X*  Fidonet:   1:163/109.9
X*  Internet:  David_Buck@Carleton.CA
X*
X*  IBM Port by Aaron A. Collins. Aaron may be reached on the following BBS'es:
X*
X*     Lattice BBS                      (708) 916-1200
X*     The Information Exchange BBS     (708) 945-5575
X*     Stillwaters BBS                  (708) 403-2826
X*
X*
X*
X*
X*  The Noise and DNoise functions (and associated functions) were written by
X*  Robert Skinner (robert@sgi.com) and are used here with his permission.
X*  They are a lot better than the noise functions I had before!
X*
X*****************************************************************************/
X
X
X#include "frame.h"
X#include "vector.h"
X#include "dkbproto.h"
X
Xextern long Calls_To_Noise, Calls_To_DNoise;
X
X#define MINX	-10000		/* Ridiculously large scaling values */
X#define MINY	MINX
X#define MINZ	MINX
X
X#define MAXSIZE 267
X#define RNDMASK 0x7FFF
X#define RNDDIVISOR (float) RNDMASK
X#define NUMBER_OF_WAVES 10
X#define SINTABSIZE 1000
X#define EPSILON	(DBL) 0.00001
X
X#define FLOOR(x) ((x) >= 0.0 ? floor(x) : (0.0 - floor(0.0 - (x)) - 1.0))
X#define FABS(x) ((x) < 0.0 ? (0.0 - x) : (x))
X#define SCURVE(a) ((a)*(a)*(3.0-2.0*(a)))
X#define REALSCALE ( 2.0 / 65535.0 )
X#define Hash3d(a,b,c) hashTable[(int)(hashTable[(int)(hashTable[(int)((a) & 0xfffL)] ^ ((b) & 0xfffL))] ^ ((c) & 0xfffL))]
X#define INCRSUM(m,s,x,y,z)	((s)*(RTable[m]*0.5		\
X					+ RTable[m+1]*(x)	\
X					+ RTable[m+2]*(y)	\
X					+ RTable[m+3]*(z)))
X
XDBL sintab [SINTABSIZE];
XDBL frequency[NUMBER_OF_WAVES];
XVECTOR Wave_Sources[NUMBER_OF_WAVES];
XDBL	RTable[MAXSIZE];
Xshort *hashTable;
Xunsigned short crctab[256] =
X{
X   0x0000, 0xc0c1, 0xc181, 0x0140, 0xc301, 0x03c0, 0x0280, 0xc241,
X   0xc601, 0x06c0, 0x0780, 0xc741, 0x0500, 0xc5c1, 0xc481, 0x0440,
X   0xcc01, 0x0cc0, 0x0d80, 0xcd41, 0x0f00, 0xcfc1, 0xce81, 0x0e40,
X   0x0a00, 0xcac1, 0xcb81, 0x0b40, 0xc901, 0x09c0, 0x0880, 0xc841,
X   0xd801, 0x18c0, 0x1980, 0xd941, 0x1b00, 0xdbc1, 0xda81, 0x1a40,
X   0x1e00, 0xdec1, 0xdf81, 0x1f40, 0xdd01, 0x1dc0, 0x1c80, 0xdc41,
X   0x1400, 0xd4c1, 0xd581, 0x1540, 0xd701, 0x17c0, 0x1680, 0xd641,
X   0xd201, 0x12c0, 0x1380, 0xd341, 0x1100, 0xd1c1, 0xd081, 0x1040,
X   0xf001, 0x30c0, 0x3180, 0xf141, 0x3300, 0xf3c1, 0xf281, 0x3240,
X   0x3600, 0xf6c1, 0xf781, 0x3740, 0xf501, 0x35c0, 0x3480, 0xf441,
X   0x3c00, 0xfcc1, 0xfd81, 0x3d40, 0xff01, 0x3fc0, 0x3e80, 0xfe41,
X   0xfa01, 0x3ac0, 0x3b80, 0xfb41, 0x3900, 0xf9c1, 0xf881, 0x3840,
X   0x2800, 0xe8c1, 0xe981, 0x2940, 0xeb01, 0x2bc0, 0x2a80, 0xea41,
X   0xee01, 0x2ec0, 0x2f80, 0xef41, 0x2d00, 0xedc1, 0xec81, 0x2c40,
X   0xe401, 0x24c0, 0x2580, 0xe541, 0x2700, 0xe7c1, 0xe681, 0x2640,
X   0x2200, 0xe2c1, 0xe381, 0x2340, 0xe101, 0x21c0, 0x2080, 0xe041,
X   0xa001, 0x60c0, 0x6180, 0xa141, 0x6300, 0xa3c1, 0xa281, 0x6240,
X   0x6600, 0xa6c1, 0xa781, 0x6740, 0xa501, 0x65c0, 0x6480, 0xa441,
X   0x6c00, 0xacc1, 0xad81, 0x6d40, 0xaf01, 0x6fc0, 0x6e80, 0xae41,
X   0xaa01, 0x6ac0, 0x6b80, 0xab41, 0x6900, 0xa9c1, 0xa881, 0x6840,
X   0x7800, 0xb8c1, 0xb981, 0x7940, 0xbb01, 0x7bc0, 0x7a80, 0xba41,
X   0xbe01, 0x7ec0, 0x7f80, 0xbf41, 0x7d00, 0xbdc1, 0xbc81, 0x7c40,
X   0xb401, 0x74c0, 0x7580, 0xb541, 0x7700, 0xb7c1, 0xb681, 0x7640,
X   0x7200, 0xb2c1, 0xb381, 0x7340, 0xb101, 0x71c0, 0x7080, 0xb041,
X   0x5000, 0x90c1, 0x9181, 0x5140, 0x9301, 0x53c0, 0x5280, 0x9241,
X   0x9601, 0x56c0, 0x5780, 0x9741, 0x5500, 0x95c1, 0x9481, 0x5440,
X   0x9c01, 0x5cc0, 0x5d80, 0x9d41, 0x5f00, 0x9fc1, 0x9e81, 0x5e40,
X   0x5a00, 0x9ac1, 0x9b81, 0x5b40, 0x9901, 0x59c0, 0x5880, 0x9841,
X   0x8801, 0x48c0, 0x4980, 0x8941, 0x4b00, 0x8bc1, 0x8a81, 0x4a40,
X   0x4e00, 0x8ec1, 0x8f81, 0x4f40, 0x8d01, 0x4dc0, 0x4c80, 0x8c41,
X   0x4400, 0x84c1, 0x8581, 0x4540, 0x8701, 0x47c0, 0x4680, 0x8641,
X   0x8201, 0x42c0, 0x4380, 0x8341, 0x4100, 0x81c1, 0x8081, 0x4040
X};
X
X
Xvoid Compute_Colour (Colour, Colour_Map, value)
X   COLOUR *Colour;
X   COLOUR_MAP *Colour_Map;
X   DBL value;
X   {
X   register int i;
X   COLOUR_MAP_ENTRY *Entry;
X   register DBL fraction;
X
X   for (i = 0, Entry = &(Colour_Map->Colour_Map_Entries[0]) ; i < Colour_Map -> Number_Of_Entries ; i++, Entry++)
X      if ((value >= Entry->start) && (value <= Entry->end))
X      {
X      fraction = (value - Entry->start) / (Entry->end - Entry->start);
X      Colour -> Red = Entry->Start_Colour.Red + fraction * (Entry->End_Colour.Red - Entry->Start_Colour.Red);
X      Colour -> Green = Entry->Start_Colour.Green + fraction * (Entry->End_Colour.Green - Entry->Start_Colour.Green);
X      Colour -> Blue = Entry->Start_Colour.Blue + fraction * (Entry->End_Colour.Blue - Entry->Start_Colour.Blue);
X      Colour -> Alpha = Entry->Start_Colour.Alpha + fraction * (Entry->End_Colour.Alpha - Entry->Start_Colour.Alpha);
X      return;
X      }
X
X   Colour -> Red = 0.0;
X   Colour -> Green = 0.0;
X   Colour -> Blue = 0.0;
X   Colour -> Alpha = 0.0;
X   printf ("No colour for value: %f\n", value);
X   return;
X   }
X
Xvoid Initialize_Noise ()
X   {
X   register int i = 0;
X   VECTOR point;
X
X   InitRTable();
X
X   for (i = 0 ; i < SINTABSIZE ; i++)
X      sintab[i] = sin(i/(DBL)SINTABSIZE * (3.14159265359 * 2.0));
X
X   for (i = 0 ; i < NUMBER_OF_WAVES ; i++)
X      {
X      DNoise (&point, (DBL) i, 0.0, 0.0);
X      VNormalize (Wave_Sources[i], point);
X      frequency[i] = (rand() & RNDMASK) / RNDDIVISOR + 0.01;
X      }
X   }
X
Xvoid InitTextureTable()
X   {
X   int i, j, temp;
X
X   srand(0);
X
X   hashTable = (short int *) malloc(4096*sizeof(short int));
X   for (i = 0; i < 4096; i++)
X      hashTable[i] = i;
X   for (i = 4095; i >= 0; i--)
X      {
X      j = rand() % 4096;
X      temp = hashTable[i];
X      hashTable[i] = hashTable[j];
X      hashTable[j] = temp;
X      }
X   }
X
X
X/* modified by AAC to work properly with little bitty integers (16 bits) */
X
Xvoid InitRTable()
X   {
X   int i;
X   VECTOR rp;
X
X   InitTextureTable();
X
X   for (i = 0; i < MAXSIZE; i++)
X      {
X      rp.x = rp.y = rp.z = (DBL)i;
X      RTable[i] = (unsigned int) R(&rp) * REALSCALE - 1.0;
X      }
X   }
X
X
Xint R(v)
X   VECTOR *v;
X   {
X   v->x *= .12345;
X   v->y *= .12345;
X   v->z *= .12345;
X
X   return (Crc16((char *) v, sizeof(VECTOR)));
X   }
X
X/*
X * Note that passing a VECTOR array to Crc16 and interpreting it as
X * an array of chars means that machines with different floating-point
X * representation schemes will evaluate Noise(point) differently.
X */
X
Xint Crc16(buf, count)
X   register char *buf;
X   register int  count;
X   {
X   register unsigned short crc = 0;
X
X   while (count--)
X      crc = (crc >> 8) ^ crctab[ (unsigned char) (crc ^ *buf++) ];
X
X   return ((int) crc);
X   }
X
X
X/*
X	Robert's Skinner's Perlin-style "Noise" function - modified by AAC
X	to ensure uniformly distributed clamped values between 0 and 1.0...
X*/
X
XDBL Noise(x, y, z)
X   DBL x, y, z;
X   {
X   register long ix, iy, iz, jx, jy, jz;
X   DBL sx, sy, sz, tx, ty, tz;
X   DBL sum;
X   short m;
X
X
X   Calls_To_Noise++;
X   /* ensures the values are positive. */
X   x -= MINX;
X   y -= MINY;
X   z -= MINZ;
X
X   /* its equivalent integer lattice point. */
X   ix = (long)x; iy = (long)y; iz = (long)z;
X   jx = ix + 1; jy = iy + 1; jz = iz + 1;
X
X   sx = SCURVE(x - ix); sy = SCURVE(y - iy); sz = SCURVE(z - iz);
X
X   /* the complement values of sx,sy,sz */
X   tx = 1.0 - sx; ty = 1.0 - sy; tz = 1.0 - sz;
X
X   /*
X    *  interpolate!
X    */
X   m = Hash3d( ix, iy, iz ) & 0xFF;
X   sum = INCRSUM(m,(tx*ty*tz),(x-ix),(y-iy),(z-iz));
X
X   m = Hash3d( jx, iy, iz ) & 0xFF;
X   sum += INCRSUM(m,(sx*ty*tz),(x-jx),(y-iy),(z-iz));
X
X   m = Hash3d( ix, jy, iz ) & 0xFF;
X   sum += INCRSUM(m,(tx*sy*tz),(x-ix),(y-jy),(z-iz));
X
X   m = Hash3d( jx, jy, iz ) & 0xFF;
X   sum += INCRSUM(m,(sx*sy*tz),(x-jx),(y-jy),(z-iz));
X
X   m = Hash3d( ix, iy, jz ) & 0xFF;
X   sum += INCRSUM(m,(tx*ty*sz),(x-ix),(y-iy),(z-jz));
X
X   m = Hash3d( jx, iy, jz ) & 0xFF;
X   sum += INCRSUM(m,(sx*ty*sz),(x-jx),(y-iy),(z-jz));
X
X   m = Hash3d( ix, jy, jz ) & 0xFF;
X   sum += INCRSUM(m,(tx*sy*sz),(x-ix),(y-jy),(z-jz));
X
X   m = Hash3d( jx, jy, jz ) & 0xFF;
X   sum += INCRSUM(m,(sx*sy*sz),(x-jx),(y-jy),(z-jz));
X
X   sum = sum + 0.5;          /* range at this point -0.5 - 0.5... */
X	
X   if (sum < 0.0)
X      sum = 0.0;
X   if (sum > 1.0)
X      sum = 1.0;
X
X   return (sum);
X   }
X
X
X/*
X       Vector-valued version of "Noise"
X*/
X
Xvoid DNoise(result, x, y, z)
X   VECTOR *result;
X   DBL x, y, z;
X   {
X   register long ix, iy, iz, jx, jy, jz;
X   DBL px, py, pz, s;
X   DBL sx, sy, sz, tx, ty, tz;
X   short m;
X
X   Calls_To_DNoise++;
X   /* ensures the values are positive. */
X   x -= MINX;
X   y -= MINY;
X   z -= MINZ;
X
X   /* its equivalent integer lattice point. */
X   ix = (long)x; iy = (long)y; iz = (long)z;
X   jx = ix+1; jy = iy + 1; jz = iz + 1;
X
X   sx = SCURVE(x - ix); sy = SCURVE(y - iy); sz = SCURVE(z - iz);
X
X   /* the complement values of sx,sy,sz */
X   tx = 1.0 - sx; ty = 1.0 - sy; tz = 1.0 - sz;
X
X   /*
X    *  interpolate!
X    */
X   m = Hash3d( ix, iy, iz ) & 0xFF;
X   px = x-ix;  py = y-iy;  pz = z-iz;
X   s = tx*ty*tz;
X   result->x = INCRSUM(m,s,px,py,pz);
X   result->y = INCRSUM(m+4,s,px,py,pz);
X   result->z = INCRSUM(m+8,s,px,py,pz);
X
X   m = Hash3d( jx, iy, iz ) & 0xFF;
X   px = x-jx;
X   s = sx*ty*tz;
X   result->x += INCRSUM(m,s,px,py,pz);
X   result->y += INCRSUM(m+4,s,px,py,pz);
X   result->z += INCRSUM(m+8,s,px,py,pz);
X
X   m = Hash3d( jx, jy, iz ) & 0xFF;
X   py = y-jy;
X   s = sx*sy*tz;
X   result->x += INCRSUM(m,s,px,py,pz);
X   result->y += INCRSUM(m+4,s,px,py,pz);
X   result->z += INCRSUM(m+8,s,px,py,pz);
X
X   m = Hash3d( ix, jy, iz ) & 0xFF;
X   px = x-ix;
X   s = tx*sy*tz;
X   result->x += INCRSUM(m,s,px,py,pz);
X   result->y += INCRSUM(m+4,s,px,py,pz);
X   result->z += INCRSUM(m+8,s,px,py,pz);
X
X   m = Hash3d( ix, jy, jz ) & 0xFF;
X   pz = z-jz;
X   s = tx*sy*sz;
X   result->x += INCRSUM(m,s,px,py,pz);
X   result->y += INCRSUM(m+4,s,px,py,pz);
X   result->z += INCRSUM(m+8,s,px,py,pz);
X
X   m = Hash3d( jx, jy, jz ) & 0xFF;
X   px = x-jx;
X   s = sx*sy*sz;
X   result->x += INCRSUM(m,s,px,py,pz);
X   result->y += INCRSUM(m+4,s,px,py,pz);
X   result->z += INCRSUM(m+8,s,px,py,pz);
X
X   m = Hash3d( jx, iy, jz ) & 0xFF;
X   py = y-iy;
X   s = sx*ty*sz;
X   result->x += INCRSUM(m,s,px,py,pz);
X   result->y += INCRSUM(m+4,s,px,py,pz);
X   result->z += INCRSUM(m+8,s,px,py,pz);
X
X   m = Hash3d( ix, iy, jz ) & 0xFF;
X   px = x-ix;
X   s = tx*ty*sz;
X   result->x += INCRSUM(m,s,px,py,pz);
X   result->y += INCRSUM(m+4,s,px,py,pz);
X   result->z += INCRSUM(m+8,s,px,py,pz);
X   }
X
XDBL Turbulence (x, y, z)
X   DBL x, y, z;
X   {
X   register DBL pixelSize = 0.1;
X   register DBL t = 0.0;
X   register DBL scale, value;
X
X   for (scale = 1.0 ; scale > pixelSize ; scale *= 0.5) {
X      value = Noise (x/scale, y/scale, z/scale);
X      t += FABS (value) * scale;
X      }
X   return (t);
X   }
X
Xvoid DTurbulence (result, x, y, z)
X   VECTOR *result;
X   DBL x, y, z;
X   {
X   register DBL pixelSize = 0.01;
X   register DBL scale;
X   VECTOR value;
X
X   result -> x = 0.0;
X   result -> y = 0.0;
X   result -> z = 0.0;
X
X   value.x = value.y = value.z = 0.0;
X
X   for (scale = 1.0 ; scale > pixelSize ; scale *= 0.5) {
X      DNoise(&value, x/scale, y/scale, z/scale);
X      result -> x += value.x * scale;
X      result -> y += value.y * scale;
X      result -> z += value.z * scale;
X      }
X   }
X
XDBL cycloidal (value)
X   DBL value;
X   {
X
X   if (value >= 0.0)
X      return (sintab [(int)((value - floor (value)) * SINTABSIZE)]);
X   else
X      return (0.0 - sintab [(int)((0.0 - (value + floor (0.0 - value)))
X                                    * SINTABSIZE)]);
X   }
X
X
XDBL Triangle_Wave (value)
X   DBL value;
X   {
X   register DBL offset;
X
X   if (value >= 0.0) offset = value - floor(value);
X   else offset = value - (-1.0 - floor(FABS(value)));
X
X   if (offset >= 0.5) return (2.0 * (1.0 - offset));
X   else return (2.0 * offset);
X   }
X
X
Xint Bozo (x, y, z, Object, Colour)
XDBL x, y, z;
XOBJECT *Object;
XCOLOUR *Colour;
X   {
X   register DBL noise, turb;
X   COLOUR New_Colour;
X   VECTOR BozoTurbulence;
X
X
X   if ((turb = Object->Object_Texture->Turbulence) != 0.0)
X      {
X      DTurbulence (&BozoTurbulence, x, y, z);
X      x += BozoTurbulence.x * turb;
X      y += BozoTurbulence.y * turb;
X      z += BozoTurbulence.z * turb;
X      }
X
X   noise = Noise (x, y, z);
X
X   if (Object -> Object_Texture->Colour_Map != NULL) {
X      Compute_Colour (&New_Colour, Object->Object_Texture->Colour_Map, noise);
X      Colour -> Red += New_Colour.Red;
X      Colour -> Green += New_Colour.Green;
X      Colour -> Blue += New_Colour.Blue;
X      Colour -> Alpha += New_Colour.Alpha;
X      return (0);
X      }
X
X   if (noise < 0.4) {
X      Colour -> Red += 1.0;
X      Colour -> Green += 1.0;
X      Colour -> Blue += 1.0;
X      return (0);
X      }
X
X   if (noise < 0.6) {
X      Colour -> Green += 1.0;
X      return (0);
X      }
X
X   if (noise < 0.8) {
X      Colour -> Blue += 1.0;
X      return (0);
X      }
X
X   Colour -> Red += 1.0;
X   return (0);
X   }
X
Xint marble (x, y, z, Object, colour)
X   DBL x, y, z;
X   OBJECT *Object;
X   COLOUR *colour;
X   {
X   register DBL noise, hue;
X   COLOUR New_Colour;
X
X   noise = Triangle_Wave(x + Turbulence(x, y, z) * Object -> Object_Texture->Turbulence);
X
X   if (Object -> Object_Texture->Colour_Map != NULL)
X      {
X      Compute_Colour (&New_Colour, Object->Object_Texture->Colour_Map, noise);
X      colour -> Red += New_Colour.Red;
X      colour -> Green += New_Colour.Green;
X      colour -> Blue += New_Colour.Blue;
X      colour -> Alpha += New_Colour.Alpha;
X      return (0);
X      }
X
X   if (noise < 0.0)
X      {
X      colour -> Red += 0.9;
X      colour -> Green += 0.8;
X      colour -> Blue += 0.8;
X      }
X   else if (noise < 0.9)
X      {
X      colour -> Red += 0.9;
X      hue = 0.8 - noise * 0.8;
X      colour -> Green += hue;
X      colour -> Blue += hue;
X      }
X   return (0);
X   }
X
X
Xvoid ripples (x, y, z, Object, Vector)
X   DBL x, y, z;
X   OBJECT *Object;
X   VECTOR *Vector;
X   {
X   register int i;
X   VECTOR point;
X   register DBL length, scalar, index;
X
X   for (i = 0 ; i < NUMBER_OF_WAVES ; i++) {
X      point.x = x;
X      point.y = y;
X      point.z = z;
X      VSub (point, point, Wave_Sources[i]);
X      VDot (length, point, point);
X      if (length == 0.0)
X         length = 1.0;
X
X      length = sqrt(length);
X      index = length*Object->Object_Texture->Frequency
X                    + Object -> Object_Texture->Phase;
X      scalar = cycloidal (index) * Object -> Object_Texture->Bump_Amount;
X      VScale (point, point, scalar/length/(DBL)NUMBER_OF_WAVES);
X      VAdd (*Vector, *Vector, point);
X      }
X   VNormalize (*Vector, *Vector);
X   }
X
Xvoid waves (x, y, z, Object, Vector)
X   DBL x, y, z;
X   OBJECT *Object;
X   VECTOR *Vector;
X   {
X   register int i;
X   VECTOR point;
X   register DBL length, scalar, index, sinValue ;
X
X   for (i = 0 ; i < NUMBER_OF_WAVES ; i++) {
X      point.x = x;
X      point.y = y;
X      point.z = z;
X      VSub (point, point, Wave_Sources[i]);
X      VDot (length, point, point);
X      if (length == 0.0)
X         length = 1.0;
X
X      length = sqrt(length);
X      index = (length * Object -> Object_Texture->Frequency * frequency[i])
X                   + Object -> Object_Texture->Phase;
X      sinValue = cycloidal (index);
X
X      scalar =  sinValue * Object -> Object_Texture->Bump_Amount /
X                  frequency[i];
X      VScale (point, point, scalar/length/(DBL)NUMBER_OF_WAVES);
X      VAdd (*Vector, *Vector, point);
X      }
X   VNormalize (*Vector, *Vector);
X   }
X
Xint wood (x, y, z, Object, colour)
X   DBL x, y, z;
X   OBJECT *Object;
X   COLOUR *colour;
X   {
X   register DBL noise, length;
X   VECTOR WoodTurbulence;
X   VECTOR point;
X   COLOUR New_Colour;
X
X   DTurbulence (&WoodTurbulence, x, y, z);
X
X   point.x = cycloidal((x + WoodTurbulence.x)
X               * Object -> Object_Texture->Turbulence);
X   point.y = cycloidal((y + WoodTurbulence.y)
X               * Object -> Object_Texture->Turbulence);
X   point.z = 0.0;
X
X   point.x += x;
X   point.y += y;
X   point.z += z;
X
X   VLength (length, point);
X
X   noise = Triangle_Wave(length);
X
X   if (Object -> Object_Texture->Colour_Map != NULL) {
X      Compute_Colour (&New_Colour, Object->Object_Texture->Colour_Map, noise);
X      colour -> Red += New_Colour.Red;
X      colour -> Green += New_Colour.Green;
X      colour -> Blue += New_Colour.Blue;
X      colour -> Alpha += New_Colour.Alpha;
X      return (0);
X      }
X
X   if (noise > 0.6) {
X      colour -> Red += 0.4;
X      colour -> Green += 0.133;
X      colour -> Blue += 0.066;
X      }
X  else {
X      colour -> Red += 0.666;
X      colour -> Green += 0.312;
X      colour -> Blue += 0.2;
X      }
X      return (0);
X   }
X
X
Xvoid checker (x, y, z, Object, colour)
X   DBL x, y, z;
X   OBJECT *Object;
X   COLOUR *colour;
X   {
X   int brkindx;
X
X   brkindx = (int) FLOOR(x) + (int) FLOOR(z);
X
X   if (brkindx & 1)
X      *colour = Object -> Object_Texture->Colour1;
X   else
X      *colour = Object -> Object_Texture->Colour2;
X   return;
X   }
X
X/*
X   Ideas garnered from SIGGRAPH '85 Volume 19 Number 3, "An Image Synthesizer"
X   By Ken Perlin.
X*/
X
X
X/*	
X	With a little reflectivity and brilliance, can look like organ pipe
X	metal.   With tiny scaling values can look like masonry or concrete.
X	Works with color maps, supposedly. (?)
X*/
X
Xvoid spotted (x, y, z, Object, Colour)
X   DBL x, y, z;
X   OBJECT *Object;
X   COLOUR *Colour;
X   {
X   register DBL noise;
X   COLOUR New_Colour;
X
X   noise = Noise (x, y, z);
X
X   if (Object -> Object_Texture->Colour_Map != NULL)
X      {
X      Compute_Colour (&New_Colour, Object->Object_Texture->Colour_Map, noise);
X      Colour -> Red += New_Colour.Red;
X      Colour -> Green += New_Colour.Green;
X      Colour -> Blue += New_Colour.Blue;
X      Colour -> Alpha += New_Colour.Alpha;
X      return;
X      }
X
X   Colour -> Red += noise;             /* "white (1.0) * noise" */
X   Colour -> Green += noise;
X   Colour -> Blue += noise;
X
X   return;
X   }
X
Xvoid bumps (x, y, z, Object, normal)
X   DBL x, y, z;
X   OBJECT *Object;
X   VECTOR *normal;
X   {
X   VECTOR bump_turb;
X
X   if (Object -> Object_Texture->Bump_Amount == 0.0)
X      return;                            /* why are we here?? */
X
X   DNoise (&bump_turb, x, y, z);         /* Get Normal Displacement Val. */
X   VScale(bump_turb, bump_turb, Object->Object_Texture->Bump_Amount);
X   VAdd (*normal, *normal, bump_turb);   /* displace "normal" */
X   VNormalize (*normal, *normal);        /* normalize normal! */
X   return;
X   }
X
X/*
X   dents is similar to bumps, but uses noise() to control the amount of
X   dnoise() perturbation of the object normal...
X*/
X
Xvoid dents (x, y, z, Object, normal)
X   DBL x, y, z;
X   OBJECT *Object;
X   VECTOR *normal;
X   {
X   VECTOR stucco_turb;
X   DBL noise;
X
X   if (Object -> Object_Texture->Bump_Amount == 0.0)
X      return;                           /* why are we here?? */
X
X   noise = Noise (x, y, z);
X
X   noise =  noise * noise * noise * Object->Object_Texture->Bump_Amount;
X
X   DNoise (&stucco_turb, x, y, z);       /* Get Normal Displacement Val. */
X	
X   VScale (stucco_turb, stucco_turb, noise);
X   VAdd (*normal, *normal, stucco_turb); /* displace "normal" */
X   VNormalize (*normal, *normal);        /* normalize normal! */
X   return;
X   }
X
X
Xvoid agate (x, y, z, Object, colour)
X   DBL x, y, z;
X   OBJECT *Object;
X   COLOUR *colour;
X   {
X   register DBL noise, hue;
X   COLOUR New_Colour;
X
X   noise = cycloidal(1.3 * Turbulence(x, y, z) + 1.1 * z) + 1;
X   noise *= 0.5;
X   noise = pow(noise, 0.77);
X
X   if (Object -> Object_Texture->Colour_Map != NULL)
X      {
X      Compute_Colour (&New_Colour, Object->Object_Texture->Colour_Map, noise);
X      colour -> Red += New_Colour.Red;
X      colour -> Green += New_Colour.Green;
X      colour -> Blue += New_Colour.Blue;
X      colour -> Alpha += New_Colour.Alpha;
X      return;
X      }
X
X   hue = 1.0 - noise;
X
X   if (noise < 0.5)
X      {
X      colour -> Red += (1.0 - (noise / 10));
X      colour -> Green += (1.0 - (noise / 5));
X      colour -> Blue += hue;
X      }
X   else if (noise < 0.6)
X      {
X      colour -> Red += 0.9;
X      colour -> Green += 0.7;
X      colour -> Blue += hue;
X      }
X   else
X      {
X      colour -> Red += (0.6 + hue);
X      colour -> Green += (0.3 + hue);
X      colour -> Blue += hue;
X      }
X   return;
X   }
X
X
X/*
X   Ideas garnered from the April 89 Byte Graphics Supplement on RenderMan,
X   refined from "The RenderMan Companion, by Steve Upstill of Pixar, (C) 1990
X   Addison-Wesley.
X*/
X
X
X/*
X   wrinkles - This is my implementation of the dented() routine, using
X   a surface iterative fractal derived from DTurbulence.  This is a 3-D vers.
X   (thanks to DNoise()...) of the usual version using the singular Noise()...
X   Seems to look a lot like wrinkles, however... (hmmm)
X*/
X
Xvoid wrinkles (x, y, z, Object, normal)
X   DBL x, y, z;
X   OBJECT *Object;
X   VECTOR *normal;
X   {
X   register int i;
X   register DBL scale = 1.0;
X   VECTOR result, value;
X
X   if (Object -> Object_Texture->Bump_Amount == 0.0)
X      return;                                /* why are we here?? */
X
X   result.x = 0.0;
X   result.y = 0.0;
X   result.z = 0.0;
X
X   for (i = 0; i < 10 ; scale *= 2.0, i++)
X      {
X      DNoise(&value, x * scale, y * scale, z * scale);   /* * scale,*/
X      result.x += FABS (value.x / scale);
X      result.y += FABS (value.y / scale);
X      result.z += FABS (value.z / scale);
X      }
X
X   VScale(result, result, Object->Object_Texture->Bump_Amount);
X   VAdd (*normal, *normal, result);             /* displace "normal" */
X   VNormalize (*normal, *normal);               /* normalize normal! */
X   return;
X   }
X
X
X/*
X   Granite - kind of a union of the "spotted" and the "dented" textures,
X   using a 1/f fractal noise function for color values.  Typically used
X   w/ small scaling values.  Should work with colour maps for pink granite...
X*/
X
X
Xvoid granite (x, y, z, Object, Colour)
X   DBL x, y, z;
X   OBJECT *Object;
X   COLOUR *Colour;
X   {
X   register int i;
X   register DBL temp, noise = 0.0, freq = 1.0;
X   COLOUR New_Colour;
X
X   for (i = 0; i < 6 ; freq *= 2.0, i++)
X      {
X      temp = 0.5 - Noise (x * 4 * freq, y * 4 * freq, z * 4 * freq);
X      temp = FABS(temp);
X      noise += temp / freq;
X      }
X
X   if (Object -> Object_Texture->Colour_Map != NULL)
X      {
X      Compute_Colour (&New_Colour, Object->Object_Texture->Colour_Map, noise);
X      Colour -> Red += New_Colour.Red;
X      Colour -> Green += New_Colour.Green;
X      Colour -> Blue += New_Colour.Blue;
X      Colour -> Alpha += New_Colour.Alpha;
X      return;
X      }
X
X   Colour -> Red += noise;                  /* "white (1.0) * noise" */
X   Colour -> Green += noise;
X   Colour -> Blue += noise;
X
X   return;
X   }
X
X/*
X   Further Ideas Garnered from "The RenderMan Companion" (Addison Wesley)
X*/
X
X
X/*
X   Color Gradient Texture - gradient based on the fractional values of x, y or
X   z, based on whether or not the given directional vector is a 1.0 or a 0.0.
X   Note - ONLY works with colour maps, preferably one that is circular - i.e.
X   the last defined colour (value 1.001) is the same as the first colour (with
X   a value of 0.0) in the map.  The basic concept of this is from DBW Render,
X   but Dave Wecker's only supports simple Y axis gradients.
X*/
X
Xvoid gradient (x, y, z, Object, Colour)
X   DBL x, y, z;
X   OBJECT *Object;
X   COLOUR *Colour;
X   {
X   COLOUR New_Colour;
X   DBL value = 0.0, turb;
X   VECTOR GradTurbulence;
X
X   if ((turb = Object->Object_Texture->Turbulence) != 0.0)
X      {
X      DTurbulence (&GradTurbulence, x, y, z);
X      x += GradTurbulence.x * turb;
X      y += GradTurbulence.y * turb;
X      z += GradTurbulence.z * turb;
X      }
X
X   if (Object -> Object_Texture->Colour_Map == NULL)
X      return;
X   if (Object -> Object_Texture->Texture_Gradient.x != 0.0)
X      {
X      x = FABS(x);
X      value += x - FLOOR(x);	/* obtain fractional X component */
X      }
X   if (Object -> Object_Texture->Texture_Gradient.y != 0.0)
X      {
X      y = FABS(y);
X      value += y - FLOOR(y);	/* obtain fractional Y component */
X   }
X   if (Object -> Object_Texture->Texture_Gradient.z != 0.0)
X      {
X      z = FABS(z);
X      value += z - FLOOR(z);	/* obtain fractional Z component */
X      }
X   value = ((value > 1.0) ? fmod(value, 1.0) : value); /* clamp to 1.0 */
X   Compute_Colour(&New_Colour, Object->Object_Texture->Colour_Map, value);
X   Colour -> Red += New_Colour.Red;
X   Colour -> Green += New_Colour.Green;
X   Colour -> Blue += New_Colour.Blue;
X   Colour -> Alpha += New_Colour.Alpha;
X   return;
X   }
X
X/*
X   2-D to 3-D Procedural Texture Mapping of a Bitmapped Image onto an Object:
X	   
X   Simplistic method of object image projection devised by DKB and AAC.
X
X   1. Transform texture in 3-D space if requested.
X   2. Determine local object 2-d coords from 3-d coords by <X Y Z> triple.
X   3. Return pixel color value at that position on the 2-d plane of "Image".
X   3. Map colour value in Image [0..255] to a more normal colour range [0..1].
X*/
X
Xvoid texture_map (x, y, z, Object, colour)
X   DBL x, y, z;
X   OBJECT *Object;
X   COLOUR *colour;
X   {
X   /* determine local object 2-d coords from 3-d coords */
X   /* "unwrap" object 2-d coord onto flat 2-d plane */
X   /* return pixel color value at that posn on 2-d plane */
X
X   int xcoor, ycoor, index;
X   TEXTURE *local_texture;
X   DBL width, height, turb;
X   VECTOR TextureTurbulence;
X
X   local_texture = Object->Object_Texture;
X
X   if ((turb = local_texture->Turbulence) != 0.0)
X      {
X      DTurbulence (&TextureTurbulence, x, y, z);
X      x += TextureTurbulence.x * turb;
X      y += TextureTurbulence.y * turb;
X      z += TextureTurbulence.z * turb;
X      }
X
X   width = local_texture->Image->width;
X   height = local_texture->Image->height;
X
X   if (local_texture -> Texture_Gradient.x != 0.0) {
X      if ((local_texture->Once_Flag) &&
X           ((x < 0.0) || (x > 1.0))) {
X         *colour = Object -> Object_Colour;
X         return;
X         }
X
X      if (local_texture -> Texture_Gradient.x > 0)
X         xcoor = (int) fmod (x * width, width);
X      else ycoor = (int) fmod (x * height, height);
X      }
X
X   if (local_texture -> Texture_Gradient.y != 0.0) {
X      if ((local_texture->Once_Flag) &&
X           ((y < 0.0) || (y > 1.0))) {
X         *colour = Object -> Object_Colour;
X         return;
X         }
X
X      if (local_texture -> Texture_Gradient.y > 0)
X         xcoor = (int) fmod (y * width, width);
X      else ycoor = (int) fmod (y * height, height);
X      }
X
X   if (local_texture -> Texture_Gradient.z != 0.0) {
X      if ((local_texture->Once_Flag) &&
X           ((z < 0.0) || (z > 1.0))) {
X         *colour = Object -> Object_Colour;
X         return;
X         }
X
X      if (local_texture -> Texture_Gradient.z > 0)
X         xcoor = (int) fmod (z * width, width);
X      else ycoor = (int) fmod (z * height, height);
X      }
X
X   if (xcoor < 0)
X      xcoor += local_texture->Image->iwidth;
X   if (ycoor < 0)
X      ycoor += local_texture->Image->iheight;
X
X   if ((xcoor >= local_texture->Image->iwidth) ||
X       (ycoor >= local_texture->Image->iheight) ||
X       (xcoor < 0) || (ycoor < 0))
X     printf ("Out of range\n");
X
X   index = (unsigned)ycoor* (unsigned)local_texture->Image->iwidth +
X           (unsigned)xcoor;
X
X   Make_Colour (colour, (DBL) local_texture->Image->red[index]/255.0,
X                        (DBL) local_texture->Image->green[index]/255.0,
X                        (DBL) local_texture->Image->blue[index]/255.0);
X   }
X
END_OF_FILE
if test 28884 -ne `wc -c <'src/texture.c'`; then
    echo shar: \"'src/texture.c'\" unpacked with wrong size!
fi
# end of 'src/texture.c'
fi
echo shar: End of archive 8 \(of 10\).
cp /dev/null ark8isdone
MISSING=""
for I in 1 2 3 4 5 6 7 8 9 10 ; do
    if test ! -f ark${I}isdone ; then
	MISSING="${MISSING} ${I}"
    fi
done
if test "${MISSING}" = "" ; then
    echo You have unpacked all 10 archives.
    rm -f ark[1-9]isdone ark[1-9][0-9]isdone
else
    echo You still need to unpack the following archives:
    echo "        " ${MISSING}
fi
##  End of shell archive.
exit 0
-- 
Mail submissions (sources or binaries) to <amiga@uunet.uu.net>.
Mail comments to the moderator at <amiga-request@uunet.uu.net>.
Post requests for sources, and general discussion to comp.sys.amiga.