home *** CD-ROM | disk | FTP | other *** search
/ Encyclopedia of Graphics File Formats Companion / GFF_CD.ISO / formats / ttddd / spec / t3d_doc / waterfal.lzh / WATER.C next >
C/C++ Source or Header  |  1992-08-18  |  7KB  |  214 lines

  1. #include <stdio.h>
  2. #include <exec/types.h>
  3. #include <math.h>
  4. #include <stdlib.h>
  5. #define SWAP(a,b) tempr=(a);(a)=(b);(b)=tempr
  6.  
  7. /* WATERFALL.C  
  8. by Steve Worley
  9. June 1991
  10.  
  11. Makes a series of objects which when "played" sequentially in Imagine,
  12. creates a waterfall-like cascade.  This is very much a hack and is
  13. unpolished and unsupported, but so many people bugged me to let them
  14. play with the code, I am making this freely distributable.
  15.  
  16. Quick directions: This is about all you'll get: I do not want to "support" this
  17. code and answer a lot of questions about it, so this is about all you'll get
  18. for docs. :-)  Sorry.
  19.  
  20. The idea is to generate a series of objects, like 200 or so. Each of these
  21. objects is just a large grouped set of perfect spheres. 
  22. If these objects are each shown for one frame, and the objects differ only
  23. by the positions of the individual spheres, the result will be an animated 
  24. bunch of spheres moving around. This program computes the motion of the spheres
  25. through a physical simulation.  Some of the statements are pretty hardwired,
  26. like setting velocity = 9.82  (this is the acceleration of gravity in M/S) and
  27. the size and color of the spheres. Messy coding? You bet, but I never expected
  28. to release this. :-)
  29.  
  30. This program outputs a zillion small files numbered  sequentially and ending
  31. with the extension "ttddd." These are to be read in by Glenn Lewis' fine
  32. TTDDD program which will convert them into Imagine objects. (I wrote a little
  33. script that I used ADOS's "execute" on.
  34.  
  35. Then you need to load these into Imagine. Start a new project, make it 200
  36. frames (or however long). Add a new actor by loading the first of the outputted
  37. waterfall objects. In Action, change the actor bar for the object to start and
  38. end on frame one. Use "Add" mode and add a new bar to the actor subrow from
  39. frame 2 to frame 2, using the filename of the SECOND waterfall object.
  40. Add frame 3, object three, frame 4, object 4, etc. This sounds amazingly
  41. annoying to do, and that's true. :-) But it is pretty fast: I did 250 frames
  42. in about 20 minutes. 
  43.  
  44. Anyway, "save changes" then set up your camera and lights in Detail, and
  45. render a preview... tah dah!
  46.  
  47. Enjoy.  Please remember this is a hack and nothing polished. As for
  48. support, well, you're on your own. 
  49.  
  50.  
  51. -Steve
  52.  
  53. */
  54.  
  55.  
  56.  
  57. main(argc,argv)
  58. int argc;
  59. char **argv;
  60. {
  61. double x[100],y[100],z[100],vx[100],vy[100];
  62. double vz[100],cx,cy,cr,crs,sx,sy,sz,sr,srs,py,temp,px,pz,cz,dx,dy,dz;
  63. int t, num,frame;
  64. double time, dt,framespersec;
  65. int NUMATOMS;
  66. FILE *outfile;
  67. char tempstring[50];
  68. char outfilename[50];
  69.  
  70.  
  71. /* Setup */
  72. NUMATOMS=550;
  73. framespersec=30.;
  74. num=0;
  75. frame=0;
  76. dt=.001;
  77.  
  78.  
  79. for (t=0;t<NUMATOMS;t++)
  80.   {
  81.     x[t]=0;
  82.     y[t]=0;
  83.     z[t]=0;
  84.     vx[t]=0;
  85.     vy[t]=0;
  86.     vz[t]=0;
  87.   }
  88.  
  89. /*prime!*/
  90. time=0.0000000001;
  91. while (time<5.0)
  92.   {
  93.     if (num<NUMATOMS && time >= num*0.1 ) /*release rate*/
  94.       {
  95.         
  96.         x[num]=(rand()%4000)/2000-2.0;
  97.         y[num]=(rand()%4000)/2000.;
  98.         z[num]=10.5+(rand()%1000)/2000.;
  99.         vz[num]=-.5+(rand()%2000)/1000.;
  100.         vy[num]=-1.+(rand()%2000)/1000.;
  101.         vx[num]=1.5+(rand()%1000)/1000.;
  102.     num++;
  103.       }
  104.     
  105.     for (t=0;t<num;t++)
  106.       {
  107.  
  108.     /*
  109.         for (i=t+1;i<num;i++)
  110.     {
  111.     r=sqrt(
  112.     */
  113.     /*too big?*/
  114.     
  115.     if (vx[t]>100.) vx[t]= 50.+(rand()%5000)/100.;
  116.     if (vy[t]>100.) vy[t]= 50.+(rand()%5000)/100.;
  117.     if (vz[t]>100.) vz[t]= 50.+(rand()%5000)/100.;
  118.     if (vx[t]<-100.) vx[t]= -(50.+(rand()%5000)/100.);
  119.     if (vy[t]<-100.) vy[t]= -(50.+(rand()%5000)/100.);
  120.     if (vz[t]<-100.) vz[t]= -(50.+(rand()%5000)/100.);
  121.     
  122.     /* Moveem!*/
  123.     vz[t]-=9.82*dt;
  124.  
  125.     vz[t]+= (-.5+(rand()%1000)/1000.)*dt;
  126.     vx[t]+= (-.4+(rand()%1000)/1000.)*dt;
  127.     vy[t]+= (-.5+(rand()%1000)/1000.)*dt;
  128.  
  129.    px=x[t]+dt*vx[t];
  130.    py=y[t]+dt*vy[t];
  131.    pz=z[t]+dt*vz[t];
  132.     
  133.     /* boundries*/
  134.     if (px<5.)
  135.      {
  136.       if (pz<10.)
  137.       {
  138.         vz[t]= fabs(.90*vz[t]);
  139.         if (vz[t]>1.0) {vx[t]*=.98;  vy[t]*=.98;}
  140.        } 
  141.     }
  142.      else if (pz<0.)
  143.       {
  144.         vz[t]= fabs(.90*vz[t]);
  145.         if (vz[t]>1.0)  {vx[t]*=.98; vy[t]*=.98;}
  146.        } 
  147.    
  148.            
  149.     z[t]+=dt*vz[t];
  150.     x[t]+=dt*vx[t];
  151.     y[t]+=dt*vy[t];
  152.    if (fabs(vz[t])>10.) vz[t]=vz[t]-.2*vz[t]*dt;
  153.    } /* Loop on all particles*/
  154.       
  155.    time+=dt;
  156.    if (framespersec*time>frame)
  157.    { 
  158.       temp=sqrt(3.)/7.;
  159.       frame++;
  160.       strcpy(outfilename,"ram:fall");
  161.       stci_d(tempstring,frame);
  162.       if (frame<100) strcat(outfilename,"0");
  163.       if (frame<10) strcat(outfilename,"0");      
  164.       strcat(outfilename,tempstring); 
  165.       strcat(outfilename,".ttddd");
  166.       outfile=fopen(outfilename,"wb");
  167.       fprintf(outfile,"OBJ Begin\nDESC Begin\nNAME \"WATERFALL\"\n");
  168.       fprintf(outfile,"SHAP Shape = 2\nSHAP Lamp = 2\n END DESC\n\n");
  169.       for (t=0;t<num;t++)
  170.       {
  171.         fprintf(outfile,"DESC Begin\nNAME \"Sphere\"\nSHAP Shape = 0\n");
  172.         fprintf(outfile,"POSI X=%0.2f Y=%0.2f Z=%0.2f\n",x[t],y[t],z[t]-1.0);
  173.         fprintf(outfile,"SIZE X=.1 Y=.1 Z=.1\n COLR R=50 G=130 B=253\n");
  174.         fprintf(outfile,"End DESC\n TOBJ\n");
  175.       }
  176.       fprintf(outfile,"\nTOBJ\nEnd Obj\n");
  177. /*      fprintf(outfile,"SHAP Shap 2\nSHAP Lamp 0\n\n");
  178.       fprintf(outfile,"PNTS PCount %d\n",num*4);
  179.       fprintf(outfile,"EDGE ECount %d\n",num*6);
  180.       fprintf(outfile,"FACE TCount %d\n",num*4);
  181.       for (t
  182.       { 
  183.       fprintf(outfile,"PNTS Point %d %f %f %f\n",t*4,x[t]-.12,y[t]-temp,z[t]); 
  184.       fprintf(outfile,"PNTS Point %d %f %f %f\n",t*4+1,x[t]+.12,y[t]-temp,z[t]);
  185.       fprintf(outfile,"PNTS Point %d %f %f %f\n",t*4+2,x[t],y[t]+temp,z[t]);
  186.       fprintf(outfile,"PNTS Point %d %f %f %f\n",t*4+3,x[t],y[t],z[t]+.5); 
  187.       fprintf(outfile,"EDGE Edge %d %d %d\n",t*6,  t*4,  t*4+1);
  188.       fprintf(outfile,"EDGE Edge %d %d %d\n",t*6+1,t*4+1,t*4+2);
  189.       fprintf(outfile,"EDGE Edge %d %d %d\n",t*6+2,t*4+2,t*4);
  190.       fprintf(outfile,"EDGE Edge %d %d %d\n",t*6+3,t*4,  t*4+3);
  191.       fprintf(outfile,"EDGE Edge %d %d %d\n",t*6+4,t*4+1,t*4+3);
  192.       fprintf(outfile,"EDGE Edge %d %d %d\n",t*6+5,t*4+2,t*4+3);
  193.       fprintf(outfile,"FACE Connect %d %d %d %d\n",t*4,  t*6,  t*6+1,t*6+2);
  194.       fprintf(outfile,"FACE Connect %d %d %d %d\n",t*4+1,t*6,  t*6+3,t*6+4);
  195.       fprintf(outfile,"FACE Connect %d %d %d %d\n",t*4+2,t*6+1,t*6+4,t*6+5);
  196.       fprintf(outfile,"FACE Connect %d %d %d %d\n",t*4+3,t*6+2,t*6+5,t*6+3);
  197.    }
  198.       fprintf(outfile,"\n\nEnd DESC\nTOBJ\nEnd Obj\n");
  199.       
  200.       */
  201.       
  202.       fclose(outfile);
  203.    printf("t= %0.2lf x=%0.2lf y=%0.2lf z=%0.2lf vx=%0.2lf vy=%0.2lf vz=%lf\n",
  204.        time,x[0],y[0],z[0],vx[0],vy[0],vz[0]);
  205.       
  206.    }
  207.  
  208.   }
  209.  
  210. }
  211.  
  212.  
  213.  
  214.