现贴上代码 /* * demo name: light * data: 09/07/04 * author: cl_gamer@qq.com */ /*************************************************************** 在direct3d中使用光照。 D3DLIGHT9 D3DMATERIAL9 ***************************************************************/ #include <d3d9.h> #include <d3dx9.h> #include <assert.h> #define WIN_CLASS "light" #define WIN_TITLE "DEMO: light" #define WIN_WIDTH 800 #define WIN_HEIGHT 600 // functions bool InitD3D(HWND hwnd, bool windowed); bool InitObjects(); void RenderScene(); void Shutdown(); // D3D object and device LPDIRECT3D9 g_D3D = NULL; LPDIRECT3DDEVICE9 g_D3DD = NULL; // matrices D3DXMATRIX g_ProjectionMatrix; // 投影矩阵 D3DXMATRIX g_ViewMatrix; // 视图矩阵 D3DXMATRIX g_WorldMatrix; // 世界矩阵 // mesh LPD3DXMESH g_teapot = NULL; LPD3DXMESH g_cube = NULL; LPD3DXMESH g_sphere = NULL; LPD3DXMESH g_torus = NULL; D3DMATERIAL9 g_Material; // scene light source D3DLIGHT9 g_Light; #pragma comment (lib, "d3d9.lib") #pragma comment (lib, "d3dx9.lib") 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 ph, LPSTR cmd, int s) { // 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 ); if(!InitD3D(hwnd, true)) { return false; } // Show and uptata window ShowWindow( hwnd, SW_SHOWDEFAULT ); UpdateWindow( hwnd ); // message loop MSG msg; ZeroMemory( &msg, sizeof(msg) ); while ( WM_QUIT != msg.message ) { if(PeekMessage(&msg, NULL, 0U, 0U, PM_REMOVE)) { TranslateMessage(&msg); DispatchMessage(&msg); } else { RenderScene(); } } // unregister UnregisterClass(WIN_CLASS, wcex.hInstance); return 0; } /*********************************************************************** 原型:bool InitD3D(HWND hwnd, bool windowed = 1 ) 参数:hwnd 窗口句柄 Windowed 窗口模式标志. 默认为窗口模式 前置条件:hwnd不为空 返回值:函数成功执行返回 true 备注:函数执行成功,将为hwnd指向的窗口创建一个D3D对象和D3D设备,然后创建 世界矩阵,初始化渲染对象。 **********************************************************************/ bool InitD3D(HWND hwnd, bool windowed = 1) { // create the D3D object g_D3D = Direct3DCreate9(D3D_SDK_VERSION); assert(g_D3D); // get the desktop dissplay mode D3DDISPLAYMODE displayMode; if(FAILED(g_D3D->GetAdapterDisplayMode(D3DADAPTER_DEFAULT, &displayMode))) { MessageBox(NULL, "g_D3D->GetAdapterDisplayMode", "ERROR", MB_OK); return false; } // set up the structure used to create the D3DD D3DPRESENT_PARAMETERS d3dpp; ZeroMemory(&d3dpp, sizeof(d3dpp)); if(windowed) { ; } else { d3dpp.BackBufferWidth = WIN_WIDTH; d3dpp.BackBufferHeight = WIN_HEIGHT; } d3dpp.Windowed = windowed; d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD; d3dpp.BackBufferFormat = displayMode.Format; // create D3DD if(FAILED(g_D3D->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hwnd, D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &g_D3DD))) { MessageBox(NULL, "g_D3D->CreateDevice", "ERROR", MB_OK); return false; } // set the projection matrix D3DXMatrixPerspectiveFovLH(&g_ProjectionMatrix, 45.0f, WIN_WIDTH/WIN_HEIGHT, 0.1f, 1000.0f); g_D3DD->SetTransform(D3DTS_PROJECTION, &g_ProjectionMatrix); // 初始化渲染对象 if(!InitObjects()) { return false; } return true; } /********************************************************* 原型:bool InitObjects() 备注:执行成功后,渲染对象就绪,准备渲染 *********************************************************/ bool InitObjects() { // set default rendering states g_D3DD->SetRenderState(D3DRS_LIGHTING, TRUE); g_D3DD->SetRenderState(D3DRS_AMBIENT, D3DCOLOR_COLORVALUE(0.3f,0.3f,0.3f,1.0f)); // setup the light source g_Light.Type = D3DLIGHT_DIRECTIONAL; g_Light.Direction = D3DXVECTOR3(0.0f, 0.0f, 1.0f); g_Light.Diffuse.r = g_Light.Diffuse.g = 1; g_Light.Diffuse.b = g_Light.Diffuse.a = 1; g_Light.Specular.r = g_Light.Specular.g = 1; g_Light.Specular.b = g_Light.Specular.a = 1; // register the light g_D3DD->SetLight(0, &g_Light); g_D3DD->LightEnable(0, TRUE); // setup the material properties ZeroMemory(&g_Material, sizeof(g_Material)); g_Material.Diffuse.r = g_Material.Ambient.r = 0.6f; g_Material.Diffuse.g = g_Material.Ambient.g = 0.6f; g_Material.Diffuse.b = g_Material.Ambient.b = 0.7f; g_Material.Specular.r = 0.4f; g_Material.Specular.b = 0.4f; g_Material.Specular.b = 0.4f; g_Material.Power = 2.0f; // create the objects if(FAILED(D3DXCreateTeapot(g_D3DD, &g_teapot, NULL))) { MessageBox(NULL, "g_D3D->CreateDevice", "ERROR", MB_OK); return false; } if(FAILED(D3DXCreateBox(g_D3DD, 2, 2, 2, &g_cube, NULL))) { MessageBox(NULL, "g_D3D->CreateDevice", "ERROR", MB_OK); return false; } if(FAILED(D3DXCreateSphere(g_D3DD, 1.5, 25, 25, &g_sphere, NULL))) { MessageBox(NULL, "g_D3D->CreateDevice", "ERROR", MB_OK); return false; } if(FAILED(D3DXCreateTorus(g_D3DD, 0.5f, 1.2f, 25, 25, &g_torus, NULL))) { MessageBox(NULL, "g_D3D->CreateDevice", "ERROR", MB_OK); return false; } // define camera infomation D3DXVECTOR3 cameraPos(0.0f, 0.0f, -8.0f); D3DXVECTOR3 LookAtPos(0.0f, 0.0f, 0.0f); D3DXVECTOR3 upDir(0.0f, 1.0f, 0.0f); // build view matrix D3DXMatrixLookAtLH(&g_ViewMatrix, &cameraPos, &LookAtPos, &upDir); return true; } /********************************************************************** 原型:void RenderScene() 备注:渲染场景 **********************************************************************/ void RenderScene() { // cleat 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_D3DD->SetMaterial(&g_Material); g_teapot->DrawSubset(0); // draw cube D3DXMatrixTranslation(&g_WorldMatrix, -2.0f, -2.0, 0.0f); g_D3DD->SetTransform(D3DTS_WORLD, &g_WorldMatrix); g_D3DD->SetMaterial(&g_Material); g_cube->DrawSubset(0); // draw sphere D3DXMatrixTranslation(&g_WorldMatrix, 2.0f, 2.0, 0.0f); g_D3DD->SetTransform(D3DTS_WORLD, &g_WorldMatrix); g_D3DD->SetMaterial(&g_Material); g_sphere->DrawSubset(0); // draw torus D3DXMatrixTranslation(&g_WorldMatrix, -2.0f, 2.0, 0.0f); g_D3DD->SetTransform(D3DTS_WORLD, &g_WorldMatrix); g_D3DD->SetMaterial(&g_Material); g_torus->DrawSubset(0); // ender the scene. stop rendering g_D3DD->EndScene(); // display g_D3DD->Present(NULL, NULL, NULL, NULL); } /****************************************************************** 原型:void Shutdown() 功能:收尾,释放资源 *******************************************************************/ void Shutdown() { // release all resourse if(g_D3DD != NULL) g_D3DD->Release(); if(g_D3D != NULL) g_D3D->Release(); if(g_teapot != NULL) { g_teapot->Release(); g_teapot = NULL; } if(g_cube != NULL) { g_cube->Release(); g_cube = NULL; } if(g_sphere != NULL) { g_sphere->Release(); g_sphere = NULL; } if(g_torus != NULL) { g_torus->Release(); g_torus = NULL; } }