DX加载.x文件实例

#include <windows.h> #include <d3dx9.h> #include <vector> using namespace std; HINSTANCE hInstance; IDirect3DDevice9 *Device = 0; const D3DXCOLOR WHITE( D3DCOLOR_XRGB(255, 255, 255) ); //设置全局变量,一个ID3DXMesh对象存储从XFile中加载的网格数据,另外两个变量分别存储该网格的材质和纹理数据 ID3DXMesh *Mesh = 0; vector<D3DMATERIAL9>Mtrls(0); vector<IDirect3DTexture9*>Textures(0); bool Setup() { //导入XFile数据 ID3DXBuffer* adjBuffer = 0; ID3DXBuffer* mtrlBuffer = 0; DWORD numMtrls = 0; D3DXLoadMeshFromX("bigship1.x", D3DXMESH_MANAGED, Device, &adjBuffer, &mtrlBuffer, 0, &numMtrls, &Mesh); //加载XFile文件后,我们必须遍历D3DXMATRIAL数组中的元素,并加载该网格所引用的纹理数据 if(mtrlBuffer!=0 && numMtrls!=0) { D3DXMATERIAL *mtrls = (D3DXMATERIAL*)mtrlBuffer->GetBufferPointer(); for(int i=0; i<numMtrls; i++) { //设置MatD3D的属性 mtrls[i].MatD3D.Ambient = mtrls[i].MatD3D.Diffuse; //保存第i个材质 Mtrls.push_back(mtrls[i].MatD3D); //检查第i个材质是否有纹理 if(mtrls[i].pTextureFilename != 0) { //是的话导入纹理信息 IDirect3DTexture9* tex = 0; D3DXCreateTextureFromFile(Device, mtrls[i].pTextureFilename, &tex); //保存导入的纹理 Textures.push_back(tex); } else { //不是的话 Textures.push_back(0); } } } Mesh->OptimizeInplace( D3DXMESHOPT_ATTRSORT | D3DXMESHOPT_COMPACT | D3DXMESHOPT_VERTEXCACHE, (DWORD*)adjBuffer->GetBufferPointer(), 0, 0, 0); // // Set texture filters. // Device->SetSamplerState(0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR); Device->SetSamplerState(0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR); Device->SetSamplerState(0, D3DSAMP_MIPFILTER, D3DTEXF_POINT); // // Set Lights. // D3DLIGHT9 dir; ZeroMemory(&dir, sizeof(dir)); dir.Type = D3DLIGHT_DIRECTIONAL; dir.Diffuse = WHITE; dir.Specular = WHITE * 0.3f; dir.Ambient = WHITE * 0.6f; dir.Direction = D3DXVECTOR3(1.0f, 0.0f, 0.0f); // // Set and Enable the light. // Device->SetLight(0, &dir); Device->LightEnable(0, true); // // Set camera. // D3DXVECTOR3 pos(4.0f, 4.0f, -13.0f); D3DXVECTOR3 target(0.0f, 0.0f, 0.0f); D3DXVECTOR3 up(0.0f, 1.0f, 0.0f); D3DXMATRIX V; D3DXMatrixLookAtLH( &V, &pos, &target, &up); Device->SetTransform(D3DTS_VIEW, &V); // // Set projection matrix. // D3DXMATRIX proj; D3DXMatrixPerspectiveFovLH( &proj, D3DX_PI * 0.5f, // 90 - degree 800.0f/600.0f, 1.0f, 1000.0f); Device->SetTransform(D3DTS_PROJECTION, &proj); return true; } void Cleanup() { } void Display(float time) { if(Device) { static float y = 0.0f; D3DXMATRIX yRot; D3DXMatrixRotationY(&yRot, y); y = y + time; if(y>6.28f) y = 0.0f; D3DXMATRIX World = yRot; Device ->SetTransform(D3DTS_WORLD, &World); Device ->Clear(0, 0, D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER, 0x0000ffff, 1.0f, 0); Device ->BeginScene(); for(int i=0; i<Mtrls.size(); i++) { Device ->SetMaterial(&Mtrls[i]); Device ->SetTexture(0, Textures[i]); Mesh ->DrawSubset(i); } Device ->EndScene(); Device ->Present(0, 0, 0, 0); } } LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) { switch(uMsg) { case WM_DESTROY: PostQuitMessage(0); break; case WM_KEYDOWN: if( wParam == VK_ESCAPE ) DestroyWindow(hWnd); break; default: return DefWindowProc(hWnd, uMsg, wParam, lParam); } return 0; } int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd) { MSG msg; ZeroMemory(&msg, sizeof(msg)); WNDCLASSEX wc; wc.cbClsExtra = NULL; wc.cbSize = sizeof(wc); wc.cbWndExtra = NULL; wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH); wc.hCursor = LoadCursor(NULL, IDC_ARROW); wc.hIcon = LoadIcon(NULL, IDI_WINLOGO); wc.hIconSm = NULL; wc.hInstance = hInstance; wc.lpfnWndProc = WndProc; wc.lpszClassName = "draw"; wc.lpszMenuName = NULL; wc.style = CS_DROPSHADOW; RegisterClassEx(&wc); HWND hWnd = CreateWindowEx(WS_EX_COMPOSITED, "draw", "draw", WS_HSCROLL, 100, 100, 800, 600, NULL, NULL, hInstance, NULL); ShowWindow(hWnd, SW_NORMAL); UpdateWindow(hWnd); IDirect3D9 *D3D9 = 0; D3D9 = Direct3DCreate9(D3D_SDK_VERSION); D3DCAPS9 caps; D3D9 ->GetDeviceCaps(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, &caps); int vp = 0; if(caps.DevCaps & D3DDEVCAPS_HWTRANSFORMANDLIGHT) vp = D3DCREATE_HARDWARE_VERTEXPROCESSING; else vp = D3DCREATE_SOFTWARE_VERTEXPROCESSING; D3DPRESENT_PARAMETERS d3dpp; d3dpp.BackBufferWidth = 800; d3dpp.BackBufferHeight = 600; d3dpp.BackBufferFormat = D3DFMT_A8R8G8B8; d3dpp.BackBufferCount = 1; d3dpp.MultiSampleType = D3DMULTISAMPLE_NONE; d3dpp.MultiSampleQuality = 0; d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD; d3dpp.hDeviceWindow = hWnd; d3dpp.Windowed = true; d3dpp.EnableAutoDepthStencil = true; d3dpp.AutoDepthStencilFormat = D3DFMT_D24S8; d3dpp.Flags = 0; d3dpp.FullScreen_RefreshRateInHz = D3DPRESENT_RATE_DEFAULT; d3dpp.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE; HRESULT hr = D3D9-> CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd, vp, &d3dpp, &Device); while(msg.message != WM_QUIT) { if(PeekMessage(&msg, NULL, NULL, NULL, PM_REMOVE)) { TranslateMessage(&msg); DispatchMessage(&msg); } else { Setup(); Display(0.016f); } } return 0; }

这个实例可以看出之前的学习相对没有什么效率,很多效果,如灯光等等,都是之前做过的,可是现在又有点模糊了的。所以这次可以对后面的学习一个比较好的总结

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值