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

游戏

开发平台:

Visual C++

  1. // DEMOII9_2_8b.CPP - 3D water molecule demo for gouraud shading, 8-bit version
  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        8    // 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. // read keyboard and other devices here
  372. DInput_Read_Keyboard(); 
  373. // game logic here...
  374.  
  375. // reset the render list
  376. Reset_RENDERLIST4DV2(&rend_list);
  377. // modes and lights
  378. // wireframe mode
  379. if (keyboard_state[DIK_W])
  380.    {
  381.    // toggle wireframe mode
  382.    if (++wireframe_mode > 1)
  383.        wireframe_mode=0;
  384.    Wait_Clock(100); // wait, so keyboard doesn't bounce
  385.    } // end if
  386. // backface removal
  387. if (keyboard_state[DIK_B])
  388.    {
  389.    // toggle backface removal
  390.    backface_mode = -backface_mode;
  391.    Wait_Clock(100); // wait, so keyboard doesn't bounce
  392.    } // end if
  393. // lighting
  394. if (keyboard_state[DIK_L])
  395.    {
  396.    // toggle lighting engine completely
  397.    lighting_mode = -lighting_mode;
  398.    Wait_Clock(100); // wait, so keyboard doesn't bounce
  399.    } // end if
  400. // toggle ambient light
  401. if (keyboard_state[DIK_A])
  402.    {
  403.    // toggle ambient light
  404.    if (lights[AMBIENT_LIGHT_INDEX].state == LIGHTV1_STATE_ON)
  405.       lights[AMBIENT_LIGHT_INDEX].state = LIGHTV1_STATE_OFF;
  406.    else
  407.       lights[AMBIENT_LIGHT_INDEX].state = LIGHTV1_STATE_ON;
  408.    Wait_Clock(100); // wait, so keyboard doesn't bounce
  409.    } // end if
  410. // toggle infinite light
  411. if (keyboard_state[DIK_I])
  412.    {
  413.    // toggle ambient light
  414.    if (lights[INFINITE_LIGHT_INDEX].state == LIGHTV1_STATE_ON)
  415.       lights[INFINITE_LIGHT_INDEX].state = LIGHTV1_STATE_OFF;
  416.    else
  417.       lights[INFINITE_LIGHT_INDEX].state = LIGHTV1_STATE_ON;
  418.    Wait_Clock(100); // wait, so keyboard doesn't bounce
  419.    } // end if
  420. // toggle point light
  421. if (keyboard_state[DIK_P])
  422.    {
  423.    // toggle point light
  424.    if (lights[POINT_LIGHT_INDEX].state == LIGHTV1_STATE_ON)
  425.       lights[POINT_LIGHT_INDEX].state = LIGHTV1_STATE_OFF;
  426.    else
  427.       lights[POINT_LIGHT_INDEX].state = LIGHTV1_STATE_ON;
  428.    Wait_Clock(100); // wait, so keyboard doesn't bounce
  429.    } // end if
  430. // toggle spot light
  431. if (keyboard_state[DIK_S])
  432.    {
  433.    // toggle spot light
  434.    if (lights[SPOT_LIGHT2_INDEX].state == LIGHTV1_STATE_ON)
  435.       lights[SPOT_LIGHT2_INDEX].state = LIGHTV1_STATE_OFF;
  436.    else
  437.       lights[SPOT_LIGHT2_INDEX].state = LIGHTV1_STATE_ON;
  438.    Wait_Clock(100); // wait, so keyboard doesn't bounce
  439.    } // end if
  440. // help menu
  441. if (keyboard_state[DIK_H])
  442.    {
  443.    // toggle help menu 
  444.    help_mode = -help_mode;
  445.    Wait_Clock(100); // wait, so keyboard doesn't bounce
  446.    } // end if
  447. // z-sorting
  448. if (keyboard_state[DIK_Z])
  449.    {
  450.    // toggle z sorting
  451.    zsort_mode = -zsort_mode;
  452.    Wait_Clock(100); // wait, so keyboard doesn't bounce
  453.    } // end if
  454. static float plight_ang = 0, slight_ang = 0; // angles for light motion
  455. // move point light source in ellipse around game world
  456. lights[POINT_LIGHT_INDEX].pos.x = 1000*Fast_Cos(plight_ang);
  457. lights[POINT_LIGHT_INDEX].pos.y = 100;
  458. lights[POINT_LIGHT_INDEX].pos.z = 1000*Fast_Sin(plight_ang);
  459. if ((plight_ang+=3) > 360)
  460.     plight_ang = 0;
  461. // move spot light source in ellipse around game world
  462. lights[SPOT_LIGHT2_INDEX].pos.x = 1000*Fast_Cos(slight_ang);
  463. lights[SPOT_LIGHT2_INDEX].pos.y = 200;
  464. lights[SPOT_LIGHT2_INDEX].pos.z = 1000*Fast_Sin(slight_ang);
  465. if ((slight_ang-=5) < 0)
  466.     slight_ang = 360;
  467. // generate camera matrix
  468. Build_CAM4DV1_Matrix_Euler(&cam, CAM_ROT_SEQ_ZYX);
  469. // use these to rotate objects
  470. static float x_ang = 0, y_ang = 0, z_ang = 0;
  471. //////////////////////////////////////////////////////////////////////////
  472. // constant shaded water
  473. // reset the object (this only matters for backface and object removal)
  474. Reset_OBJECT4DV2(&obj_constant_water);
  475. // set position of constant shaded water
  476. obj_constant_water.world_pos.x = -50;
  477. obj_constant_water.world_pos.y = 0;
  478. obj_constant_water.world_pos.z = 120;
  479. // generate rotation matrix around y axis
  480. Build_XYZ_Rotation_MATRIX4X4(x_ang, y_ang, z_ang, &mrot);
  481. // rotate the local coords of the object
  482. Transform_OBJECT4DV2(&obj_constant_water, &mrot, TRANSFORM_LOCAL_TO_TRANS,1);
  483. // perform world transform
  484. Model_To_World_OBJECT4DV2(&obj_constant_water, TRANSFORM_TRANS_ONLY);
  485. // insert the object into render list
  486. Insert_OBJECT4DV2_RENDERLIST4DV2(&rend_list, &obj_constant_water,0);
  487. //////////////////////////////////////////////////////////////////////////
  488. // flat shaded water
  489. // reset the object (this only matters for backface and object removal)
  490. Reset_OBJECT4DV2(&obj_flat_water);
  491. // set position of constant shaded water
  492. obj_flat_water.world_pos.x = 0;
  493. obj_flat_water.world_pos.y = 0;
  494. obj_flat_water.world_pos.z = 120;
  495. // generate rotation matrix around y axis
  496. Build_XYZ_Rotation_MATRIX4X4(x_ang, y_ang, z_ang, &mrot);
  497. // rotate the local coords of the object
  498. Transform_OBJECT4DV2(&obj_flat_water, &mrot, TRANSFORM_LOCAL_TO_TRANS,1);
  499. // perform world transform
  500. Model_To_World_OBJECT4DV2(&obj_flat_water, TRANSFORM_TRANS_ONLY);
  501. // insert the object into render list
  502. Insert_OBJECT4DV2_RENDERLIST4DV2(&rend_list, &obj_flat_water,0);
  503. //////////////////////////////////////////////////////////////////////////
  504. // gouraud shaded water
  505. // reset the object (this only matters for backface and object removal)
  506. Reset_OBJECT4DV2(&obj_gouraud_water);
  507. // set position of constant shaded water
  508. obj_gouraud_water.world_pos.x = 50;
  509. obj_gouraud_water.world_pos.y = 0;
  510. obj_gouraud_water.world_pos.z = 120;
  511. // generate rotation matrix around y axis
  512. Build_XYZ_Rotation_MATRIX4X4(x_ang, y_ang, z_ang, &mrot);
  513. // rotate the local coords of the object
  514. Transform_OBJECT4DV2(&obj_gouraud_water, &mrot, TRANSFORM_LOCAL_TO_TRANS,1);
  515. // perform world transform
  516. Model_To_World_OBJECT4DV2(&obj_gouraud_water, TRANSFORM_TRANS_ONLY);
  517. // insert the object into render list
  518. Insert_OBJECT4DV2_RENDERLIST4DV2(&rend_list, &obj_gouraud_water,0);
  519. // update rotation angles
  520. if ((x_ang+=1) > 360) x_ang = 0;
  521. if ((y_ang+=2) > 360) y_ang = 0;
  522. if ((z_ang+=3) > 360) z_ang = 0;
  523. // remove backfaces
  524. if (backface_mode==1)
  525.    Remove_Backfaces_RENDERLIST4DV2(&rend_list, &cam);
  526. // light scene all at once 
  527. if (lighting_mode==1)
  528.    Light_RENDERLIST4DV2_World(&rend_list, &cam, lights, 4);
  529. // apply world to camera transform
  530. World_To_Camera_RENDERLIST4DV2(&rend_list, &cam);
  531. // sort the polygon list (hurry up!)
  532. if (zsort_mode == 1)
  533.    Sort_RENDERLIST4DV2(&rend_list,  SORT_POLYLIST_AVGZ);
  534. // apply camera to perspective transformation
  535. Camera_To_Perspective_RENDERLIST4DV2(&rend_list, &cam);
  536. // apply screen transform
  537. Perspective_To_Screen_RENDERLIST4DV2(&rend_list, &cam);
  538. sprintf(work_string,"Lighting [%s]: Ambient=%d, Infinite=%d, Point=%d, Spot=%d | Zsort [%s], BckFceRM [%s]", 
  539.                                                                                  ((lighting_mode == 1) ? "ON" : "OFF"),
  540.                                                                                  lights[AMBIENT_LIGHT_INDEX].state,
  541.                                                                                  lights[INFINITE_LIGHT_INDEX].state, 
  542.                                                                                  lights[POINT_LIGHT_INDEX].state,
  543.                                                                                  lights[SPOT_LIGHT2_INDEX].state,
  544.                                                                                  ((zsort_mode == 1) ? "ON" : "OFF"),
  545.                                                                                  ((backface_mode == 1) ? "ON" : "OFF"));
  546. Draw_Text_GDI(work_string, 0, WINDOW_HEIGHT-34, RGB(0,255,0), lpddsback);
  547. // draw instructions
  548. Draw_Text_GDI("Press ESC to exit. Press <H> for Help.", 0, 0, RGB(0,255,0), lpddsback);
  549. // should we display help
  550. int text_y = 16;
  551. if (help_mode==1)
  552.     {
  553.     // draw help menu
  554.     Draw_Text_GDI("<A>..............Toggle ambient light source.", 0, text_y+=12, RGB(255,255,255), lpddsback);
  555.     Draw_Text_GDI("<I>..............Toggle infinite light source.", 0, text_y+=12, RGB(255,255,255), lpddsback);
  556.     Draw_Text_GDI("<P>..............Toggle point light source.", 0, text_y+=12, RGB(255,255,255), lpddsback);
  557.     Draw_Text_GDI("<S>..............Toggle spot light source.", 0, text_y+=12, RGB(255,255,255), lpddsback);
  558.     Draw_Text_GDI("<W>..............Toggle wire frame/solid mode.", 0, text_y+=12, RGB(255,255,255), lpddsback);
  559.     Draw_Text_GDI("<B>..............Toggle backface removal.", 0, text_y+=12, RGB(255,255,255), lpddsback);
  560.     Draw_Text_GDI("<H>..............Toggle Help.", 0, text_y+=12, RGB(255,255,255), lpddsback);
  561.     Draw_Text_GDI("<ESC>............Exit demo.", 0, text_y+=12, RGB(255,255,255), lpddsback);
  562.     } // end help
  563. // lock the back buffer
  564. DDraw_Lock_Back_Surface();
  565. // reset number of polys rendered
  566. debug_polys_rendered_per_frame = 0;
  567. // render the object
  568. if (wireframe_mode  == 0)
  569.    Draw_RENDERLIST4DV2_Wire(&rend_list, back_buffer, back_lpitch);
  570. else
  571. if (wireframe_mode  == 1)
  572.    Draw_RENDERLIST4DV2_Solid(&rend_list, back_buffer, back_lpitch);
  573. // unlock the back buffer
  574. DDraw_Unlock_Back_Surface();
  575. // flip the surfaces
  576. DDraw_Flip();
  577. // sync to 30ish fps
  578. Wait_Clock(30);
  579. // check of user is trying to exit
  580. if (KEY_DOWN(VK_ESCAPE) || keyboard_state[DIK_ESCAPE])
  581.     {
  582.     PostMessage(main_window_handle, WM_DESTROY,0,0);
  583.     } // end if
  584. // return success
  585. return(1);
  586.  
  587. } // end Game_Main
  588. //////////////////////////////////////////////////////////