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

游戏

开发平台:

Visual C++

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