demoII11_1.cpp
上传用户:husern
上传日期:2018-01-20
资源大小:42486k
文件大小:29k
源码类别:

游戏

开发平台:

Visual C++

  1. // DEMOII11_1.CPP - Z buffering demo 
  2. // READ THIS!
  3. // To compile make sure to include DDRAW.LIB, DSOUND.LIB,
  4. // DINPUT.LIB, DINPUT8.LIB, WINMM.LIB in the project link list, and of course 
  5. // the C++ source modules T3DLIB1-9.CPP and the headers T3DLIB1-9.H
  6. // be in the working directory of the compiler
  7. // INCLUDES ///////////////////////////////////////////////
  8. #define DEBUG_ON
  9. #define INITGUID       // make sure al the COM interfaces are available
  10.                        // instead of this you can include the .LIB file
  11.                        // DXGUID.LIB
  12. #define WIN32_LEAN_AND_MEAN  
  13. #include <windows.h>   // include important windows stuff
  14. #include <windowsx.h> 
  15. #include <mmsystem.h>
  16. #include <iostream.h> // include important C/C++ stuff
  17. #include <conio.h> 
  18. #include <stdlib.h> 
  19. #include <malloc.h> 
  20. #include <memory.h>
  21. #include <string.h>   
  22. #include <stdarg.h>
  23. #include <stdio.h>    
  24. #include <math.h>
  25. #include <io.h>
  26. #include <fcntl.h>
  27. #include <ddraw.h>  // directX includes 
  28. #include <dsound.h>
  29. #include <dmksctrl.h>
  30. #include <dmusici.h>
  31. #include <dmusicc.h>
  32. #include <dmusicf.h>
  33. #include <dinput.h>
  34. #include "T3DLIB1.h" // game library includes
  35. #include "T3DLIB2.h"
  36. #include "T3DLIB3.h"
  37. #include "T3DLIB4.h"
  38. #include "T3DLIB5.h"
  39. #include "T3DLIB6.h"
  40. #include "T3DLIB7.h"
  41. #include "T3DLIB8.h"
  42. #include "T3DLIB9.h"
  43. // DEFINES ////////////////////////////////////////////////
  44. // defines for windows interface
  45. #define WINDOW_CLASS_NAME "WIN3DCLASS"  // class name
  46. #define WINDOW_TITLE      "T3D Graphics Console Ver 2.0"
  47. #define WINDOW_WIDTH      800  // size of window
  48. #define WINDOW_HEIGHT     600
  49. #define WINDOW_BPP        16    // bitdepth of window (8,16,24 etc.)
  50.                                 // note: if windowed and not
  51.                                 // fullscreen then bitdepth must
  52.                                 // be same as system bitdepth
  53.                                 // also if 8-bit the a pallete
  54.                                 // is created and attached
  55.    
  56. #define WINDOWED_APP      0    // 0 not windowed, 1 windowed 
  57. // create some constants for ease of access
  58. #define AMBIENT_LIGHT_INDEX   0 // ambient light index
  59. #define INFINITE_LIGHT_INDEX  1 // infinite light index
  60. #define POINT_LIGHT_INDEX     2 // point light index
  61. #define SPOT_LIGHT1_INDEX     4 // point light index
  62. #define SPOT_LIGHT2_INDEX     3 // spot light index
  63. #define CAM_DECEL            (.25)   // deceleration/friction
  64. #define MAX_SPEED             20
  65. #define NUM_OBJECTS           5      // number of objects system loads
  66. #define NUM_SCENE_OBJECTS     500    // number of scenery objects
  67. #define UNIVERSE_RADIUS       2000   // size of universe
  68. // PROTOTYPES /////////////////////////////////////////////
  69. // game console
  70. int Game_Init(void *parms=NULL);
  71. int Game_Shutdown(void *parms=NULL);
  72. int Game_Main(void *parms=NULL);
  73. // GLOBALS ////////////////////////////////////////////////
  74. HWND main_window_handle           = NULL; // save the window handle
  75. HINSTANCE main_instance           = NULL; // save the instance
  76. char buffer[2048];                        // used to print text
  77. // initialize camera position and direction
  78. POINT4D  cam_pos    = {0,0,0,1};
  79. POINT4D  cam_target = {0,0,0,1};
  80. VECTOR4D cam_dir    = {0,0,0,1};
  81. // all your initialization code goes here...
  82. VECTOR4D vscale={1.0,1.0,1.0,1}, 
  83.          vpos = {0,0,150,1}, 
  84.          vrot = {0,0,0,1};
  85. CAM4DV1         cam;                    // the single camera
  86. OBJECT4DV2_PTR  obj_work;               // pointer to active working object
  87. OBJECT4DV2      obj_array[NUM_OBJECTS], // array of objects 
  88.                 obj_scene;              // general scenery object
  89.                
  90. // filenames of objects to load
  91. char *object_filenames[NUM_OBJECTS] = { "cube_flat_01.cob",
  92.                                         "cube_gouraud_01.cob",
  93.                                         "cube_flat_textured_01.cob",
  94.                                         "sphere02.cob",
  95.                                         "sphere03.cob",
  96.                                       };
  97. int curr_object = 2;                  // currently active object index
  98. POINT4D         scene_objects[NUM_SCENE_OBJECTS]; // positions of scenery objects
  99. RENDERLIST4DV2  rend_list;      // the render list
  100. RGBAV1 white, gray, black, red, green, blue; // general colors
  101. // physical model defines
  102. float cam_speed  = 0;       // speed of the camera/jeep
  103. ZBUFFERV1 zbuffer;          // out little z buffer!
  104. // FUNCTIONS //////////////////////////////////////////////
  105. LRESULT CALLBACK WindowProc(HWND hwnd, 
  106.     UINT msg, 
  107.                             WPARAM wparam, 
  108.                             LPARAM lparam)
  109. {
  110. // this is the main message handler of the system
  111. PAINTSTRUCT ps;    // used in WM_PAINT
  112. HDC hdc;    // handle to a device context
  113. // what is the message 
  114. switch(msg)
  115. {
  116. case WM_CREATE: 
  117.         {
  118. // do initialization stuff here
  119. return(0);
  120. } break;
  121.     case WM_PAINT:
  122.          {
  123.          // start painting
  124.          hdc = BeginPaint(hwnd,&ps);
  125.          // end painting
  126.          EndPaint(hwnd,&ps);
  127.          return(0);
  128.         } break;
  129. case WM_DESTROY: 
  130. {
  131. // kill the application
  132. PostQuitMessage(0);
  133. return(0);
  134. } break;
  135. default:break;
  136.     } // end switch
  137. // process any messages that we didn't take care of 
  138. return (DefWindowProc(hwnd, msg, wparam, lparam));
  139. } // end WinProc
  140. // WINMAIN ////////////////////////////////////////////////
  141. int WINAPI WinMain( HINSTANCE hinstance,
  142. HINSTANCE hprevinstance,
  143. LPSTR lpcmdline,
  144. int ncmdshow)
  145. {
  146. // this is the winmain function
  147. WNDCLASS winclass; // this will hold the class we create
  148. HWND  hwnd; // generic window handle
  149. MSG  msg; // generic message
  150. HDC      hdc;       // generic dc
  151. PAINTSTRUCT ps;     // generic paintstruct
  152. // first fill in the window class stucture
  153. winclass.style = CS_DBLCLKS | CS_OWNDC | 
  154.                           CS_HREDRAW | CS_VREDRAW;
  155. winclass.lpfnWndProc = WindowProc;
  156. winclass.cbClsExtra = 0;
  157. winclass.cbWndExtra = 0;
  158. winclass.hInstance = hinstance;
  159. winclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
  160. winclass.hCursor = LoadCursor(NULL, IDC_ARROW);
  161. winclass.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
  162. winclass.lpszMenuName = NULL; 
  163. winclass.lpszClassName = WINDOW_CLASS_NAME;
  164. // register the window class
  165. if (!RegisterClass(&winclass))
  166. return(0);
  167. // create the window, note the test to see if WINDOWED_APP is
  168. // true to select the appropriate window flags
  169. if (!(hwnd = CreateWindow(WINDOW_CLASS_NAME, // class
  170.   WINDOW_TITLE,  // title
  171.   (WINDOWED_APP ? (WS_OVERLAPPED | WS_SYSMENU | WS_CAPTION) : (WS_POPUP | WS_VISIBLE)),
  172.     0,0,    // x,y
  173.   WINDOW_WIDTH,  // width
  174.                           WINDOW_HEIGHT, // height
  175.   NULL,    // handle to parent 
  176.   NULL,    // handle to menu
  177.   hinstance,// instance
  178.   NULL))) // creation parms
  179. return(0);
  180. // save the window handle and instance in a global
  181. main_window_handle = hwnd;
  182. main_instance      = hinstance;
  183. // resize the window so that client is really width x height
  184. if (WINDOWED_APP)
  185. {
  186. // now resize the window, so the client area is the actual size requested
  187. // since there may be borders and controls if this is going to be a windowed app
  188. // if the app is not windowed then it won't matter
  189. RECT window_rect = {0,0,WINDOW_WIDTH-1,WINDOW_HEIGHT-1};
  190. // make the call to adjust window_rect
  191. AdjustWindowRectEx(&window_rect,
  192.      GetWindowStyle(main_window_handle),
  193.      GetMenu(main_window_handle) != NULL,  
  194.      GetWindowExStyle(main_window_handle));
  195. // save the global client offsets, they are needed in DDraw_Flip()
  196. window_client_x0 = -window_rect.left;
  197. window_client_y0 = -window_rect.top;
  198. // now resize the window with a call to MoveWindow()
  199. MoveWindow(main_window_handle,
  200.            0,                                    // x position
  201.            0,                                    // y position
  202.            window_rect.right - window_rect.left, // width
  203.            window_rect.bottom - window_rect.top, // height
  204.            FALSE);
  205. // show the window, so there's no garbage on first render
  206. ShowWindow(main_window_handle, SW_SHOW);
  207. } // end if windowed
  208. // perform all game console specific initialization
  209. Game_Init();
  210. // disable CTRL-ALT_DEL, ALT_TAB, comment this line out 
  211. // if it causes your system to crash
  212. SystemParametersInfo(SPI_SCREENSAVERRUNNING, TRUE, NULL, 0);
  213. // enter main event loop
  214. while(1)
  215. {
  216. if (PeekMessage(&msg,NULL,0,0,PM_REMOVE))
  217. // test if this is a quit
  218.         if (msg.message == WM_QUIT)
  219.            break;
  220. // translate any accelerator keys
  221. TranslateMessage(&msg);
  222. // send the message to the window proc
  223. DispatchMessage(&msg);
  224. } // end if
  225.     
  226.     // main game processing goes here
  227.     Game_Main();
  228. } // end while
  229. // shutdown game and release all resources
  230. Game_Shutdown();
  231. // enable CTRL-ALT_DEL, ALT_TAB, comment this line out 
  232. // if it causes your system to crash
  233. SystemParametersInfo(SPI_SCREENSAVERRUNNING, FALSE, NULL, 0);
  234. // return to Windows like this
  235. return(msg.wParam);
  236. } // end WinMain
  237. // T3D II GAME PROGRAMMING CONSOLE FUNCTIONS ////////////////
  238. int Game_Init(void *parms)
  239. {
  240. // this function is where you do all the initialization 
  241. // for your game
  242. int index; // looping var
  243. // start up DirectDraw (replace the parms as you desire)
  244. DDraw_Init(WINDOW_WIDTH, WINDOW_HEIGHT, WINDOW_BPP, WINDOWED_APP);
  245. // initialize directinput
  246. DInput_Init();
  247. // acquire the keyboard 
  248. DInput_Init_Keyboard();
  249. // add calls to acquire other directinput devices here...
  250. // initialize directsound and directmusic
  251. DSound_Init();
  252. DMusic_Init();
  253. // hide the mouse
  254. if (!WINDOWED_APP)
  255.     ShowCursor(FALSE);
  256. // seed random number generator
  257. srand(Start_Clock()); 
  258. Open_Error_File("ERROR.TXT");
  259. // initialize math engine
  260. Build_Sin_Cos_Tables();
  261. // initialize the camera with 90 FOV, normalized coordinates
  262. Init_CAM4DV1(&cam,      // the camera object
  263.              CAM_MODEL_EULER, // the euler model
  264.              &cam_pos,  // initial camera position
  265.              &cam_dir,  // initial camera angles
  266.              &cam_target,      // no target
  267.              15.0,        // near and far clipping planes
  268.              12000.0,
  269.              120.0,      // field of view in degrees
  270.              WINDOW_WIDTH,   // size of final screen viewport
  271.              WINDOW_HEIGHT);
  272. // set a scaling vector
  273. VECTOR4D_INITXYZ(&vscale,20.00,20.00,20.00); 
  274. // load all the objects in
  275. for (int index_obj=0; index_obj < NUM_OBJECTS; index_obj++)
  276.     {
  277.     Load_OBJECT4DV2_COB(&obj_array[index_obj], object_filenames[index_obj],  
  278.                         &vscale, &vpos, &vrot, VERTEX_FLAGS_SWAP_YZ  |
  279.                                                VERTEX_FLAGS_TRANSFORM_LOCAL 
  280.                                                /* VERTEX_FLAGS_TRANSFORM_LOCAL_WORLD*/ );
  281.     } // end for index_obj
  282. // set current object
  283. curr_object = 2;
  284. obj_work = &obj_array[curr_object];
  285. // load in the scenery object that we will place all over the place
  286. Load_OBJECT4DV2_COB(&obj_scene, "cube_gouraud_01.cob",  
  287.                         &vscale, &vpos, &vrot, VERTEX_FLAGS_SWAP_YZ  |
  288.                                                VERTEX_FLAGS_TRANSFORM_LOCAL 
  289.                                                /* VERTEX_FLAGS_TRANSFORM_LOCAL_WORLD*/ );
  290. // position the scenery objects randomly
  291. for (index = 0; index < NUM_SCENE_OBJECTS; index++)
  292.     {
  293.     // randomly position object
  294.     scene_objects[index].x = RAND_RANGE(-UNIVERSE_RADIUS, UNIVERSE_RADIUS);
  295.     scene_objects[index].y = RAND_RANGE(-200, 200);
  296.     scene_objects[index].z = RAND_RANGE(-UNIVERSE_RADIUS, UNIVERSE_RADIUS);
  297.     } // end for
  298. // set up lights
  299. Reset_Lights_LIGHTV2(lights2, MAX_LIGHTS);
  300. // create some working colors
  301. white.rgba = _RGBA32BIT(255,255,255,0);
  302. gray.rgba  = _RGBA32BIT(100,100,100,0);
  303. black.rgba = _RGBA32BIT(0,0,0,0);
  304. red.rgba   = _RGBA32BIT(255,0,0,0);
  305. green.rgba = _RGBA32BIT(0,255,0,0);
  306. blue.rgba  = _RGBA32BIT(0,0,255,0);
  307. // ambient light
  308. Init_Light_LIGHTV2(lights2,               // array of lights to work with
  309.                    AMBIENT_LIGHT_INDEX,   
  310.                    LIGHTV2_STATE_ON,      // turn the light on
  311.                    LIGHTV2_ATTR_AMBIENT,  // ambient light type
  312.                    gray, black, black,    // color for ambient term only
  313.                    NULL, NULL,            // no need for pos or dir
  314.                    0,0,0,                 // no need for attenuation
  315.                    0,0,0);                // spotlight info NA
  316. VECTOR4D dlight_dir = {-1,0,-1,1}; 
  317. // directional light
  318. Init_Light_LIGHTV2(lights2,               // array of lights to work with
  319.                    INFINITE_LIGHT_INDEX,  
  320.                    LIGHTV2_STATE_ON,      // turn the light on
  321.                    LIGHTV2_ATTR_INFINITE, // infinite light type
  322.                    black, gray, black,    // color for diffuse term only
  323.                    NULL, &dlight_dir,     // need direction only
  324.                    0,0,0,                 // no need for attenuation
  325.                    0,0,0);                // spotlight info NA
  326. VECTOR4D plight_pos = {0,200,0,1};
  327. // point light
  328. Init_Light_LIGHTV2(lights2,               // array of lights to work with
  329.                    POINT_LIGHT_INDEX,
  330.                    LIGHTV2_STATE_ON,      // turn the light on
  331.                    LIGHTV2_ATTR_POINT,    // pointlight type
  332.                    black, green, black,    // color for diffuse term only
  333.                    &plight_pos, NULL,     // need pos only
  334.                    0,.002,0,              // linear attenuation only
  335.                    0,0,1);                // spotlight info NA
  336. VECTOR4D slight2_pos = {0,1000,0,1};
  337. VECTOR4D slight2_dir = {-1,0,-1,1};
  338. // spot light2
  339. Init_Light_LIGHTV2(lights2,                  // array of lights to work with
  340.                    SPOT_LIGHT2_INDEX,
  341.                    LIGHTV2_STATE_ON,         // turn the light on
  342.                    LIGHTV2_ATTR_SPOTLIGHT2,  // spot light type 2
  343.                    black, red, black,        // color for diffuse term only
  344.                    &slight2_pos, &slight2_dir, // need pos only
  345.                    0,.001,0,                 // linear attenuation only
  346.                    0,0,1);    
  347. // create lookup for lighting engine
  348. RGB_16_8_IndexedRGB_Table_Builder(DD_PIXEL_FORMAT565,  // format we want to build table for
  349.                                   palette,             // source palette
  350.                                   rgblookup);          // lookup table
  351. // create the z buffer
  352. Create_Zbuffer(&zbuffer,
  353.                WINDOW_WIDTH,
  354.                WINDOW_HEIGHT,
  355.                ZBUFFER_ATTR_32BIT);
  356. // return success
  357. return(1);
  358. } // end Game_Init
  359. ///////////////////////////////////////////////////////////
  360. int Game_Shutdown(void *parms)
  361. {
  362. // this function is where you shutdown your game and
  363. // release all resources that you allocated
  364. // shut everything down
  365. // release all your resources created for the game here....
  366. // now directsound
  367. DSound_Stop_All_Sounds();
  368. DSound_Delete_All_Sounds();
  369. DSound_Shutdown();
  370. // directmusic
  371. DMusic_Delete_All_MIDI();
  372. DMusic_Shutdown();
  373. // shut down directinput
  374. DInput_Release_Keyboard();
  375. // shutdown directinput
  376. DInput_Shutdown();
  377. // shutdown directdraw last
  378. DDraw_Shutdown();
  379. Close_Error_File();
  380. Delete_Zbuffer(&zbuffer);
  381. // return success
  382. return(1);
  383. } // end Game_Shutdown
  384. //////////////////////////////////////////////////////////
  385. int Game_Main(void *parms)
  386. {
  387. // this is the workhorse of your game it will be called
  388. // continuously in real-time this is like main() in C
  389. // all the calls for you game go here!
  390. static MATRIX4X4 mrot;   // general rotation matrix
  391. static float plight_ang = 0, 
  392.              slight_ang = 0; // angles for light motion
  393. // use these to rotate objects
  394. static float x_ang = 0, y_ang = 0, z_ang = 0;
  395. // state variables for different rendering modes and help
  396. static int wireframe_mode = 1;
  397. static int backface_mode  = 1;
  398. static int lighting_mode  = 1;
  399. static int help_mode      = 1;
  400. static int zsort_mode     = 1;
  401. static int x_clip_mode    = 1;
  402. static int y_clip_mode    = 1;
  403. static int z_clip_mode    = 1;
  404. static int z_buffer_mode  = 1;
  405. static int display_mode    = 1;
  406. char work_string[256]; // temp string
  407. int index; // looping var
  408. // start the timing clock
  409. Start_Clock();
  410. // clear the drawing surface 
  411. //DDraw_Fill_Surface(lpddsback, 0);
  412. // draw the sky
  413. Draw_Rectangle(0,0, WINDOW_WIDTH, WINDOW_HEIGHT*.5, RGB16Bit(180,180,220), lpddsback);
  414. // draw the ground
  415. Draw_Rectangle(0,WINDOW_HEIGHT*.5, WINDOW_WIDTH, WINDOW_HEIGHT, RGB16Bit(190,190,230), lpddsback);
  416. // read keyboard and other devices here
  417. DInput_Read_Keyboard();
  418. // game logic here...
  419. // reset the render list
  420. Reset_RENDERLIST4DV2(&rend_list);
  421. // modes and lights
  422. // wireframe mode
  423. if (keyboard_state[DIK_W]) 
  424.    {
  425.    // toggle wireframe mode
  426.    if (++wireframe_mode > 1)
  427.        wireframe_mode=0;
  428.    Wait_Clock(100); // wait, so keyboard doesn't bounce
  429.    } // end if
  430. // backface removal
  431. if (keyboard_state[DIK_B])
  432.    {
  433.    // toggle backface removal
  434.    backface_mode = -backface_mode;
  435.    Wait_Clock(100); // wait, so keyboard doesn't bounce
  436.    } // end if
  437. // lighting
  438. if (keyboard_state[DIK_L])
  439.    {
  440.    // toggle lighting engine completely
  441.    lighting_mode = -lighting_mode;
  442.    Wait_Clock(100); // wait, so keyboard doesn't bounce
  443.    } // end if
  444. // toggle ambient light
  445. if (keyboard_state[DIK_A])
  446.    {
  447.    // toggle ambient light
  448.    if (lights2[AMBIENT_LIGHT_INDEX].state == LIGHTV2_STATE_ON)
  449.       lights2[AMBIENT_LIGHT_INDEX].state = LIGHTV2_STATE_OFF;
  450.    else
  451.       lights2[AMBIENT_LIGHT_INDEX].state = LIGHTV2_STATE_ON;
  452.    Wait_Clock(100); // wait, so keyboard doesn't bounce
  453.    } // end if
  454. // toggle infinite light
  455. if (keyboard_state[DIK_I])
  456.    {
  457.    // toggle ambient light
  458.    if (lights2[INFINITE_LIGHT_INDEX].state == LIGHTV2_STATE_ON)
  459.       lights2[INFINITE_LIGHT_INDEX].state = LIGHTV2_STATE_OFF;
  460.    else
  461.       lights2[INFINITE_LIGHT_INDEX].state = LIGHTV2_STATE_ON;
  462.    Wait_Clock(100); // wait, so keyboard doesn't bounce
  463.    } // end if
  464. // toggle point light
  465. if (keyboard_state[DIK_P])
  466.    {
  467.    // toggle point light
  468.    if (lights2[POINT_LIGHT_INDEX].state == LIGHTV2_STATE_ON)
  469.       lights2[POINT_LIGHT_INDEX].state = LIGHTV2_STATE_OFF;
  470.    else
  471.       lights2[POINT_LIGHT_INDEX].state = LIGHTV2_STATE_ON;
  472.    Wait_Clock(100); // wait, so keyboard doesn't bounce
  473.    } // end if
  474. // toggle spot light
  475. if (keyboard_state[DIK_S])
  476.    {
  477.    // toggle spot light
  478.    if (lights2[SPOT_LIGHT2_INDEX].state == LIGHTV2_STATE_ON)
  479.       lights2[SPOT_LIGHT2_INDEX].state = LIGHTV2_STATE_OFF;
  480.    else
  481.       lights2[SPOT_LIGHT2_INDEX].state = LIGHTV2_STATE_ON;
  482.    Wait_Clock(100); // wait, so keyboard doesn't bounce
  483.    } // end if
  484. // help menu
  485. if (keyboard_state[DIK_H])
  486.    {
  487.    // toggle help menu 
  488.    help_mode = -help_mode;
  489.    Wait_Clock(100); // wait, so keyboard doesn't bounce
  490.    } // end if
  491. // z-sorting
  492. if (keyboard_state[DIK_S])
  493.    {
  494.    // toggle z sorting
  495.    zsort_mode = -zsort_mode;
  496.    Wait_Clock(100); // wait, so keyboard doesn't bounce
  497.    } // end if
  498. // z buffer
  499. if (keyboard_state[DIK_Z])
  500.    {
  501.    // toggle z buffer
  502.    z_buffer_mode = -z_buffer_mode;
  503.    Wait_Clock(100); // wait, so keyboard doesn't bounce
  504.    } // end if
  505. // display mode
  506. if (keyboard_state[DIK_D])
  507.    {
  508.    // toggle display mode
  509.    display_mode = -display_mode;
  510.    Wait_Clock(100); // wait, so keyboard doesn't bounce
  511.    } // end if
  512. // forward/backward
  513. if (keyboard_state[DIK_UP])
  514.    {
  515.    // move forward
  516.    if ( (cam_speed+=1) > MAX_SPEED) cam_speed = MAX_SPEED;
  517.    } // end if
  518. else
  519. if (keyboard_state[DIK_DOWN])
  520.    {
  521.    // move backward
  522.    if ((cam_speed-=1) < -MAX_SPEED) cam_speed = -MAX_SPEED;
  523.    } // end if
  524. // rotate around y axis or yaw
  525. if (keyboard_state[DIK_RIGHT])
  526.    {
  527.    cam.dir.y+=5;
  528.    } // end if
  529. if (keyboard_state[DIK_LEFT])
  530.    {
  531.    cam.dir.y-=5;
  532.    } // end if
  533. // move to next object
  534. if (keyboard_state[DIK_N])
  535.    {
  536.    if (++curr_object >= NUM_OBJECTS)
  537.       curr_object = 0;
  538.    // update pointer
  539.    obj_work = &obj_array[curr_object];
  540.    Wait_Clock(100); // wait, so keyboard doesn't bounce
  541.    } // end if
  542. // decelerate camera
  543. if (cam_speed > (CAM_DECEL) ) cam_speed-=CAM_DECEL;
  544. else
  545. if (cam_speed < (-CAM_DECEL) ) cam_speed+=CAM_DECEL;
  546. else
  547.    cam_speed = 0;
  548. // move camera
  549. cam.pos.x += cam_speed*Fast_Sin(cam.dir.y);
  550. cam.pos.z += cam_speed*Fast_Cos(cam.dir.y);
  551. // move point light source in ellipse around game world
  552. lights2[POINT_LIGHT_INDEX].pos.x = 1000*Fast_Cos(plight_ang);
  553. lights2[POINT_LIGHT_INDEX].pos.y = 100;
  554. lights2[POINT_LIGHT_INDEX].pos.z = 1000*Fast_Sin(plight_ang);
  555. if ((plight_ang+=3) > 360)
  556.     plight_ang = 0;
  557. // move spot light source in ellipse around game world
  558. lights2[SPOT_LIGHT2_INDEX].pos.x = 1000*Fast_Cos(slight_ang);
  559. lights2[SPOT_LIGHT2_INDEX].pos.y = 200;
  560. lights2[SPOT_LIGHT2_INDEX].pos.z = 1000*Fast_Sin(slight_ang);
  561. if ((slight_ang-=5) < 0)
  562.     slight_ang = 360;
  563. obj_work->world_pos.x = cam.pos.x + 150*Fast_Sin(cam.dir.y);
  564. obj_work->world_pos.y = cam.pos.y + 0;
  565. obj_work->world_pos.z = cam.pos.z + 150*Fast_Cos(cam.dir.y);
  566. // generate camera matrix
  567. Build_CAM4DV1_Matrix_Euler(&cam, CAM_ROT_SEQ_ZYX);
  568. ////////////////////////////////////////////////////////
  569. // insert the scenery into universe
  570. for (index = 0; index < NUM_SCENE_OBJECTS; index++)
  571.     {
  572.     // reset the object (this only matters for backface and object removal)
  573.     Reset_OBJECT4DV2(&obj_scene);
  574.     // set position of tower
  575.     obj_scene.world_pos.x = scene_objects[index].x;
  576.     obj_scene.world_pos.y = scene_objects[index].y;
  577.     obj_scene.world_pos.z = scene_objects[index].z;
  578.     // attempt to cull object   
  579.     if (!Cull_OBJECT4DV2(&obj_scene, &cam, CULL_OBJECT_XYZ_PLANES))
  580.        {
  581.        MAT_IDENTITY_4X4(&mrot);
  582.        // rotate the local coords of the object
  583.        Transform_OBJECT4DV2(&obj_scene, &mrot, TRANSFORM_LOCAL_TO_TRANS,1);
  584.        // perform world transform
  585.        Model_To_World_OBJECT4DV2(&obj_scene, TRANSFORM_TRANS_ONLY);
  586.        // insert the object into render list
  587.        Insert_OBJECT4DV2_RENDERLIST4DV2(&rend_list, &obj_scene,0);
  588.        } // end if
  589.  
  590.     } // end for
  591. ///////////////////////////////////////////////////////////////
  592. // insert the player object into universe
  593. // reset the object (this only matters for backface and object removal)
  594. Reset_OBJECT4DV2(obj_work);
  595. // generate rotation matrix around y axis
  596. Build_XYZ_Rotation_MATRIX4X4(x_ang, cam.dir.y + y_ang, z_ang, &mrot);
  597. //MAT_IDENTITY_4X4(&mrot);
  598. // rotate the local coords of the object
  599. Transform_OBJECT4DV2(obj_work, &mrot, TRANSFORM_LOCAL_TO_TRANS,1);
  600. // perform world transform
  601. Model_To_World_OBJECT4DV2(obj_work, TRANSFORM_TRANS_ONLY);
  602. // insert the object into render list
  603. Insert_OBJECT4DV2_RENDERLIST4DV2(&rend_list, obj_work,0);
  604. // update rotation angles
  605. if ((x_ang+=.2) > 360) x_ang = 0;
  606. if ((y_ang+=.4) > 360) y_ang = 0;
  607. if ((z_ang+=.8) > 360) z_ang = 0;
  608. // reset number of polys rendered
  609. debug_polys_rendered_per_frame = 0;
  610. debug_polys_lit_per_frame = 0;
  611. // remove backfaces
  612. if (backface_mode==1)
  613.    Remove_Backfaces_RENDERLIST4DV2(&rend_list, &cam);
  614. // apply world to camera transform
  615. World_To_Camera_RENDERLIST4DV2(&rend_list, &cam);
  616. // clip the polygons themselves now
  617. Clip_Polys_RENDERLIST4DV2(&rend_list, &cam, ((x_clip_mode == 1) ? CLIP_POLY_X_PLANE : 0) | 
  618.                                             ((y_clip_mode == 1) ? CLIP_POLY_Y_PLANE : 0) | 
  619.                                             ((z_clip_mode == 1) ? CLIP_POLY_Z_PLANE : 0) );
  620. // light scene all at once 
  621. if (lighting_mode==1)
  622.    {
  623.    Transform_LIGHTSV2(lights2, 4, &cam.mcam, TRANSFORM_LOCAL_TO_TRANS);
  624.    Light_RENDERLIST4DV2_World2_16(&rend_list, &cam, lights2, 4);
  625.    } // end if
  626. // sort the polygon list (hurry up!)
  627. if (zsort_mode == 1)
  628.    Sort_RENDERLIST4DV2(&rend_list,  SORT_POLYLIST_AVGZ);
  629. // apply camera to perspective transformation
  630. Camera_To_Perspective_RENDERLIST4DV2(&rend_list, &cam);
  631. // apply screen transform
  632. Perspective_To_Screen_RENDERLIST4DV2(&rend_list, &cam);
  633. // lock the back buffer
  634. DDraw_Lock_Back_Surface();
  635. // reset number of polys rendered
  636. debug_polys_rendered_per_frame = 0;
  637. // render the renderinglist
  638. if (wireframe_mode  == 0)
  639.    Draw_RENDERLIST4DV2_Wire16(&rend_list, back_buffer, back_lpitch);
  640. else
  641. if (wireframe_mode  == 1)
  642.    {
  643.    if (z_buffer_mode == 1)
  644.    {
  645.    // initialize zbuffer to 16000 fixed point
  646.    Clear_Zbuffer(&zbuffer, (16000 << FIXP16_SHIFT));
  647.    Draw_RENDERLIST4DV2_SolidZB16(&rend_list, back_buffer, back_lpitch, (UCHAR *)zbuffer.zbuffer, WINDOW_WIDTH*4);
  648.    }
  649. else
  650.    {
  651.    // initialize zbuffer to 32000 fixed point
  652.    Draw_RENDERLIST4DV2_Solid16(&rend_list, back_buffer, back_lpitch);
  653.    } 
  654.    } // end if
  655. // dislay z buffer graphically (sorta)
  656. if (display_mode==-1)
  657. {
  658. // use z buffer visualization mode
  659. // copy each line of the z buffer into the back buffer and translate each z pixel
  660. // into a color
  661. USHORT *screen_ptr = (USHORT *)back_buffer;
  662. UINT   *zb_ptr    =  (UINT *)zbuffer.zbuffer;
  663. for (int y = 0; y < WINDOW_HEIGHT; y++)
  664.     {
  665.     for (int x = 0; x < WINDOW_WIDTH; x++)
  666.         {
  667.         // z buffer is 32 bit, so be carefull
  668.         UINT zpixel = zb_ptr[x + y*WINDOW_WIDTH];
  669.         zpixel = (zpixel/4096 & 0xffff);
  670.         screen_ptr[x + y* (back_lpitch >> 1)] = (USHORT)zpixel;
  671.         } // end for
  672.     } // end for y
  673. } // end if
  674. // unlock the back buffer
  675. DDraw_Unlock_Back_Surface();
  676. sprintf(work_string,"Lighting [%s]: Ambient=%d, Infinite=%d, Point=%d, Spot=%d, BckFceRM [%s], Zsort [%s], Zbuffer [%s]", 
  677.                                                                                  ((lighting_mode == 1) ? "ON" : "OFF"),
  678.                                                                                  lights[AMBIENT_LIGHT_INDEX].state,
  679.                                                                                  lights[INFINITE_LIGHT_INDEX].state, 
  680.                                                                                  lights[POINT_LIGHT_INDEX].state,
  681.                                                                                  lights[SPOT_LIGHT2_INDEX].state,
  682.                                                                                  ((backface_mode == 1) ? "ON" : "OFF"),
  683.                                                                                  ((zsort_mode == 1) ? "ON" : "OFF"),
  684.                                                                                  ((z_buffer_mode == 1) ? "ON" : "OFF"));
  685. Draw_Text_GDI(work_string, 0, WINDOW_HEIGHT-34-16, RGB(0,255,0), lpddsback);
  686. // draw instructions
  687. Draw_Text_GDI("Press ESC to exit. Press <H> for Help.", 0, 0, RGB(0,255,0), lpddsback);
  688. // should we display help
  689. int text_y = 16;
  690. if (help_mode==1)
  691.     {
  692.     // draw help menu
  693.     Draw_Text_GDI("<A>..............Toggle ambient light source.", 0, text_y+=12, RGB(255,255,255), lpddsback);
  694.     Draw_Text_GDI("<I>..............Toggle infinite light source.", 0, text_y+=12, RGB(255,255,255), lpddsback);
  695.     Draw_Text_GDI("<P>..............Toggle point light source.", 0, text_y+=12, RGB(255,255,255), lpddsback);
  696.     Draw_Text_GDI("<S>..............Toggle spot light source.", 0, text_y+=12, RGB(255,255,255), lpddsback);
  697.     Draw_Text_GDI("<N>..............Next object.", 0, text_y+=12, RGB(255,255,255), lpddsback);
  698.     Draw_Text_GDI("<W>..............Toggle wire frame/solid mode.", 0, text_y+=12, RGB(255,255,255), lpddsback);
  699.     Draw_Text_GDI("<B>..............Toggle backface removal.", 0, text_y+=12, RGB(255,255,255), lpddsback);
  700.     Draw_Text_GDI("<S>..............Toggle Z sorting.", 0, text_y+=12, RGB(255,255,255), lpddsback);
  701.     Draw_Text_GDI("<Z>..............Toggle Z buffering.", 0, text_y+=12, RGB(255,255,255), lpddsback);
  702.     Draw_Text_GDI("<D>..............Toggle Normal 3D display / Z buffer visualization mode.", 0, text_y+=12, RGB(255,255,255), lpddsback);
  703.     Draw_Text_GDI("<H>..............Toggle Help.", 0, text_y+=12, RGB(255,255,255), lpddsback);
  704.     Draw_Text_GDI("<ESC>............Exit demo.", 0, text_y+=12, RGB(255,255,255), lpddsback);
  705.     } // end help
  706. sprintf(work_string,"Polys Rendered: %d, Polys lit: %d", debug_polys_rendered_per_frame, debug_polys_lit_per_frame);
  707. Draw_Text_GDI(work_string, 0, WINDOW_HEIGHT-34-16-16, RGB(0,255,0), lpddsback);
  708. sprintf(work_string,"CAM [%5.2f, %5.2f, %5.2f]",  cam.pos.x, cam.pos.y, cam.pos.z);
  709. Draw_Text_GDI(work_string, 0, WINDOW_HEIGHT-34-16-16-16, RGB(0,255,0), lpddsback);
  710. // flip the surfaces
  711. DDraw_Flip();
  712. // sync to 30ish fps
  713. Wait_Clock(30);
  714. // check of user is trying to exit
  715. if (KEY_DOWN(VK_ESCAPE) || keyboard_state[DIK_ESCAPE])
  716.     {
  717.     PostMessage(main_window_handle, WM_DESTROY,0,0);
  718.     } // end if
  719. // return success
  720. return(1);
  721.  
  722. } // end Game_Main
  723. //////////////////////////////////////////////////////////