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

游戏

开发平台:

Visual C++

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