VC.NET Game Development Chapter II Create 3D Scene

xiaoxiao2021-03-06  42

The code is still a bit problem, wait for me to debug, I will write more detailed points, but the comment is very detailed, I don't think it will be explained. I don't know that the memory overflows me again. Yesterday's problems have been solved // **************************************** **************************************************** / / Description: / / Development environment: .NET 7.1.3091 // Operating system: WindowsXP SP2 // s D K: Microsoft DirectX 9.0 SDK // Computer: // CPU :: Inter 2.6C // Memory: 1G DDR400 // Graphics: FX5900 128MB // Hard disk: 120g * 2 8MB 7200 // motherboard: inter865pe // ******************************************** ****************************** # Pragma Comment (lib, "D3D9 .lib ") # Pragma Comment (lib," d3dx9.lib ") # Pragma Comment (lib," dinput8.lib ") # Pragma Comment (Lib," DxGuid.lib ")

#include #include #include #include

#define safe_release (p) {if (p) {(p) -> Release (); (p) = null;}}

#define _fullscreen // Settings Are Window Mode or full screen mode

// declare function int InitGraph (HWND hwnd); void InitInput (); void RefreshGraph (); void CloseGraph (); int PASCAL WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow); static BOOL InitWindow (HINSTANCE hInstance, INT ncmdshow; LRESULT CALLBACK WinProc (HWND HWND, UINT MESSAGE, WPARAM WPARAM, LPARAM LPARAM);

// Variable Description HWND hWnd; WNDCLASS WC; LPD3DXMESH Mesh; // model DWORD nMater; // number of material D3DMATERIAL9 * Mater; // Material LPDIRECT3DTEXTURE9 * Tex; // texture LPDIRECT3D9 pD3D; // D3D objects LPDIRECT3DDEVICE9 pDev; // D3D device D3DDISPLAYMODE d3ddm; // D3D display mode D3DPRESENT_PARAMETERS d3dpp; // D3D image display method LPDIRECTINPUT8 pInput; // DInput objects LPDIRECTINPUTDEVICE8 pInputDev; // DInput apparatus int mousex = -200, mousey = 25; // initializes the mouse position float x = 0, y = 0, z = 0; // protagonist position float eyeex = 0, Eyey = 0, Eyez = 0; // Eye location float height = 4; // The protagonist Height INTITGRAPH (HWND HWND) {IF (NULL == (PD3D = Direct3DCreate9)) Return -1; // Create a D3D object if (Failed (PD3D-> GetAdapterDisplayMode (D3DADADAPTER_DEFAULT, & D3DDM)) Return -1; // Get current display mode

ZeromeMory (& D3DPP, SIZEOF (D3DPP));

#if defined // If you need a program to run d3dpp.windowed = true; d3dpp.swapeffect = D3DSWAPEFFECT_DISCARD; #ENDIF

#if defined (_FullScreen) // If you need to run in full-screen mode d3dpp.Windowed = FALSE; d3dpp.hDeviceWindow = hwnd; d3dpp.SwapEffect = D3DSWAPEFFECT_FLIP; d3dpp.BackBufferCount = 1; // there is a back buffer d3dpp.BackBufferWidth; / / The screen width is 800 pixels D3dpp.backbufferHeight = 600; / / screen length is 600 pixels / / Immediately display refresh image D3DPP.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE; #ENDIF

D3dpp.backbufferformat = d3ddm.format; // Color depth of the desktop color depth (if not the D3DFMT_A8R8G8B8 OR D3DFMT_X8R8G8B8

// Turn on Auto Dexeration Buffer D3DPP.EnableAutodePthstencil = true; D3DPP.AutodePthstencilFormat = D3DFMT_D16; // 16-bit automatic depth cache

