direct 中的世界矩阵和网面 /* * Demo Name: Shapes * Author: cl_gamer@qq.com */ #include <windows.h> #include <assert.h> #include <d3d9.h> #include <d3dx9.h> #define WIN_CLASS "Tenderfoot" #define WIN_TITLE "Tenderfoot Shapes" #define WIN_WIDTH 800 #define WIN_HEIGHT 600 #pragma comment (lib, "d3d9.lib") #pragma comment (lib, "d3dx9.lib") // function bool InitD3D(HWND hWnd, bool fullscreen); bool InitObjects(); void RenderScene(); void Shutdown(); // globals // d3d LPDIRECT3D9 g_D3D = NULL; LPDIRECT3DDEVICE9 g_D3DD = NULL; // matrices D3DXMATRIX g_projection; D3DXMATRIX g_ViewMatrix; D3DXMATRIX g_WorldMatrix; // Mesh objects LPD3DXMESH g_teapot = NULL; LPD3DXMESH g_cube = NULL; LPD3DXMESH g_sphere = NULL; LPD3DXMESH g_torus = NULL; LRESULT WINAPI MsgProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) { switch ( msg ) { case WM_DESTROY: PostQuitMessage(0); break; case WM_KEYUP: if( VK_ESCAPE == wParam ) { PostQuitMessage(0); } break; default: break; } return DefWindowProc(hwnd, msg, wParam, lParam); } int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { // desige window class WNDCLASSEX wcex; wcex.cbSize = sizeof( wcex ); wcex.style = CS_CLASSDC; wcex.lpfnWndProc = (WNDPROC)MsgProc; wcex.cbClsExtra = 0L; wcex.cbWndExtra = 0L; wcex.hInstance = hInstance; wcex.hIcon = NULL; wcex.hCursor = NULL; wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1); wcex.lpszMenuName = NULL; wcex.lpszClassName = WIN_CLASS; wcex.hIconSm = NULL; // register window class RegisterClassEx( &wcex ); // Create the application's window HWND hwnd = CreateWindowEx( 0, WIN_CLASS, WIN_TITLE, WS_OVERLAPPEDWINDOW, 0, 0, WIN_WIDTH, WIN_HEIGHT, (HWND)NULL, (HMENU)NULL, hInstance, NULL ); // Show and uptata window ShowWindow( hwnd, SW_SHOWDEFAULT ); UpdateWindow( hwnd ); // Message loop MSG msg; ZeroMemory( &msg, sizeof(msg) ); if(!InitD3D(hwnd, false)) { return 0; } while( msg.message != WM_QUIT ) { if( PeekMessage( &msg, NULL, 0U, 0U, PM_REMOVE ) ) { TranslateMessage( &msg ); DispatchMessage( &msg ); } else { RenderScene(); } } // Release any and all resource Shutdown(); UnregisterClass( WIN_CLASS, wcex.hInstance ); return 0; } bool InitD3D(HWND hwnd, bool fullscreen) { if( NULL == ( g_D3D = Direct3DCreate9( D3D_SDK_VERSION ) ) ) { MessageBox( NULL, "fail to create D3D !", "Error", MB_OK ); return false; } // get the desktop display mode D3DDISPLAYMODE d3ddm; if( D3D_OK != g_D3D->GetAdapterDisplayMode( D3DADAPTER_DEFAULT, &d3ddm ) ) { MessageBox( NULL, "fail to GetAdapterDisplayMode !", "Error", MB_OK ); return false; } // Set up the sturcte used to create the d3dd D3DPRESENT_PARAMETERS d3dpp; ZeroMemory( &d3dpp, sizeof( d3dpp ) ); if (fullscreen) { d3dpp.BackBufferWidth = WIN_WIDTH; d3dpp.BackBufferHeight = WIN_HEIGHT; } d3dpp.Windowed = !fullscreen; d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD; d3dpp.BackBufferFormat = d3ddm.Format; // Create the D3DDevice if ( FAILED( g_D3D->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hwnd, D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &g_D3DD ) ) ) { return false; } // Set the projection matrix D3DXMatrixPerspectiveFovLH( &g_projection, 45.0f, WIN_WIDTH/WIN_HEIGHT, 0.1f, 1000.0f); g_D3DD->SetTransform( D3DTS_PROJECTION, &g_projection ); // Initialize any objects we will be displaying if(InitObjects()) { return true; } return true; } bool InitObjects() { // set default rendering states g_D3DD->SetRenderState(D3DRS_LIGHTING, FALSE); // Create the objects if(FAILED(D3DXCreateTeapot(g_D3DD, &g_teapot, NULL))) { return 0; } if(FAILED(D3DXCreateBox(g_D3DD, 2, 2, 2, &g_cube, NULL))) { return 0; } if(FAILED(D3DXCreateSphere(g_D3DD, 1.5, 25, 25, &g_sphere, NULL))) { return 0; } if(FAILED(D3DXCreateTorus(g_D3DD, 0.5f, 1.2f, 25, 25, &g_torus, NULL))) { return 0; } // Define camera information. D3DXVECTOR3 cameraPos(0.0f, 0.0f, -8.0f); D3DXVECTOR3 lookAtPos(0.0f, 0.0f, 0.0f); D3DXVECTOR3 upDir(0.0f, 1.0f, 0.0f); // bulid view matrix D3DXMatrixLookAtLH(&g_ViewMatrix, &cameraPos, &lookAtPos, &upDir); return true; } void RenderScene() { assert(g_D3DD); // clear the backbuffer g_D3DD->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0, 0, 0), 1.0F, 0); // begin the scene. start rendering g_D3DD->BeginScene(); // apply the view g_D3DD->SetTransform(D3DTS_VIEW, &g_ViewMatrix); // Draw teapot D3DXMatrixTranslation(&g_WorldMatrix, 2.0f, -2.0, 0.0f); g_D3DD->SetTransform(D3DTS_WORLD, &g_WorldMatrix); g_teapot->DrawSubset(0); // draw cube D3DXMatrixTranslation(&g_WorldMatrix, -2.0f, -2.0f, 0.0f); g_D3DD->SetTransform(D3DTS_WORLD, &g_WorldMatrix); g_cube->DrawSubset(0); // draw sphere D3DXMatrixTranslation(&g_WorldMatrix, 2.0f, 2.0, 0.0f); g_D3DD->SetTransform(D3DTS_WORLD, &g_WorldMatrix); g_sphere->DrawSubset(0); // draw torus D3DXMatrixTranslation(&g_WorldMatrix, -2.0f, 2.0, 0.0f); g_D3DD->SetTransform(D3DTS_WORLD, &g_WorldMatrix); g_torus->DrawSubset(0); // end the scene. stop rendering g_D3DD->EndScene(); // display the scene g_D3DD->Present(NULL, NULL, NULL, NULL); } void Shutdown() { // Release all resource. if(NULL != g_D3DD) { g_D3DD->Release(); } if(NULL != g_D3D) { g_D3D->Release(); } if(NULL != g_teapot) { g_teapot->Release(); } if(NULL != g_cube) { g_cube->Release(); g_teapot = NULL; } if(NULL != g_sphere) { g_sphere->Release(); g_cube = NULL; } if(NULL != g_torus) { g_torus->Release(); g_torus = NULL; } }