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

游戏

开发平台:

Visual C++

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