// Create a D3D device // ************************************************************ ***********problem appear************************************* **************************************** i (failed (PD3D-> CreateDevice (D3DADADAPTER_DEFAULT, D3DDEVTYPE_HAL, HWND, D3DCREATE_SOFTWARE_VERTEXPROCESSING, & D3DPP, & pDev))) // D3D device to create return 1; // if (FAILED (pD3D-> CreateDevice (D3DADAPTER_DEFAULT, D3DDEVTYPE_REF, hWnd, D3DCREATE_SOFTWARE_VERTEXPROCESSING, & d3dpp, & pDev))) // {// return false; //} // ******************************************************** *********************************************************** ************************************ / / ------------ -------------------------------------------------- ------------------------------------------------- PDEV -> SetRenderState (D3DRS_LIGHTING, FALSE); pDev-> SetRenderState (D3DRS_CULLMODE, D3DCULL_NONE); pDev-> SetRenderState (D3DRS_ZENABLE, TRUE); D3DXMATRIX matWorld; // world matrix defined D3DXMatrixIdentity (& matWorld); which is defined as the matrix, both Do not change the scene PDEV-> SetTransform (D3DTS_WORLD, & MATWORLD); // Set matrix D3DXMATRIX MATPROJ; / / Define Projection Matrix D3DXMatrixPersp EctiveFovlh (& matproj, D3DX_PI / 2, // The viewing angle is set to 90 degrees 800.0F / 600.0F, 0.01F, // eye can be seen more than 500.0F); // Eye can look far PDEV-> setTransform (D3DTS_PROJECTION, & MATPROJ); // Setting Matrix // ------------------------------------- -------------------------------------------------- ---------------------- LPD3DXBuffer PBuffer; // Temporary Cache D3DXMATERIAL * TMATER; // Temporary Material D3DXLoadMeshFromx ("Tiger.x", //. X file Name D3DXMESH_MANAGED, / / ​​Description Mesh is managed by DX Manage PDEV, // Just created D3D device null, // This can make an LPD3DXBUFFER object, save each triangle in the scene with that triangle contact & PBuffer, Null, & nmater, & mesh); // The group of things just defined above

TMATER = (D3DXMATERIAL *) PBuffer-> getBufferPointer (); // Get material data pointer Mater = new d3dmaterial9 [nmater]; tex = new lpdirect3dtexture9 [nmater]; for (DWORD i = 0; i Release (); dword * padj = new dword [mesh-> getnumface () * 3]; mesh-> generateadjacency (0.0f, padj); Mesh-> OptimizeInplace (D3DXMESHOPT_VERTEXCACHE, PADJ, NULL, NULL, NULL); return -1;} void InitInput () // initialize Dinput {DirectInput8Create (GetModuleHandle (NULL), DIRECTINPUT_VERSION, IID_IDirectInput8, (void **) & pInput, NULL); pInput-> CreateDevice (GUID_SysMouse, & pInputDev, NULL ); pInputDev-> SetDataFormat (& c_dfDIMouse); pInputDev-> SetCooperativeLevel (hWnd, DISCL_EXCLUSIVE | DISCL_FOREGROUND); pInputDev-> Acquire ();} void ReadMouse () // read mouse state {DIMOUSESTATE mouse_stat; // mouse state pInputDev -> getDeviceState (sizeof (limited) & mouse_stat); mousex = mouse_stat.lx; mousey = mo Use_stat.ly;}

Void refreshgraph () {if (mousey> 314) mousey = 314; if (mousey <-314) mousey = -314; Eyey = Y Height-Tan ((FLOAT) / 200); Eyex = x sin (Float) / 200); Eyez = z COS ((FLOAT) (Mousex) / 200); D3DXMAMATRIX Matview; D3DXMatrixlookatlh (& Matview, & D3DxVector3 (x, y height, z), & D3DXVector3 (Eyex, Eyey , Eyez), & D3DXVector3 (0.0F, 1.0F, 0.0F)); PDEV-> Settransform (D3DTS_VIEW, & MATVIEW); PDEV-> Clear (0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB (0, 0), 1.0 f, 0); PDEV-> Beginscene ();

