home *** CD-ROM | disk | FTP | other *** search
/ Black Art of 3D Game Programming / Black_Art_of_3D_Game_Programming.iso / source / borland / chap_14 / objects.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-02-07  |  6.0 KB  |  274 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[4];   // the test objects
  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 objects from the command line
  42.  
  43. for (index=0; index<4; index++)
  44.     {
  45.     if (!PLG_Load_Object((object_ptr)&test_object[index],argv[1],1))
  46.        {
  47.        printf("\nCouldn't find file %s",argv[1]);
  48.        return;
  49.  
  50.        } // end if
  51.  
  52.     } // end for index
  53.  
  54. // position the objects 300 units in front of user
  55.  
  56. for (index=0; index<4; index++)
  57.     Position_Object((object_ptr)&test_object[index],-150+index*100,0,300);
  58.  
  59. // set the viewpoint
  60.  
  61. view_point.x = 0;
  62. view_point.y = 0;
  63. view_point.z = 0;
  64.  
  65. // create the sin/cos lookup tables used for the rotation function
  66.  
  67. Build_Look_Up_Tables();
  68.  
  69. // set graphics to mode 13h
  70.  
  71. Set_Graphics_Mode(GRAPHICS_MODE13);
  72.  
  73. // allocate double buffer
  74.  
  75. Create_Double_Buffer(200);
  76.  
  77. // read the 3d color palette off disk
  78.  
  79. Load_Palette_Disk("standard.pal",(RGB_palette_ptr)&color_palette_3d);
  80. Write_Palette(0,255,(RGB_palette_ptr)&color_palette_3d);
  81.  
  82. // install the isr keyboard driver
  83.  
  84. Keyboard_Install_Driver();
  85.  
  86. // set viewing distance
  87.  
  88. viewing_distance = 250;
  89.  
  90. // main event loop
  91.  
  92. while(!done)
  93.      {
  94.  
  95.      // compute starting time of this frame
  96.  
  97.      starting_time = Timer_Query();
  98.  
  99.      // erase the screen
  100.  
  101.      Fill_Double_Buffer(0);
  102.  
  103.      // test what key(s) user is pressing
  104.  
  105.      // test if user is moving viewpoint in positive X
  106.  
  107.      if (keyboard_state[MAKE_RIGHT])
  108.         view_point.x+=5;
  109.  
  110.      // test if user is moving viewpoint in negative X
  111.  
  112.      if (keyboard_state[MAKE_LEFT])
  113.         view_point.x-=5;
  114.  
  115.      // test if user is moving viewpoint in positive Y
  116.  
  117.      if (keyboard_state[MAKE_UP])
  118.         view_point.y+=5;
  119.  
  120.      // test if user is moving viewpoint in negative Y
  121.  
  122.      if (keyboard_state[MAKE_DOWN])
  123.         view_point.y-=5;
  124.  
  125.      // test if user is moving viewpoint in positive Z
  126.  
  127.      if (keyboard_state[MAKE_PGUP])
  128.         view_point.z+=5;
  129.  
  130.      // test if user is moving viewpoint in negative Z
  131.  
  132.      if (keyboard_state[MAKE_PGDWN])
  133.         view_point.z-=5;
  134.  
  135.      // this section takes care of view angle rotation
  136.  
  137.  
  138.      if (keyboard_state[MAKE_Z])
  139.         {
  140.  
  141.         if ((view_angle.ang_x+=10)>360)
  142.            view_angle.ang_x = 0;
  143.  
  144.  
  145.         } // end if
  146.  
  147.      if (keyboard_state[MAKE_A])
  148.         {
  149.  
  150.         if ((view_angle.ang_x-=10)<0)
  151.            view_angle.ang_x = 360;
  152.  
  153.         } // end if
  154.  
  155.      if (keyboard_state[MAKE_X])
  156.         {
  157.  
  158.         if ((view_angle.ang_y+=10)>360)
  159.            view_angle.ang_y = 0;
  160.  
  161.         } // end if
  162.  
  163.      if (keyboard_state[MAKE_S])
  164.         {
  165.  
  166.         if ((view_angle.ang_y-=5)<0)
  167.            view_angle.ang_y = 360;
  168.  
  169.         } // end if
  170.  
  171.      if (keyboard_state[MAKE_C])
  172.         {
  173.  
  174.         if ((view_angle.ang_z+=5)>360)
  175.            view_angle.ang_z = 0;
  176.  
  177.         } // end if
  178.  
  179.  
  180.      if (keyboard_state[MAKE_D])
  181.         {
  182.  
  183.         if ((view_angle.ang_z-=5)<0)
  184.            view_angle.ang_z = 360;
  185.  
  186.         } // end if
  187.  
  188.      // test for exit key
  189.  
  190.      if (keyboard_state[MAKE_ESC])
  191.         done=1;
  192.  
  193.  
  194.      // create the global world to camera transformation matrix
  195.  
  196.      Create_World_To_Camera();
  197.  
  198.      // blank object removal message area
  199.  
  200.      sprintf(buffer,"Objects Removed                       ");
  201.      Print_String_DB(0,180,10,buffer,0);
  202.  
  203.      // process each object
  204.  
  205.      for (index=0; index<4; index++)
  206.      {
  207.      // test if this object should be processed
  208.  
  209.      if (!Remove_Object(&test_object[index],OBJECT_CULL_XYZ_MODE))
  210.         {
  211.  
  212.         // convert to world coordinates
  213.  
  214.         Local_To_World_Object((object_ptr)&test_object[index]);
  215.  
  216.         // shade and remove backfaces, ignore the backface part for now
  217.         // notice that backface shadin and backface removal is done in world coordinates
  218.  
  219.         Remove_Backfaces_And_Shade((object_ptr)&test_object[index]);
  220.  
  221.         // convert to camera coordinates
  222.  
  223.         World_To_Camera_Object((object_ptr)&test_object[index]);
  224.  
  225.         // draw the object
  226.  
  227.         Draw_Object_Solid((object_ptr)&test_object[index]);
  228.  
  229.         } // end if object is visible
  230.      else
  231.         {
  232.  
  233.         sprintf(buffer,"%d, ",index);
  234.         Print_String_DB(128+index*16,180,10,buffer,0);
  235.         }
  236.  
  237.      } // end for index
  238.  
  239.      // print out viewpoint
  240.  
  241.      sprintf(buffer,"Viewpoint is at (%d,%d,%d)     ",(int)view_point.x,
  242.                                                       (int)view_point.y,
  243.                                                       (int)view_point.z);
  244.  
  245.      Print_String_DB(0,0,10,buffer,0);
  246.  
  247.      sprintf(buffer,"Viewangle is at (%d,%d,%d)     ",(int)view_angle.ang_x,
  248.                                                       (int)view_angle.ang_y,
  249.                                                       (int)view_angle.ang_z);
  250.      Print_String_DB(0,10,10,buffer,0);
  251.  
  252.      // display double buffer
  253.  
  254.      Display_Double_Buffer(double_buffer,0);
  255.  
  256.      // lock onto 18 frames per second max
  257.  
  258.      while((Timer_Query()-starting_time)<1);
  259.  
  260.      } // end while
  261.  
  262. // restore graphics mode back to text
  263.  
  264. Set_Graphics_Mode(TEXT_MODE);
  265.  
  266. // restore the old keyboard driver
  267.  
  268. Keyboard_Remove_Driver();
  269.  
  270. } // end main
  271.  
  272.  
  273.  
  274.