home *** CD-ROM | disk | FTP | other *** search
/ Black Art of 3D Game Programming / Black_Art_of_3D_Game_Programming.iso / source / borland / chap_12 / solidemo.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-01-31  |  4.8 KB  |  206 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
  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 object to right
  101.  
  102.      if (keyboard_state[MAKE_RIGHT])
  103.         test_object.world_pos.x+=5;
  104.  
  105.      // test if user is moving object to left
  106.  
  107.      if (keyboard_state[MAKE_LEFT])
  108.         test_object.world_pos.x-=5;
  109.  
  110.      // test if user is moving object up
  111.  
  112.      if (keyboard_state[MAKE_UP])
  113.         test_object.world_pos.y-=5;
  114.  
  115.      // test if user is moving object down
  116.  
  117.      if (keyboard_state[MAKE_DOWN])
  118.         test_object.world_pos.y+=5;
  119.  
  120.      // test if user is moving object farther
  121.  
  122.      if (keyboard_state[MAKE_PGUP])
  123.         test_object.world_pos.z+=15;
  124.  
  125.      // test if user is moving object nearer
  126.  
  127.      if (keyboard_state[MAKE_PGDWN])
  128.         test_object.world_pos.z-=15;
  129.  
  130.      // test for exit key
  131.  
  132.      if (keyboard_state[MAKE_ESC])
  133.         done=1;
  134.  
  135.  
  136.      // rotate the object on all three axes
  137.  
  138.      Rotate_Object((object_ptr)&test_object,2,4,6);
  139.  
  140. //////////////////////////////////////////////////////////////////////////////
  141.  
  142.      // convert the local coordinates into world and camera coordinates for shading
  143.      // and projection. note the viewer is at (0,0,0) with angles 0,0,0 so the transformaton
  144.      // is simply to add the world position to each local vertex
  145.  
  146.      for (index=0; index<test_object.num_vertices; index++)
  147.          {
  148.  
  149.          test_object.vertices_camera[index].x =
  150.          test_object.vertices_world[index].x  =
  151.  
  152.                    test_object.vertices_local[index].x+test_object.world_pos.x;
  153.  
  154.          test_object.vertices_camera[index].y =
  155.          test_object.vertices_world[index].y  =
  156.  
  157.                    test_object.vertices_local[index].y+test_object.world_pos.y;
  158.  
  159.          test_object.vertices_camera[index].z =
  160.          test_object.vertices_world[index].z  =
  161.  
  162.                    test_object.vertices_local[index].z+test_object.world_pos.z;
  163.  
  164.          } // end for index
  165.  
  166. //////////////////////////////////////////////////////////////////////////////
  167.  
  168.      // shade and remove backfaces, ignore the backface part for now
  169.  
  170.      Remove_Backfaces_And_Shade((object_ptr)&test_object);
  171.  
  172.      // draw the object
  173.  
  174.      Draw_Object_Solid((object_ptr)&test_object);
  175.  
  176.      // print out position of object
  177.  
  178.      sprintf(buffer,"Object is at (%d,%d,%d)     ",(int)test_object.world_pos.x,
  179.                                                    (int)test_object.world_pos.y,
  180.                                                    (int)test_object.world_pos.z);
  181.  
  182.      Print_String_DB(0,0,9,buffer,0);
  183.  
  184.      // display double buffer
  185.  
  186.      Display_Double_Buffer(double_buffer,0);
  187.  
  188.      // lock onto 18 frames per second max
  189.  
  190.      while((Timer_Query()-starting_time)<1);
  191.  
  192.      } // end while
  193.  
  194. // restore graphics mode back to text
  195.  
  196. Set_Graphics_Mode(TEXT_MODE);
  197.  
  198. // restore the old keyboard driver
  199.  
  200. Keyboard_Remove_Driver();
  201.  
  202. } // end main
  203.  
  204.  
  205.  
  206.