demoII10_1.cpp
Upload User: husern
Upload Date: 2018-01-20
Package Size: 42486k
Code Size: 26k
Category:

Game Program

Development Platform:

Visual C++

  1. // DEMOII10_1.CPP - 3D clipping demo rotating with flat shading,lights, and clipping
  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-8.CPP and the headers T3DLIB1-8.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. // DEFINES ////////////////////////////////////////////////
  43. // defines for windows interface
  44. #define WINDOW_CLASS_NAME "WIN3DCLASS"  // class name
  45. #define WINDOW_TITLE      "T3D Graphics Console Ver 2.0"
  46. #define WINDOW_WIDTH      800  // size of window
  47. #define WINDOW_HEIGHT     600
  48. #define WINDOW_BPP        16    // bitdepth of window (8,16,24 etc.)
  49.                                 // note: if windowed and not
  50.                                 // fullscreen then bitdepth must
  51.                                 // be same as system bitdepth
  52.                                 // also if 8-bit the a pallete
  53.                                 // is created and attached
  54.    
  55. #define WINDOWED_APP      0 // 0 not windowed, 1 windowed
  56. // create some constants for ease of access
  57. #define AMBIENT_LIGHT_INDEX   0 // ambient light index
  58. #define INFINITE_LIGHT_INDEX  1 // infinite light index
  59. #define POINT_LIGHT_INDEX     2 // point light index
  60. #define SPOT_LIGHT1_INDEX     4 // point light index
  61. #define SPOT_LIGHT2_INDEX     3 // spot light index
  62. #define NUM_OBJECTS           6 // number of objects system loads
  63. // PROTOTYPES /////////////////////////////////////////////
  64. // game console
  65. int Game_Init(void *parms=NULL);
  66. int Game_Shutdown(void *parms=NULL);
  67. int Game_Main(void *parms=NULL);
  68. // GLOBALS ////////////////////////////////////////////////
  69. HWND main_window_handle           = NULL; // save the window handle
  70. HINSTANCE main_instance           = NULL; // save the instance
  71. char buffer[2048];                        // used to print text
  72. // initialize camera position and direction
  73. POINT4D  cam_pos    = {0,0,0,1};
  74. POINT4D  cam_target = {0,0,0,1};
  75. VECTOR4D cam_dir    = {0,0,0,1};
  76. // all your initialization code goes here...
  77. VECTOR4D vscale={1.0,1.0,1.0,1}, 
  78.          vpos = {0,0,150,1}, 
  79.          vrot = {0,0,0,1};
  80. CAM4DV1        cam;       // the single camera
  81. OBJECT4DV2_PTR  obj_work;               // pointer to active working object
  82. OBJECT4DV2      obj_array[NUM_OBJECTS]; // array of objects 
  83.                
  84. RENDERLIST4DV2 rend_list; // the render list
  85. RGBAV1 white, gray, black, red, green, blue; // general colors
  86. // filenames of objects to load
  87. char *object_filenames[NUM_OBJECTS] = { "cube_flat_01.cob",
  88.                                         "cube_gouraud_01.cob",
  89.                                         "cube_flat_textured_01.cob",
  90.                                         "sphere02.cob",
  91.                                         "sphere03.cob",
  92.                                         "hammer03.cob",
  93.                                       };
  94. int curr_object = 0;                  // currently active object index
  95. // FUNCTIONS //////////////////////////////////////////////
  96. LRESULT CALLBACK WindowProc(HWND hwnd, 
  97.     UINT msg, 
  98.                             WPARAM wparam, 
  99.                             LPARAM lparam)
  100. {
  101. // this is the main message handler of the system
  102. PAINTSTRUCT ps;    // used in WM_PAINT
  103. HDC hdc;    // handle to a device context
  104. // what is the message 
  105. switch(msg)
  106. {
  107. case WM_CREATE: 
  108.         {
  109. // do initialization stuff here
  110. return(0);
  111. } break;
  112.     case WM_PAINT:  
  113.          {
  114.          // start painting
  115.          hdc = BeginPaint(hwnd,&ps);
  116.          // end painting
  117.          EndPaint(hwnd,&ps);
  118.          return(0);
  119.         } break;
  120. case WM_DESTROY: 
  121. {
  122. // kill the application
  123. PostQuitMessage(0);
  124. return(0);
  125. } break;
  126. default:break;
  127.     } // end switch
  128. // process any messages that we didn't take care of 
  129. return (DefWindowProc(hwnd, msg, wparam, lparam));
  130. } // end WinProc
  131. // WINMAIN ////////////////////////////////////////////////
  132. int WINAPI WinMain( HINSTANCE hinstance,
  133. HINSTANCE hprevinstance,
  134. LPSTR lpcmdline,
  135. int ncmdshow)
  136. {
  137. // this is the winmain function
  138. WNDCLASS winclass; // this will hold the class we create
  139. HWND  hwnd; // generic window handle
  140. MSG  msg; // generic message
  141. HDC      hdc;       // generic dc
  142. PAINTSTRUCT ps;     // generic paintstruct
  143. // first fill in the window class stucture
  144. winclass.style = CS_DBLCLKS | CS_OWNDC | 
  145.                           CS_HREDRAW | CS_VREDRAW;
  146. winclass.lpfnWndProc = WindowProc;
  147. winclass.cbClsExtra = 0;
  148. winclass.cbWndExtra = 0;
  149. winclass.hInstance = hinstance;
  150. winclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
  151. winclass.hCursor = LoadCursor(NULL, IDC_ARROW);
  152. winclass.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
  153. winclass.lpszMenuName = NULL; 
  154. winclass.lpszClassName = WINDOW_CLASS_NAME;
  155. // register the window class
  156. if (!RegisterClass(&winclass))
  157. return(0);
  158. // create the window, note the test to see if WINDOWED_APP is
  159. // true to select the appropriate window flags
  160. if (!(hwnd = CreateWindow(WINDOW_CLASS_NAME, // class
  161.   WINDOW_TITLE,  // title
  162.   (WINDOWED_APP ? (WS_OVERLAPPED | WS_SYSMENU | WS_CAPTION) : (WS_POPUP | WS_VISIBLE)),
  163.     0,0,    // x,y
  164.   WINDOW_WIDTH,  // width
  165.                           WINDOW_HEIGHT, // height
  166.   NULL,    // handle to parent 
  167.   NULL,    // handle to menu
  168.   hinstance,// instance
  169.   NULL))) // creation parms
  170. return(0);
  171. // save the window handle and instance in a global
  172. main_window_handle = hwnd;
  173. main_instance      = hinstance;
  174. // resize the window so that client is really width x height
  175. if (WINDOWED_APP)
  176. {
  177. // now resize the window, so the client area is the actual size requested
  178. // since there may be borders and controls if this is going to be a windowed app
  179. // if the app is not windowed then it won't matter
  180. RECT window_rect = {0,0,WINDOW_WIDTH-1,WINDOW_HEIGHT-1};
  181. // make the call to adjust window_rect
  182. AdjustWindowRectEx(&window_rect,
  183.      GetWindowStyle(main_window_handle),
  184.      GetMenu(main_window_handle) != NULL,  
  185.      GetWindowExStyle(main_window_handle));
  186. // save the global client offsets, they are needed in DDraw_Flip()
  187. window_client_x0 = -window_rect.left;
  188. window_client_y0 = -window_rect.top;
  189. // now resize the window with a call to MoveWindow()
  190. MoveWindow(main_window_handle,
  191.            0,                                    // x position
  192.            0,                                    // y position
  193.            window_rect.right - window_rect.left, // width
  194.            window_rect.bottom - window_rect.top, // height
  195.            FALSE);
  196. // show the window, so there's no garbage on first render
  197. ShowWindow(main_window_handle, SW_SHOW);
  198. } // end if windowed
  199. // perform all game console specific initialization
  200. Game_Init();
  201. // disable CTRL-ALT_DEL, ALT_TAB, comment this line out 
  202. // if it causes your system to crash
  203. SystemParametersInfo(SPI_SCREENSAVERRUNNING, TRUE, NULL, 0);
  204. // enter main event loop
  205. while(1)
  206. {
  207. if (PeekMessage(&msg,NULL,0,0,PM_REMOVE))
  208. // test if this is a quit
  209.         if (msg.message == WM_QUIT)
  210.            break;
  211. // translate any accelerator keys
  212. TranslateMessage(&msg);
  213. // send the message to the window proc
  214. DispatchMessage(&msg);
  215. } // end if
  216.     
  217.     // main game processing goes here
  218.     Game_Main();
  219. } // end while
  220. // shutdown game and release all resources
  221. Game_Shutdown();
  222. // enable CTRL-ALT_DEL, ALT_TAB, comment this line out 
  223. // if it causes your system to crash
  224. SystemParametersInfo(SPI_SCREENSAVERRUNNING, FALSE, NULL, 0);
  225. // return to Windows like this
  226. return(msg.wParam);
  227. } // end WinMain
  228. // T3D II GAME PROGRAMMING CONSOLE FUNCTIONS ////////////////
  229. int Game_Init(void *parms)
  230. {
  231. // this function is where you do all the initialization 
  232. // for your game
  233. int index; // looping var
  234. // start up DirectDraw (replace the parms as you desire)
  235. DDraw_Init(WINDOW_WIDTH, WINDOW_HEIGHT, WINDOW_BPP, WINDOWED_APP);
  236. // initialize directinput
  237. DInput_Init();
  238. // acquire the keyboard 
  239. DInput_Init_Keyboard();
  240. // add calls to acquire other directinput devices here...
  241. // initialize directsound and directmusic
  242. DSound_Init();
  243. DMusic_Init();
  244. // hide the mouse
  245. if (!WINDOWED_APP)
  246.     ShowCursor(FALSE);
  247. // seed random number generator
  248. srand(Start_Clock()); 
  249. Open_Error_File("ERROR.TXT");
  250. // initialize math engine
  251. Build_Sin_Cos_Tables();
  252. // initialize the camera with 90 FOV, normalized coordinates
  253. Init_CAM4DV1(&cam,      // the camera object
  254.              CAM_MODEL_EULER, // the euler model
  255.              &cam_pos,  // initial camera position
  256.              &cam_dir,  // initial camera angles
  257.              &cam_target,      // no target
  258.              100.0,        // near and far clipping planes
  259.              1000.0,
  260.              120.0,      // field of view in degrees
  261.              WINDOW_WIDTH,   // size of final screen viewport
  262.              WINDOW_HEIGHT);
  263. // load flat shaded cube
  264. VECTOR4D_INITXYZ(&vscale,20.00,20.00,20.00); 
  265. // load all the objects in
  266. for (int index_obj=0; index_obj < NUM_OBJECTS; index_obj++)
  267.     {
  268.     Load_OBJECT4DV2_COB(&obj_array[index_obj], object_filenames[index_obj],  
  269.                         &vscale, &vpos, &vrot, VERTEX_FLAGS_SWAP_YZ  |
  270.                                                VERTEX_FLAGS_TRANSFORM_LOCAL 
  271.                                                /* VERTEX_FLAGS_TRANSFORM_LOCAL_WORLD*/ );
  272.     } // end for index_obj
  273. // set current object
  274. curr_object = 0;
  275. obj_work = &obj_array[curr_object];
  276. // set up lights
  277. Reset_Lights_LIGHTV2(lights2, MAX_LIGHTS);
  278. // create some working colors
  279. white.rgba = _RGBA32BIT(255,255,255,0);
  280. gray.rgba  = _RGBA32BIT(100,100,100,0);
  281. black.rgba = _RGBA32BIT(0,0,0,0);
  282. red.rgba   = _RGBA32BIT(255,0,0,0);
  283. green.rgba = _RGBA32BIT(0,255,0,0);
  284. blue.rgba  = _RGBA32BIT(0,0,255,0);
  285. // ambient light
  286. Init_Light_LIGHTV2(lights2,
  287.                    AMBIENT_LIGHT_INDEX,   
  288.                    LIGHTV2_STATE_ON,      // turn the light on
  289.                    LIGHTV2_ATTR_AMBIENT,  // ambient light type
  290.                    gray, black, black,    // color for ambient term only
  291.                    NULL, NULL,            // no need for pos or dir
  292.                    0,0,0,                 // no need for attenuation
  293.                    0,0,0);                // spotlight info NA
  294. VECTOR4D dlight_dir = {-1,0,-1,1};
  295. // directional light
  296. Init_Light_LIGHTV2(lights2,
  297.                    INFINITE_LIGHT_INDEX,  
  298.                    LIGHTV2_STATE_ON,      // turn the light on
  299.                    LIGHTV2_ATTR_INFINITE, // infinite light type
  300.                    black, gray, black,    // color for diffuse term only
  301.                    NULL, &dlight_dir,     // need direction only
  302.                    0,0,0,                 // no need for attenuation
  303.                    0,0,0);                // spotlight info NA
  304. VECTOR4D plight_pos = {0,200,0,1};
  305. // point light
  306. Init_Light_LIGHTV2(lights2,
  307.                    POINT_LIGHT_INDEX,
  308.                    LIGHTV2_STATE_ON,      // turn the light on
  309.                    LIGHTV2_ATTR_POINT,    // pointlight type
  310.                    black, green, black,   // color for diffuse term only
  311.                    &plight_pos, NULL,     // need pos only
  312.                    0,.001,0,              // linear attenuation only
  313.                    0,0,1);                // spotlight info NA
  314. VECTOR4D slight2_pos = {0,200,0,1};
  315. VECTOR4D slight2_dir = {-1,0,-1,1};
  316. // spot light2
  317. Init_Light_LIGHTV2(lights2,
  318.                    SPOT_LIGHT2_INDEX,
  319.                    LIGHTV2_STATE_ON,         // turn the light on
  320.                    LIGHTV2_ATTR_SPOTLIGHT2,  // spot light type 2
  321.                    black, red, black,      // color for diffuse term only
  322.                    &slight2_pos, &slight2_dir, // need pos only
  323.                    0,.001,0,                 // linear attenuation only
  324.                    0,0,1);    
  325. // create lookup for lighting engine
  326. RGB_16_8_IndexedRGB_Table_Builder(DD_PIXEL_FORMAT565,  // format we want to build table for
  327.                                   palette,             // source palette
  328.                                   rgblookup);          // lookup table
  329. // return success
  330. return(1);
  331. } // end Game_Init
  332. ///////////////////////////////////////////////////////////
  333. int Game_Shutdown(void *parms)
  334. {
  335. // this function is where you shutdown your game and
  336. // release all resources that you allocated
  337. // shut everything down
  338. // release all your resources created for the game here....
  339. // now directsound
  340. DSound_Stop_All_Sounds();
  341. DSound_Delete_All_Sounds();
  342. DSound_Shutdown();
  343. // directmusic
  344. DMusic_Delete_All_MIDI();
  345. DMusic_Shutdown();
  346. // shut down directinput
  347. DInput_Release_Keyboard();
  348. // shutdown directinput
  349. DInput_Shutdown();
  350. // shutdown directdraw last
  351. DDraw_Shutdown();
  352. Close_Error_File();
  353. // return success
  354. return(1);
  355. } // end Game_Shutdown
  356. //////////////////////////////////////////////////////////
  357. int Game_Main(void *parms)
  358. {
  359. // this is the workhorse of your game it will be called
  360. // continuously in real-time this is like main() in C
  361. // all the calls for you game go here!
  362. static MATRIX4X4 mrot;   // general rotation matrix
  363. // these are used to create a circling camera
  364. static float view_angle = 0; 
  365. static float camera_distance = 6000;
  366. static VECTOR4D pos = {0,0,0,0};
  367. static float tank_speed;
  368. static float turning = 0;
  369. // state variables for different rendering modes and help
  370. static int wireframe_mode = 1;
  371. static int backface_mode  = 1;
  372. static int lighting_mode  = 1;
  373. static int help_mode      = 1;
  374. static int zsort_mode     = 1;
  375. static int x_clip_mode    = 1;
  376. static int y_clip_mode    = 1;
  377. static int z_clip_mode    = 1;
  378. char work_string[256]; // temp string
  379. int index; // looping var
  380. // start the timing clock
  381. Start_Clock();
  382. // clear the drawing surface 
  383. DDraw_Fill_Surface(lpddsback, 0);
  384. // draw the sky
  385. //Draw_Rectangle(0,0, WINDOW_WIDTH, WINDOW_HEIGHT/2, RGB16Bit(0,35,50), lpddsback);
  386. // draw the ground
  387. //Draw_Rectangle(0,WINDOW_HEIGHT/2-1, WINDOW_WIDTH, WINDOW_HEIGHT, RGB16Bit(20,12,0), lpddsback);
  388. // read keyboard and other devices here
  389. DInput_Read_Keyboard();
  390. // game logic here...
  391. // reset the render list
  392. Reset_RENDERLIST4DV2(&rend_list);
  393. // modes and lights
  394. // wireframe mode
  395. if (keyboard_state[DIK_W])
  396.    {
  397.    // toggle wireframe mode
  398.    if (++wireframe_mode > 1)
  399.        wireframe_mode=0;
  400.    Wait_Clock(100); // wait, so keyboard doesn't bounce
  401.    } // end if
  402. // backface removal
  403. if (keyboard_state[DIK_B])
  404.    {
  405.    // toggle backface removal
  406.    backface_mode = -backface_mode;
  407.    Wait_Clock(100); // wait, so keyboard doesn't bounce
  408.    } // end if
  409. // lighting
  410. if (keyboard_state[DIK_L])
  411.    {
  412.    // toggle lighting engine completely
  413.    lighting_mode = -lighting_mode;
  414.    Wait_Clock(100); // wait, so keyboard doesn't bounce
  415.    } // end if
  416. // toggle ambient light
  417. if (keyboard_state[DIK_A])
  418.    {
  419.    // toggle ambient light
  420.    if (lights2[AMBIENT_LIGHT_INDEX].state == LIGHTV2_STATE_ON)
  421.       lights2[AMBIENT_LIGHT_INDEX].state = LIGHTV2_STATE_OFF;
  422.    else
  423.       lights2[AMBIENT_LIGHT_INDEX].state = LIGHTV2_STATE_ON;
  424.    Wait_Clock(100); // wait, so keyboard doesn't bounce
  425.    } // end if
  426. // toggle infinite light
  427. if (keyboard_state[DIK_I])
  428.    {
  429.    // toggle ambient light
  430.    if (lights2[INFINITE_LIGHT_INDEX].state == LIGHTV2_STATE_ON)
  431.       lights2[INFINITE_LIGHT_INDEX].state = LIGHTV2_STATE_OFF;
  432.    else
  433.       lights2[INFINITE_LIGHT_INDEX].state = LIGHTV2_STATE_ON;
  434.    Wait_Clock(100); // wait, so keyboard doesn't bounce
  435.    } // end if
  436. // toggle point light
  437. if (keyboard_state[DIK_P])
  438.    {
  439.    // toggle point light
  440.    if (lights2[POINT_LIGHT_INDEX].state == LIGHTV2_STATE_ON)
  441.       lights2[POINT_LIGHT_INDEX].state = LIGHTV2_STATE_OFF;
  442.    else
  443.       lights2[POINT_LIGHT_INDEX].state = LIGHTV2_STATE_ON;
  444.    Wait_Clock(100); // wait, so keyboard doesn't bounce
  445.    } // end if
  446. // toggle spot light
  447. if (keyboard_state[DIK_S])
  448.    {
  449.    // toggle spot light
  450.    if (lights2[SPOT_LIGHT2_INDEX].state == LIGHTV2_STATE_ON)
  451.       lights2[SPOT_LIGHT2_INDEX].state = LIGHTV2_STATE_OFF;
  452.    else
  453.       lights2[SPOT_LIGHT2_INDEX].state = LIGHTV2_STATE_ON;
  454.    Wait_Clock(100); // wait, so keyboard doesn't bounce
  455.    } // end if
  456. // help menu
  457. if (keyboard_state[DIK_H])
  458.    {
  459.    // toggle help menu 
  460.    help_mode = -help_mode;
  461.    Wait_Clock(100); // wait, so keyboard doesn't bounce
  462.    } // end if
  463. // z-sorting
  464. if (keyboard_state[DIK_Z])
  465.    {
  466.    // toggle z sorting
  467.    zsort_mode = -zsort_mode;
  468.    Wait_Clock(100); // wait, so keyboard doesn't bounce
  469.    } // end if
  470. // clipping
  471. if (keyboard_state[DIK_X])
  472.    {
  473.    // toggle x clipping
  474.    x_clip_mode = -x_clip_mode;
  475.    Wait_Clock(100); // wait, so keyboard doesn't bounce
  476.    } // end if
  477. if (keyboard_state[DIK_Y])
  478.    {
  479.    // toggle y clipping
  480.    y_clip_mode = -y_clip_mode;
  481.    Wait_Clock(100); // wait, so keyboard doesn't bounce
  482.    } // end if
  483. if (keyboard_state[DIK_Z])
  484.    {
  485.    // toggle z clipping
  486.    z_clip_mode = -z_clip_mode;
  487.    Wait_Clock(100); // wait, so keyboard doesn't bounce
  488.    } // end if
  489. // object and camera movement
  490. // rotate around y axis or yaw
  491. if (keyboard_state[DIK_RIGHT])
  492.    {
  493.    cam.dir.y+=4;
  494.    } // end if
  495. if (keyboard_state[DIK_LEFT])
  496.    {
  497.    cam.dir.y-=4;
  498.    } // end if
  499. if (keyboard_state[DIK_UP])
  500.    {
  501.    obj_work->world_pos.z+=5;
  502.    } // end if
  503. if (keyboard_state[DIK_DOWN])
  504.    {
  505.    obj_work->world_pos.z-=5;
  506.    } // end if
  507. // move to next object
  508. if (keyboard_state[DIK_O])
  509.    {
  510.    if (++curr_object >= NUM_OBJECTS)
  511.       curr_object = 0;
  512.    // update pointer
  513.    obj_work = &obj_array[curr_object];
  514.    Wait_Clock(100); // wait, so keyboard doesn't bounce
  515.    } // end if
  516. static float plight_ang = 0, slight_ang = 0; // angles for light motion 
  517. // move point light source in ellipse around game world
  518. lights2[POINT_LIGHT_INDEX].pos.x = 1000*Fast_Cos(plight_ang);
  519. lights2[POINT_LIGHT_INDEX].pos.y = 100;
  520. lights2[POINT_LIGHT_INDEX].pos.z = 1000*Fast_Sin(plight_ang);
  521. if ((plight_ang+=3) > 360) 
  522.     plight_ang = 0;
  523. // move spot light source in ellipse around game world
  524. lights2[SPOT_LIGHT2_INDEX].pos.x = 1000*Fast_Cos(slight_ang);
  525. lights2[SPOT_LIGHT2_INDEX].pos.y = 200;
  526. lights2[SPOT_LIGHT2_INDEX].pos.z = 1000*Fast_Sin(slight_ang);
  527. if ((slight_ang-=5) < 0)
  528.     slight_ang = 360;
  529. // generate camera matrix
  530. Build_CAM4DV1_Matrix_Euler(&cam, CAM_ROT_SEQ_ZYX);
  531. // use these to rotate objects
  532. static float x_ang = 0, y_ang = 0, z_ang = 0;
  533. //////////////////////////////////////////////////////////////////////////
  534. // flat shaded textured cube
  535. // reset the object (this only matters for backface and object removal)
  536. Reset_OBJECT4DV2(obj_work);
  537. // generate rotation matrix around y axis
  538. Build_XYZ_Rotation_MATRIX4X4(x_ang, y_ang, z_ang, &mrot);
  539. // rotate the local coords of the object
  540. Transform_OBJECT4DV2(obj_work, &mrot, TRANSFORM_LOCAL_TO_TRANS,1);
  541. // perform world transform
  542. Model_To_World_OBJECT4DV2(obj_work, TRANSFORM_TRANS_ONLY);
  543. // insert the object into render list
  544. Insert_OBJECT4DV2_RENDERLIST4DV2(&rend_list, obj_work,0);
  545. // reset number of polys rendered
  546. debug_polys_rendered_per_frame = 0;
  547. debug_polys_lit_per_frame = 0;
  548. // update rotation angles
  549. if ((x_ang+=.2) > 360) x_ang = 0;
  550. if ((y_ang+=.4) > 360) y_ang = 0;
  551. if ((z_ang+=.8) > 360) z_ang = 0;
  552. // remove backfaces
  553. if (backface_mode==1)
  554.    Remove_Backfaces_RENDERLIST4DV2(&rend_list, &cam);
  555. // apply world to camera transform
  556. World_To_Camera_RENDERLIST4DV2(&rend_list, &cam);
  557. // clip the polygons themselves now
  558. Clip_Polys_RENDERLIST4DV2(&rend_list, &cam, ((x_clip_mode == 1) ? CLIP_POLY_X_PLANE : 0) | 
  559.                                             ((y_clip_mode == 1) ? CLIP_POLY_Y_PLANE : 0) | 
  560.                                             ((z_clip_mode == 1) ? CLIP_POLY_Z_PLANE : 0) );
  561. // light scene all at once 
  562. if (lighting_mode==1)
  563.    {
  564.    Transform_LIGHTSV2(lights2, 4, &cam.mcam, TRANSFORM_LOCAL_TO_TRANS);
  565.    Light_RENDERLIST4DV2_World2_16(&rend_list, &cam, lights2, 4);
  566.    } // end if
  567. // sort the polygon list (hurry up!)
  568. if (zsort_mode == 1)
  569.    Sort_RENDERLIST4DV2(&rend_list,  SORT_POLYLIST_AVGZ);
  570. // apply camera to perspective transformation
  571. Camera_To_Perspective_RENDERLIST4DV2(&rend_list, &cam);
  572. // apply screen transform
  573. Perspective_To_Screen_RENDERLIST4DV2(&rend_list, &cam);
  574. // lock the back buffer
  575. DDraw_Lock_Back_Surface();
  576. // reset number of polys rendered
  577. debug_polys_rendered_per_frame = 0;
  578. // render the object
  579. if (wireframe_mode  == 0)
  580.    Draw_RENDERLIST4DV2_Wire16(&rend_list, back_buffer, back_lpitch);
  581. else
  582. if (wireframe_mode  == 1)
  583.    Draw_RENDERLIST4DV2_Solid16(&rend_list, back_buffer, back_lpitch);
  584. // unlock the back buffer
  585. DDraw_Unlock_Back_Surface();
  586. sprintf(work_string,"Lighting [%s]: Ambient=%d, Infinite=%d, Point=%d, Spot=%d, BckFceRM [%s]", 
  587.                                                                                  ((lighting_mode == 1) ? "ON" : "OFF"),
  588.                                                                                  lights2[AMBIENT_LIGHT_INDEX].state,
  589.                                                                                  lights2[INFINITE_LIGHT_INDEX].state, 
  590.                                                                                  lights2[POINT_LIGHT_INDEX].state,
  591.                                                                                  lights2[SPOT_LIGHT2_INDEX].state,
  592.                                                                                  ((backface_mode == 1) ? "ON" : "OFF"));
  593. Draw_Text_GDI(work_string, 0, WINDOW_HEIGHT-34, RGB(0,255,0), lpddsback);
  594. sprintf(work_string,"X Clipping [%s], Y Clipping [%s], Z Clipping [%s],  ",((x_clip_mode == 1) ? "ON" : "OFF") ,
  595.                                                                            ((y_clip_mode == 1) ? "ON" : "OFF") ,
  596.                                                                            ((z_clip_mode == 1) ? "ON" : "OFF") );
  597. Draw_Text_GDI(work_string, 0, WINDOW_HEIGHT-34-16, RGB(0,255,0), lpddsback);
  598. // draw instructions
  599. Draw_Text_GDI("Press ESC to exit. Press <H> for Help.", 0, 0, RGB(0,255,0), lpddsback);
  600. // should we display help
  601. int text_y = 16;
  602. if (help_mode==1)
  603.     {
  604.     // draw help menu
  605.     Draw_Text_GDI("<A>..............Toggle ambient light source.", 0, text_y+=12, RGB(255,255,255), lpddsback);
  606.     Draw_Text_GDI("<I>..............Toggle infinite light source.", 0, text_y+=12, RGB(255,255,255), lpddsback);
  607.     Draw_Text_GDI("<P>..............Toggle point light source.", 0, text_y+=12, RGB(255,255,255), lpddsback);
  608.     Draw_Text_GDI("<S>..............Toggle spot light source.", 0, text_y+=12, RGB(255,255,255), lpddsback);
  609.     Draw_Text_GDI("<W>..............Toggle wire frame/solid mode.", 0, text_y+=12, RGB(255,255,255), lpddsback);
  610.     Draw_Text_GDI("<B>..............Toggle backface removal.", 0, text_y+=12, RGB(255,255,255), lpddsback);
  611.     Draw_Text_GDI("<X>..............Toggle X axis clipping.", 0, text_y+=12, RGB(255,255,255), lpddsback);
  612.     Draw_Text_GDI("<Y>..............Toggle Y axis clipping.", 0, text_y+=12, RGB(255,255,255), lpddsback);
  613.     Draw_Text_GDI("<Z>..............Toggle Z axis clipping.", 0, text_y+=12, RGB(255,255,255), lpddsback);
  614.     Draw_Text_GDI("<O>..............Select next object.", 0, text_y+=12, RGB(255,255,255), lpddsback);
  615.     Draw_Text_GDI("<H>..............Toggle Help.", 0, text_y+=12, RGB(255,255,255), lpddsback);
  616.     Draw_Text_GDI("<ESC>............Exit demo.", 0, text_y+=12, RGB(255,255,255), lpddsback);
  617.     } // end help
  618. sprintf(work_string,"Polys Rendered: %d, Polys lit: %d", debug_polys_rendered_per_frame, debug_polys_lit_per_frame);
  619. Draw_Text_GDI(work_string, 0, WINDOW_HEIGHT-34-16-16, RGB(0,255,0), lpddsback);
  620. sprintf(work_string,"CAM [%5.2f, %5.2f, %5.2f], OBJ [%5.2f, %5.2f, %5.2f], Near_Z = %5.2f, Far_Z = %5.2f", 
  621.                             cam.pos.x, cam.pos.y, cam.pos.z, 
  622.                             obj_work->world_pos.x,  obj_work->world_pos.y,  obj_work->world_pos.z,  
  623.                             cam.near_clip_z, cam.far_clip_z);
  624. Draw_Text_GDI(work_string, 0, WINDOW_HEIGHT-34-16-16-16, RGB(0,255,0), lpddsback);
  625. // flip the surfaces
  626. DDraw_Flip();
  627. // sync to 30ish fps
  628. Wait_Clock(30);
  629. // check of user is trying to exit
  630. if (KEY_DOWN(VK_ESCAPE) || keyboard_state[DIK_ESCAPE])
  631.     {
  632.     PostMessage(main_window_handle, WM_DESTROY,0,0);
  633.     } // end if
  634. // return success
  635. return(1);
  636.  
  637. } // end Game_Main
  638. //////////////////////////////////////////////////////////