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

游戏

开发平台:

Visual C++

  1. // DEMOII15_1.CPP - MD2 Animation 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-13.CPP and the headers T3DLIB1-13.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. #include "T3DLIB11.h"
  45. #include "T3DLIB12.h"
  46. #include "T3DLIB13.h"
  47.                                  
  48. // DEFINES ////////////////////////////////////////////////
  49. // defines for windows interface
  50. #define WINDOW_CLASS_NAME "WIN3DCLASS"  // class name
  51. #define WINDOW_TITLE      "T3D Graphics Console Ver 2.0"
  52. #define WINDOW_WIDTH      800  // size of window
  53. #define WINDOW_HEIGHT     600
  54. #define WINDOW_BPP        16    // bitdepth of window (8,16,24 etc.)
  55.                                 // note: if windowed and not
  56.                                 // fullscreen then bitdepth must
  57.                                 // be same as system bitdepth
  58.                                 // also if 8-bit the a pallete
  59.                                 // is created and attached
  60.    
  61. #define WINDOWED_APP      0 // 0 not windowed, 1 windowed
  62. // create some constants for ease of access
  63. #define AMBIENT_LIGHT_INDEX   0 // ambient light index 
  64. #define INFINITE_LIGHT_INDEX  1 // infinite light index
  65. #define POINT_LIGHT_INDEX     2 // point light index
  66. #define POINT_LIGHT2_INDEX    3 // point light index
  67. // terrain defines
  68. #define TERRAIN_WIDTH         4000
  69. #define TERRAIN_HEIGHT        4000
  70. #define TERRAIN_SCALE         700
  71. #define MAX_SPEED             20
  72. #define PITCH_RETURN_RATE (.33)  // how fast the pitch straightens out
  73. #define PITCH_CHANGE_RATE (.02)  // the rate that pitch changes relative to height 
  74. #define CAM_HEIGHT_SCALER (.3)   // percentage cam height changes relative to height
  75. #define VELOCITY_SCALER   (.025) // rate velocity changes with height
  76. #define CAM_DECEL         (.25)  // deceleration/friction
  77. #define NUM_OBJECTS          3   // number of objects per class
  78. #define NUM_LIGHT_OBJECTS    5   // number of light models
  79. // PROTOTYPES /////////////////////////////////////////////
  80. // game console
  81. int Game_Init(void *parms=NULL);
  82. int Game_Shutdown(void *parms=NULL);
  83. int Game_Main(void *parms=NULL);
  84. // GLOBALS ////////////////////////////////////////////////
  85. HWND main_window_handle           = NULL; // save the window handle
  86. HINSTANCE main_instance           = NULL; // save the instance
  87. char buffer[2048];                        // used to print text
  88. // initialize camera position and direction
  89. POINT4D  cam_pos    = {0,500,-400,1};
  90. POINT4D  cam_target = {0,0,0,1};
  91. VECTOR4D cam_dir    = {0,0,0,1};
  92. // all your initialization code goes here...
  93. VECTOR4D vscale={1.0,1.0,1.0,1}, 
  94.          vpos = {0,0,150,1}, 
  95.          vrot = {0,0,0,1};
  96. CAM4DV1         cam;            // the single camera
  97. OBJECT4DV2      obj_terrain;    // the terrain object
  98. OBJECT4DV2_PTR  obj_light;
  99. OBJECT4DV2      obj_light_array[NUM_LIGHT_OBJECTS];
  100. OBJECT4DV2      obj_model;      // this holds the mech 
  101. BITMAP_IMAGE background_bmp;    // holds the background
  102. #define INDEX_RED_LIGHT_INDEX       0
  103. #define INDEX_GREEN_LIGHT_INDEX     1
  104. #define INDEX_BLUE_LIGHT_INDEX      2
  105. #define INDEX_YELLOW_LIGHT_INDEX    3
  106. #define INDEX_WHITE_LIGHT_INDEX     4
  107. // filenames of objects to load
  108. char *object_light_filenames[NUM_LIGHT_OBJECTS] = {
  109.                                             "cube_constant_red_01.cob",
  110.                                             "cube_constant_green_01.cob",  
  111.                                             "cube_constant_blue_01.cob",
  112.                                             "cube_constant_yellow_01.cob",
  113.                                             "cube_constant_white_01.cob",
  114.                                             };
  115. int curr_light_object  = 0;
  116. RENDERLIST4DV2  rend_list;      // the render list
  117. RGBAV1          white,          // general colors 
  118.                 gray, 
  119.                 black, 
  120.                 red, 
  121.                 green, 
  122.                 blue,
  123.                 yellow,
  124.                 orange; 
  125. ZBUFFERV1 zbuffer;          // our little z buffer!
  126. RENDERCONTEXTV1  rc;        // the rendering context;
  127. MD2_CONTAINER obj_md2;      // this holds the md2 object 
  128. // physical model defines play with these to change the feel of the vehicle
  129. float gravity       = -.40;    // general gravity
  130. float vel_y         = 0;       // the y velocity of camera/jeep
  131. float cam_speed     = 0;       // speed of the camera/jeep
  132. float sea_level     = 50;      // sea level of the simulation
  133. float gclearance    = 150;     // clearance from the camera to the ground
  134. float neutral_pitch = 10;      // the neutral pitch of the camera
  135. // sounds
  136. int wind_sound_id = -1;
  137. // game imagery assets
  138. BOB cockpit;               // the cockpit image (if needed)
  139. // FUNCTIONS //////////////////////////////////////////////
  140. LRESULT CALLBACK WindowProc(HWND hwnd, 
  141.     UINT msg, 
  142.                             WPARAM wparam, 
  143.                             LPARAM lparam)
  144. {
  145. // this is the main message handler of the system
  146. PAINTSTRUCT ps;    // used in WM_PAINT
  147. HDC hdc;    // handle to a device context
  148. // what is the message 
  149. switch(msg)
  150. {
  151. case WM_CREATE: 
  152.         {
  153. // do initialization stuff here
  154. return(0);
  155. } break;
  156.     case WM_PAINT:
  157.          {
  158.          // start painting
  159.          hdc = BeginPaint(hwnd,&ps);
  160.          // end painting
  161.          EndPaint(hwnd,&ps);
  162.          return(0);
  163.         } break;
  164. case WM_DESTROY: 
  165. {
  166. // kill the application
  167. PostQuitMessage(0);
  168. return(0);
  169. } break;
  170. default:break;
  171.     } // end switch
  172. // process any messages that we didn't take care of 
  173. return (DefWindowProc(hwnd, msg, wparam, lparam));
  174. } // end WinProc
  175. // WINMAIN ////////////////////////////////////////////////
  176. int WINAPI WinMain( HINSTANCE hinstance,
  177. HINSTANCE hprevinstance,
  178. LPSTR lpcmdline,
  179. int ncmdshow)
  180. {
  181. // this is the winmain function
  182. WNDCLASS winclass; // this will hold the class we create
  183. HWND  hwnd; // generic window handle
  184. MSG  msg; // generic message
  185. HDC      hdc;       // generic dc
  186. PAINTSTRUCT ps;     // generic paintstruct
  187. // first fill in the window class stucture
  188. winclass.style = CS_DBLCLKS | CS_OWNDC | 
  189.                           CS_HREDRAW | CS_VREDRAW;
  190. winclass.lpfnWndProc = WindowProc;
  191. winclass.cbClsExtra = 0;
  192. winclass.cbWndExtra = 0;
  193. winclass.hInstance = hinstance;
  194. winclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
  195. winclass.hCursor = LoadCursor(NULL, IDC_ARROW);
  196. winclass.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
  197. winclass.lpszMenuName = NULL; 
  198. winclass.lpszClassName = WINDOW_CLASS_NAME;
  199. // register the window class
  200. if (!RegisterClass(&winclass))
  201. return(0);
  202. // create the window, note the test to see if WINDOWED_APP is
  203. // true to select the appropriate window flags
  204. if (!(hwnd = CreateWindow(WINDOW_CLASS_NAME, // class
  205.   WINDOW_TITLE,  // title
  206.   (WINDOWED_APP ? (WS_OVERLAPPED | WS_SYSMENU | WS_CAPTION) : (WS_POPUP | WS_VISIBLE)),
  207.     0,0,    // x,y
  208.   WINDOW_WIDTH,  // width
  209.                           WINDOW_HEIGHT, // height
  210.   NULL,    // handle to parent 
  211.   NULL,    // handle to menu
  212.   hinstance,// instance
  213.   NULL))) // creation parms
  214. return(0);
  215. // save the window handle and instance in a global
  216. main_window_handle = hwnd;
  217. main_instance      = hinstance;
  218. // resize the window so that client is really width x height
  219. if (WINDOWED_APP)
  220. {
  221. // now resize the window, so the client area is the actual size requested
  222. // since there may be borders and controls if this is going to be a windowed app
  223. // if the app is not windowed then it won't matter
  224. RECT window_rect = {0,0,WINDOW_WIDTH-1,WINDOW_HEIGHT-1};
  225. // make the call to adjust window_rect
  226. AdjustWindowRectEx(&window_rect,
  227.      GetWindowStyle(main_window_handle),
  228.      GetMenu(main_window_handle) != NULL,  
  229.      GetWindowExStyle(main_window_handle));
  230. // save the global client offsets, they are needed in DDraw_Flip()
  231. window_client_x0 = -window_rect.left;
  232. window_client_y0 = -window_rect.top;
  233. // now resize the window with a call to MoveWindow()
  234. MoveWindow(main_window_handle,
  235.            0,                                    // x position
  236.            0,                                    // y position
  237.            window_rect.right - window_rect.left, // width
  238.            window_rect.bottom - window_rect.top, // height
  239.            FALSE);
  240. // show the window, so there's no garbage on first render
  241. ShowWindow(main_window_handle, SW_SHOW);
  242. } // end if windowed
  243. // perform all game console specific initialization
  244. Game_Init();
  245. // disable CTRL-ALT_DEL, ALT_TAB, comment this line out 
  246. // if it causes your system to crash
  247. SystemParametersInfo(SPI_SCREENSAVERRUNNING, TRUE, NULL, 0);
  248. // enter main event loop
  249. while(1)
  250. {
  251. if (PeekMessage(&msg,NULL,0,0,PM_REMOVE))
  252. // test if this is a quit
  253.         if (msg.message == WM_QUIT)
  254.            break;
  255. // translate any accelerator keys
  256. TranslateMessage(&msg);
  257. // send the message to the window proc
  258. DispatchMessage(&msg);
  259. } // end if
  260.     
  261.     // main game processing goes here
  262.     Game_Main();
  263. } // end while
  264. // shutdown game and release all resources
  265. Game_Shutdown();
  266. // enable CTRL-ALT_DEL, ALT_TAB, comment this line out 
  267. // if it causes your system to crash
  268. SystemParametersInfo(SPI_SCREENSAVERRUNNING, FALSE, NULL, 0);
  269. // return to Windows like this
  270. return(msg.wParam);
  271. } // end WinMain
  272. // T3D II GAME PROGRAMMING CONSOLE FUNCTIONS ////////////////
  273. int Game_Init(void *parms)
  274. {
  275. // this function is where you do all the initialization 
  276. // for your game
  277. int index; // looping var
  278. // start up DirectDraw (replace the parms as you desire)
  279. DDraw_Init2(WINDOW_WIDTH, WINDOW_HEIGHT, WINDOW_BPP, WINDOWED_APP,0);
  280. // initialize directinput
  281. DInput_Init();
  282. // acquire the keyboard 
  283. DInput_Init_Keyboard();
  284. // add calls to acquire other directinput devices here...
  285. // initialize directsound and directmusic
  286. DSound_Init();
  287. DMusic_Init();
  288. // hide the mouse
  289. if (!WINDOWED_APP)
  290.     ShowCursor(FALSE);
  291. // seed random number generator
  292. srand(Start_Clock()); 
  293. Open_Error_File("MD2ERROR.TXT");
  294. // initialize math engine
  295. Build_Sin_Cos_Tables();
  296. // initialize the camera with 90 FOV, normalized coordinates
  297. Init_CAM4DV1(&cam,            // the camera object
  298.              CAM_MODEL_EULER, // the euler model
  299.              &cam_pos,        // initial camera position
  300.              &cam_dir,        // initial camera angles
  301.              &cam_target,     // no target
  302.              10.0,            // near and far clipping planes
  303.              12000.0,
  304.              90.0,            // field of view in degrees
  305.              WINDOW_WIDTH,    // size of final screen viewport
  306.              WINDOW_HEIGHT);
  307. VECTOR4D terrain_pos = {0,0,0,0}; 
  308. Generate_Terrain_OBJECT4DV2(&obj_terrain,            // pointer to object
  309.                             TERRAIN_WIDTH,           // width in world coords on x-axis
  310.                             TERRAIN_HEIGHT,          // height (length) in world coords on z-axis
  311.                             TERRAIN_SCALE,           // vertical scale of terrain
  312.                             "height_grass_40_40_01.bmp",  // filename of height bitmap encoded in 256 colors
  313.                             "stone256_256_01.bmp", // "grass256_256_01.bmp", //"checker2562562.bmp",   // filename of texture map
  314.                              RGB16Bit(255,255,255),  // color of terrain if no texture        
  315.                              &terrain_pos,           // initial position
  316.                              NULL,                   // initial rotations
  317.                              POLY4DV2_ATTR_RGB16  
  318.                              //| POLY4DV2_ATTR_SHADE_MODE_FLAT 
  319.                              | POLY4DV2_ATTR_SHADE_MODE_GOURAUD
  320.                              | POLY4DV2_ATTR_SHADE_MODE_TEXTURE);
  321. // set a scaling vector
  322. VECTOR4D_INITXYZ(&vscale, 20, 20, 20); 
  323. // load all the light objects in
  324. for (int index_obj=0; index_obj < NUM_LIGHT_OBJECTS; index_obj++)
  325.     {
  326.     Load_OBJECT4DV2_COB2(&obj_light_array[index_obj], object_light_filenames[index_obj],  
  327.                         &vscale, &vpos, &vrot, VERTEX_FLAGS_INVERT_WINDING_ORDER 
  328.                                                | VERTEX_FLAGS_TRANSFORM_LOCAL 
  329.                                                | VERTEX_FLAGS_TRANSFORM_LOCAL_WORLD
  330.                                               ,0 );
  331.     } // end for index
  332.    
  333. // set current object
  334. curr_light_object = 0;
  335. obj_light    = &obj_light_array[curr_light_object];
  336. // set up lights
  337. Reset_Lights_LIGHTV2(lights2, MAX_LIGHTS);
  338. // create some working colors
  339. white.rgba   = _RGBA32BIT(255,255,255,0);
  340. gray.rgba    = _RGBA32BIT(100,100,100,0);
  341. black.rgba   = _RGBA32BIT(0,0,0,0);
  342. red.rgba     = _RGBA32BIT(255,0,0,0);
  343. green.rgba   = _RGBA32BIT(0,255,0,0);
  344. blue.rgba    = _RGBA32BIT(0,0,255,0);
  345. orange.rgba  = _RGBA32BIT(255,128,0,0);
  346. yellow.rgba  = _RGBA32BIT(255,255,0,0);
  347. // ambient light
  348. Init_Light_LIGHTV2(lights2,
  349.                    AMBIENT_LIGHT_INDEX,   
  350.                    LIGHTV2_STATE_ON,      // turn the light on
  351.                    LIGHTV2_ATTR_AMBIENT,  // ambient light type
  352.                    gray, black, black,    // color for ambient term only
  353.                    NULL, NULL,            // no need for pos or dir
  354.                    0,0,0,                 // no need for attenuation
  355.                    0,0,0);                // spotlight info NA
  356. VECTOR4D dlight_dir = {-1,1,-1,1};
  357. // directional light
  358. Init_Light_LIGHTV2(lights2,
  359.                    INFINITE_LIGHT_INDEX,  
  360.                    LIGHTV2_STATE_ON,      // turn the light on
  361.                    LIGHTV2_ATTR_INFINITE, // infinite light type
  362.                    black, gray, black,    // color for diffuse term only
  363.                    NULL, &dlight_dir,     // need direction only
  364.                    0,0,0,                 // no need for attenuation
  365.                    0,0,0);                // spotlight info NA
  366. VECTOR4D plight_pos = {0,500,0,1};
  367. // point light
  368. Init_Light_LIGHTV2(lights2,
  369.                    POINT_LIGHT_INDEX,
  370.                    LIGHTV2_STATE_ON,      // turn the light on
  371.                    LIGHTV2_ATTR_POINT,    // pointlight type
  372.                    black, orange, black,   // color for diffuse term only
  373.                    &plight_pos, NULL,     // need pos only
  374.                    0,.001,0,              // linear attenuation only
  375.                    0,0,1);                // spotlight info NA
  376. // point light
  377. Init_Light_LIGHTV2(lights2,
  378.                    POINT_LIGHT2_INDEX,
  379.                    LIGHTV2_STATE_ON,     // turn the light on
  380.                    LIGHTV2_ATTR_POINT,   // pointlight type
  381.                    black, yellow, black,  // color for diffuse term only
  382.                    &plight_pos, NULL,    // need pos only
  383.                    0,.002,0,             // linear attenuation only
  384.                    0,0,1);               // spotlight info NA
  385. VECTOR4D slight2_pos = {0,200,0,1};
  386. VECTOR4D slight2_dir = {-1,1,-1,1};
  387. // create lookup for lighting engine
  388. RGB_16_8_IndexedRGB_Table_Builder(DD_PIXEL_FORMAT565,  // format we want to build table for
  389.                                   palette,             // source palette
  390.                                   rgblookup);          // lookup table
  391. // create the z buffer
  392. Create_Zbuffer(&zbuffer,
  393.                WINDOW_WIDTH,
  394.                WINDOW_HEIGHT,
  395.                ZBUFFER_ATTR_32BIT);
  396.  
  397. // build alpha lookup table
  398. RGB_Alpha_Table_Builder(NUM_ALPHA_LEVELS, rgb_alpha_table);
  399. // load background sounds
  400. wind_sound_id = DSound_Load_WAV("STATIONTHROB.WAV");
  401. // start the sounds
  402. DSound_Play(wind_sound_id, DSBPLAY_LOOPING);
  403. DSound_Set_Volume(wind_sound_id, 100);
  404. #if 0
  405. // load in the cockpit image
  406. Create_BOB(&cockpit, 0,0,800,600,2, BOB_ATTR_VISIBLE | BOB_ATTR_SINGLE_FRAME, DDSCAPS_SYSTEMMEMORY, 0, 16); 
  407. Load_Bitmap_File(&bitmap16bit, "lego02.BMP");
  408. Load_Frame_BOB16(&cockpit, &bitmap16bit,0,0,0,BITMAP_EXTRACT_MODE_ABS);
  409. Unload_Bitmap_File(&bitmap16bit);
  410. Load_Bitmap_File(&bitmap16bit, "lego02b.BMP");
  411. Load_Frame_BOB16(&cockpit, &bitmap16bit,1,0,0,BITMAP_EXTRACT_MODE_ABS);
  412. Unload_Bitmap_File(&bitmap16bit);
  413. #endif
  414. // load background image that scrolls 
  415. Load_Bitmap_File(&bitmap16bit, "sunset800_600_03.bmp");
  416. Create_Bitmap(&background_bmp,0,0,800,600,16);
  417. Load_Image_Bitmap16(&background_bmp, &bitmap16bit,0,0,BITMAP_EXTRACT_MODE_ABS);
  418. Unload_Bitmap_File(&bitmap16bit);
  419. static VECTOR4D vs = {4,4,4,1};
  420. static VECTOR4D vp = {0,0,0,1};
  421. // load the md2 object
  422. Load_Object_MD2(&obj_md2,                            // the loaded md2 file placed in container
  423.      "./md2/q2mdl-tekkblade/tris.md2", //  "D:/Games/quakeII/baseq2/players/male/tris.md2",    // the filename of the .MD2 model
  424.      &vs, 
  425.      &vp,
  426.      NULL,          
  427.      "./md2/q2mdl-tekkblade/blade_black.bmp", //"D:/Games/quakeII/baseq2/players/male/claymore.bmp",   // the texture filename for the model
  428.      POLY4DV2_ATTR_RGB16 | POLY4DV2_ATTR_SHADE_MODE_FLAT | POLY4DV2_ATTR_SHADE_MODE_TEXTURE,
  429.      RGB16Bit(255,255,255),
  430.      VERTEX_FLAGS_SWAP_YZ);                          // control ordering etc.
  431. // prepare OBJECT4DV2 for md2
  432. Prepare_OBJECT4DV2_For_MD2(&obj_model,   // pointer to destination object
  433.                            &obj_md2);    // md2 object to extract frame from
  434. // set the animation
  435. Set_Animation_MD2(&obj_md2,MD2_ANIM_STATE_STANDING_IDLE, MD2_ANIM_LOOP);
  436. #if 0
  437. // play with these for more speed :)
  438. // set single precission
  439. _control87( _PC_24, _MCW_PC );
  440. // set to flush mode
  441. _control87( _DN_FLUSH, _MCW_DN );
  442. // set rounding mode
  443. _control87( _RC_NEAR, _MCW_RC );
  444. #endif
  445. // return success
  446. return(1);
  447. } // end Game_Init
  448. ///////////////////////////////////////////////////////////
  449. int Game_Shutdown(void *parms)
  450. {
  451. // this function is where you shutdown your game and
  452. // release all resources that you allocated
  453. // shut everything down
  454. // release all your resources created for the game here....
  455. // now directsound
  456. DSound_Stop_All_Sounds();
  457. DSound_Delete_All_Sounds();
  458. DSound_Shutdown();
  459. // directmusic
  460. DMusic_Delete_All_MIDI();
  461. DMusic_Shutdown();
  462. // shut down directinput
  463. DInput_Release_Keyboard();
  464. // shutdown directinput
  465. DInput_Shutdown();
  466. // shutdown directdraw last
  467. DDraw_Shutdown();
  468. Close_Error_File();
  469. // return success
  470. return(1);
  471. } // end Game_Shutdown
  472. //////////////////////////////////////////////////////////
  473. int Game_Main(void *parms)
  474. {
  475. // this is the workhorse of your game it will be called
  476. // continuously in real-time this is like main() in C
  477. // all the calls for you game go here!
  478. static MATRIX4X4 mrot;   // general rotation matrix
  479. static float plight_ang = 0, 
  480.              slight_ang = 0; // angles for light motion
  481. // use these to rotate objects
  482. static float x_ang = 0, y_ang = 0, z_ang = 0;
  483. // state variables for different rendering modes and help
  484. static int wireframe_mode   = 1;
  485. static int backface_mode    = 1;
  486. static int lighting_mode    = 1;
  487. static int help_mode        = 1;
  488. static int zsort_mode       = -1;
  489. static int x_clip_mode      = 1;
  490. static int y_clip_mode      = 1;
  491. static int z_clip_mode      = 1;
  492. static float hl = 300, // artificial light height
  493.              ks = 1.25; // generic scaling factor to make things look good
  494. char work_string[256]; // temp string
  495. int index; // looping var
  496. // start the timing clock
  497. Start_Clock();
  498. // clear the drawing surface 
  499. DDraw_Fill_Surface(lpddsback, 0);
  500. // draw the sky
  501. Draw_Rectangle(0,0, WINDOW_WIDTH-1, WINDOW_HEIGHT-1, RGB16Bit(50,50,200), lpddsback);
  502. // draw the ground
  503. //Draw_Rectangle(0,WINDOW_HEIGHT*.38, WINDOW_WIDTH, WINDOW_HEIGHT, RGB16Bit(25,50,110), lpddsback);
  504. // read keyboard and other devices here
  505. DInput_Read_Keyboard();
  506. // game logic here...
  507. // reset the render list
  508. Reset_RENDERLIST4DV2(&rend_list);
  509. // modes and lights
  510. // wireframe mode
  511. if (keyboard_state[DIK_W]) 
  512.    {
  513.    // toggle wireframe mode
  514.    if (++wireframe_mode > 1)
  515.        wireframe_mode=0;
  516.    Wait_Clock(100); // wait, so keyboard doesn't bounce
  517.    } // end if
  518. // backface removal
  519. if (keyboard_state[DIK_B])
  520.    {
  521.    // toggle backface removal
  522.    backface_mode = -backface_mode;
  523.    Wait_Clock(100); // wait, so keyboard doesn't bounce
  524.    } // end if
  525. // lighting
  526. if (keyboard_state[DIK_L])
  527.    {
  528.    // toggle lighting engine completely
  529.    lighting_mode = -lighting_mode;
  530.    Wait_Clock(100); // wait, so keyboard doesn't bounce
  531.    } // end if
  532. // toggle ambient light
  533. if (keyboard_state[DIK_A])
  534.    {
  535.    // toggle ambient light
  536.    if (lights2[AMBIENT_LIGHT_INDEX].state == LIGHTV2_STATE_ON)
  537.       lights2[AMBIENT_LIGHT_INDEX].state = LIGHTV2_STATE_OFF;
  538.    else
  539.       lights2[AMBIENT_LIGHT_INDEX].state = LIGHTV2_STATE_ON;
  540.    Wait_Clock(100); // wait, so keyboard doesn't bounce
  541.    } // end if
  542. // toggle infinite light
  543. if (keyboard_state[DIK_I])
  544.    {
  545.    // toggle ambient light
  546.    if (lights2[INFINITE_LIGHT_INDEX].state == LIGHTV2_STATE_ON)
  547.       lights2[INFINITE_LIGHT_INDEX].state = LIGHTV2_STATE_OFF;
  548.    else
  549.       lights2[INFINITE_LIGHT_INDEX].state = LIGHTV2_STATE_ON;
  550.    Wait_Clock(100); // wait, so keyboard doesn't bounce
  551.    } // end if
  552. // toggle point light
  553. if (keyboard_state[DIK_P])
  554.    {
  555.    // toggle point light
  556.    if (lights2[POINT_LIGHT_INDEX].state == LIGHTV2_STATE_ON)
  557.       lights2[POINT_LIGHT_INDEX].state = LIGHTV2_STATE_OFF;
  558.    else
  559.       lights2[POINT_LIGHT_INDEX].state = LIGHTV2_STATE_ON;
  560.    // toggle point light
  561.    if (lights2[POINT_LIGHT2_INDEX].state == LIGHTV2_STATE_ON)
  562.       lights2[POINT_LIGHT2_INDEX].state = LIGHTV2_STATE_OFF;
  563.    else
  564.       lights2[POINT_LIGHT2_INDEX].state = LIGHTV2_STATE_ON;
  565.    Wait_Clock(100); // wait, so keyboard doesn't bounce
  566.    } // end if
  567. // help menu
  568. if (keyboard_state[DIK_H])
  569.    {
  570.    // toggle help menu 
  571.    help_mode = -help_mode;
  572.    Wait_Clock(100); // wait, so keyboard doesn't bounce
  573.    } // end if
  574. // z-sorting
  575. if (keyboard_state[DIK_Z])
  576.    {
  577.    // toggle z sorting
  578.    zsort_mode = -zsort_mode;
  579.    Wait_Clock(100); // wait, so keyboard doesn't bounce
  580.    } // end if
  581. // next animation
  582. if (keyboard_state[DIK_2])
  583.    {
  584.    if (++obj_md2.anim_state >= NUM_MD2_ANIMATIONS)
  585.       obj_md2.anim_state = 0;  
  586.    Set_Animation_MD2(&obj_md2, obj_md2.anim_state, MD2_ANIM_SINGLE_SHOT);
  587.    Wait_Clock(100); // wait, so keyboard doesn't bounce
  588.    } // end if
  589. // previous animation
  590. if (keyboard_state[DIK_1])
  591.    {
  592.    if (--obj_md2.anim_state < 0)
  593.       obj_md2.anim_state = NUM_MD2_ANIMATIONS-1;  
  594.    Set_Animation_MD2(&obj_md2, obj_md2.anim_state, MD2_ANIM_SINGLE_SHOT);
  595.    Wait_Clock(100); // wait, so keyboard doesn't bounce
  596.    } // end if
  597. // replay animation
  598. if (keyboard_state[DIK_3])
  599.    {
  600.    Set_Animation_MD2(&obj_md2, obj_md2.anim_state, MD2_ANIM_SINGLE_SHOT);
  601.    Wait_Clock(100); // wait, so keyboard doesn't bounce
  602.    } // end if
  603. // replay animation
  604. if (keyboard_state[DIK_4])
  605.    {
  606.    Set_Animation_MD2(&obj_md2, obj_md2.anim_state, MD2_ANIM_LOOP);
  607.    Wait_Clock(100); // wait, so keyboard doesn't bounce
  608.    } // end if
  609. // forward/backward
  610. if (keyboard_state[DIK_UP])
  611.    {
  612.    // move forward
  613.    if ( (cam_speed+=1) > MAX_SPEED) cam_speed = MAX_SPEED;
  614.    } // end if
  615. else
  616. if (keyboard_state[DIK_DOWN])
  617.    {
  618.    // move backward
  619.    if ((cam_speed-=1) < -MAX_SPEED) cam_speed = -MAX_SPEED;
  620.    } // end if
  621. // rotate around y axis or yaw
  622. if (keyboard_state[DIK_RIGHT])
  623.    {
  624.    cam.dir.y+=5;
  625.    // scroll the background
  626.    Scroll_Bitmap(&background_bmp, -10);
  627.    } // end if
  628. if (keyboard_state[DIK_LEFT])
  629.    {
  630.    cam.dir.y-=5;
  631.    // scroll the background
  632.    Scroll_Bitmap(&background_bmp, 10);
  633.    } // end if
  634. // scroll sky slowly
  635. Scroll_Bitmap(&background_bmp, -1);
  636. // motion section /////////////////////////////////////////////////////////
  637. // terrain following, simply find the current cell we are over and then
  638. // index into the vertex list and find the 4 vertices that make up the 
  639. // quad cell we are hovering over and then average the values, and based
  640. // on the current height and the height of the terrain push the player upward
  641. // the terrain generates and stores some results to help with terrain following
  642. //ivar1 = columns;
  643. //ivar2 = rows;
  644. //fvar1 = col_vstep;
  645. //fvar2 = row_vstep;
  646. int cell_x = (cam.pos.x  + TERRAIN_WIDTH/2) / obj_terrain.fvar1;
  647. int cell_y = (cam.pos.z  + TERRAIN_HEIGHT/2) / obj_terrain.fvar1;
  648. static float terrain_height, delta;
  649. // test if we are on terrain
  650. if ( (cell_x >=0) && (cell_x < obj_terrain.ivar1) && (cell_y >=0) && (cell_y < obj_terrain.ivar2) )
  651.    {
  652.    // compute vertex indices into vertex list of the current quad
  653.    int v0 = cell_x + cell_y*obj_terrain.ivar2;
  654.    int v1 = v0 + 1;
  655.    int v2 = v1 + obj_terrain.ivar2;
  656.    int v3 = v0 + obj_terrain.ivar2;   
  657.    // now simply index into table 
  658.    terrain_height = 0.25 * (obj_terrain.vlist_trans[v0].y + obj_terrain.vlist_trans[v1].y +
  659.                             obj_terrain.vlist_trans[v2].y + obj_terrain.vlist_trans[v3].y);
  660.    // compute height difference
  661.    delta = terrain_height - (cam.pos.y - gclearance);
  662.    // test for penetration
  663.    if (delta > 0)
  664.       {
  665.       // apply force immediately to camera (this will give it a springy feel)
  666.       vel_y+=(delta * (VELOCITY_SCALER));
  667.       // test for pentration, if so move up immediately so we don't penetrate geometry
  668.       cam.pos.y+=(delta*CAM_HEIGHT_SCALER);
  669.       // now this is more of a hack than the physics model :) let move the front
  670.       // up and down a bit based on the forward velocity and the gradient of the 
  671.       // hill
  672.       cam.dir.x -= (delta*PITCH_CHANGE_RATE);
  673.  
  674.       } // end if
  675.    } // end if
  676. // decelerate camera
  677. if (cam_speed > (CAM_DECEL) ) cam_speed-=CAM_DECEL;
  678. else
  679. if (cam_speed < (-CAM_DECEL) ) cam_speed+=CAM_DECEL;
  680. else
  681.    cam_speed = 0;
  682. // force camera to seek a stable orientation
  683. if (cam.dir.x > (neutral_pitch+PITCH_RETURN_RATE)) cam.dir.x -= (PITCH_RETURN_RATE);
  684. else
  685. if (cam.dir.x < (neutral_pitch-PITCH_RETURN_RATE)) cam.dir.x += (PITCH_RETURN_RATE);
  686.  else 
  687.    cam.dir.x = neutral_pitch;
  688. // apply gravity
  689. vel_y+=gravity;
  690. // test for absolute sea level and push upward..
  691. if (cam.pos.y < sea_level)
  692.    { 
  693.    vel_y = 0; 
  694.    cam.pos.y = sea_level;
  695.    } // end if
  696. // move camera
  697. cam.pos.x += cam_speed*Fast_Sin(cam.dir.y);
  698. cam.pos.z += cam_speed*Fast_Cos(cam.dir.y);
  699. cam.pos.y += vel_y;
  700. // move point light source in ellipse around game world
  701. lights2[POINT_LIGHT_INDEX].pos.x = 500*Fast_Cos(plight_ang);
  702. //lights2[POINT_LIGHT_INDEX].pos.y = 200;
  703. lights2[POINT_LIGHT_INDEX].pos.z = 500*Fast_Sin(plight_ang);
  704. // move point light source in ellipse around game world
  705. lights2[POINT_LIGHT2_INDEX].pos.x = 200*Fast_Cos(-2*plight_ang);
  706. //lights2[POINT_LIGHT2_INDEX].pos.y = 400;
  707. lights2[POINT_LIGHT2_INDEX].pos.z = 200*Fast_Sin(-2*plight_ang);
  708. if ((plight_ang+=1) > 360)
  709.     plight_ang = 0;
  710. // generate camera matrix
  711. Build_CAM4DV1_Matrix_Euler(&cam, CAM_ROT_SEQ_ZYX);
  712. //////////////////////////////////////////////////////////////////////////
  713. // the terrain
  714. // reset the object (this only matters for backface and object removal)
  715. Reset_OBJECT4DV2(&obj_terrain);
  716. // generate rotation matrix around y axis
  717. //Build_XYZ_Rotation_MATRIX4X4(x_ang, y_ang, z_ang, &mrot);
  718. MAT_IDENTITY_4X4(&mrot); 
  719. // rotate the local coords of the object
  720. Transform_OBJECT4DV2(&obj_terrain, &mrot, TRANSFORM_LOCAL_TO_TRANS,1);
  721. // perform world transform
  722. Model_To_World_OBJECT4DV2(&obj_terrain, TRANSFORM_TRANS_ONLY);
  723. // insert the object into render list
  724. Insert_OBJECT4DV2_RENDERLIST4DV2(&rend_list, &obj_terrain,0);
  725. //////////////////////////////////////////////////////////////////////////
  726. int v0, v1, v2, v3; // used to track vertices
  727. VECTOR4D pl,  // position of the light
  728.          po,  // position of the occluder object/vertex
  729.          vlo, // vector from light to object
  730.          ps;  // position of the shadow
  731. float    rs,  // radius of shadow 
  732.          t;   // parameter t
  733. //////////////////////////////////////////////////////////////////////////
  734. // render model, this next section draws each copy of the mech model
  735. //////////////////////////////////////////////////////////////////////////
  736. // animate the model
  737. Animate_MD2(&obj_md2);
  738. // extract the frame of animation from vertex banks
  739. Extract_MD2_Frame(&obj_model,  // pointer to destination object
  740.                   &obj_md2);   // md2 object to extract frame from
  741. // set position of object 
  742. obj_model.world_pos.x = 0;
  743. obj_model.world_pos.y = 100;
  744. obj_model.world_pos.z = 0;
  745. // reset the object (this only matters for backface and object removal)
  746. Reset_OBJECT4DV2(&obj_model);
  747. // create identity matrix
  748. MAT_IDENTITY_4X4(&mrot);
  749. // transform the local coords of the object
  750. Transform_OBJECT4DV2(&obj_model, &mrot, TRANSFORM_LOCAL_TO_TRANS,1);
  751. // perform world transform
  752. Model_To_World_OBJECT4DV2(&obj_model, TRANSFORM_TRANS_ONLY);
  753. // insert the object into render list
  754. Insert_OBJECT4DV2_RENDERLIST4DV2(&rend_list, &obj_model,0);
  755. // set position of object 
  756. obj_model.world_pos.x = 0;
  757. obj_model.world_pos.y = 100;
  758. obj_model.world_pos.z = 200;
  759. // reset the object (this only matters for backface and object removal)
  760. Reset_OBJECT4DV2(&obj_model);
  761. // create identity matrix
  762. MAT_IDENTITY_4X4(&mrot);
  763. // transform the local coords of the object
  764. Transform_OBJECT4DV2(&obj_model, &mrot, TRANSFORM_LOCAL_TO_TRANS,1);
  765. // perform world transform
  766. Model_To_World_OBJECT4DV2(&obj_model, TRANSFORM_TRANS_ONLY);
  767. // insert the object into render list
  768. Insert_OBJECT4DV2_RENDERLIST4DV2(&rend_list, &obj_model,0);
  769. //////////////////////////////////////////////////////////////////////////
  770. //////////////////////////////////////////////////////////////////////////
  771. // draw all the light objects to represent the position of light sources
  772. // reset the object (this only matters for backface and object removal)
  773. Reset_OBJECT4DV2(&obj_light_array[INDEX_RED_LIGHT_INDEX]);
  774. // set position of object to light
  775. obj_light_array[INDEX_RED_LIGHT_INDEX].world_pos = lights2[POINT_LIGHT_INDEX].pos;
  776. // create identity matrix
  777. MAT_IDENTITY_4X4(&mrot);
  778. // transform the local coords of the object
  779. Transform_OBJECT4DV2(&obj_light_array[INDEX_RED_LIGHT_INDEX], &mrot, TRANSFORM_LOCAL_TO_TRANS,1);
  780. // perform world transform
  781. Model_To_World_OBJECT4DV2(&obj_light_array[INDEX_RED_LIGHT_INDEX], TRANSFORM_TRANS_ONLY);
  782. // insert the object into render list
  783. Insert_OBJECT4DV2_RENDERLIST4DV2(&rend_list, &obj_light_array[INDEX_RED_LIGHT_INDEX],0);
  784. // reset the object (this only matters for backface and object removal)
  785. Reset_OBJECT4DV2(&obj_light_array[INDEX_YELLOW_LIGHT_INDEX]);
  786. // set position of object to light
  787. obj_light_array[INDEX_YELLOW_LIGHT_INDEX].world_pos = lights2[POINT_LIGHT2_INDEX].pos;
  788. // create identity matrix
  789. MAT_IDENTITY_4X4(&mrot);
  790. // transform the local coords of the object
  791. Transform_OBJECT4DV2(&obj_light_array[INDEX_YELLOW_LIGHT_INDEX], &mrot, TRANSFORM_LOCAL_TO_TRANS,1);
  792. // perform world transform
  793. Model_To_World_OBJECT4DV2(&obj_light_array[INDEX_YELLOW_LIGHT_INDEX], TRANSFORM_TRANS_ONLY);
  794. // insert the object into render list
  795. Insert_OBJECT4DV2_RENDERLIST4DV2(&rend_list, &obj_light_array[INDEX_YELLOW_LIGHT_INDEX],0);
  796. ////////////////////////////////////////////////////////////////////////////////////
  797. // reset number of polys rendered
  798. debug_polys_rendered_per_frame = 0;
  799. debug_polys_lit_per_frame = 0;
  800. // perform rendering pass one
  801. // remove backfaces
  802. if (backface_mode==1)
  803.    Remove_Backfaces_RENDERLIST4DV2(&rend_list, &cam);
  804. // apply world to camera transform
  805. World_To_Camera_RENDERLIST4DV2(&rend_list, &cam);
  806. // clip the polygons themselves now
  807. Clip_Polys_RENDERLIST4DV2(&rend_list, &cam, CLIP_POLY_X_PLANE | CLIP_POLY_Y_PLANE | CLIP_POLY_Z_PLANE );
  808. // light scene all at once 
  809. if (lighting_mode==1)
  810.    {
  811.    Transform_LIGHTSV2(lights2, 4, &cam.mcam, TRANSFORM_LOCAL_TO_TRANS);
  812.    Light_RENDERLIST4DV2_World2_16(&rend_list, &cam, lights2, 4);
  813.    } // end if
  814. // sort the polygon list (hurry up!)
  815. if (zsort_mode == 1)
  816.    Sort_RENDERLIST4DV2(&rend_list,  SORT_POLYLIST_AVGZ);
  817. // apply camera to perspective transformation
  818. Camera_To_Perspective_RENDERLIST4DV2(&rend_list, &cam);
  819. // apply screen transform
  820. Perspective_To_Screen_RENDERLIST4DV2(&rend_list, &cam);
  821. // lock the back buffer
  822. DDraw_Lock_Back_Surface();
  823. // draw background
  824. Draw_Bitmap16(&background_bmp, back_buffer, back_lpitch,0);
  825. // reset number of polys rendered
  826. debug_polys_rendered_per_frame = 0;
  827. // render the object
  828. if (wireframe_mode  == 0)
  829.    Draw_RENDERLIST4DV2_Wire16(&rend_list, back_buffer, back_lpitch);
  830. else
  831. if (wireframe_mode  == 1)
  832.    {
  833.    // perspective mode affine texturing
  834.       // set up rendering context
  835.       rc.attr =    RENDER_ATTR_ZBUFFER  
  836.               // | RENDER_ATTR_ALPHA  
  837.               // | RENDER_ATTR_MIPMAP  
  838.               // | RENDER_ATTR_BILERP
  839.                  | RENDER_ATTR_TEXTURE_PERSPECTIVE_AFFINE;
  840.    // initialize zbuffer to 0 fixed point
  841.    Clear_Zbuffer(&zbuffer, (16000 << FIXP16_SHIFT));
  842.    // set up remainder of rendering context
  843.    rc.video_buffer   = back_buffer;
  844.    rc.lpitch         = back_lpitch;
  845.    rc.mip_dist       = 0;
  846.    rc.zbuffer        = (UCHAR *)zbuffer.zbuffer;
  847.    rc.zpitch         = WINDOW_WIDTH*4;
  848.    rc.rend_list      = &rend_list;
  849.    rc.texture_dist   = 0;
  850.    rc.alpha_override = -1;
  851.    // render scene
  852.    Draw_RENDERLIST4DV2_RENDERCONTEXTV1_16_2(&rc);
  853.    } // end if
  854. // now make second rendering pass and draw shadow
  855. // reset the render list
  856. Reset_RENDERLIST4DV2(&rend_list);
  857. //////////////////////////////////////////////////////////////////////////
  858. // project shaded object into shadow by projecting it's vertices onto
  859. // the ground plane
  860. // reset the object (this only matters for backface and object removal)
  861. Reset_OBJECT4DV2(&obj_model);
  862. // save the shading attributes/color of each polygon, and override them with
  863. // attributes of a shadow then restore them
  864. int pcolor[OBJECT4DV2_MAX_POLYS],    // used to store color
  865.     pattr[OBJECT4DV2_MAX_POLYS];     // used to store attribute
  866. // save all the color and attributes for each polygon
  867. for (int pindex = 0; pindex < obj_model.num_polys; pindex++)
  868.     {
  869.     // save attribute and color
  870.     pattr[pindex]  = obj_model.plist[pindex].attr;
  871.     pcolor[pindex] = obj_model.plist[pindex].color;
  872.   
  873.     // set attributes for shadow rendering
  874.     obj_model.plist[pindex].attr    = POLY4DV2_ATTR_RGB16 | POLY4DV2_ATTR_SHADE_MODE_CONSTANT | POLY4DV2_ATTR_TRANSPARENT;
  875.     obj_model.plist[pindex].color   = RGB16Bit(50,50,50) + (7 << 24);
  876.     } // end for pindex
  877. // create identity matrix
  878. MAT_IDENTITY_4X4(&mrot);
  879. // solve for t when the projected vertex intersects ground plane
  880. pl = lights2[POINT_LIGHT_INDEX].pos;
  881. // transform each local/model vertex of the object mesh and store result
  882. // in "transformed" vertex list, note 
  883. for (int vertex=0; vertex < obj_model.num_vertices; vertex++)
  884.     {
  885.     POINT4D presult; // hold result of each transformation
  886.     // compute parameter t0 when projected ray pierces y=0 plane
  887.     VECTOR4D vi;
  888.     // set position of object 
  889.     obj_model.world_pos.x = 0;
  890.     obj_model.world_pos.y = 100;
  891.     obj_model.world_pos.z = 0;
  892.     // transform coordinates to worldspace right now...
  893.     VECTOR4D_Add(&obj_model.vlist_local[vertex].v, &obj_model.world_pos, &vi);
  894.     float t0 = -pl.y / (vi.y - pl.y);
  895.     // transform point
  896.     obj_model.vlist_trans[vertex].v.x = pl.x + t0*(vi.x - pl.x);
  897.     obj_model.vlist_trans[vertex].v.y = 10.0; // pl.y + t0*(vi.y - pl.y);
  898.     obj_model.vlist_trans[vertex].v.z = pl.z + t0*(vi.z - pl.z);
  899.     obj_model.vlist_trans[vertex].v.w = 1.0;
  900.     } // end for index
  901. // insert the object into render list
  902. Insert_OBJECT4DV2_RENDERLIST4DV2(&rend_list, &obj_model,0);
  903. // and now second shadow object from second light source...
  904. // solve for t when the projected vertex intersects
  905. pl = lights2[POINT_LIGHT_INDEX].pos; 
  906. // transform each local/model vertex of the object mesh and store result
  907. // in "transformed" vertex list
  908. for (vertex=0; vertex < obj_model.num_vertices; vertex++)
  909.     {
  910.     POINT4D presult; // hold result of each transformation
  911.     // compute parameter t0 when projected ray pierces y=0 plane
  912.     VECTOR4D vi;
  913.     // set position of object 
  914.     obj_model.world_pos.x = 0;
  915.     obj_model.world_pos.y = 100;
  916.     obj_model.world_pos.z = 200;
  917.     // transform coordinates to worldspace right now...
  918.     VECTOR4D_Add(&obj_model.vlist_local[vertex].v, &obj_model.world_pos, &vi);
  919.     float t0 = -pl.y / (vi.y - pl.y);
  920.     // transform point
  921.     obj_model.vlist_trans[vertex].v.x = pl.x + t0*(vi.x - pl.x);
  922.     obj_model.vlist_trans[vertex].v.y = 10.0; // pl.y + t0*(vi.y - pl.y);
  923.     obj_model.vlist_trans[vertex].v.z = pl.z + t0*(vi.z - pl.z);
  924.     obj_model.vlist_trans[vertex].v.w = 1.0;
  925.     } // end for index
  926. // insert the object into render list
  927. Insert_OBJECT4DV2_RENDERLIST4DV2(&rend_list, &obj_model,0);
  928. // restore attributes and color
  929. for (pindex = 0; pindex < obj_model.num_polys; pindex++)
  930.     {
  931.     // save attribute and color
  932.     obj_model.plist[pindex].attr  = pattr[pindex];
  933.     obj_model.plist[pindex].color = pcolor[pindex]; 
  934.   
  935.     } // end for pindex
  936. //////////////////////////////////////////////////////////////////////////
  937. // remove backfaces
  938. if (backface_mode==1)
  939.    Remove_Backfaces_RENDERLIST4DV2(&rend_list, &cam);
  940. // apply world to camera transform
  941. World_To_Camera_RENDERLIST4DV2(&rend_list, &cam);
  942. // clip the polygons themselves now
  943. Clip_Polys_RENDERLIST4DV2(&rend_list, &cam, CLIP_POLY_X_PLANE | CLIP_POLY_Y_PLANE | CLIP_POLY_Z_PLANE );
  944. // light scene all at once 
  945. if (lighting_mode==1)
  946.    {
  947.    Transform_LIGHTSV2(lights2, 4, &cam.mcam, TRANSFORM_LOCAL_TO_TRANS);
  948.    Light_RENDERLIST4DV2_World2_16(&rend_list, &cam, lights2, 4);
  949.    } // end if
  950. // sort the polygon list (hurry up!)
  951. if (zsort_mode == 1)
  952.    Sort_RENDERLIST4DV2(&rend_list,  SORT_POLYLIST_AVGZ);
  953. // apply camera to perspective transformation
  954. Camera_To_Perspective_RENDERLIST4DV2(&rend_list, &cam);
  955. // apply screen transform
  956. Perspective_To_Screen_RENDERLIST4DV2(&rend_list, &cam);
  957. // render the object
  958. if (wireframe_mode  == 0)
  959.    Draw_RENDERLIST4DV2_Wire16(&rend_list, back_buffer, back_lpitch);
  960. else
  961. if (wireframe_mode  == 1)
  962.    {
  963.    // perspective mode affine texturing
  964.    // set up rendering context
  965.    rc.attr =    RENDER_ATTR_ZBUFFER  
  966.                 | RENDER_ATTR_ALPHA  
  967.              // | RENDER_ATTR_MIPMAP  
  968.              // | RENDER_ATTR_BILERP
  969.                 | RENDER_ATTR_TEXTURE_PERSPECTIVE_AFFINE;
  970.    // initialize zbuffer to 0 fixed point
  971.    //Clear_Zbuffer(&zbuffer, (16000 << FIXP16_SHIFT));
  972.    // set up remainder of rendering context
  973.    rc.video_buffer   = back_buffer;
  974.    rc.lpitch         = back_lpitch;
  975.    rc.mip_dist       = 0;
  976.    rc.zbuffer        = (UCHAR *)zbuffer.zbuffer;
  977.    rc.zpitch         = WINDOW_WIDTH*4;
  978.    rc.rend_list      = &rend_list;
  979.    rc.texture_dist   = 0;
  980.    rc.alpha_override = -1;
  981.    // render scene
  982.    Draw_RENDERLIST4DV2_RENDERCONTEXTV1_16_3(&rc);
  983.    } // end if
  984. // unlock the back buffer
  985. DDraw_Unlock_Back_Surface();
  986. // draw cockpit
  987. //Draw_BOB16(&cockpit, lpddsback);
  988. // draw instructions
  989. Draw_Text_GDI("Press ESC to exit. Press <H> for Help.", 0, 0, RGB(255,255,255), lpddsback);
  990. // should we display help
  991. int text_y = 16;
  992. if (help_mode==1)
  993.     {
  994.     // draw help menu
  995.     Draw_Text_GDI("<A>..............Toggle ambient light source.", 0, text_y+=12, RGB(255,255,255), lpddsback);
  996.     Draw_Text_GDI("<I>..............Toggle infinite light source.", 0, text_y+=12, RGB(255,255,255), lpddsback);
  997.     Draw_Text_GDI("<P>..............Toggle point light source.", 0, text_y+=12, RGB(255,255,255), lpddsback);
  998.     Draw_Text_GDI("<W>..............Toggle wire frame/solid mode.", 0, text_y+=12, RGB(255,255,255), lpddsback);
  999.     Draw_Text_GDI("<B>..............Toggle backface removal.", 0, text_y+=12, RGB(255,255,255), lpddsback);
  1000.     Draw_Text_GDI("<Z>..............Toggle Z-sorting.", 0, text_y+=12, RGB(255,255,255), lpddsback);
  1001.     Draw_Text_GDI("<1>,<2>..........Previous/Next Animation.", 0, text_y+=12, RGB(255,255,255), lpddsback);
  1002.     Draw_Text_GDI("<3>,<4>..........Play Animation Single Shot/Looped.", 0, text_y+=12, RGB(255,255,255), lpddsback);
  1003.     Draw_Text_GDI("<H>..............Toggle Help.", 0, text_y+=12, RGB(255,255,255), lpddsback);
  1004.     Draw_Text_GDI("<ESC>............Exit demo.", 0, text_y+=12, RGB(255,255,255), lpddsback);
  1005.     } // end help
  1006. sprintf(work_string,"Lighting [%s]: Ambient=%d, Infinite=%d, Point=%d, BckFceRM [%s], Zsort[%s]", 
  1007.                                                                                  ((lighting_mode == 1) ? "ON" : "OFF"),
  1008.                                                                                  lights2[AMBIENT_LIGHT_INDEX].state,
  1009.                                                                                  lights2[INFINITE_LIGHT_INDEX].state, 
  1010.                                                                                  lights2[POINT_LIGHT_INDEX].state,
  1011.                                                                                  ((backface_mode == 1) ? "ON" : "OFF"),
  1012.                                                                                  ((zsort_mode == 1) ? "ON" : "OFF") );
  1013. Draw_Text_GDI(work_string, 0+1, WINDOW_HEIGHT-34+1, RGB(0,0,0), lpddsback);
  1014. Draw_Text_GDI(work_string, 0, WINDOW_HEIGHT-34, RGB(255,255,255), lpddsback);
  1015. sprintf(work_string,"Polys Rendered: %d, Polys lit: %d Anim[%d]=%s Frm=%d", debug_polys_rendered_per_frame, debug_polys_lit_per_frame, obj_md2.anim_state,md2_anim_strings[obj_md2.anim_state], obj_md2.curr_frame );
  1016. Draw_Text_GDI(work_string, 0+1, WINDOW_HEIGHT-34-2*16+1, RGB(0,0,0), lpddsback);
  1017. Draw_Text_GDI(work_string, 0, WINDOW_HEIGHT-34-2*16, RGB(255,255,255), lpddsback);
  1018. sprintf(work_string,"CAM [%5.2f, %5.2f, %5.2f], CELL [%d, %d]",  cam.pos.x, cam.pos.y, cam.pos.z, cell_x, cell_y);
  1019. Draw_Text_GDI(work_string, 0+1, WINDOW_HEIGHT-34-3*16+1, RGB(0,0,0), lpddsback);
  1020. Draw_Text_GDI(work_string, 0, WINDOW_HEIGHT-34-3*16, RGB(255,255,255), lpddsback);
  1021. // flip the surfaces
  1022. DDraw_Flip2();
  1023. // sync to 30ish fps
  1024. Wait_Clock(30);
  1025. // check of user is trying to exit
  1026. if (KEY_DOWN(VK_ESCAPE) || keyboard_state[DIK_ESCAPE])
  1027.     {
  1028.     PostMessage(main_window_handle, WM_DESTROY,0,0);
  1029.     } // end if
  1030. // return success
  1031. return(1);
  1032.   
  1033. } // end Game_Main
  1034. //////////////////////////////////////////////////////////