Заголовочный файл:
#ifndef _DX3D_H_.
#define _DX3D_H_.
#include «d3d9.h» .
#include «d3dx9.h» .
HRESULT DXinit (IDirect3D9 **dxInterf, IDirect3DDevice9 **dxDev, HWND hWnd, DWORD width, DWORD height, BOOL fScrn);
DWORD LoadMesh (char *filename, IDirect3DDevice9 *dxDev, ID3DXMesh **dxMesh, LPDIRECT3DTEXTURE9 **dxTexture, char *texFilename, D3DMATERIAL9 **dxMaterial);
#endif.
Файл исходного кода:
#include «dx3D.h» .
HRESULT DXinit (IDirect3D9 **dxInterf, IDirect3DDevice9 **dxDev, HWND hWnd, DWORD width, DWORD height, BOOL fScrn).
{.
if ((*dxInterf = Direct3DCreate9(D3D_SDK_VERSION)) == NULL).
return E_FAIL;
D3DPRESENT_PARAMETERS dxPP;
ZeroMemory (&dxPP, sizeof (dxPP));
dxPP.BackBufferWidth = width;
dxPP.BackBufferHeight = height;
dxPP.AutoDepthStencilFormat = D3DFMT_D24S8;
dxPP.EnableAutoDepthStencil = TRUE;
RECT wndView;
RECT wndClient;
GetWindowRect (hWnd, &wndView);
GetClientRect (hWnd, &wndClient);
width = width + (wndView.right-wndView.left) — (wndClient.right-wndClient.left);
height = height + (wndView.bottom-wndView.top) — (wndClient.bottom-wndClient.top);
MoveWindow (hWnd, wndView. left, wndView. top, width, height, TRUE);
D3DDISPLAYMODE disMode;
(*dxInterf)->GetAdapterDisplayMode (D3DADAPTER_DEFAULT, &disMode);
dxPP.BackBufferFormat = disMode. Format;
dxPP.SwapEffect = D3DSWAPEFFECT_DISCARD;
dxPP.Windowed = TRUE;
HRESULT hRes;
DWORD fl = D3DCREATE_MIXED_VERTEXPROCESSING;
if (FAILED (hRes = (*dxInterf)->CreateDevice (D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd, fl, &dxPP, dxDev))).
return hRes;
float aspect = (float)dxPP.BackBufferWidth / (float)dxPP.BackBufferHeight;
D3DXMATRIX matr;
D3DXMatrixPerspectiveFovLH (&matr, D3DX_PI / 4.0f, aspect, 1.0f, 5000.0f);
- (*dxDev)->SetTransform (D3DTS_PROJECTION, &matr);
- (*dxDev)->SetRenderState (D3DRS_LIGHTING, TRUE);
return S_OK;
}.
DWORD LoadMesh (char *filename, IDirect3DDevice9 *dxDev, ID3DXMesh **dxMesh, LPDIRECT3DTEXTURE9 **dxTexture, char *texFilename, D3DMATERIAL9 **dxMaterial).
{.
LPD3DXBUFFER dxBuffer;
DWORD nMaterial;
D3DXLoadMeshFromX (filename, D3DXMESH_SYSTEMMEM, dxDev, NULL, &dxBuffer, NULL, &nMaterial, dxMesh);
D3DXMATERIAL* d3dxMaterials = (D3DXMATERIAL*)dxBuffer->GetBufferPointer ();
- (*dxTexture) = new LPDIRECT3DTEXTURE9[nMaterial];
- (*dxMaterial) = new D3DMATERIAL9[nMaterial];
for (DWORD i=0; i.
{.
(*dxMaterial)[i] = d3dxMaterials[i]. MatD3D;
if (FAILED (D3DXCreateTextureFromFile (dxDev, d3dxMaterials[i]. pTextureFilename, &(*dxTexture)[i]))).
if (FAILED (D3DXCreateTextureFromFile (dxDev, texFilename, &(*dxTexture)[i]))).
(*dxTexture)[i] = NULL;
}.
return nMaterial;
}.