home *** CD-ROM | disk | FTP | other *** search
/ 3D Madness! Companion CD / 3DMADNESS.iso / dosshow / zero / wave3d / wave3d.c < prev    next >
C/C++ Source or Header  |  1993-03-05  |  22KB  |  664 lines

  1. /*-------------------------------------------------------------------------*
  2.  * WAVE3D.C : sample animation demo
  3.  *
  4.  * (c) 1993 ZeRO Computing S.a.s,  all rights reserved
  5.  *
  6.  *-------------------------------------------------------------------------*/
  7.  
  8. #define  RHO_MIN_LIM    1
  9. #define  RHO_MAX_LIM    88
  10.  
  11. /*--- Include files ---*/
  12. #include <stdlib.h>
  13. #include <stdio.h>
  14. #include <math.h>
  15. #include <time.h>
  16. #include <string.h>
  17. #include <conio.h>
  18.  
  19. /*--- ZeRO 3DLIB include file ---*/
  20. #include "zbnt.h"
  21. #include "ms_key.h"
  22.  
  23. float    zvalues[2600];
  24.  
  25. /*-------------------------------------------------------------------------*
  26.  * main
  27.  *-------------------------------------------------------------------------*/
  28. void     main( void )
  29. {
  30. H_OBJ3D        hObj3D, hObjLight;
  31. H_MDL3D        hModel3D,hModelLight;
  32. H_OBJ2D        hHelp;
  33. H_MDL2D        hBitmap, hMHelp, hTextOver;
  34. OBJ_INFO       obj;
  35. H_VPORT        vp3d,vp2d1,vp2d2;
  36. SPOT_LIGHT     spot;
  37. H_SPOTL        hSpot;
  38. H_FONT         hFont;
  39. GRAPHADAPTER   hardware_info;
  40. KBDCODE        keyboard;
  41.  
  42. float    px, py, pz, sq, i;
  43. float    tt;
  44. int      play, kk;
  45.  
  46. float    theta, rho, radius, min_radius, step, parallel, ambient = 0.2;
  47. float    lx , ly , lz;
  48. short    dx, dy, x, y, s1, mouse;
  49. float    max_x, min_x, max_y, min_y, max_z, min_z, centre_x, centre_y, centre_z;
  50. float    light_radius, angle, animation_step, seconds, frames_per_second;
  51. float    spot_int;
  52. char     end,light = TRUE, spot_on = TRUE, str[80], animate, over;
  53. clock_t  start, elapsed_time;
  54. int      bit=4, first, frames = 0, off = 5;
  55. int      help , help_x = 0, help_y = 0;
  56.  
  57.  
  58.  
  59.  
  60. mouse = InitMouse();
  61. DisableMouseCursor();
  62. DefineMouseSensitivity( 8,16 );
  63.  
  64. /**********************************************/
  65. /* Load 3D & 2D WORLD FILES and PALETTE FILES */
  66. /**********************************************/
  67.  
  68. printf("Loading World Data...\n");
  69.  
  70. LoadWorld( "wave.wrd" );
  71. LoadPalette( "wave.pal" );
  72.  
  73. if ( ! ( LoadWorld( "wave2d.wrd") ) )
  74.    exit ( -1 );
  75.  
  76. printf("Loading Font...\n");
  77. if ( ( hFont = LoadFont( "font.fnt") ) == NULL )
  78.    exit( -1 );
  79.  
  80. /************************************************************************/
  81. /* Get Model Handles.                                                   */
  82. /* All Object are initialized into the world trough their MODEL HANDLES */
  83. /************************************************************************/
  84.  
  85. if ( ( hModel3D = GetModel3DHandle("flatzoll") ) == NULL )
  86.    {
  87.    printf("Error: 3D Model not present\n");
  88.    exit( -1 );
  89.    }
  90.  
  91. if ( ( hModelLight = GetModel3DHandle("sph10") ) == NULL )
  92.    {
  93.    printf("Error: 3D Model not present\n");
  94.    exit( -1 );
  95.    }
  96.  
  97. if ( ( hBitmap  = GetModel2DHandle("frame") ) == NULL )
  98.    {
  99.    printf("Error: 2D Model not present\n");
  100.    exit( -1 );
  101.    }
  102.  
  103. if ( ( hMHelp   = GetModel2DHandle("help") ) == NULL )
  104.    {
  105.    printf("Error: 2D Model not present\n");
  106.    exit( -1 );
  107.    }
  108.  
  109. if ( ( hTextOver = GetModel2DHandle("text_over") ) == NULL )
  110.    {
  111.    printf("Error: 2D Model not present\n");
  112.    exit( -1 );
  113.    }
  114.  
  115. /*****************************************/
  116. /* Init Graphics Adapter and Set Palette */
  117. /*****************************************/
  118.  
  119. InitGraphics( &hardware_info );
  120. SetPalette();
  121.  
  122. /******************************/
  123. /* Sets Frame Rate to Maximum */
  124. /******************************/
  125. SetMaxFrameRate( 0 );
  126.  
  127. /****************************************/
  128. /* Creates Viewport:                    */
  129. /* DIRECT_2D  : Title Bar               */
  130. /* MAIN_3D    : Main3D Viewport         */
  131. /* OVERLAY_3D : Overlay for Help Screen */
  132. /****************************************/
  133.  
  134. vp2d1    = CreateViewport( DIRECT_2D, 0 , 0, 320, 200, 0 );
  135. vp3d     = CreateViewport( MAIN_3D ,  0, 30, 320, 170, 0 );
  136. vp2d2    = CreateViewport( OVERLAY_3D , vp3d );
  137.  
  138.  
  139. /******************************************************/
  140. /* Disable eventual Object Merging in Main3D Viewport */
  141. /******************************************************/
  142.  
  143. SetMerging( vp3d , FALSE );
  144.  
  145.  
  146. /******************************************************/
  147. /* Draws Frame box in DIRECT_2D Viewport              */
  148. /******************************************************/
  149. DrawObj2D( vp2d1 , hBitmap , 0.0 , 0.0 );
  150.  
  151.  
  152. /***************************************************************************/
  153. /* Initializes Help Object in OVERLAY_3D Viewport and hides it immediately */
  154. /***************************************************************************/
  155. if ( ( hHelp   = InitObj2D( vp2d2 , hMHelp , help_x , help_y , FALSE ) ) == NULL  )
  156.    {
  157.    printf("Error InitObj2D\n");
  158.    exit( -1 );
  159.    }
  160.  
  161. /*******************************************/
  162. /* Sets Ambient Light for MAIN_3D Viewport */
  163. /*******************************************/
  164. SetAmbientLight( vp3d, ambient , ambient , ambient );
  165.  
  166. /**********************************************************/
  167. /* Initializes CARTESIAN GRID Object in MAIN_3D viewport  */
  168. /* Sets Object Shading type to FLAT                       */
  169. /* Calculates Dimension and centers it onto screen        */
  170. /**********************************************************/
  171. hObj3D = InitObj3D( vp3d, hModel3D , 0.0, 0.0, 0.0 , TRUE );
  172.  
  173. SetObj3DShadingType( hObj3D, FLAT );
  174. GetObj3DInfo( hObj3D , &obj );
  175.  
  176. max_x = ( obj.alfa_x + obj.centre_x );
  177. max_y = ( obj.beta_y + obj.centre_y );
  178. max_z = ( obj.gamma_z + obj.centre_z );
  179. min_x = ( -obj.alfa_x + obj.centre_x );
  180. min_y = ( -obj.beta_y + obj.centre_y );
  181. min_z = ( -obj.gamma_z + obj.centre_z );
  182.  
  183. centre_x = ( max_x + min_x ) / 2;
  184. centre_y = ( max_y + min_y ) / 2;
  185. centre_z = ( max_z + min_z ) / 2;
  186.  
  187. TranslateObj3D( hObj3D , -centre_x , -centre_y, -centre_z );
  188.  
  189.  
  190. min_radius =  2 * max3( ( max_x - min_x ) / 2, ( max_y - min_y ) / 2, ( max_z - min_z ) / 2 );
  191.  
  192. /************************************************************/
  193. /* Initializes Spot-Light Object and assigns it its Overlay */
  194. /************************************************************/
  195.  
  196. hObjLight = InitObj3D( vp3d, hModelLight , lx = ( 2 * centre_x) , ly = -centre_y, lz = 10.0 , TRUE );
  197. ScaleObj3D( hObjLight, 0.1 );
  198. SetObj3DOverlay( hObjLight , hTextOver , FALSE );
  199.  
  200. light_radius = sqrt ( ( centre_x * centre_x ) + ( centre_y * centre_y ) );
  201. light_radius = centre_x;
  202.  
  203. radius = 5 * min_radius;
  204.  
  205.  
  206. /************************/
  207. /* Sets Viewing Optics  */
  208. /************************/
  209.  
  210. SetOptics( vp3d, 1.0, 3.0, radius * 30.0 );
  211.  
  212.  
  213. i = 0;
  214. theta = rho = 0.0;
  215. end = FALSE;
  216. step = radius / 100;
  217. angle = 0.0;
  218.  
  219. /*********************/
  220. /* Creates SpotLight */
  221. /*********************/
  222. spot.x = 0.0;
  223. spot.y = 0.0;
  224. spot.z = 0.0;
  225. spot.r = 0.4;
  226. spot_int = spot.g = 0.4;
  227. spot.b = 0.4;
  228. spot.warn_light = FALSE;
  229.  
  230. hSpot = CreateSpotLight( vp3d , &spot );
  231.  
  232. parallel = 0.5;
  233. animation_step = 0.1;
  234. animate = TRUE;
  235. first = TRUE;
  236. help = FALSE;
  237. over = FALSE;
  238.  
  239. start = clock();
  240.  
  241. tt = 0.0;
  242. while ( ! end )
  243.       {
  244.       /************************************************************************/
  245.       /* Applies Sin() Function to Cartesian Grid Object to achieve animation */
  246.       /************************************************************************/
  247.       if ( animate )
  248.          {
  249.          if ( play == 1 )
  250.             {
  251.             tt += 0.1;
  252.             if( tt > 2.5 )  play = -1;
  253.             }
  254.          else
  255.             {
  256.             tt -= 0.1;
  257.             if( tt < 0.1 )  play = +1;
  258.             }
  259.  
  260.          for ( kk = 0; kk < 51*51 ; kk++ )
  261.             {
  262.             float xx, yy, rr;
  263.  
  264.             xx = ( kk % 51 )  ;
  265.             yy = (  ( (float) kk) / 51.0 ) ;
  266.             rr = ( xx - 25.0 )*( xx - 25.0 )+( yy - 25.0 )*( yy - 25.0 ) ;
  267.             if(  fabs(rr)  !=  0.0  )
  268.                zvalues[kk] =  100.0 * sin( tt * rr / 30.0 ) / rr  ;
  269.             else
  270.                zvalues[kk] = 1.0;
  271.             }
  272.  
  273.          LiftCartesian( hObj3D , zvalues );
  274.          }
  275.  
  276.       /***********************************/
  277.       /* Checks MOUSE and KEYBOARD Input */
  278.       /***********************************/
  279.  
  280.       if ( mouse )
  281.          {
  282.          ReadMouseMotionNumber( &dx, &dy );
  283.          ReadMouseCursorPos( &x, &y, &s1 );
  284.  
  285.          if ( s1 == 0 )
  286.             {
  287.             theta += (float) dx * 0.2;
  288.             rho += (float) dy * 0.2;
  289.             }
  290.          else
  291.             if ( s1 == 2 )
  292.                {
  293.                theta += (float) dx * 0.04;
  294.                rho += (float) dy * 0.04;
  295.                }
  296.             else
  297.                if ( s1 == 1 )
  298.                   {
  299.                   radius += (float) ( dy * ( step / 2.0 ) );
  300.  
  301.                   if ( radius < min_radius )
  302.                      radius = min_radius;
  303.                   }
  304.          }
  305.  
  306.          while ( KeyBoardReady() )
  307.                {
  308.                ReadKeyBoard( &keyboard );
  309.                switch ( keyboard.ascii )
  310.                       {
  311.                       case 0:
  312.                         switch( keyboard.scan )
  313.                            {
  314.                            case 45:
  315.                               end = TRUE;
  316.                               break;
  317.  
  318.                            case 72:  /* up arrow */
  319.                               rho -= 0.5;
  320.                               break;
  321.  
  322.                            case 80:  /* down arrow */
  323.                               rho += 0.5;
  324.                               break;
  325.  
  326.                            case 75:  /* left arrow */
  327.                               theta -= 0.5;
  328.                               break;
  329.  
  330.                            case 77:  /* right arrow */
  331.                               theta += 0.5;
  332.                               break;
  333.  
  334.                            case 141:
  335.                               rho  -= 3.0;
  336.                               break;
  337.  
  338.                            case 145:
  339.                               rho  += 3.0;
  340.                               break;
  341.  
  342.                            case 116:
  343.                               theta += 3.0;
  344.                               break;
  345.  
  346.                            case 115:
  347.                               theta -= 3.0;
  348.                               break;
  349.  
  350.                            case 144:
  351.                               radius -= ( 10 * step);
  352.                               if ( radius < min_radius )
  353.                                  radius = min_radius;
  354.                               break;
  355.  
  356.                            case 142:
  357.                               radius += ( 10 * step);
  358.                               break;
  359.  
  360.                            case  59:
  361.                               if ( light )
  362.                                  {
  363.                                  parallel -= 0.05;
  364.                                  if ( parallel < 0.0 )
  365.                                     parallel = 0.0;
  366.                                  }
  367.                               break;
  368.  
  369.                            case  60:
  370.                               if ( light )
  371.                                  {
  372.                                  parallel += 0.05;
  373.                                  if ( parallel > 1.0 )
  374.                                     parallel = 1.0;
  375.                                  }
  376.                               break;
  377.  
  378.                            case 61:
  379.                               ambient -= 0.05;
  380.                               if ( ambient< 0.0 )
  381.                                  ambient = 0.0;
  382.                               SetAmbientLight( vp3d, ambient, ambient, ambient );
  383.                               break;
  384.  
  385.                            case 62:
  386.                               ambient += 0.05;
  387.                               if ( ambient > 1.0 )
  388.                                  ambient = 1.0;
  389.                               SetAmbientLight( vp3d, ambient, ambient, ambient );
  390.                               break;
  391.  
  392.                            case 63:
  393.                               if ( spot_on )
  394.                                  {
  395.                                  spot_int -=0.05;
  396.                                  if ( spot_int < 0.0 )
  397.                                     spot_int = 0.0;
  398.                                  spot.g = spot_int;
  399.  
  400.                                  ChangeSpotLightIntensity( hSpot, spot_int, spot_int, spot_int );
  401.                                  }
  402.                               break;
  403.  
  404.                            case 64:
  405.                               if ( spot_on )
  406.                                  {
  407.                                  spot_int +=0.05;
  408.                                  if ( spot_int > 1.0 )
  409.                                     spot_int = 1.0;
  410.                                  spot.g = spot_int;
  411.  
  412.                                  ChangeSpotLightIntensity( hSpot, spot_int, spot_int, spot_int );
  413.                                  }
  414.                               break;
  415.  
  416.  
  417.                            case 65:
  418.                               animation_step -= 0.05;
  419.                               if ( animation_step < 0 )
  420.                                  animation_step = 0;
  421.                               break;
  422.  
  423.                            case 66:
  424.                               animation_step += 0.05;
  425.                               if ( animation_step > 1.0 )
  426.                                  animation_step = 1.0;
  427.                               break;
  428.  
  429.                            case 67:
  430.                               lz -= 1;
  431.                               if ( lz < 4 )
  432.                                  lz = 4;
  433.                               break;
  434.  
  435.                            case 68:
  436.                               lz += 1;
  437.                               break;
  438.  
  439.                            case 152:
  440.                               if ( help )
  441.                                  {
  442.                                  help_y -= 5;
  443.                                  if ( help_y < 0 )
  444.                                     help_y = 0;
  445.                                  PositionObj2D( hHelp, help_x , help_y  );
  446.                                  }
  447.                               break;
  448.  
  449.                            case 160:
  450.                               if ( help )
  451.                                  {
  452.                                  help_y += 5;
  453.                                  if ( help_y > 320 )
  454.                                     help_y = 320;
  455.                                  PositionObj2D( hHelp, help_x , help_y  );
  456.                                  }
  457.                               break;
  458.  
  459.                            case 155:
  460.                               if ( help )
  461.                                  {
  462.                                  help_x -= 5;
  463.                                  if ( help_x < 0 )
  464.                                     help_x = 0;
  465.                                  PositionObj2D( hHelp, help_x , help_y  );
  466.                                  }
  467.                               break;
  468.  
  469.                            case 157:
  470.                               if ( help )
  471.                                  {
  472.                                  help_x += 5;
  473.                                  if ( help_x > 320 )
  474.                                     help_x = 320;
  475.                                  PositionObj2D( hHelp, help_x , help_y  );
  476.                                  }
  477.                               break;
  478.  
  479.                            }
  480.                            break;
  481.  
  482.                       case '+':
  483.                            radius -= step;
  484.                            if ( radius < min_radius )
  485.                               radius = min_radius;
  486.                            break;
  487.  
  488.                       case '-':
  489.                            radius += step;
  490.                            break;
  491.  
  492.                       case '8':
  493.                            rho -= 0.5;
  494.                            break;
  495.  
  496.                       case '2':
  497.                            rho += 0.5;
  498.                            break;
  499.  
  500.                       case '4':
  501.                            theta -= 0.5;
  502.                            break;
  503.  
  504.                       case '6':
  505.                            theta += 0.5;
  506.                            break;
  507.  
  508.                       case 'l':
  509.                       case 'L':
  510.                            light = !light;
  511.                            break;
  512.  
  513.                       case 's':
  514.                       case 'S':
  515.                            HideObj3D( hObjLight , spot_on );
  516.  
  517.                            spot_on = !spot_on;
  518.                            if ( spot_on )
  519.                               hSpot = CreateSpotLight( vp3d , &spot );
  520.                            else
  521.                               DeleteSpotLight( hSpot );
  522.  
  523.                            break;
  524.  
  525.                       case 'a':
  526.                       case 'A':
  527.                            animate = !animate;
  528.                            break;
  529.  
  530.                       case 'h':
  531.                       case 'H':
  532.                            HideObj2D( hHelp, help );
  533.                            help = !help;
  534.                            break;
  535.  
  536.                       case 'o':
  537.                       case 'O':
  538.                            over = !over;
  539.                            EnableObj3DOverlay( hObjLight, over );
  540.                            break;
  541.                       }
  542.                }
  543.  
  544.          /* avoids flipping over the top */
  545.          if ( rho  < RHO_MIN_LIM )
  546.             rho = RHO_MIN_LIM;
  547.          else
  548.             if ( rho > RHO_MAX_LIM )
  549.                rho = RHO_MAX_LIM;
  550.  
  551.          /*****************************************/
  552.          /* Calculates new Point of View Position */
  553.          /*****************************************/
  554.          pz =  radius * cos ( DEGtoRAD( rho ) );
  555.          py =  radius * sin ( DEGtoRAD( rho ) ) * cos( DEGtoRAD( theta ) );
  556.          px =  radius * sin ( DEGtoRAD( rho ) ) * sin( DEGtoRAD( theta ) );
  557.  
  558.          sq = sqrt( px * px + py * py + pz * pz );
  559.          MovePw( vp3d, px, py, pz, - px / sq , -py / sq , - pz / sq, 0.0 );
  560.  
  561.          /****************************************/
  562.          /* If SpotLight is enabled.             */
  563.          /* Calculates new position and moves it */
  564.          /****************************************/
  565.          if ( spot_on )
  566.             {
  567.             lx = light_radius * cos ( angle );
  568.             ly = light_radius * sin ( angle );
  569.  
  570.             PositionObj3D ( hObjLight , lx  , ly , lz );
  571.  
  572.             MoveSpotLight( hSpot , lx , ly , lz );
  573.             }
  574.  
  575.          /*******************************************/
  576.          /* If Parallel Light is enabled, update it */
  577.          /*******************************************/
  578.          if ( light )
  579.             SetParallelSource( vp3d , parallel, parallel, parallel, -px, -py, -pz );
  580.  
  581.          /*******************/
  582.          /* Draws New Frame */
  583.          /*******************/
  584.          DrawFrame();
  585.  
  586.          angle += animation_step;
  587.          elapsed_time = clock();
  588.          frames++;
  589.  
  590.          /********************************************/
  591.          /* Draw Information onto DIRECT_2D Viewport */
  592.          /********************************************/
  593.          if ( ( ! ( frames % bit ) ) || ( first ) )
  594.             {
  595.             seconds = ( ( double ) ( elapsed_time - start ) / ( double ) CLK_TCK );
  596.  
  597.             DrawObj2D( vp2d1 , hBitmap , 0.0 , 0.0 );
  598.  
  599.             frames_per_second = frames / seconds;
  600.  
  601.             sprintf(str , " ZeRO 3D Animation Demo" );
  602.             DrawText( vp2d1 , hFont , str , 10 + off , 2 , 255 , 255, 255 );
  603.  
  604.             sprintf(str , "(c)  1993  ZeRO  Computing  S.a.s.");
  605.             DrawText( vp2d1 , hFont , str , 150 + off, 2 , 255 , 255 , 255 );
  606.  
  607.             sprintf(str , " Frames/sec: %5.2f", frames_per_second );
  608.             DrawText( vp2d1 , hFont , str , 180 + off, 14 , 255 , 255 , 255 );
  609.  
  610.             sprintf(str , "PAR: %3.2f", parallel );
  611.             DrawText( vp2d1 , hFont , str , 10 + off, 14 , 255 , 255 , 255 );
  612.  
  613.             sprintf(str , "AMB: %3.2f", ambient );
  614.             DrawText( vp2d1 , hFont , str , 55 + off, 14 , 255 , 255 , 255 );
  615.  
  616.             if ( spot_on )
  617.                sprintf(str , "SPT: %3.2f", spot_int );
  618.             else
  619.                sprintf(str , "SPT: *** ", spot_int );
  620.  
  621.             DrawText( vp2d1 , hFont , str , 105 + off, 14 , 255 , 255 , 255 );
  622.  
  623.  
  624.             if ( light )
  625.                DrawText( vp2d1, hFont , "*", 303 + off, 14 , 255 , 255 , 255 );
  626.  
  627.             if ( animate )
  628.                DrawText( vp2d1, hFont , "A", 297 + off, 14 , 255 , 255 , 255 );
  629.  
  630.             bit = (int) frames_per_second;
  631.  
  632.             if ( bit == 0 )
  633.                bit = 1;
  634.  
  635.             frames = 0;
  636.  
  637.             start = clock();
  638.             first = FALSE;
  639.             }
  640.          }
  641.  
  642. MyExit( 1 );
  643.  
  644. puts("╔═════════════════════════════════════════════════════════════════════════════╗");
  645. puts("║ For more information about the ZeRO Computing 3D real-time libraries        ║");
  646. puts("║ please contact :                                                            ║");
  647. puts("║                  ZeRO Computing S.a.s.                                      ║");
  648. puts("║                  via T. Cremona, 29                                         ║");
  649. puts("║                  20145 Milano - ITALY                                       ║");
  650. puts("║                  Phone: +39.2.48017946                                      ║");
  651. puts("║                  FAX:   +39.2.58114647                                      ║");
  652. puts("║                  Internet : zero@bench.sublink.ORG                          ║");
  653. puts("║                  BIX      : ppennisi                                        ║");
  654. puts("║                  CIS      : 100022,411                                      ║");
  655. puts("║                  DELPHI   : MSANTOLI                                        ║");
  656. puts("╚═════════════════════════════════════════════════════════════════════════════╝");
  657.  
  658. printf("\n\n");
  659.  
  660. } /*--- end of main ---*/
  661.  
  662.  
  663.  
  664.