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

游戏

开发平台:

Visual C++

  1. // DEMOII12_7.CPP - perspective texturing demo with support for linear piecewise
  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-10.CPP and the headers T3DLIB1-10.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. // DEFINES ////////////////////////////////////////////////
  45. // defines for windows interface
  46. #define WINDOW_CLASS_NAME "WIN3DCLASS"  // class name
  47. #define WINDOW_TITLE      "T3D Graphics Console Ver 2.0"
  48. #define WINDOW_WIDTH      800  // size of window
  49. #define WINDOW_HEIGHT     600
  50. #define WINDOW_BPP        16    // bitdepth of window (8,16,24 etc.)
  51.                                 // note: if windowed and not
  52.                                 // fullscreen then bitdepth must
  53.                                 // be same as system bitdepth
  54.                                 // also if 8-bit the a pallete
  55.                                 // is created and attached
  56.    
  57. #define WINDOWED_APP      0 // 0 not windowed, 1 windowed
  58. // create some constants for ease of access
  59. #define AMBIENT_LIGHT_INDEX   0 // ambient light index
  60. #define INFINITE_LIGHT_INDEX  1 // infinite light index
  61. #define POINT_LIGHT_INDEX     2 // point light index
  62. #define POINT_LIGHT2_INDEX    3 // point light index
  63. // terrain defines
  64. #define TERRAIN_WIDTH         4000
  65. #define TERRAIN_HEIGHT        4000
  66. #define TERRAIN_SCALE         700
  67. #define MAX_SPEED             20
  68. #define PITCH_RETURN_RATE (.33) // how fast the pitch straightens out
  69. #define PITCH_CHANGE_RATE (.02) // the rate that pitch changes relative to height
  70. #define CAM_HEIGHT_SCALER (.3)  // percentage cam height changes relative to height
  71. #define VELOCITY_SCALER (.025)  // rate velocity changes with height
  72. #define CAM_DECEL       (.25)   // deceleration/friction
  73. // PROTOTYPES /////////////////////////////////////////////
  74. // game console
  75. int Game_Init(void *parms=NULL);
  76. int Game_Shutdown(void *parms=NULL);
  77. int Game_Main(void *parms=NULL);
  78. // GLOBALS ////////////////////////////////////////////////
  79. HWND main_window_handle           = NULL; // save the window handle
  80. HINSTANCE main_instance           = NULL; // save the instance
  81. char buffer[2048];                        // used to print text
  82. // initialize camera position and direction
  83. POINT4D  cam_pos    = {0,500,-200,1};
  84. POINT4D  cam_target = {0,0,0,1};
  85. VECTOR4D cam_dir    = {0,0,0,1};
  86. // all your initialization code goes here...
  87. VECTOR4D vscale={1.0,1.0,1.0,1}, 
  88.          vpos = {0,0,150,1}, 
  89.          vrot = {0,0,0,1};
  90. CAM4DV1         cam;            // the single camera
  91. OBJECT4DV2      obj_terrain;    // the terrain object
  92. RENDERLIST4DV2  rend_list;      // the render list
  93. RGBAV1          white,          // general colors 
  94.                 gray, 
  95.                 black, 
  96.                 red, 
  97.                 green, 
  98.                 blue,
  99.                 yellow,
  100.                 orange; 
  101. ZBUFFERV1 zbuffer;   // our little z buffer!
  102. RENDERCONTEXTV1  rc; // the rendering context;
  103. // physical model defines play with these to change the feel of the vehicle
  104. float gravity    = -.40;    // general gravity
  105. float vel_y      = 0;       // the y velocity of camera/jeep
  106. float cam_speed  = 0;       // speed of the camera/jeep
  107. float sea_level  = 50;      // sea level of the simulation
  108. float gclearance = 75;      // clearance from the camera to the ground
  109. float neutral_pitch = 10;   // the neutral pitch of the camera
  110. // sounds
  111. int wind_sound_id = -1;
  112. // game imagery assets
  113. BOB cockpit;               // the cockpit image
  114. // FUNCTIONS //////////////////////////////////////////////
  115. LRESULT CALLBACK WindowProc(HWND hwnd, 
  116.     UINT msg, 
  117.                             WPARAM wparam, 
  118.                             LPARAM lparam)
  119. {
  120. // this is the main message handler of the system
  121. PAINTSTRUCT ps;    // used in WM_PAINT
  122. HDC hdc;    // handle to a device context
  123. // what is the message 
  124. switch(msg)
  125. {
  126. case WM_CREATE: 
  127.         {
  128. // do initialization stuff here
  129. return(0);
  130. } break;
  131.     case WM_PAINT:
  132.          {
  133.          // start painting
  134.          hdc = BeginPaint(hwnd,&ps);
  135.          // end painting
  136.          EndPaint(hwnd,&ps);
  137.          return(0);
  138.         } break;
  139. case WM_DESTROY: 
  140. {
  141. // kill the application
  142. PostQuitMessage(0);
  143. return(0);
  144. } break;
  145. default:break;
  146.     } // end switch
  147. // process any messages that we didn't take care of 
  148. return (DefWindowProc(hwnd, msg, wparam, lparam));
  149. } // end WinProc
  150. // WINMAIN ////////////////////////////////////////////////
  151. int WINAPI WinMain( HINSTANCE hinstance,
  152. HINSTANCE hprevinstance,
  153. LPSTR lpcmdline,
  154. int ncmdshow)
  155. {
  156. // this is the winmain function
  157. WNDCLASS winclass; // this will hold the class we create
  158. HWND  hwnd; // generic window handle
  159. MSG  msg; // generic message
  160. HDC      hdc;       // generic dc
  161. PAINTSTRUCT ps;     // generic paintstruct
  162. // first fill in the window class stucture
  163. winclass.style = CS_DBLCLKS | CS_OWNDC | 
  164.                           CS_HREDRAW | CS_VREDRAW;
  165. winclass.lpfnWndProc = WindowProc;
  166. winclass.cbClsExtra = 0;
  167. winclass.cbWndExtra = 0;
  168. winclass.hInstance = hinstance;
  169. winclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
  170. winclass.hCursor = LoadCursor(NULL, IDC_ARROW);
  171. winclass.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
  172. winclass.lpszMenuName = NULL; 
  173. winclass.lpszClassName = WINDOW_CLASS_NAME;
  174. // register the window class
  175. if (!RegisterClass(&winclass))
  176. return(0);
  177. // create the window, note the test to see if WINDOWED_APP is
  178. // true to select the appropriate window flags
  179. if (!(hwnd = CreateWindow(WINDOW_CLASS_NAME, // class
  180.   WINDOW_TITLE,  // title
  181.   (WINDOWED_APP ? (WS_OVERLAPPED | WS_SYSMENU | WS_CAPTION) : (WS_POPUP | WS_VISIBLE)),
  182.     0,0,    // x,y
  183.   WINDOW_WIDTH,  // width
  184.                           WINDOW_HEIGHT, // height
  185.   NULL,    // handle to parent 
  186.   NULL,    // handle to menu
  187.   hinstance,// instance
  188.   NULL))) // creation parms
  189. return(0);
  190. // save the window handle and instance in a global
  191. main_window_handle = hwnd;
  192. main_instance      = hinstance;
  193. // resize the window so that client is really width x height
  194. if (WINDOWED_APP)
  195. {
  196. // now resize the window, so the client area is the actual size requested
  197. // since there may be borders and controls if this is going to be a windowed app
  198. // if the app is not windowed then it won't matter
  199. RECT window_rect = {0,0,WINDOW_WIDTH-1,WINDOW_HEIGHT-1};
  200. // make the call to adjust window_rect
  201. AdjustWindowRectEx(&window_rect,
  202.      GetWindowStyle(main_window_handle),
  203.      GetMenu(main_window_handle) != NULL,  
  204.      GetWindowExStyle(main_window_handle));
  205. // save the global client offsets, they are needed in DDraw_Flip()
  206. window_client_x0 = -window_rect.left;
  207. window_client_y0 = -window_rect.top;
  208. // now resize the window with a call to MoveWindow()
  209. MoveWindow(main_window_handle,
  210.            0,                                    // x position
  211.            0,                                    // y position
  212.            window_rect.right - window_rect.left, // width
  213.            window_rect.bottom - window_rect.top, // height
  214.            FALSE);
  215. // show the window, so there's no garbage on first render
  216. ShowWindow(main_window_handle, SW_SHOW);
  217. } // end if windowed
  218. // perform all game console specific initialization
  219. Game_Init();
  220. // disable CTRL-ALT_DEL, ALT_TAB, comment this line out 
  221. // if it causes your system to crash
  222. SystemParametersInfo(SPI_SCREENSAVERRUNNING, TRUE, NULL, 0);
  223. // enter main event loop
  224. while(1)
  225. {
  226. if (PeekMessage(&msg,NULL,0,0,PM_REMOVE))
  227. // test if this is a quit
  228.         if (msg.message == WM_QUIT)
  229.            break;
  230. // translate any accelerator keys
  231. TranslateMessage(&msg);
  232. // send the message to the window proc
  233. DispatchMessage(&msg);
  234. } // end if
  235.     
  236.     // main game processing goes here
  237.     Game_Main();
  238. } // end while
  239. // shutdown game and release all resources
  240. Game_Shutdown();
  241. // enable CTRL-ALT_DEL, ALT_TAB, comment this line out 
  242. // if it causes your system to crash
  243. SystemParametersInfo(SPI_SCREENSAVERRUNNING, FALSE, NULL, 0);
  244. // return to Windows like this
  245. return(msg.wParam);
  246. } // end WinMain
  247. // T3D II GAME PROGRAMMING CONSOLE FUNCTIONS ////////////////
  248. int Game_Init(void *parms)
  249. {
  250. // this function is where you do all the initialization 
  251. // for your game
  252. int index; // looping var
  253. // start up DirectDraw (replace the parms as you desire)
  254. DDraw_Init(WINDOW_WIDTH, WINDOW_HEIGHT, WINDOW_BPP, WINDOWED_APP);
  255. // initialize directinput
  256. DInput_Init();
  257. // acquire the keyboard 
  258. DInput_Init_Keyboard();
  259. // add calls to acquire other directinput devices here...
  260. // initialize directsound and directmusic
  261. DSound_Init();
  262. DMusic_Init();
  263. // hide the mouse
  264. if (!WINDOWED_APP)
  265.     ShowCursor(FALSE);
  266. // seed random number generator
  267. srand(Start_Clock()); 
  268. Open_Error_File("ERROR.TXT");
  269. // initialize math engine
  270. Build_Sin_Cos_Tables();
  271. // initialize the camera with 90 FOV, normalized coordinates
  272. Init_CAM4DV1(&cam,            // the camera object
  273.              CAM_MODEL_EULER, // the euler model
  274.              &cam_pos,        // initial camera position
  275.              &cam_dir,        // initial camera angles
  276.              &cam_target,     // no target
  277.              10.0,            // near and far clipping planes
  278.              12000.0,
  279.              90.0,            // field of view in degrees
  280.              WINDOW_WIDTH,    // size of final screen viewport
  281.              WINDOW_HEIGHT);
  282. VECTOR4D terrain_pos = {0,0,0,0}; 
  283. Generate_Terrain_OBJECT4DV2(&obj_terrain,            // pointer to object
  284.                             TERRAIN_WIDTH,           // width in world coords on x-axis
  285.                             TERRAIN_HEIGHT,          // height (length) in world coords on z-axis
  286.                             TERRAIN_SCALE,           // vertical scale of terrain
  287.                             "checkerheight03.bmp",  // filename of height bitmap encoded in 256 colors
  288.                             "checker2562562.bmp",   // filename of texture map
  289.                              RGB16Bit(255,255,255),  // color of terrain if no texture        
  290.                              &terrain_pos,           // initial position
  291.                              NULL,                   // initial rotations
  292.                              POLY4DV2_ATTR_RGB16  
  293.                              | POLY4DV2_ATTR_SHADE_MODE_FLAT 
  294.                              // | POLY4DV2_ATTR_SHADE_MODE_GOURAUD
  295.                              | POLY4DV2_ATTR_SHADE_MODE_TEXTURE);
  296. // the shading attributes we would like
  297. // set up lights
  298. Reset_Lights_LIGHTV2(lights2, MAX_LIGHTS);
  299. // create some working colors
  300. white.rgba = _RGBA32BIT(255,255,255,0);
  301. gray.rgba  = _RGBA32BIT(100,100,100,0);
  302. black.rgba = _RGBA32BIT(0,0,0,0);
  303. red.rgba   = _RGBA32BIT(255,0,0,0);
  304. green.rgba = _RGBA32BIT(0,255,0,0);
  305. blue.rgba  = _RGBA32BIT(0,0,255,0);
  306. orange.rgba = _RGBA32BIT(255,128,0,0);
  307. yellow.rgba  = _RGBA32BIT(255,255,0,0);
  308. // ambient light
  309. Init_Light_LIGHTV2(lights2,
  310.                    AMBIENT_LIGHT_INDEX,   
  311.                    LIGHTV2_STATE_ON,      // turn the light on
  312.                    LIGHTV2_ATTR_AMBIENT,  // ambient light type
  313.                    gray, black, black,    // color for ambient term only
  314.                    NULL, NULL,            // no need for pos or dir
  315.                    0,0,0,                 // no need for attenuation
  316.                    0,0,0);                // spotlight info NA
  317. VECTOR4D dlight_dir = {-1,1,-1,1};
  318. // directional light
  319. Init_Light_LIGHTV2(lights2,
  320.                    INFINITE_LIGHT_INDEX,  
  321.                    LIGHTV2_STATE_ON,      // turn the light on
  322.                    LIGHTV2_ATTR_INFINITE, // infinite light type
  323.                    black, gray, black,    // color for diffuse term only
  324.                    NULL, &dlight_dir,     // need direction only
  325.                    0,0,0,                 // no need for attenuation
  326.                    0,0,0);                // spotlight info NA
  327. VECTOR4D plight_pos = {0,200,0,1};
  328. // point light
  329. Init_Light_LIGHTV2(lights2,
  330.                    POINT_LIGHT_INDEX,
  331.                    LIGHTV2_STATE_ON,      // turn the light on
  332.                    LIGHTV2_ATTR_POINT,    // pointlight type
  333.                    black, yellow, black,   // color for diffuse term only
  334.                    &plight_pos, NULL,     // need pos only
  335.                    0,.001,0,              // linear attenuation only
  336.                    0,0,1);                // spotlight info NA
  337. // point light
  338. Init_Light_LIGHTV2(lights2,
  339.                    POINT_LIGHT2_INDEX,
  340.                    LIGHTV2_STATE_ON,     // turn the light on
  341.                    LIGHTV2_ATTR_POINT,   // pointlight type
  342.                    black, red, black,   // color for diffuse term only
  343.                    &plight_pos, NULL,    // need pos only
  344.                    0,.002,0,            // linear attenuation only
  345.                    0,0,1);               // spotlight info NA
  346. VECTOR4D slight2_pos = {0,200,0,1};
  347. VECTOR4D slight2_dir = {-1,1,-1,1};
  348. // create lookup for lighting engine
  349. RGB_16_8_IndexedRGB_Table_Builder(DD_PIXEL_FORMAT565,  // format we want to build table for
  350.                                   palette,             // source palette
  351.                                   rgblookup);          // lookup table
  352. // create the z buffer
  353. Create_Zbuffer(&zbuffer,
  354.                WINDOW_WIDTH,
  355.                WINDOW_HEIGHT,
  356.                ZBUFFER_ATTR_32BIT);
  357. // load background sounds
  358. wind_sound_id = DSound_Load_WAV("WIND.WAV");
  359. // start the sounds
  360. DSound_Play(wind_sound_id, DSBPLAY_LOOPING);
  361. DSound_Set_Volume(wind_sound_id, 100);
  362. #if 0
  363. // load in the cockpit image
  364. Load_Bitmap_File(&bitmap16bit, "lego01.BMP");
  365. Create_BOB(&cockpit, 0,0,800,600,1, BOB_ATTR_VISIBLE | BOB_ATTR_SINGLE_FRAME, DDSCAPS_SYSTEMMEMORY, 0, 16); 
  366. Load_Frame_BOB16(&cockpit, &bitmap16bit,0,0,0,BITMAP_EXTRACT_MODE_ABS);
  367. Unload_Bitmap_File(&bitmap16bit);
  368. #endif
  369. // set single precission
  370. //_control87( _PC_24, MCW_PC );
  371. // return success
  372. return(1);
  373. } // end Game_Init
  374. ///////////////////////////////////////////////////////////
  375. int Game_Shutdown(void *parms)
  376. {
  377. // this function is where you shutdown your game and
  378. // release all resources that you allocated
  379. // shut everything down
  380. // release all your resources created for the game here....
  381. // now directsound
  382. DSound_Stop_All_Sounds();
  383. DSound_Delete_All_Sounds();
  384. DSound_Shutdown();
  385. // directmusic
  386. DMusic_Delete_All_MIDI();
  387. DMusic_Shutdown();
  388. // shut down directinput
  389. DInput_Release_Keyboard();
  390. // shutdown directinput
  391. DInput_Shutdown();
  392. // shutdown directdraw last
  393. DDraw_Shutdown();
  394. Close_Error_File();
  395. // return success
  396. return(1);
  397. } // end Game_Shutdown
  398. //////////////////////////////////////////////////////////
  399. int Game_Main(void *parms)
  400. {
  401. // this is the workhorse of your game it will be called
  402. // continuously in real-time this is like main() in C
  403. // all the calls for you game go here!
  404. static MATRIX4X4 mrot;   // general rotation matrix
  405. static float plight_ang = 0, 
  406.              slight_ang = 0; // angles for light motion
  407. // use these to rotate objects
  408. static float x_ang = 0, y_ang = 0, z_ang = 0;
  409. // state variables for different rendering modes and help
  410. static int wireframe_mode   = 1;
  411. static int backface_mode    = 1;
  412. static int lighting_mode    = 1;
  413. static int help_mode        = 1;
  414. static int zsort_mode       = 1;
  415. static int x_clip_mode      = 1;
  416. static int y_clip_mode      = 1;
  417. static int z_clip_mode      = 1;
  418. static int perspective_mode = 0;
  419. static char  *perspective_modes[3] = {"AFFINE", "LINEAR PIECEWISE", "PERSPECTIVE CORRECT" };
  420. char work_string[256]; // temp string
  421. int index; // looping var
  422. // start the timing clock
  423. Start_Clock();
  424. // clear the drawing surface 
  425. DDraw_Fill_Surface(lpddsback, 0);
  426. // draw the sky
  427. Draw_Rectangle(0,0, WINDOW_WIDTH, WINDOW_HEIGHT, RGB16Bit(50,50,255), lpddsback);
  428. // draw the ground
  429. //Draw_Rectangle(0,WINDOW_HEIGHT*.38, WINDOW_WIDTH, WINDOW_HEIGHT, RGB16Bit(25,50,110), lpddsback);
  430. // read keyboard and other devices here
  431. DInput_Read_Keyboard();
  432. // game logic here...
  433. // reset the render list
  434. Reset_RENDERLIST4DV2(&rend_list);
  435. // modes and lights
  436. // wireframe mode
  437. if (keyboard_state[DIK_W]) 
  438.    {
  439.    // toggle wireframe mode
  440.    if (++wireframe_mode > 1)
  441.        wireframe_mode=0;
  442.    Wait_Clock(100); // wait, so keyboard doesn't bounce
  443.    } // end if
  444. // backface removal
  445. if (keyboard_state[DIK_B])
  446.    {
  447.    // toggle backface removal
  448.    backface_mode = -backface_mode;
  449.    Wait_Clock(100); // wait, so keyboard doesn't bounce
  450.    } // end if
  451. // lighting
  452. if (keyboard_state[DIK_L])
  453.    {
  454.    // toggle lighting engine completely
  455.    lighting_mode = -lighting_mode;
  456.    Wait_Clock(100); // wait, so keyboard doesn't bounce
  457.    } // end if
  458. // toggle ambient light
  459. if (keyboard_state[DIK_A])
  460.    {
  461.    // toggle ambient light
  462.    if (lights2[AMBIENT_LIGHT_INDEX].state == LIGHTV2_STATE_ON)
  463.       lights2[AMBIENT_LIGHT_INDEX].state = LIGHTV2_STATE_OFF;
  464.    else
  465.       lights2[AMBIENT_LIGHT_INDEX].state = LIGHTV2_STATE_ON;
  466.    Wait_Clock(100); // wait, so keyboard doesn't bounce
  467.    } // end if
  468. // toggle infinite light
  469. if (keyboard_state[DIK_I])
  470.    {
  471.    // toggle ambient light
  472.    if (lights2[INFINITE_LIGHT_INDEX].state == LIGHTV2_STATE_ON)
  473.       lights2[INFINITE_LIGHT_INDEX].state = LIGHTV2_STATE_OFF;
  474.    else
  475.       lights2[INFINITE_LIGHT_INDEX].state = LIGHTV2_STATE_ON;
  476.    Wait_Clock(100); // wait, so keyboard doesn't bounce
  477.    } // end if
  478. // toggle point light
  479. if (keyboard_state[DIK_P])
  480.    {
  481.    // toggle point light
  482.    if (lights2[POINT_LIGHT_INDEX].state == LIGHTV2_STATE_ON)
  483.       lights2[POINT_LIGHT_INDEX].state = LIGHTV2_STATE_OFF;
  484.    else
  485.       lights2[POINT_LIGHT_INDEX].state = LIGHTV2_STATE_ON;
  486.    // toggle point light
  487.    if (lights2[POINT_LIGHT2_INDEX].state == LIGHTV2_STATE_ON)
  488.       lights2[POINT_LIGHT2_INDEX].state = LIGHTV2_STATE_OFF;
  489.    else
  490.       lights2[POINT_LIGHT2_INDEX].state = LIGHTV2_STATE_ON;
  491.    Wait_Clock(100); // wait, so keyboard doesn't bounce
  492.    } // end if
  493. // help menu
  494. if (keyboard_state[DIK_H])
  495.    {
  496.    // toggle help menu 
  497.    help_mode = -help_mode;
  498.    Wait_Clock(100); // wait, so keyboard doesn't bounce
  499.    } // end if
  500. // z-sorting
  501. if (keyboard_state[DIK_Z])
  502.    {
  503.    // toggle z sorting
  504.    zsort_mode = -zsort_mode;
  505.    Wait_Clock(100); // wait, so keyboard doesn't bounce
  506.    } // end if
  507. // perspective mode
  508. if (keyboard_state[DIK_T])
  509.    {
  510.    if (++perspective_mode > 2)
  511.       perspective_mode=0;
  512.    Wait_Clock(100); // wait, so keyboard doesn't bounce
  513.    } // end if
  514. // forward/backward
  515. if (keyboard_state[DIK_UP])
  516.    {
  517.    // move forward
  518.    if ( (cam_speed+=1) > MAX_SPEED) cam_speed = MAX_SPEED;
  519.    } // end if
  520. else
  521. if (keyboard_state[DIK_DOWN])
  522.    {
  523.    // move backward
  524.    if ((cam_speed-=1) < -MAX_SPEED) cam_speed = -MAX_SPEED;
  525.    } // end if
  526. // rotate around y axis or yaw
  527. if (keyboard_state[DIK_RIGHT])
  528.    {
  529.    cam.dir.y+=5;
  530.    } // end if
  531. if (keyboard_state[DIK_LEFT])
  532.    {
  533.    cam.dir.y-=5;
  534.    } // end if
  535. // motion section /////////////////////////////////////////////////////////
  536. // terrain following, simply find the current cell we are over and then
  537. // index into the vertex list and find the 4 vertices that make up the 
  538. // quad cell we are hovering over and then average the values, and based
  539. // on the current height and the height of the terrain push the player upward
  540. // the terrain generates and stores some results to help with terrain following
  541. //ivar1 = columns;
  542. //ivar2 = rows;
  543. //fvar1 = col_vstep;
  544. //fvar2 = row_vstep;
  545. int cell_x = (cam.pos.x  + TERRAIN_WIDTH/2) / obj_terrain.fvar1;
  546. int cell_y = (cam.pos.z  + TERRAIN_HEIGHT/2) / obj_terrain.fvar1;
  547. static float terrain_height, delta;
  548. // test if we are on terrain
  549. if ( (cell_x >=0) && (cell_x < obj_terrain.ivar1) && (cell_y >=0) && (cell_y < obj_terrain.ivar2) )
  550.    {
  551.    // compute vertex indices into vertex list of the current quad
  552.    int v0 = cell_x + cell_y*obj_terrain.ivar2;
  553.    int v1 = v0 + 1;
  554.    int v2 = v1 + obj_terrain.ivar2;
  555.    int v3 = v0 + obj_terrain.ivar2;   
  556.    // now simply index into table 
  557.    terrain_height = 0.25 * (obj_terrain.vlist_trans[v0].y + obj_terrain.vlist_trans[v1].y +
  558.                             obj_terrain.vlist_trans[v2].y + obj_terrain.vlist_trans[v3].y);
  559.    // compute height difference
  560.    delta = terrain_height - (cam.pos.y - gclearance);
  561.    // test for penetration
  562.    if (delta > 0)
  563.       {
  564.       // apply force immediately to camera (this will give it a springy feel)
  565.       vel_y+=(delta * (VELOCITY_SCALER));
  566.       // test for pentration, if so move up immediately so we don't penetrate geometry
  567.       cam.pos.y+=(delta*CAM_HEIGHT_SCALER);
  568.       // now this is more of a hack than the physics model :) let move the front
  569.       // up and down a bit based on the forward velocity and the gradient of the 
  570.       // hill
  571.       cam.dir.x -= (delta*PITCH_CHANGE_RATE);
  572.  
  573.       } // end if
  574.    } // end if
  575. // decelerate camera
  576. if (cam_speed > (CAM_DECEL) ) cam_speed-=CAM_DECEL;
  577. else
  578. if (cam_speed < (-CAM_DECEL) ) cam_speed+=CAM_DECEL;
  579. else
  580.    cam_speed = 0;
  581. // force camera to seek a stable orientation
  582. if (cam.dir.x > (neutral_pitch+PITCH_RETURN_RATE)) cam.dir.x -= (PITCH_RETURN_RATE);
  583. else
  584. if (cam.dir.x < (neutral_pitch-PITCH_RETURN_RATE)) cam.dir.x += (PITCH_RETURN_RATE);
  585.  else 
  586.    cam.dir.x = neutral_pitch;
  587. // apply gravity
  588. vel_y+=gravity;
  589. // test for absolute sea level and push upward..
  590. if (cam.pos.y < sea_level)
  591.    { 
  592.    vel_y = 0; 
  593.    cam.pos.y = sea_level;
  594.    } // end if
  595. // move camera
  596. cam.pos.x += cam_speed*Fast_Sin(cam.dir.y);
  597. cam.pos.z += cam_speed*Fast_Cos(cam.dir.y);
  598. cam.pos.y += vel_y;
  599. // move point light source in ellipse around game world
  600. lights2[POINT_LIGHT_INDEX].pos.x = 1000*Fast_Cos(plight_ang);
  601. lights2[POINT_LIGHT_INDEX].pos.y = 100;
  602. lights2[POINT_LIGHT_INDEX].pos.z = 1000*Fast_Sin(plight_ang);
  603. // move point light source in ellipse around game world
  604. lights2[POINT_LIGHT2_INDEX].pos.x = 500*Fast_Cos(-2*plight_ang);
  605. lights2[POINT_LIGHT2_INDEX].pos.y = 200;
  606. lights2[POINT_LIGHT2_INDEX].pos.z = 1000*Fast_Sin(-2*plight_ang);
  607. if ((plight_ang+=3) > 360)
  608.     plight_ang = 0;
  609. // generate camera matrix
  610. Build_CAM4DV1_Matrix_Euler(&cam, CAM_ROT_SEQ_ZYX);
  611. //////////////////////////////////////////////////////////////////////////
  612. // flat shaded textured cube
  613. // reset the object (this only matters for backface and object removal)
  614. Reset_OBJECT4DV2(&obj_terrain);
  615. // generate rotation matrix around y axis
  616. //Build_XYZ_Rotation_MATRIX4X4(x_ang, y_ang, z_ang, &mrot);
  617. MAT_IDENTITY_4X4(&mrot); 
  618. // rotate the local coords of the object
  619. Transform_OBJECT4DV2(&obj_terrain, &mrot, TRANSFORM_LOCAL_TO_TRANS,1);
  620. // perform world transform
  621. Model_To_World_OBJECT4DV2(&obj_terrain, TRANSFORM_TRANS_ONLY);
  622. // insert the object into render list
  623. Insert_OBJECT4DV2_RENDERLIST4DV2(&rend_list, &obj_terrain,0);
  624. // reset number of polys rendered
  625. debug_polys_rendered_per_frame = 0;
  626. debug_polys_lit_per_frame = 0;
  627. // update rotation angles
  628. //if ((x_ang+=.2) > 360) x_ang = 0; 
  629. //if ((y_ang+=.4) > 360) y_ang = 0;
  630. //if ((z_ang+=.8) > 360) z_ang = 0;
  631.  
  632. // remove backfaces
  633. if (backface_mode==1)
  634.    Remove_Backfaces_RENDERLIST4DV2(&rend_list, &cam);
  635. // apply world to camera transform
  636. World_To_Camera_RENDERLIST4DV2(&rend_list, &cam);
  637. // clip the polygons themselves now
  638. Clip_Polys_RENDERLIST4DV2(&rend_list, &cam, CLIP_POLY_X_PLANE | CLIP_POLY_Y_PLANE | CLIP_POLY_Z_PLANE );
  639. // light scene all at once 
  640. if (lighting_mode==1)
  641.    {
  642.    Transform_LIGHTSV2(lights2, 4, &cam.mcam, TRANSFORM_LOCAL_TO_TRANS);
  643.    Light_RENDERLIST4DV2_World2_16(&rend_list, &cam, lights2, 4);
  644.    } // end if
  645. // sort the polygon list (hurry up!)
  646. if (zsort_mode == 1)
  647.    Sort_RENDERLIST4DV2(&rend_list,  SORT_POLYLIST_AVGZ);
  648. // apply camera to perspective transformation
  649. Camera_To_Perspective_RENDERLIST4DV2(&rend_list, &cam);
  650. // apply screen transform
  651. Perspective_To_Screen_RENDERLIST4DV2(&rend_list, &cam);
  652. // lock the back buffer
  653. DDraw_Lock_Back_Surface();
  654. // reset number of polys rendered
  655. debug_polys_rendered_per_frame = 0;
  656. // render the object
  657. if (wireframe_mode  == 0)
  658.    Draw_RENDERLIST4DV2_Wire16(&rend_list, back_buffer, back_lpitch);
  659. else
  660. if (wireframe_mode  == 1)
  661.    {
  662.    // perspective mode affine texturing
  663.    if (perspective_mode == 0)
  664.       {
  665.       // set up rendering context
  666.       rc.attr =    RENDER_ATTR_INVZBUFFER  
  667.               // | RENDER_ATTR_ALPHA  
  668.               // | RENDER_ATTR_MIPMAP  
  669.               // | RENDER_ATTR_BILERP
  670.                  | RENDER_ATTR_TEXTURE_PERSPECTIVE_AFFINE;
  671.       } // end if
  672.    else // linear piece wise texturing
  673.    if (perspective_mode == 1)
  674.       {
  675.       // set up rendering context
  676.       rc.attr =    RENDER_ATTR_INVZBUFFER  
  677.               // | RENDER_ATTR_ALPHA  
  678.               // | RENDER_ATTR_MIPMAP  
  679.               // | RENDER_ATTR_BILERP
  680.                  | RENDER_ATTR_TEXTURE_PERSPECTIVE_LINEAR;
  681.       } // end if
  682.    else // perspective correct texturing
  683.    if (perspective_mode == 2)
  684.       {
  685.       // set up rendering context
  686.       rc.attr =    RENDER_ATTR_INVZBUFFER  
  687.               // | RENDER_ATTR_ALPHA  
  688.               // | RENDER_ATTR_MIPMAP  
  689.               // | RENDER_ATTR_BILERP
  690.                  | RENDER_ATTR_TEXTURE_PERSPECTIVE_CORRECT;
  691.       } // end if
  692.    // initialize zbuffer to 0 fixed point
  693.    Clear_Zbuffer(&zbuffer, (0 << FIXP16_SHIFT));
  694.    // set up remainder of rendering context
  695.    rc.video_buffer   = back_buffer;
  696.    rc.lpitch         = back_lpitch;
  697.    rc.mip_dist       = 4500;
  698.    rc.zbuffer        = (UCHAR *)zbuffer.zbuffer;
  699.    rc.zpitch         = WINDOW_WIDTH*4;
  700.    rc.rend_list      = &rend_list;
  701.    rc.texture_dist   = 0;
  702.    rc.alpha_override = -1;
  703.    // render scene
  704.    Draw_RENDERLIST4DV2_RENDERCONTEXTV1_16(&rc);
  705.    } // end if
  706. // unlock the back buffer
  707. DDraw_Unlock_Back_Surface();
  708. // draw cockpit
  709. //Draw_BOB16(&cockpit, lpddsback);
  710. sprintf(work_string,"Lighting [%s]: Ambient=%d, Infinite=%d, Point=%d, BckFceRM [%s], Texture MODE [%s]", 
  711.                                                                                  ((lighting_mode == 1) ? "ON" : "OFF"),
  712.                                                                                  lights2[AMBIENT_LIGHT_INDEX].state,
  713.                                                                                  lights2[INFINITE_LIGHT_INDEX].state, 
  714.                                                                                  lights2[POINT_LIGHT_INDEX].state,
  715.                                                                                  ((backface_mode == 1) ? "ON" : "OFF"),
  716.                                                                                  (perspective_modes[perspective_mode]));
  717. Draw_Text_GDI(work_string, 0, WINDOW_HEIGHT-34, RGB(0,255,0), lpddsback);
  718. // draw instructions
  719. Draw_Text_GDI("Press ESC to exit. Press <H> for Help.", 0, 0, RGB(0,255,0), lpddsback);
  720. // should we display help
  721. int text_y = 16;
  722. if (help_mode==1)
  723.     {
  724.     // draw help menu
  725.     Draw_Text_GDI("<A>..............Toggle ambient light source.", 0, text_y+=12, RGB(255,255,255), lpddsback);
  726.     Draw_Text_GDI("<I>..............Toggle infinite light source.", 0, text_y+=12, RGB(255,255,255), lpddsback);
  727.     Draw_Text_GDI("<P>..............Toggle point light source.", 0, text_y+=12, RGB(255,255,255), lpddsback);
  728.     Draw_Text_GDI("<W>..............Toggle wire frame/solid mode.", 0, text_y+=12, RGB(255,255,255), lpddsback);
  729.     Draw_Text_GDI("<B>..............Toggle backface removal.", 0, text_y+=12, RGB(255,255,255), lpddsback);
  730.     Draw_Text_GDI("<T>..............Select thru texturing modes.", 0, text_y+=12, RGB(255,255,255), lpddsback);
  731.     Draw_Text_GDI("<H>..............Toggle Help.", 0, text_y+=12, RGB(255,255,255), lpddsback);
  732.     Draw_Text_GDI("<ESC>............Exit demo.", 0, text_y+=12, RGB(255,255,255), lpddsback);
  733.     } // end help
  734. sprintf(work_string,"Polys Rendered: %d, Polys lit: %d", debug_polys_rendered_per_frame, debug_polys_lit_per_frame);
  735. Draw_Text_GDI(work_string, 0, WINDOW_HEIGHT-34-16-16, RGB(0,255,0), lpddsback);
  736. 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);
  737. Draw_Text_GDI(work_string, 0, WINDOW_HEIGHT-34-16-16-16, RGB(0,255,0), lpddsback);
  738. // flip the surfaces
  739. DDraw_Flip();
  740. // sync to 30ish fps
  741. Wait_Clock(30);
  742. // check of user is trying to exit
  743. if (KEY_DOWN(VK_ESCAPE) || keyboard_state[DIK_ESCAPE])
  744.     {
  745.     PostMessage(main_window_handle, WM_DESTROY,0,0);
  746.     } // end if
  747. // return success
  748. return(1);
  749.  
  750. } // end Game_Main
  751. //////////////////////////////////////////////////////////