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

游戏

开发平台:

Visual C++

  1. // DEMOII13_5.CPP - bounding volumes/octree 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-11.CPP and the headers T3DLIB1-11.H
  6. // be in the working directory of the compiler
  7. // INCLUDES ///////////////////////////////////////////////
  8. #define DEBUG_ON
  9. #define INITGUID       // make sure al the COM interfaces are available
  10.                        // instead of this you can include the .LIB file
  11.                        // DXGUID.LIB
  12. #define WIN32_LEAN_AND_MEAN  
  13. #include <windows.h>   // include important windows stuff
  14. #include <windowsx.h> 
  15. #include <mmsystem.h>
  16. #include <iostream.h> // include important C/C++ stuff
  17. #include <conio.h>  
  18. #include <stdlib.h> 
  19. #include <malloc.h>  
  20. #include <memory.h> 
  21. #include <string.h>    
  22. #include <stdarg.h> 
  23. #include <stdio.h>     
  24. #include <math.h>
  25. #include <io.h> 
  26. #include <fcntl.h> 
  27. #include <ddraw.h>  // directX includes 
  28. #include <dsound.h>
  29. #include <dmksctrl.h>
  30. #include <dmusici.h>
  31. #include <dmusicc.h>
  32. #include <dmusicf.h>
  33. #include <dinput.h>
  34. #include "T3DLIB1.h" // game library includes
  35. #include "T3DLIB2.h"
  36. #include "T3DLIB3.h"
  37. #include "T3DLIB4.h"
  38. #include "T3DLIB5.h"
  39. #include "T3DLIB6.h"
  40. #include "T3DLIB7.h"
  41. #include "T3DLIB8.h"
  42. #include "T3DLIB9.h"
  43. #include "T3DLIB10.h"
  44. #include "T3DLIB11.h"
  45. // DEFINES ////////////////////////////////////////////////
  46. // defines for windows interface
  47. #define WINDOW_CLASS_NAME "WIN3DCLASS"  // class name
  48. #define WINDOW_TITLE      "T3D Graphics Console Ver 2.0"
  49. #define WINDOW_WIDTH      800  // size of window
  50. #define WINDOW_HEIGHT     600
  51. #define WINDOW_BPP        16    // bitdepth of window (8,16,24 etc.)
  52.                                 // note: if windowed and not
  53.                                 // fullscreen then bitdepth must
  54.                                 // be same as system bitdepth
  55.                                 // also if 8-bit the a pallete
  56.                                 // is created and attached
  57.    
  58. #define WINDOWED_APP      0    // 0 not windowed, 1 windowed 
  59.  
  60. // create some constants for ease of access
  61. #define AMBIENT_LIGHT_INDEX   0 // ambient light index
  62. #define INFINITE_LIGHT_INDEX  1 // infinite light index
  63. #define POINT_LIGHT_INDEX     2 // point light index
  64. #define SPOT_LIGHT1_INDEX     4 // point light index
  65. #define SPOT_LIGHT2_INDEX     3 // spot light index
  66. #define CAM_DECEL            (.25)   // deceleration/friction
  67. #define MAX_SPEED             20
  68. #define NUM_OBJECTS           4      // number of objects system loads
  69. #define NUM_SCENE_OBJECTS     500    // number of scenery objects 
  70. #define UNIVERSE_RADIUS       1000   // size of universe
  71. // TYPES //////////////////////////////////////////////////
  72.  
  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,0,0,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_PTR  obj_work;               // pointer to active working object
  92. OBJECT4DV2      obj_array[NUM_OBJECTS], // array of objects 
  93.                 obj_scene;              // general scenery object
  94. BHV_NODEV1      bhv_tree;               // the bounding hierarchical volume tree
  95.                 
  96. // filenames of objects to load
  97. char *object_filenames[NUM_OBJECTS] = { 
  98.                                         "cube_flat_textured_01.cob",
  99.                                         "cube_flat_textured_02.cob", 
  100.                                         "fire_cube01.cob",
  101.                                         "earth01.cob",
  102.                                         };
  103. int curr_object = 0;                  // currently active object index
  104. OBJ_CONTAINERV1   scene_objects[NUM_SCENE_OBJECTS]; // positions of scenery objects
  105. RENDERLIST4DV2  rend_list;      // the render list
  106. RGBAV1 white, gray, black, red, green, blue; // general colors
  107. // physical model defines
  108. float cam_speed  = 0;       // speed of the camera/jeep
  109. ZBUFFERV1 zbuffer;          // out little z buffer!
  110. RENDERCONTEXTV1 rc;         // the rendering context
  111. BOB background;             // the background 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_Init2(WINDOW_WIDTH, WINDOW_HEIGHT, WINDOW_BPP, WINDOWED_APP,1);
  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.              10.0,        // near and far clipping planes
  276.              12000.0,
  277.              120.0,      // field of view in degrees
  278.              WINDOW_WIDTH,   // size of final screen viewport
  279.              WINDOW_HEIGHT);
  280.  
  281. // set a scaling vector
  282. VECTOR4D_INITXYZ(&vscale,20.00,20.00,20.00); 
  283. // load all the objects in
  284. for (int index_obj=0; index_obj < NUM_OBJECTS; index_obj++)
  285.     {
  286.     Load_OBJECT4DV2_COB2(&obj_array[index_obj], object_filenames[index_obj],  
  287.                         &vscale, &vpos, &vrot, VERTEX_FLAGS_SWAP_YZ 
  288.                                                 | VERTEX_FLAGS_TRANSFORM_LOCAL 
  289.                                                // | VERTEX_FLAGS_TRANSFORM_LOCAL_WORLD
  290.                                                | VERTEX_FLAGS_INVERT_TEXTURE_V
  291.                                                ,0 );
  292.     } // end for index_obj
  293. // set current object
  294. curr_object = 0;
  295. obj_work = &obj_array[curr_object];
  296. // position the scenery objects randomly
  297. for (index = 0; index < NUM_SCENE_OBJECTS; index++)
  298.     {
  299.     // set master object link
  300.     scene_objects[index].obj = obj_work;
  301.     // randomly position object
  302.     scene_objects[index].pos.x = RAND_RANGE(-UNIVERSE_RADIUS, UNIVERSE_RADIUS);
  303.     scene_objects[index].pos.y = RAND_RANGE(-200, 200);
  304.     scene_objects[index].pos.z = RAND_RANGE(-UNIVERSE_RADIUS, UNIVERSE_RADIUS);
  305.     scene_objects[index].rot.y = RAND_RANGE(0,360);
  306.     scene_objects[index].auxi[0] = RAND_RANGE(1,5);
  307.     // set state of object container
  308.     scene_objects[index].state = ((OBJECT4DV2_PTR)scene_objects[index].obj)->state;
  309.     // set attributes
  310.     scene_objects[index].attr = ((OBJECT4DV2_PTR)scene_objects[index].obj)->attr;
  311.     } // end for
  312.                                            
  313. // set up lights
  314. Reset_Lights_LIGHTV2(lights2, MAX_LIGHTS);
  315. // create some working colors
  316. white.rgba = _RGBA32BIT(255,255,255,0);
  317. gray.rgba  = _RGBA32BIT(150,150,150,0);
  318. black.rgba = _RGBA32BIT(0,0,0,0);
  319. red.rgba   = _RGBA32BIT(255,0,0,0);
  320. green.rgba = _RGBA32BIT(0,255,0,0);
  321. blue.rgba  = _RGBA32BIT(0,0,255,0);
  322. // ambient light
  323. Init_Light_LIGHTV2(lights2,               // array of lights to work with
  324.                    AMBIENT_LIGHT_INDEX,   
  325.                    LIGHTV2_STATE_ON,      // turn the light on
  326.                    LIGHTV2_ATTR_AMBIENT,  // ambient light type
  327.                    gray, black, black,    // color for ambient term only
  328.                    NULL, NULL,            // no need for pos or dir
  329.                    0,0,0,                 // no need for attenuation
  330.                    0,0,0);                // spotlight info NA
  331. VECTOR4D dlight_dir = {-1,0,-1,1}; 
  332. // directional light
  333. Init_Light_LIGHTV2(lights2,               // array of lights to work with
  334.                    INFINITE_LIGHT_INDEX,  
  335.                    LIGHTV2_STATE_ON,      // turn the light on
  336.                    LIGHTV2_ATTR_INFINITE, // infinite light type
  337.                    black, gray, black,    // color for diffuse term only
  338.                    NULL, &dlight_dir,     // need direction only
  339.                    0,0,0,                 // no need for attenuation
  340.                    0,0,0);                // spotlight info NA
  341. VECTOR4D plight_pos = {0,200,0,1};
  342. // point light
  343. Init_Light_LIGHTV2(lights2,               // array of lights to work with
  344.                    POINT_LIGHT_INDEX,
  345.                    LIGHTV2_STATE_ON,      // turn the light on
  346.                    LIGHTV2_ATTR_POINT,    // pointlight type
  347.                    black, green, black,    // color for diffuse term only
  348.                    &plight_pos, NULL,     // need pos only
  349.                    0,.002,0,              // linear attenuation only
  350.                    0,0,1);                // spotlight info NA
  351. VECTOR4D slight2_pos = {0,1000,0,1};
  352. VECTOR4D slight2_dir = {-1,0,-1,1};
  353. // spot light2
  354. Init_Light_LIGHTV2(lights2,                  // array of lights to work with
  355.                    SPOT_LIGHT2_INDEX,
  356.                    LIGHTV2_STATE_ON,         // turn the light on
  357.                    LIGHTV2_ATTR_SPOTLIGHT2,  // spot light type 2
  358.                    black, red, black,        // color for diffuse term only
  359.                    &slight2_pos, &slight2_dir, // need pos only
  360.                    0,.001,0,                 // linear attenuation only
  361.                    0,0,1);    
  362. // create lookup for lighting engine
  363. RGB_16_8_IndexedRGB_Table_Builder(DD_PIXEL_FORMAT565,  // format we want to build table for
  364.                                   palette,             // source palette
  365.                                   rgblookup);          // lookup table
  366. // create the z buffer
  367. Create_Zbuffer(&zbuffer,
  368.                WINDOW_WIDTH,
  369.                WINDOW_HEIGHT,
  370.                ZBUFFER_ATTR_32BIT);
  371. // create the alpha lookup table
  372. RGB_Alpha_Table_Builder(NUM_ALPHA_LEVELS, rgb_alpha_table);
  373. // load in the background
  374. Create_BOB(&background, 0,0,800,600,1, BOB_ATTR_VISIBLE | BOB_ATTR_SINGLE_FRAME, DDSCAPS_SYSTEMMEMORY, 0, 16); 
  375. Load_Bitmap_File(&bitmap16bit, "nebblue01.bmp");
  376. Load_Frame_BOB16(&background, &bitmap16bit,0,0,0,BITMAP_EXTRACT_MODE_ABS);
  377. Unload_Bitmap_File(&bitmap16bit);
  378. // build the bounding hierarchical volume tree with 3 divisions per level
  379. BHV_Build_Tree(&bhv_tree, 
  380.                 scene_objects, 
  381.                 NUM_SCENE_OBJECTS, 
  382.                 0,
  383.                 3,
  384.                 UNIVERSE_RADIUS);
  385. // return success 
  386. return(1);
  387. } // end Game_Init
  388. ///////////////////////////////////////////////////////////
  389. int Game_Shutdown(void *parms)
  390. {
  391. // this function is where you shutdown your game and
  392. // release all resources that you allocated
  393. // shut everything down
  394. // release all your resources created for the game here....
  395. // now directsound
  396. DSound_Stop_All_Sounds();
  397. DSound_Delete_All_Sounds();
  398. DSound_Shutdown();
  399. // directmusic
  400. DMusic_Delete_All_MIDI();
  401. DMusic_Shutdown();
  402. // shut down directinput
  403. DInput_Release_Keyboard();
  404. // shutdown directinput
  405. DInput_Shutdown();
  406. // shutdown directdraw last
  407. DDraw_Shutdown();
  408. Close_Error_File();
  409. Delete_Zbuffer(&zbuffer);
  410. // return success
  411. return(1);
  412. } // end Game_Shutdown
  413. //////////////////////////////////////////////////////////
  414. int Game_Main(void *parms)
  415. {
  416. // this is the workhorse of your game it will be called
  417. // continuously in real-time this is like main() in C
  418. // all the calls for you game go here!
  419. static MATRIX4X4 mrot;   // general rotation matrix
  420. static float plight_ang = 0, 
  421.              slight_ang = 0; // angles for light motion
  422. // use these to rotate objects
  423. static float x_ang = 0, y_ang = 0, z_ang = 0;
  424. // state variables for different rendering modes and help
  425. static int wireframe_mode = 1;
  426. static int backface_mode  = 1;
  427. static int lighting_mode  = 1;
  428. static int help_mode      = 1;
  429. static int zsort_mode     = 1;
  430. static int x_clip_mode    = 1;
  431. static int y_clip_mode    = 1;
  432. static int z_clip_mode    = 1;
  433. static int bilinear_mode  = 1;
  434. char work_string[256]; // temp string
  435. int index; // looping var
  436. // start the timing clock
  437. Start_Clock();
  438. // clear the drawing surface 
  439. //DDraw_Fill_Surface(lpddsback, 0);
  440. // draw the sky
  441. //Draw_Rectangle(0,0, WINDOW_WIDTH, WINDOW_HEIGHT, RGB16Bit(250,190,80), lpddsback);
  442. lpddsback->Blt(NULL, background.images[0], NULL, DDBLT_WAIT, NULL); 
  443. // draw the ground
  444. //Draw_Rectangle(0,WINDOW_HEIGHT*.5, WINDOW_WIDTH, WINDOW_HEIGHT, RGB16Bit(190,190,230), lpddsback);
  445. // read keyboard and other devices here
  446. DInput_Read_Keyboard();
  447. // game logic here...
  448. // reset the render list
  449. Reset_RENDERLIST4DV2(&rend_list);
  450. // modes and lights
  451. // wireframe mode
  452. if (keyboard_state[DIK_W]) 
  453.    {
  454.    // toggle wireframe mode
  455.    if (++wireframe_mode > 1)
  456.        wireframe_mode=0;
  457.    Wait_Clock(100); // wait, so keyboard doesn't bounce
  458.    } // end if
  459. // lighting
  460. if (keyboard_state[DIK_L])
  461.    {
  462.    // toggle lighting engine completely
  463.    lighting_mode = -lighting_mode;
  464.    Wait_Clock(100); // wait, so keyboard doesn't bounce
  465.    } // end if
  466. // toggle ambient light
  467. if (keyboard_state[DIK_A])
  468.    {
  469.    // toggle ambient light
  470.    if (lights2[AMBIENT_LIGHT_INDEX].state == LIGHTV2_STATE_ON)
  471.       lights2[AMBIENT_LIGHT_INDEX].state = LIGHTV2_STATE_OFF;
  472.    else
  473.       lights2[AMBIENT_LIGHT_INDEX].state = LIGHTV2_STATE_ON;
  474.    Wait_Clock(100); // wait, so keyboard doesn't bounce
  475.    } // end if
  476. // toggle infinite light
  477. if (keyboard_state[DIK_I])
  478.    {
  479.    // toggle ambient light
  480.    if (lights2[INFINITE_LIGHT_INDEX].state == LIGHTV2_STATE_ON)
  481.       lights2[INFINITE_LIGHT_INDEX].state = LIGHTV2_STATE_OFF;
  482.    else
  483.       lights2[INFINITE_LIGHT_INDEX].state = LIGHTV2_STATE_ON;
  484.    Wait_Clock(100); // wait, so keyboard doesn't bounce
  485.    } // end if
  486. // toggle point light
  487. if (keyboard_state[DIK_P])
  488.    {
  489.    // toggle point light
  490.    if (lights2[POINT_LIGHT_INDEX].state == LIGHTV2_STATE_ON)
  491.       lights2[POINT_LIGHT_INDEX].state = LIGHTV2_STATE_OFF;
  492.    else
  493.       lights2[POINT_LIGHT_INDEX].state = LIGHTV2_STATE_ON;
  494.    Wait_Clock(100); // wait, so keyboard doesn't bounce
  495.    } // end if
  496. // toggle spot light
  497. if (keyboard_state[DIK_S])
  498.    {
  499.    // toggle spot light
  500.    if (lights2[SPOT_LIGHT2_INDEX].state == LIGHTV2_STATE_ON)
  501.       lights2[SPOT_LIGHT2_INDEX].state = LIGHTV2_STATE_OFF;
  502.    else
  503.       lights2[SPOT_LIGHT2_INDEX].state = LIGHTV2_STATE_ON;
  504.    Wait_Clock(100); // wait, so keyboard doesn't bounce
  505.    } // end if
  506. // help menu
  507. if (keyboard_state[DIK_H])
  508.    {
  509.    // toggle help menu 
  510.    help_mode = -help_mode;
  511.    Wait_Clock(100); // wait, so keyboard doesn't bounce
  512.    } // end if
  513. // z-sorting
  514. if (keyboard_state[DIK_S])
  515.    {
  516.    // toggle z sorting
  517.    zsort_mode = -zsort_mode;
  518.    Wait_Clock(100); // wait, so keyboard doesn't bounce
  519.    } // end if
  520. // forward/backward
  521. if (keyboard_state[DIK_UP])
  522.    {
  523.    // move forward
  524.    if ( (cam_speed+=1) > MAX_SPEED) cam_speed = MAX_SPEED;
  525.    } // end if
  526. else
  527. if (keyboard_state[DIK_DOWN])
  528.    {
  529.    // move backward
  530.    if ((cam_speed-=1) < -MAX_SPEED) cam_speed = -MAX_SPEED;
  531.    } // end if
  532. // rotate around y axis or yaw
  533. if (keyboard_state[DIK_RIGHT])
  534.    {
  535.    cam.dir.y+=5;
  536.    } // end if
  537. if (keyboard_state[DIK_LEFT])
  538.    {
  539.    cam.dir.y-=5;
  540.    } // end if
  541. // move to next object
  542. if (keyboard_state[DIK_N])
  543.    {
  544.    if (++curr_object >= NUM_OBJECTS)
  545.       curr_object = 0;
  546.  
  547.    // update pointer
  548.    obj_work = &obj_array[curr_object];
  549.    Wait_Clock(100); // wait, so keyboard doesn't bounce
  550.    } // end if
  551. // toggle bilinear mode
  552. if (keyboard_state[DIK_B])
  553.    {
  554.    bilinear_mode = -bilinear_mode;
  555.    Wait_Clock(100); // wait, so keyboard doesn't bounce
  556.    } // end if
  557. // decelerate camera
  558. if (cam_speed > (CAM_DECEL) ) cam_speed-=CAM_DECEL;
  559. else
  560. if (cam_speed < (-CAM_DECEL) ) cam_speed+=CAM_DECEL;
  561. else
  562.    cam_speed = 0;
  563. // move camera
  564. cam.pos.x += cam_speed*Fast_Sin(cam.dir.y);
  565. cam.pos.z += cam_speed*Fast_Cos(cam.dir.y);
  566. // move point light source in ellipse around game world
  567. lights2[POINT_LIGHT_INDEX].pos.x = 1000*Fast_Cos(plight_ang);
  568. lights2[POINT_LIGHT_INDEX].pos.y = 100;
  569. lights2[POINT_LIGHT_INDEX].pos.z = 1000*Fast_Sin(plight_ang);
  570. if ((plight_ang+=3) > 360)
  571.     plight_ang = 0;
  572. // move spot light source in ellipse around game world
  573. lights2[SPOT_LIGHT2_INDEX].pos.x = 1000*Fast_Cos(slight_ang);
  574. lights2[SPOT_LIGHT2_INDEX].pos.y = 200;
  575. lights2[SPOT_LIGHT2_INDEX].pos.z = 1000*Fast_Sin(slight_ang);
  576. if ((slight_ang-=5) < 0)
  577.     slight_ang = 360;
  578. // generate camera matrix
  579. Build_CAM4DV1_Matrix_Euler(&cam, CAM_ROT_SEQ_ZYX);
  580. // reset BHV for culling
  581. BHV_Reset_Tree(&bhv_tree);
  582. // now cull the BHV ... die polygons die!
  583. bhv_nodes_visited = 0;
  584. BHV_FrustrumCull(&bhv_tree,                 // the root of the BHV  
  585.                   &cam,                     // camera to cull relative to
  586.                   CULL_OBJECT_XYZ_PLANES);  // clipping planes to consider
  587. // statistic tracking 
  588. int objects_processed = 0;
  589. ////////////////////////////////////////////////////////
  590. // insert the scenery into universe
  591. for (index = 0; index < NUM_SCENE_OBJECTS; index++)
  592.     {
  593.     // test if container has been culled already by BHV
  594.     if (scene_objects[index].state & OBJECT4DV2_STATE_CULLED)
  595.        continue;
  596.     // reset the object (this only matters for backface and object removal)
  597.     Reset_OBJECT4DV2(obj_work);
  598.     objects_processed++;
  599.     // set position of object
  600.     obj_work->world_pos.x = scene_objects[index].pos.x;
  601.     obj_work->world_pos.y = scene_objects[index].pos.y;
  602.     obj_work->world_pos.z = scene_objects[index].pos.z;
  603.     // rotate object
  604.     if ((scene_objects[index].rot.y+=scene_objects[index].auxi[0]) >= 360)
  605.          scene_objects[index].rot.y = 0;
  606.     // attempt to cull object   
  607.     if (!Cull_OBJECT4DV2(obj_work, &cam, CULL_OBJECT_XYZ_PLANES))
  608.        {
  609.        MAT_IDENTITY_4X4(&mrot);
  610.        // generate rotation matrix around y axis
  611.        Build_XYZ_Rotation_MATRIX4X4(0, scene_objects[index].rot.y, 0, &mrot);       
  612.        // rotate the local coords of the object
  613.        Transform_OBJECT4DV2(obj_work, &mrot, TRANSFORM_LOCAL_TO_TRANS,1);
  614.        // perform world transform
  615.        Model_To_World_OBJECT4DV2(obj_work, TRANSFORM_TRANS_ONLY);
  616.        // insert the object into render list
  617.        Insert_OBJECT4DV2_RENDERLIST4DV2(&rend_list,obj_work,0);
  618.        } // end if
  619.  
  620.     } // end for
  621. // reset number of polys rendered
  622. debug_polys_rendered_per_frame = 0;
  623. debug_polys_lit_per_frame = 0;
  624. // remove backfaces
  625. if (backface_mode==1)
  626.    Remove_Backfaces_RENDERLIST4DV2(&rend_list, &cam);
  627. // apply world to camera transform
  628. World_To_Camera_RENDERLIST4DV2(&rend_list, &cam);
  629. // clip the polygons themselves now
  630. Clip_Polys_RENDERLIST4DV2(&rend_list, &cam, ((x_clip_mode == 1) ? CLIP_POLY_X_PLANE : 0) | 
  631.                                             ((y_clip_mode == 1) ? CLIP_POLY_Y_PLANE : 0) | 
  632.                                             ((z_clip_mode == 1) ? CLIP_POLY_Z_PLANE : 0) );
  633. // light scene all at once 
  634. if (lighting_mode==1)
  635.    {
  636.    Transform_LIGHTSV2(lights2, 4, &cam.mcam, TRANSFORM_LOCAL_TO_TRANS);
  637.    Light_RENDERLIST4DV2_World2_16(&rend_list, &cam, lights2, 4);
  638.    } // end if
  639. // sort the polygon list (hurry up!)
  640. if (zsort_mode == 1)
  641.    Sort_RENDERLIST4DV2(&rend_list,  SORT_POLYLIST_AVGZ);
  642. // apply camera to perspective transformation
  643. Camera_To_Perspective_RENDERLIST4DV2(&rend_list, &cam);
  644. // apply screen transform
  645. Perspective_To_Screen_RENDERLIST4DV2(&rend_list, &cam);
  646. // lock the back buffer
  647. DDraw_Lock_Back_Surface();
  648. // reset number of polys rendered
  649. debug_polys_rendered_per_frame = 0;
  650. // render the renderinglist
  651. if (wireframe_mode  == 0)
  652.    Draw_RENDERLIST4DV2_Wire16(&rend_list, back_buffer, back_lpitch);
  653. else
  654. if (wireframe_mode  == 1)
  655.    {
  656.    // initialize zbuffer to 16000 fixed point
  657.    Clear_Zbuffer(&zbuffer, (000 << FIXP16_SHIFT));
  658.    // set up rendering context
  659.    rc.attr         = RENDER_ATTR_INVZBUFFER  
  660.                     // | RENDER_ATTR_ALPHA  
  661.                     // | RENDER_ATTR_MIPMAP  
  662.                      | (bilinear_mode==1 ? RENDER_ATTR_BILERP : 0)
  663.                      | RENDER_ATTR_TEXTURE_PERSPECTIVE_AFFINE;
  664.    
  665.    rc.video_buffer   = back_buffer;
  666.    rc.lpitch         = back_lpitch;
  667.    rc.mip_dist       = 3500;
  668.    rc.zbuffer        = (UCHAR *)zbuffer.zbuffer;
  669.    rc.zpitch         = WINDOW_WIDTH*4;
  670.    rc.rend_list      = &rend_list;
  671.    rc.texture_dist   = 0;
  672.    rc.alpha_override = 5;
  673.    // render scene
  674.    Draw_RENDERLIST4DV2_RENDERCONTEXTV1_16(&rc);
  675.    
  676.    } // end if
  677. // unlock the back buffer
  678. DDraw_Unlock_Back_Surface();
  679. sprintf(work_string,"Lighting [%s]: Ambient=%d, Infinite=%d, Point=%d, Spot=%d, Zsort [%s]", 
  680.                                                                                  ((lighting_mode == 1) ? "ON" : "OFF"),
  681.                                                                                  lights[AMBIENT_LIGHT_INDEX].state,
  682.                                                                                  lights[INFINITE_LIGHT_INDEX].state, 
  683.                                                                                  lights[POINT_LIGHT_INDEX].state,
  684.                                                                                  lights[SPOT_LIGHT2_INDEX].state,
  685.                                                                                  ((zsort_mode == 1) ? "ON" : "OFF"));
  686. Draw_Text_GDI(work_string, 0, WINDOW_HEIGHT-34-16, RGB(0,255,0), lpddsback);
  687. // draw instructions
  688. Draw_Text_GDI("Press ESC to exit. Press <H> for Help.", 0, 0, RGB(0,255,0), lpddsback);
  689. // should we display help
  690. int text_y = 16;
  691. if (help_mode==1)
  692.     {
  693.     // draw help menu
  694.     Draw_Text_GDI("<A>..............Toggle ambient light source.", 0, text_y+=12, RGB(255,255,255), lpddsback);
  695.     Draw_Text_GDI("<I>..............Toggle infinite light source.", 0, text_y+=12, RGB(255,255,255), lpddsback);
  696.     Draw_Text_GDI("<P>..............Toggle point light source.", 0, text_y+=12, RGB(255,255,255), lpddsback);
  697.     Draw_Text_GDI("<S>..............Toggle spot light source.", 0, text_y+=12, RGB(255,255,255), lpddsback);
  698.     Draw_Text_GDI("<N>..............Next object.", 0, text_y+=12, RGB(255,255,255), lpddsback);
  699.     Draw_Text_GDI("<W>..............Toggle wire frame/solid mode.", 0, text_y+=12, RGB(255,255,255), lpddsback);
  700.     Draw_Text_GDI("<S>..............Toggle Z sorting.", 0, text_y+=12, RGB(255,255,255), lpddsback);
  701.     Draw_Text_GDI("<H>..............Toggle Help.", 0, text_y+=12, RGB(255,255,255), lpddsback);
  702.     Draw_Text_GDI("<ESC>............Exit demo.", 0, text_y+=12, RGB(255,255,255), lpddsback);
  703.     } // end help
  704. sprintf(work_string,"Polys Rendered: %d, Polys lit: %d, Object Pre-Culled by BHV: %d, Nodes visited in BHV Tree: %d", 
  705.                                                                              debug_polys_rendered_per_frame, 
  706.                                                                              debug_polys_lit_per_frame,
  707.                                                                              NUM_SCENE_OBJECTS - objects_processed,
  708.                                                                              bhv_nodes_visited);
  709. Draw_Text_GDI(work_string, 0, WINDOW_HEIGHT-34-16-16, RGB(0,255,0), lpddsback);
  710. sprintf(work_string,"CAM [%5.2f, %5.2f, %5.2f]",  cam.pos.x, cam.pos.y, cam.pos.z);
  711. Draw_Text_GDI(work_string, 0, WINDOW_HEIGHT-34-16-16-16, RGB(0,255,0), lpddsback);
  712. // flip the surfaces
  713. DDraw_Flip2();
  714. // sync to 30ish fps
  715. Wait_Clock(30);
  716. // check of user is trying to exit
  717. if (KEY_DOWN(VK_ESCAPE) || keyboard_state[DIK_ESCAPE])
  718.     {
  719.     PostMessage(main_window_handle, WM_DESTROY,0,0);
  720.     } // end if
  721. // return success
  722. return(1);
  723.  
  724. } // end Game_Main