For (unsigned long i = 0; i setMaterial (& mat [i]); // Set material PDEV-> setTexture (0, tex [i]); // Set the texture Mesh-> DrawSubset (i); // Rendering the triangular triangles of the material in i -th} PDEV-> endscene (); if (pdev-> present (null, null, null, null) == D3DERR_DEVICELOST) { // recovery equipment if (pDev-> TestCooperativeLevel () == D3DERR_DEVICENOTRESET) {pDev-> Reset (& d3dpp);

PDEV-> setrenderstate (D3DRS_Lighting, False); PDEV-> Setrenderstate (D3DRS_CULLMODE, D3DCULL_NONE); PDEV-> SetRenderstate (D3DRS_ZENABLE, TRUE);

D3DXMATRIX matWorld; // World matrix defined D3DXMatrixIdentity (& matWorld); pDev-> SetTransform (D3DTS_WORLD, & matWorld); // set matrix D3DXMATRIX matProj; // define Projection matrix D3DXMatrixPerspectiveFovLH (& matProj, D3DX_PI / 2, 800.0f / 600.0f, 0.01 f, 500.0f); PDEV-> SetTransform (D3DTS_Projection, & Matproj); // Set matrix}}}}}}}

Void closegraph () {// Release some resource SAFE_RELEASE (PDEV); Safe_Release (PINPUTDEV); Safe_Release (PDEV); Safe_Release (PD3D);

// ******************************************************** ********************** / / Function: WinMain () // Function: Windows program portfolio function. Create a main window, process message loop / / Date: 2005.1.3 22: 34 // Edize: Zhang Xiaobin // *********************************** ***************************************************** INT PASCAL WINMAIN (Hinstance Hinstance, Hinstance Hprevinstance, LPSTR LPCMDLINE, INT NCMDSHOW) // Hinstance: The handle of the application // hprevinstance: In order to maintain compatibility with 16-bit Windows applications // lpcmdline: Pointer to command line parameter strings // NCMDSHOW: Specifies the integer {if (! INITWINDOW (Hinstance, ncmdshow)) {if (! ") {if (hWnd," Create Window Error "," Error! ", MB_OK); Return False;} // Create a main window, If you are not successful, you return false and exit the program if (Initgraph (HWND)! = 1) {MessageBox (hwnd, "initialization image engine error!", "Error!", Mb_ok); return -1;} initInput () ;

MSG msg; // Enter message cycle for (;;) {if (PEEKMESSAG, NULL, 0, 0, PM_Remove) {if (msg.Message == WM_QUIT) Break; TranslateMessage (& MSG); DispatchMessage & msg);} else {readmouse (); refreshgraph ();}} Return (int) (msg.wparam);} // ********************* ***********************************************//function : INITWINDOW () // Function: Create window // Date: 2005.1.3 22: 43 // Editor: Zhang Xiaobin // ********************** *************************************************************** Static Bool INITWINDOW (Hinstance Hinstance, int ncmdshow) {// definition window style: WNDCLASS WC; wc.style = null; // Window style wc.lpfnwndproc = (wndproc) WinProc; // window message processing function pointer wc.cbclsextra = 0; / / Assign an additional byte number wc.cbwndextra = 0 after the window class; // Assign an additional byte number wc.hinstance = hinstance; //, the application of the application corresponding to the window Wc. Hicon = Null; // window icon wc.hcursor = null; wc.hbrbackground = null; // createsolidbrush (RGB (100, 0, 0)); // Dark red background wc.lpszMenuname = null; // window menu resource Name wc.lpszclassname = "my_test"; // Name of the window class RegisterClass (& WC); // Register window // Create a window hWnd = CREATEWINDOW ("My_TEST", "My First Program", // Window Title WS_POPUP | WS_MAXIMIZE, 0, 0, 800, 600, // GetSystemm Etrics (SM_CXSCREEN), / / ​​This function returns the screen width // getSystemMetrics (SM_CYSCREEN), / / ​​This function returns the screen height null, null, hinstance, null; if (! hw) return false; showwindow (hwnd, ncmdshow); // Display window updateWindow (hwnd); // Refresh window Return True;} // ****************************** ************************************* / / Function: WinProc () // Function: Process Window Message // Date: 2005.1.3 22: 56 // Edize: Zhang Xiaobin // ******************************************** *********************************** LRESULT CALLBACK WinProc (HWnd Hwnd, Uint Message, WPARAM WPARAM, LPARAM LPARAM ) {Switch {Case WM_SETCURSOR: SetCursor (null); Return 0;

Case WM_KeyDown: // Key Message Switch (WPARAM) {CASE VK_ESCAPE: // MessageBox (hwnd, "ESC button is pressing! OK, launch!", "keyboard", mb_ok); // PostMessage (hwnd, wm_close, 0, 0); // Send WM_Close Messages for Windows; Closegraph (); DestroyWindow (HWND); Break; Case 'W': x = Eyex-x; Z = Eyez-z; Break; Case 's': x - = Eyez-z; z- = Eyez-z; Break; Case 'A': x- = Eyez-z; z = Eyex-Eyez z; // Note x just changed Break; Case 'D': X = Eyez-z; z- = Eyex-x Eyez-z; break;} return 0; // After processing a message, return 0CASE WM_CLOSE: // Prepare to exit DESTROYWINDOW (HWND); // Release Window Return 0;

Case WM_RBUTTONDOWN: MessageBox (hwnd, "right click!", "mouse", mb_ok); return 0;

Case WM_DESTROY: // If the window is released ... postquitMessage (0); // Send a WM_QUIT message RETURN 0 to the window;} // Call the default message processing Return DefWindowProc (HWND, Message, WParam, LParam);

转载请注明原文地址:https://www.9cbs.com/read-59248.html

New Post(0)