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

游戏

开发平台:

Visual C++

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