home *** CD-ROM | disk | FTP | other *** search
/ Encyclopedia of Graphics File Formats Companion / GFF_CD.ISO / formats / ttddd / code / bumpit.c < prev    next >
C/C++ Source or Header  |  1994-06-20  |  6KB  |  205 lines

  1. /* bumpit.c - Read in an Imagine object, bump the puppy, then spit it back out
  2.  *          - Written by Glenn M. Lewis - 8/6/92
  3.  *            - Modified by Charles Congdon to use public domain noise calls
  4.  *          - Reads from stdin, writes to stdout.
  5.  *          - Inspiration by Steve Worley:
  6.  
  7. Heres a cool idea! And TRIVIAL to implement! It's an object perturber.
  8. It loads in a TDDD object. It calls fractal noise three times for three
  9. displacements for each vertex. It dispaces the vertex that amount. It
  10. then saves the object back out as a new object.
  11.  
  12. Want an asteroid? Make a primative sphere in Imagine. Use "bumpit" and
  13. displace the points. Load the new object! BAM, bumpy sphere! You
  14. could even add features like craters before bumping.  
  15.  
  16. Or take a flat plane, bump only in the +Z direction. Boom, a mountain
  17. range!
  18.  
  19. bumpit max_X max_Y max_Z Initial_scale #_of_scales Scale_ratio Amp_ratio
  20.        time_ratio time
  21.  
  22. or "turbit" with the same parameters. You make 3 calls to "fractalval3"
  23. for the X Y and Z displacements. See?
  24.  
  25. Cool, useful, eh? Make that Cyberware head pulse with animated displacements!
  26.  
  27. I figure about 15 minutes to write, too. Want to give it a whirl?
  28.  
  29. -Steve
  30. ----------------------------------------------------------------------------
  31.  
  32. Here's another (option?) for bumpit. Make RADIAL perturbations. So the
  33. perfect sphere won't "fold over" on itself: it will always be radial. So
  34. DX DY DZ DR as 4 options? They can all be summed together, the fact
  35. that DR is a 3 vector is no biggie.
  36.  
  37. -Steve
  38.  
  39. */
  40.  
  41. static char rcs_id[] = "$Id: bumpit.c,v 1.7 1993/02/14 07:27:06 glewis Exp glewis $";
  42.  
  43. #include <stdio.h>
  44. #include <ctype.h>
  45. #include <math.h>
  46. #include "t3dlib.h"
  47. #include "noise.h"
  48. #ifdef __STDC__         
  49. #include <stdlib.h>     
  50. #include <strings.h>    
  51. #include "bumpit_protos.h"
  52. #endif
  53.  
  54. /* Do this with includes:  double atof(), sqrt(); */
  55. #define NUMARGS 10
  56. double args[NUMARGS];
  57. double defargs[NUMARGS] = {
  58.     1.0,    /* max_X */
  59.     1.0,    /* max_Y    */
  60.     1.0,    /* max_Z    */
  61.     1.0,    /* max_R    */
  62.     10.0,    /* Initial_scale    */
  63.     5.0,    /* #_of_scales    */
  64.     0.4,    /* Scale_ratio    */
  65.     0.4,    /* Amp_ratio    */
  66.     0.4,     /* time_ratio    */
  67.     0.0     /* time    */
  68. };
  69. #define MAX_X    args[0]
  70. #define MAX_Y    args[1]
  71. #define MAX_Z    args[2]
  72. #define MAX_R    args[3]
  73. #define INIT_SCALE    args[4]
  74. #define NUM_SCALES    args[5]
  75. #define SCALE_RATIO    args[6]
  76. #define AMP_RATIO    args[7]
  77. #define TIME_RATIO    args[8]
  78. #define TIME        args[9]
  79.  
  80. main(argc, argv)
  81. int argc;
  82. char *argv[];
  83. {
  84.     int i;
  85.     register OBJECT *o;
  86.     WORLD *world;
  87.  
  88.     if (argc-1 > NUMARGS) {
  89. USAGE:
  90.         fprintf(stderr, "Usage: %s max_X max_Y max_Z Initial_scale #_of_scales Scale_ratio Amp_ratio time_ratio time <in.iob >out.iob\n", argv[0]);
  91.         exit(-1);
  92.     }
  93.     for (i=1; i<argc; i++) {
  94.         if (argv[i][0]=='-' && argv[i][1]=='h') goto USAGE;
  95.         args[i-1] = (double)atof(argv[i]);
  96.     }
  97.     while (i-1<NUMARGS)  { args[i-1] = defargs[i-1]; i++; }
  98.  
  99.     world = read_World(stdin);
  100.     if (!world) {
  101.         fprintf(stderr, "Could not load object from stdin.\n");
  102.         exit(-1);
  103.     }
  104.  
  105.     /* Now, iterate over all objects, bumping verticies with noise */
  106.     for (o=world->object; o; o=o->next)
  107.         process_DESC(o);
  108.  
  109.     /* Write the object back out. */
  110.     write_TDDD(world, stdout);
  111.     exit(0);
  112. }
  113.  
  114. void process_DESC(object)
  115. OBJECT *object;
  116. {
  117.     register OBJECT *obj;
  118.     register DESC *desc = object->desc;
  119.     register int i;
  120.     register double val;
  121. /*    float in[6], out[6];  */
  122.         double in[3], out[3];
  123.     double rad, addX, addY, addZ, addR;
  124.  
  125.     /* Loop through list of verticies, and bump */
  126.  
  127.     if (desc->pcount) {
  128.         for (i=0; i<desc->pcount; i++) {
  129.             addX = addY = addZ = 0.0;
  130.             in[0] = (double)desc->pnts[i].x;
  131.             in[1] = (double)desc->pnts[i].y;
  132.             in[2] = (double)desc->pnts[i].z;
  133.             rad = sqrt((double)in[0]*in[0]+in[1]*in[1]+in[2]*in[2]);
  134.             if (rad != 0.0) rad = 1.0/rad;
  135.             if (MAX_X != 0.0) {
  136.                 val = wfractalval3(&in[0],
  137.                     INIT_SCALE,
  138.                     NUM_SCALES,
  139.                     SCALE_RATIO,
  140.                     AMP_RATIO,
  141.                     TIME_RATIO,
  142.                     TIME,
  143.                     &out[0]);
  144.                 addX = MAX_X*val;
  145.             }
  146.  
  147.             if (MAX_Y != 0.0) {
  148.                 in[0] = (double)desc->pnts[i].x + (double)107.28;
  149.                 in[1] = (double)desc->pnts[i].y - (double)213.72;
  150.                 in[2] = (double)desc->pnts[i].z + (double)172.12;
  151.                 val = wfractalval3(&in[0],
  152.                     INIT_SCALE,
  153.                     NUM_SCALES,
  154.                     SCALE_RATIO,
  155.                     AMP_RATIO,
  156.                     TIME_RATIO,
  157.                     TIME,
  158.                     &out[0]);
  159.                 addY = MAX_Y*val;
  160.             }
  161.  
  162.             if (MAX_Z != 0.0) {
  163.                 in[0] = (double)desc->pnts[i].x - (double)573.17;
  164.                 in[1] = (double)desc->pnts[i].y + (double)324.93;
  165.                 in[2] = (double)desc->pnts[i].z - (double)747.88;
  166.                 val = wfractalval3(&in[0],
  167.                     INIT_SCALE,
  168.                     NUM_SCALES,
  169.                      SCALE_RATIO,
  170.                     AMP_RATIO,
  171.                     TIME_RATIO,
  172.                     TIME,
  173.                     &out[0]);
  174.                 addZ = MAX_Z*val;
  175.             }
  176.  
  177.             if (MAX_R != 0.0 && rad != 0.0) {
  178.                 in[0] = (double)desc->pnts[i].x - (double)57.923;
  179.                 in[1] = (double)desc->pnts[i].y - (double)1052.7;
  180.                 in[2] = (double)desc->pnts[i].z + (double)481.08;
  181.                 val = wfractalval3(&in[0],
  182.                     INIT_SCALE,
  183.                     NUM_SCALES,
  184.                     SCALE_RATIO,
  185.                     AMP_RATIO,
  186.                     TIME_RATIO,
  187.                     TIME,
  188.                     &out[0]);
  189.                 addR = MAX_R*val;
  190.                 addX += addR*desc->pnts[i].x*rad;
  191.                 addY += addR*desc->pnts[i].y*rad;
  192.                 addZ += addR*desc->pnts[i].z*rad;
  193.             }
  194.             desc->pnts[i].x += addX;
  195.             desc->pnts[i].y += addY;
  196.             desc->pnts[i].z += addZ;
  197.         }
  198.     }
  199.  
  200.     for (obj=object->child; obj; obj=obj->next)
  201.         if (!obj->extr) process_DESC(obj);
  202. }
  203.  
  204.  
  205.