#include "dxfunc.h"
#include <windows.h>
#include "d3d9.h"
#include "d3dx9.h"
// Функция инициализации Direct3D
HRESULT DX3DInit(IDirect3D9 **ppiD3D9,
IDirect3DDevice9 **ppiD3DDevice9,
HWND hWnd,
DWORD iWidth,
DWORD iHeight,
BOOL bFullScreen)
{
// Инициализация
if((*ppiD3D9 = Direct3DCreate9(D3D_SDK_VERSION)) == NULL)
return E_FAIL;
// Заполняем основные параметры представления
D3DPRESENT_PARAMETERS d3dpp;
ZeroMemory(&d3dpp, sizeof(d3dpp));
d3dpp.BackBufferWidth = iWidth;
d3dpp.BackBufferHeight = iHeight;
// Запрос на отображение в полноэкранном режиме
int iRes;
if (!bFullScreen)
iRes=MessageBox(hWnd, "Use fullscreen mode?", "Screen", MB_YESNO | MB_ICONQUESTION);
else
iRes = IDYES;
if(iRes == IDYES)
{
//////////////////////////////////////////////////////////
// Полноэкранный режим
//////////////////////////////////////////////////////////
// Установка параметров полноэкранного режима
d3dpp.BackBufferFormat = D3DFMT_R5G6B5;
d3dpp.SwapEffect = D3DSWAPEFFECT_FLIP;
d3dpp.Windowed = FALSE;
d3dpp.FullScreen_RefreshRateInHz = D3DPRESENT_RATE_DEFAULT;
d3dpp.PresentationInterval = D3DPRESENT_INTERVAL_DEFAULT;
}
else
{
//////////////////////////////////////////////////////////
// Оконный режим
//////////////////////////////////////////////////////////
RECT wndRect;
RECT clientRect;
GetWindowRect(hWnd, &wndRect);
GetClientRect(hWnd, &clientRect);
iWidth = iWidth + (wndRect.right-wndRect.left) - (clientRect.right-clientRect.left);
iHeight = iHeight + (wndRect.bottom-wndRect.top) - (clientRect.bottom-clientRect.top);
MoveWindow(hWnd, wndRect.left, wndRect.top, iWidth, iHeight, TRUE);
// Получить формат пикселя
D3DDISPLAYMODE d3ddm;
(*ppiD3D9)->GetAdapterDisplayMode(D3DADAPTER_DEFAULT, &d3ddm);
// Установка параметров
d3dpp.BackBufferFormat = d3ddm.Format;
d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
d3dpp.Windowed = TRUE;
}
DWORD Flags= D3DCREATE_MIXED_VERTEXPROCESSING;
// Создать 3D устройство
HRESULT hRes;
if(FAILED(hRes = (*ppiD3D9)->CreateDevice(
D3DADAPTER_DEFAULT,
D3DDEVTYPE_HAL, hWnd, Flags,
&d3dpp, ppiD3DDevice9)))
return hRes;
// Установить перспективу
float Aspect = (float)d3dpp.BackBufferWidth / (float)d3dpp.BackBufferHeight;
D3DXMATRIX matProjection;
D3DXMatrixPerspectiveFovLH(&matProjection, D3DX_PI/4.0f, Aspect, 0.001f, 1000.0f);
(*ppiD3DDevice9)->SetTransform(D3DTS_PROJECTION, &matProjection);
(*ppiD3DDevice9)->SetRenderState(D3DRS_LIGHTING, FALSE);
return S_OK;
}
HRESULT DX3DInitZ(IDirect3D9 **ppiD3D9,
IDirect3DDevice9 **ppiD3DDevice9,
HWND hWnd,
DWORD iWidth,
DWORD iHeight,
BOOL bFullScreen)
{
// Инициализация
if((*ppiD3D9 = Direct3DCreate9(D3D_SDK_VERSION)) == NULL)
return E_FAIL;
// Заполняем основные параметры представления
D3DPRESENT_PARAMETERS d3dpp;
ZeroMemory(&d3dpp, sizeof(d3dpp));
d3dpp.BackBufferWidth = iWidth;
d3dpp.BackBufferHeight = iHeight;
d3dpp.AutoDepthStencilFormat = D3DFMT_D24S8;
d3dpp.EnableAutoDepthStencil = TRUE;
// Запрос на отображение в полноэкранном режиме
int iRes;
if (!bFullScreen)
iRes=MessageBox(hWnd, "Use fullscreen mode?", "Screen", MB_YESNO | MB_ICONQUESTION);
else
iRes = IDYES;
if(iRes == IDYES)
{
//////////////////////////////////////////////////////////
// Полноэкранный режим
//////////////////////////////////////////////////////////
// Установка параметров полноэкранного режима
d3dpp.BackBufferFormat = D3DFMT_R5G6B5;
d3dpp.SwapEffect = D3DSWAPEFFECT_FLIP;
d3dpp.Windowed = FALSE;
d3dpp.FullScreen_RefreshRateInHz = D3DPRESENT_RATE_DEFAULT;
d3dpp.PresentationInterval = D3DPRESENT_INTERVAL_DEFAULT;
}
else
{
//////////////////////////////////////////////////////////
// Оконный режим
//////////////////////////////////////////////////////////
RECT wndRect;
RECT clientRect;
GetWindowRect(hWnd, &wndRect);
GetClientRect(hWnd, &clientRect);
iWidth = iWidth + (wndRect.right-wndRect.left) - (clientRect.right-clientRect.left);
iHeight = iHeight + (wndRect.bottom-wndRect.top) - (clientRect.bottom-clientRect.top);
MoveWindow(hWnd, wndRect.left, wndRect.top, iWidth, iHeight, TRUE);
// Получить формат пикселя
D3DDISPLAYMODE d3ddm;
(*ppiD3D9)->GetAdapterDisplayMode(D3DADAPTER_DEFAULT, &d3ddm);
// Установка параметров
d3dpp.BackBufferFormat = d3ddm.Format;
d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
d3dpp.Windowed = TRUE;
}
DWORD Flags= D3DCREATE_MIXED_VERTEXPROCESSING;
// Создать 3D устройство
HRESULT hRes;
if(FAILED(hRes = (*ppiD3D9)->CreateDevice(
D3DADAPTER_DEFAULT,
D3DDEVTYPE_HAL, hWnd, Flags,
&d3dpp, ppiD3DDevice9)))
return hRes;
// Установить перспективу
float Aspect = (float)d3dpp.BackBufferWidth / (float)d3dpp.BackBufferHeight;
D3DXMATRIX matProjection;
D3DXMatrixPerspectiveFovLH(&matProjection, D3DX_PI/4.0f, Aspect, 1.0f, 5000.0f);
(*ppiD3DDevice9)->SetTransform(D3DTS_PROJECTION, &matProjection);
(*ppiD3DDevice9)->SetRenderState(D3DRS_LIGHTING, TRUE);
return S_OK;
}
DWORD LoadMesh(char *filename, IDirect3DDevice9 *ppiD3DDevice9,
ID3DXMesh **ppMesh,
LPDIRECT3DTEXTURE9 **pMeshTextures,
char *texturefilename,
D3DMATERIAL9 **pMeshMaterials
)
{
LPD3DXBUFFER pD3DXMtrlBuffer;
DWORD dwNumMaterials;
D3DXLoadMeshFromX(filename, D3DXMESH_SYSTEMMEM,
ppiD3DDevice9, NULL, &pD3DXMtrlBuffer, NULL, &dwNumMaterials, ppMesh);
D3DXMATERIAL* d3dxMaterials = (D3DXMATERIAL*)pD3DXMtrlBuffer->GetBufferPointer();
(*pMeshTextures) = new LPDIRECT3DTEXTURE9[dwNumMaterials];
(*pMeshMaterials) = new D3DMATERIAL9[dwNumMaterials];
for( DWORD i=0; i<dwNumMaterials; i++ )
{
// Копируем материал
(*pMeshMaterials) = d3dxMaterials.MatD3D;
// Создаем текстуру
if( FAILED(D3DXCreateTextureFromFile(ppiD3DDevice9,
d3dxMaterials.pTextureFilename, &(*pMeshTextures))))
if( FAILED(D3DXCreateTextureFromFile(ppiD3DDevice9,
texturefilename, &(*pMeshTextures))))
(*pMeshTextures) = NULL;
}
// (ID3DXMesh *)(*ppMesh)->OptimizeInplace(D3DXMESHOPT_ATTRSORT, NULL, NULL, NULL, NULL);
return dwNumMaterials;
}
bool LoadTexture(DWORD* text_buf, int iWidth, int iHeight, char* filename)
{
HANDLE hFile = CreateFile(filename, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
if(hFile == INVALID_HANDLE_VALUE)
return FALSE;
BITMAPFILEHEADER bmpfilehdr;
DWORD dwRead;
if(!ReadFile(hFile, &bmpfilehdr, sizeof(bmpfilehdr), &dwRead, NULL))
return FALSE;
char* ptr=(char*)&bmpfilehdr.bfType;
if (*ptr!='B' || *++ptr!='M')
return FALSE;
BITMAPINFOHEADER bmpinfohdr;
if(!ReadFile(hFile, &bmpinfohdr, sizeof(bmpinfohdr), &dwRead, NULL))
return FALSE;
if (bmpinfohdr.biCompression!=BI_RGB)
return 0;
int iImageSize=bmpinfohdr.biSizeImage;
if (iImageSize==0)
iImageSize=((iWidth*4+3) & ~3)*iHeight;
BYTE* buf=new BYTE[iImageSize];
if(!ReadFile(hFile, buf, iImageSize, &dwRead, NULL))
return FALSE;
if (bmpinfohdr.biBitCount!=24)
return FALSE;
int bytesgiven=(iWidth*3+3) & ~3;
DWORD* btSurf = text_buf;
BYTE* imagebits = (BYTE*)(&buf[(iHeight-1)*bytesgiven]);
for (int i=0; i<iHeight; i++)
{
RGBTRIPLE* tlImage=(RGBTRIPLE*)imagebits;
for (int p=0; p<iWidth; p++)
{
*btSurf = (DWORD)((tlImage->rgbtBlue) | (tlImage->rgbtGreen << 8) |
(tlImage->rgbtRed << 16) | (0xFF << 24));
tlImage++;
btSurf++;
}
imagebits -= bytesgiven;
}
delete [] buf;
return TRUE;
}
анимация в Х-файле
как можно запустить анимацию(скелетную).если она содержится в самом файле ?
вот исходный код этого примера на directX 9 and c++
Код:
Код:
// TemplateProj.cpp : Template Direct3D project
//
#include <windows.h>
#include "d3d9.h"
#include "d3dx9.h"
#include "..\..\common\dxfunc.h"
// Global Variables:
char szWindowClass[] = "Direct3DTemplateProj";
char szTitle[] = "Direct3D Demo by Michael Flenov";
// Direct3D objects
IDirect3D9 *pD3D = NULL;
IDirect3DDevice9 *pD3DDevice = NULL;
int iWidth=800;
int iHeight=600;
float ViewAngle=0;
DWORD dwNumMaterials;
ID3DXMesh *pMesh;
LPDIRECT3DTEXTURE9 *pMeshTextures;
D3DMATERIAL9 *pMeshMaterials;
const char DIR[16] = "../../../media/";
// Forward declarations of functions included in this code module:
int PASCAL WinMain(HINSTANCE hInst, HINSTANCE hPrev, LPSTR szCmdLine, int nCmdShow);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
bool Init(HWND hWnd);
void GraphEngine();
int PASCAL WinMain(HINSTANCE hInst, HINSTANCE hPrev, LPSTR szCmdLine, int nCmdShow)
{
WNDCLASSEX wcex;
MSG msg;
HWND hWnd;
CoInitialize(NULL);
// Register window class
wcex.cbSize = sizeof(wcex);
wcex.style = CS_CLASSDC;
wcex.lpfnWndProc = (WNDPROC)WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInst;
wcex.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = NULL;
wcex.lpszMenuName = NULL;
wcex.lpszClassName = szWindowClass;
wcex.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
if(!RegisterClassEx(&wcex))
return FALSE;
// Create the main window
hWnd = CreateWindow(szWindowClass, szTitle, WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX,
CW_USEDEFAULT, CW_USEDEFAULT, iWidth, iHeight, NULL, NULL, hInst, NULL);
if(!hWnd)
return FALSE;
ShowWindow(hWnd, SW_NORMAL);
UpdateWindow(hWnd);
if(Init(hWnd) == TRUE)
{
while (true)
{
if (PeekMessage(&msg, NULL, NULL, NULL, PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
if (msg.message == WM_QUIT) break;
}
GraphEngine();
}
}
if (pD3DDevice) {pD3DDevice= NULL; pD3DDevice=NULL;}
if (pD3D) {pD3D= NULL; pD3D=NULL;}
CoUninitialize();
return 0;
}
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch(message) {
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
bool Init(HWND hWnd)
{
if (DX3DInitZ(&pD3D, &pD3DDevice, hWnd, iWidth, iHeight, FALSE)!=S_OK)
{
MessageBox(hWnd, "DirectX Initialize Error", "Error", MB_OK);
return FALSE;
}
SetCurrentDirectory(DIR);
dwNumMaterials = LoadMesh("tiny.x", pD3DDevice,
&pMesh, &pMeshTextures,
"texture.bmp", &pMeshMaterials
);
D3DXMATRIXA16 mat;
D3DXMatrixIdentity(&mat);
D3DXMatrixRotationZ(&mat, 90);
pD3DDevice->SetTransform(D3DTS_VIEW, &mat);
D3DLIGHT9 light;
ZeroMemory(&light, sizeof(D3DLIGHT9));
light.Type = D3DLIGHT_DIRECTIONAL;
light.Direction = D3DXVECTOR3(0.5f, 0.0f, 0.5f);
light.Diffuse.r = light.Diffuse.g = light.Diffuse.b = light.Diffuse.a = 1.0f;
pD3DDevice->SetLight(0, &light);
pD3DDevice->LightEnable(0, TRUE);
return TRUE;
}
void GraphEngine()
{
pD3DDevice->Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(255,255,255), 1.0f, 0);
if (SUCCEEDED(pD3DDevice->BeginScene()))
{
ViewAngle+=0.01f;
float b = ViewAngle;
D3DMATRIX World = {
cos(b)*cos(b), cos(b)*sin(b), sin(b), 0,
-sin(b), cos(b), 0, 0,
-sin(b)*cos(b), -sin(b)*sin(b), cos(b), 0,
0, 0, 700, 1,
};
pD3DDevice->SetTransform(D3DTS_WORLD, &World);
pD3DDevice->SetRenderState(D3DRS_LIGHTING, TRUE);
for (DWORD i=0; i<dwNumMaterials; i++)
{
pD3DDevice->SetMaterial(&pMeshMaterials);
// if (pMeshTextures)
// pD3DDevice->SetTexture(0, pMeshTextures);
pMesh->DrawSubset(i);
}
pD3DDevice->EndScene();
}
pD3DDevice->Present(NULL, NULL, NULL, NULL);
}
//
#include <windows.h>
#include "d3d9.h"
#include "d3dx9.h"
#include "..\..\common\dxfunc.h"
// Global Variables:
char szWindowClass[] = "Direct3DTemplateProj";
char szTitle[] = "Direct3D Demo by Michael Flenov";
// Direct3D objects
IDirect3D9 *pD3D = NULL;
IDirect3DDevice9 *pD3DDevice = NULL;
int iWidth=800;
int iHeight=600;
float ViewAngle=0;
DWORD dwNumMaterials;
ID3DXMesh *pMesh;
LPDIRECT3DTEXTURE9 *pMeshTextures;
D3DMATERIAL9 *pMeshMaterials;
const char DIR[16] = "../../../media/";
// Forward declarations of functions included in this code module:
int PASCAL WinMain(HINSTANCE hInst, HINSTANCE hPrev, LPSTR szCmdLine, int nCmdShow);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
bool Init(HWND hWnd);
void GraphEngine();
int PASCAL WinMain(HINSTANCE hInst, HINSTANCE hPrev, LPSTR szCmdLine, int nCmdShow)
{
WNDCLASSEX wcex;
MSG msg;
HWND hWnd;
CoInitialize(NULL);
// Register window class
wcex.cbSize = sizeof(wcex);
wcex.style = CS_CLASSDC;
wcex.lpfnWndProc = (WNDPROC)WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInst;
wcex.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = NULL;
wcex.lpszMenuName = NULL;
wcex.lpszClassName = szWindowClass;
wcex.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
if(!RegisterClassEx(&wcex))
return FALSE;
// Create the main window
hWnd = CreateWindow(szWindowClass, szTitle, WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX,
CW_USEDEFAULT, CW_USEDEFAULT, iWidth, iHeight, NULL, NULL, hInst, NULL);
if(!hWnd)
return FALSE;
ShowWindow(hWnd, SW_NORMAL);
UpdateWindow(hWnd);
if(Init(hWnd) == TRUE)
{
while (true)
{
if (PeekMessage(&msg, NULL, NULL, NULL, PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
if (msg.message == WM_QUIT) break;
}
GraphEngine();
}
}
if (pD3DDevice) {pD3DDevice= NULL; pD3DDevice=NULL;}
if (pD3D) {pD3D= NULL; pD3D=NULL;}
CoUninitialize();
return 0;
}
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch(message) {
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
bool Init(HWND hWnd)
{
if (DX3DInitZ(&pD3D, &pD3DDevice, hWnd, iWidth, iHeight, FALSE)!=S_OK)
{
MessageBox(hWnd, "DirectX Initialize Error", "Error", MB_OK);
return FALSE;
}
SetCurrentDirectory(DIR);
dwNumMaterials = LoadMesh("tiny.x", pD3DDevice,
&pMesh, &pMeshTextures,
"texture.bmp", &pMeshMaterials
);
D3DXMATRIXA16 mat;
D3DXMatrixIdentity(&mat);
D3DXMatrixRotationZ(&mat, 90);
pD3DDevice->SetTransform(D3DTS_VIEW, &mat);
D3DLIGHT9 light;
ZeroMemory(&light, sizeof(D3DLIGHT9));
light.Type = D3DLIGHT_DIRECTIONAL;
light.Direction = D3DXVECTOR3(0.5f, 0.0f, 0.5f);
light.Diffuse.r = light.Diffuse.g = light.Diffuse.b = light.Diffuse.a = 1.0f;
pD3DDevice->SetLight(0, &light);
pD3DDevice->LightEnable(0, TRUE);
return TRUE;
}
void GraphEngine()
{
pD3DDevice->Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(255,255,255), 1.0f, 0);
if (SUCCEEDED(pD3DDevice->BeginScene()))
{
ViewAngle+=0.01f;
float b = ViewAngle;
D3DMATRIX World = {
cos(b)*cos(b), cos(b)*sin(b), sin(b), 0,
-sin(b), cos(b), 0, 0,
-sin(b)*cos(b), -sin(b)*sin(b), cos(b), 0,
0, 0, 700, 1,
};
pD3DDevice->SetTransform(D3DTS_WORLD, &World);
pD3DDevice->SetRenderState(D3DRS_LIGHTING, TRUE);
for (DWORD i=0; i<dwNumMaterials; i++)
{
pD3DDevice->SetMaterial(&pMeshMaterials);
// if (pMeshTextures)
// pD3DDevice->SetTexture(0, pMeshTextures);
pMesh->DrawSubset(i);
}
pD3DDevice->EndScene();
}
pD3DDevice->Present(NULL, NULL, NULL, NULL);
}
в direct x sdk есть пример