home *** CD-ROM | disk | FTP | other *** search
/ Black Art of 3D Game Programming / Black_Art_of_3D_Game_Programming.iso / source / borland / chap_15 / solzdemo.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-02-21  |  7.5 KB  |  338 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. #include "black15.h"
  27.  
  28. // G L O B A L S /////////////////////////////////////////////////////////////
  29.  
  30. object test_objects[4];                // objects in universe
  31.  
  32. // M A I N ////////////////////////////////////////////////////////////////////
  33.  
  34. void main(int argc,char **argv)
  35. {
  36.  
  37. int done=0,
  38.     z1=0,
  39.     index;
  40.  
  41. char buffer[80];
  42.  
  43. float x,y,z;
  44.  
  45. // build all look up tables
  46.  
  47. Build_Look_Up_Tables();
  48.  
  49. // load in the test object
  50.  
  51. for (index=0; index<4; index++)
  52.     {
  53.  
  54.     if (PLG_Load_Object(&test_objects[index],argv[1],1))
  55.        printf("\nplg loaded.");
  56.     else
  57.        printf("\nCouldn't load file");
  58.  
  59.     } // end for index
  60.  
  61. // set the position of the object
  62.  
  63. for (index=0; index<4; index++)
  64.     {
  65.     test_objects[index].world_pos.x=-200 + (index%4)*100;
  66.     test_objects[index].world_pos.y=0;
  67.     test_objects[index].world_pos.z=200 + 300*(index>>2);
  68.     } // end index
  69.  
  70.  
  71. // set graphics to mode 13h
  72.  
  73. Set_Graphics_Mode(GRAPHICS_MODE13);
  74.  
  75. // read the 3d color palette off disk
  76.  
  77. Load_Palette_Disk("standard.pal",(RGB_palette_ptr)&color_palette_3d);
  78. Write_Palette(0,255,(RGB_palette_ptr)&color_palette_3d);
  79.  
  80. // allocate double buffer
  81.  
  82. Create_Double_Buffer(200);
  83.  
  84. // create a 200 line z buffer (128k)
  85.  
  86. Create_Z_Buffer(200);
  87.  
  88. // initialize the z buffer with a distant value
  89.  
  90. Fill_Z_Buffer(16000);
  91.  
  92. // install the isr keyboard driver
  93.  
  94. Keyboard_Install_Driver();
  95.  
  96. // main event loop
  97.  
  98. while(!done)
  99.      {
  100.  
  101.      // compute starting time of this frame
  102.  
  103.      starting_time = Timer_Query();
  104.  
  105.      // erase all objects
  106.  
  107.      Fill_Double_Buffer(0);
  108.  
  109.      // initialize z buffer
  110.  
  111.      Fill_Z_Buffer(16000);
  112.  
  113.      // test what user is doing
  114.  
  115.      if (keyboard_state[MAKE_T])
  116.         {
  117.  
  118.         x = light_source.x;
  119.  
  120.         y = cos(.2)*light_source.y - sin(.2) * light_source.z;
  121.         z = sin(.2)*light_source.y + cos(.2) * light_source.z;
  122.  
  123.         light_source.y = y;
  124.         light_source.z = z;
  125.  
  126.         }
  127.  
  128.      if (keyboard_state[MAKE_G])
  129.         {
  130.  
  131.         y = light_source.y;
  132.  
  133.         x = cos(.2)*light_source.x + sin(.2) * light_source.z;
  134.         z = -sin(.2)*light_source.x + cos(.2) * light_source.z;
  135.  
  136.         light_source.x = x;
  137.         light_source.z = z;
  138.  
  139.         }
  140.  
  141.      if (keyboard_state[MAKE_B])
  142.         {
  143.  
  144.         z = light_source.z;
  145.  
  146.         x = cos(.2)*light_source.x - sin(.2) * light_source.y;
  147.         y = sin(.2)*light_source.x + cos(.2) * light_source.y;
  148.  
  149.         light_source.x = x;
  150.         light_source.y = y;
  151.  
  152.         }
  153.  
  154.      if (keyboard_state[MAKE_Y])
  155.         {
  156.  
  157.         x = light_source.x;
  158.  
  159.         y = cos(-.2)*light_source.y - sin(-.2) * light_source.z;
  160.         z = sin(-.2)*light_source.y + cos(-.2) * light_source.z;
  161.  
  162.         light_source.y = y;
  163.         light_source.z = z;
  164.  
  165.         }
  166.  
  167.      if (keyboard_state[MAKE_H])
  168.         {
  169.  
  170.         y = light_source.y;
  171.  
  172.         x = cos(-.2)*light_source.x + sin(-.2) * light_source.z;
  173.         z = -sin(-.2)*light_source.x + cos(-.2) * light_source.z;
  174.  
  175.         light_source.x = x;
  176.         light_source.z = z;
  177.  
  178.         }
  179.  
  180.      if (keyboard_state[MAKE_N])
  181.         {
  182.  
  183.         z = light_source.z;
  184.  
  185.         x = cos(-.2)*light_source.x - sin(-.2) * light_source.y;
  186.         y = sin(-.2)*light_source.x + cos(-.2) * light_source.y;
  187.  
  188.         light_source.x = x;
  189.         light_source.y = y;
  190.  
  191.         }
  192.  
  193.      if (keyboard_state[MAKE_UP])
  194.         view_point.y+=20;
  195.  
  196.      if (keyboard_state[MAKE_DOWN])
  197.         view_point.y-=20;
  198.  
  199.      if (keyboard_state[MAKE_RIGHT])
  200.         view_point.x+=20;
  201.  
  202.      if (keyboard_state[MAKE_LEFT])
  203.         view_point.x-=20;
  204.  
  205.      if (keyboard_state[MAKE_KEYPAD_PLUS])
  206.         view_point.z+=20;
  207.  
  208.  
  209.      if (keyboard_state[MAKE_KEYPAD_MINUS])
  210.         view_point.z-=20;
  211.  
  212.      if (keyboard_state[MAKE_LFT_BRACKET])
  213.         Scale_Object(&test_objects[0],1.1);
  214.  
  215.     if (keyboard_state[MAKE_RGT_BRACKET])
  216.        Scale_Object(&test_objects[0],.9);
  217.  
  218.      if (keyboard_state[MAKE_Z])
  219.         if ((view_angle.ang_x+=10)>360)
  220.            view_angle.ang_x = 0;
  221.  
  222.      if (keyboard_state[MAKE_A])
  223.         if ((view_angle.ang_x-=10)<0)
  224.            view_angle.ang_x = 360;
  225.  
  226.      if (keyboard_state[MAKE_X])
  227.         if ((view_angle.ang_y+=10)>360)
  228.            view_angle.ang_y = 0;
  229.  
  230.      if (keyboard_state[MAKE_S])
  231.         if ((view_angle.ang_y-=10)<0)
  232.            view_angle.ang_y = 360;
  233.  
  234.      if (keyboard_state[MAKE_C])
  235.         if ((view_angle.ang_z+=10)>360)
  236.            view_angle.ang_z = 0;
  237.  
  238.      if (keyboard_state[MAKE_D])
  239.         if ((view_angle.ang_z-=10)<0)
  240.            view_angle.ang_z = 360;
  241.  
  242.      if (keyboard_state[MAKE_ESC])
  243.         done=1;
  244.  
  245.      if (keyboard_state[MAKE_Q])
  246.         test_objects[0].world_pos.x-=2;
  247.  
  248.      if (keyboard_state[MAKE_W])
  249.         test_objects[0].world_pos.x+=2;
  250.  
  251.      Rotate_Object(&test_objects[0],3,6,9);
  252.  
  253.  
  254.      // now that user has possible moved viewpoint, create the global
  255.      // world to camera transformation matrix
  256.  
  257.      Create_World_To_Camera();
  258.  
  259.      // reset polygon list
  260.  
  261.      sprintf(buffer,"                           ");
  262.      Print_String_DB(0,0,10,buffer,0);
  263.  
  264.      Generate_Poly_List(NULL,RESET_POLY_LIST);
  265.  
  266.      for (index=0; index<4; index++)
  267.          {
  268.  
  269.          // test if object is visible
  270.  
  271.          if (!Remove_Object(&test_objects[index],OBJECT_CULL_XYZ_MODE))
  272.             {
  273.             // convert object local coordinates to world coordinate
  274.  
  275.             Local_To_World_Object(&test_objects[index]);
  276.  
  277.             // remove the backfaces and shade object
  278.  
  279.             Remove_Backfaces_And_Shade(&test_objects[index]);
  280.  
  281.             // convert world coordinates to camera coordinate
  282.  
  283.             World_To_Camera_Object(&test_objects[index]);
  284.  
  285.             // clip the objects polygons against viewing volume
  286.  
  287.             Clip_Object_3D(&test_objects[index],CLIP_Z_MODE);
  288.  
  289.             // generate the final polygon list
  290.  
  291.             Generate_Poly_List(&test_objects[index],ADD_TO_POLY_LIST);
  292.             }
  293.          else
  294.             {
  295.             sprintf(buffer,"%d, ",index);
  296.             Print_String_DB(0+index*26,0,10,buffer,0);
  297.             }
  298.          } // end for object
  299.  
  300.      // draw the polygon list using z buffer
  301.  
  302.      Draw_Poly_List_Z();
  303.  
  304.      // display double buffer
  305.  
  306.      Display_Double_Buffer(double_buffer,0);
  307.  
  308.      // lock onto 18 frames per second max
  309.  
  310.      while((Timer_Query()-starting_time)<1);
  311.  
  312.      } // end while
  313.  
  314. // restore graphics mode back to text
  315.  
  316. Set_Graphics_Mode(TEXT_MODE);
  317.  
  318. // release the z buffer memory
  319.  
  320. Delete_Z_Buffer();
  321.  
  322. // restore the old keyboard driver
  323.  
  324. Keyboard_Remove_Driver();
  325.  
  326. // print out some stats
  327.  
  328. printf("\nSettings...\n");
  329.  
  330. printf("\nview point x=%f, y=%f, z=%f",view_point.x,view_point.y,view_point.z);
  331. printf("\nview angle x=%d, y=%d, z=%d",view_angle.ang_x,view_angle.ang_y,view_angle.ang_z);
  332. printf("\nlight source x=%f, y=%f, z=%f",light_source.x,light_source.y,light_source.z);
  333.  
  334. } // end main
  335.  
  336.  
  337.  
  338.