home *** CD-ROM | disk | FTP | other *** search
/ Black Art of 3D Game Programming / Black_Art_of_3D_Game_Programming.iso / source / borland / chap_13 / sol2demo.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-02-06  |  5.3 KB  |  249 lines

  1.  
  2. // I N C L U D E S ///////////////////////////////////////////////////////////
  3.  
  4. #include <io.h>
  5. #include <conio.h>
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <dos.h>
  9. #include <bios.h>
  10. #include <fcntl.h>
  11. #include <memory.h>
  12. #include <malloc.h>
  13. #include <math.h>
  14. #include <string.h>
  15. #include <search.h>             // this one is needed for qsort()
  16.  
  17. // include all of our stuff
  18.  
  19. #include "black3.h"
  20. #include "black4.h"
  21. #include "black5.h"
  22. #include "black6.h"
  23. #include "black8.h"
  24. #include "black9.h"
  25. #include "black11.h"
  26.  
  27. // G L O B A L S /////////////////////////////////////////////////////////////
  28.  
  29. object test_object;   // the test object
  30.  
  31. // M A I N ////////////////////////////////////////////////////////////////////
  32.  
  33. void main(int argc,char **argv)
  34. {
  35.  
  36. int index,   // looping variable
  37.     done=0;  // exit flag
  38.  
  39. char buffer[80]; // used to print strings
  40.  
  41. // load in the object from the command line
  42.  
  43. if (!PLG_Load_Object(&test_object,argv[1],1))
  44.    {
  45.    printf("\nCouldn't find file %s",argv[1]);
  46.    return;
  47.  
  48.    } // end if
  49.  
  50. // position the object 300 units in front of user
  51.  
  52. Position_Object((object_ptr)&test_object,0,0,300);
  53.  
  54. // set the viewpoint
  55.  
  56. view_point.x = 0;
  57. view_point.y = 0;
  58. view_point.z = 0;
  59.  
  60. // create the sin/cos lookup tables used for the rotation function
  61.  
  62. Build_Look_Up_Tables();
  63.  
  64. // set graphics to mode 13h
  65.  
  66. Set_Graphics_Mode(GRAPHICS_MODE13);
  67.  
  68. // allocate double buffer
  69.  
  70. Create_Double_Buffer(200);
  71.  
  72. // read the 3d color palette off disk
  73.  
  74. Load_Palette_Disk("standard.pal",(RGB_palette_ptr)&color_palette_3d);
  75. Write_Palette(0,255,(RGB_palette_ptr)&color_palette_3d);
  76.  
  77. // install the isr keyboard driver
  78.  
  79. Keyboard_Install_Driver();
  80.  
  81. // set viewing distance
  82.  
  83. viewing_distance = 250;
  84.  
  85. // main event loop
  86.  
  87. while(!done)
  88.      {
  89.  
  90.      // compute starting time of this frame
  91.  
  92.      starting_time = Timer_Query();
  93.  
  94.      // erase the screen
  95.  
  96.      Fill_Double_Buffer(0);
  97.  
  98.      // test what key(s) user is pressing
  99.  
  100.      // test if user is moving viewpoint in positive X
  101.  
  102.      if (keyboard_state[MAKE_RIGHT])
  103.         view_point.x+=5;
  104.  
  105.      // test if user is moving viewpoint in negative X
  106.  
  107.      if (keyboard_state[MAKE_LEFT])
  108.         view_point.x-=5;
  109.  
  110.      // test if user is moving viewpoint in positive Y
  111.  
  112.      if (keyboard_state[MAKE_UP])
  113.         view_point.y+=5;
  114.  
  115.      // test if user is moving viewpoint in negative Y
  116.  
  117.      if (keyboard_state[MAKE_DOWN])
  118.         view_point.y-=5;
  119.  
  120.      // test if user is moving viewpoint in positive Z
  121.  
  122.      if (keyboard_state[MAKE_PGUP])
  123.         view_point.z+=5;
  124.  
  125.      // test if user is moving viewpoint in negative Z
  126.  
  127.      if (keyboard_state[MAKE_PGDWN])
  128.         view_point.z-=5;
  129.  
  130.      // this section takes care of view angle rotation
  131.  
  132.  
  133.      if (keyboard_state[MAKE_Z])
  134.         {
  135.  
  136.         if ((view_angle.ang_x+=10)>360)
  137.            view_angle.ang_x = 0;
  138.  
  139.  
  140.         } // end if
  141.  
  142.      if (keyboard_state[MAKE_A])
  143.         {
  144.  
  145.         if ((view_angle.ang_x-=10)<0)
  146.            view_angle.ang_x = 360;
  147.  
  148.         } // end if
  149.  
  150.      if (keyboard_state[MAKE_X])
  151.         {
  152.  
  153.         if ((view_angle.ang_y+=10)>360)
  154.            view_angle.ang_y = 0;
  155.  
  156.         } // end if
  157.  
  158.      if (keyboard_state[MAKE_S])
  159.         {
  160.  
  161.         if ((view_angle.ang_y-=5)<0)
  162.            view_angle.ang_y = 360;
  163.  
  164.         } // end if
  165.  
  166.      if (keyboard_state[MAKE_C])
  167.         {
  168.  
  169.         if ((view_angle.ang_z+=5)>360)
  170.            view_angle.ang_z = 0;
  171.  
  172.         } // end if
  173.  
  174.  
  175.      if (keyboard_state[MAKE_D])
  176.         {
  177.  
  178.         if ((view_angle.ang_z-=5)<0)
  179.            view_angle.ang_z = 360;
  180.  
  181.         } // end if
  182.  
  183.      // test for exit key
  184.  
  185.      if (keyboard_state[MAKE_ESC])
  186.         done=1;
  187.  
  188.      // rotate the object on all three axes
  189.  
  190.      Rotate_Object((object_ptr)&test_object,2,4,6);
  191.  
  192.      // convert to world coordinates
  193.  
  194.      Local_To_World_Object((object_ptr)&test_object);
  195.  
  196.      // shade and remove backfaces, ignore the backface part for now
  197.      // notice that backface shadin and backface removal is done in world coordinates
  198.  
  199.      Remove_Backfaces_And_Shade((object_ptr)&test_object);
  200.  
  201.      // create the global world to camera transformation matrix
  202.  
  203.      Create_World_To_Camera();
  204.  
  205.      // convert to camera coordinates
  206.  
  207.      World_To_Camera_Object((object_ptr)&test_object);
  208.  
  209.      // draw the object
  210.  
  211.      Draw_Object_Solid((object_ptr)&test_object);
  212.  
  213.      // print out viewpoint
  214.  
  215.      sprintf(buffer,"Viewpoint is at (%d,%d,%d)     ",(int)view_point.x,
  216.                                                       (int)view_point.y,
  217.                                                       (int)view_point.z);
  218.  
  219.      Print_String_DB(0,0,9,buffer,0);
  220.  
  221.      sprintf(buffer,"Viewangle is at (%d,%d,%d)     ",(int)view_angle.ang_x,
  222.                                                       (int)view_angle.ang_y,
  223.                                                       (int)view_angle.ang_z);
  224.  
  225.      Print_String_DB(0,10,9,buffer,0);
  226.  
  227.      // display double buffer
  228.  
  229.      Display_Double_Buffer(double_buffer,0);
  230.  
  231.      // lock onto 18 frames per second max
  232.  
  233.      while((Timer_Query()-starting_time)<1);
  234.  
  235.      } // end while
  236.  
  237. // restore graphics mode back to text
  238.  
  239. Set_Graphics_Mode(TEXT_MODE);
  240.  
  241. // restore the old keyboard driver
  242.  
  243. Keyboard_Remove_Driver();
  244.  
  245. } // end main
  246.  
  247.  
  248.  
  249.