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

游戏

开发平台:

Visual C++

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