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

游戏

开发平台:

Visual C++

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