#include <stdafx.h>
#include <d3d9.h>
#include <d3dx9.h>
#pragma comment (lib,"d3d9.lib")
#pragma comment (lib,"d3dx9.lib")
LPDIRECT3D9 g_pD3D = NULL;
LPDIRECT3DDEVICE9 Device = NULL;
LPDIRECT3DVERTEXBUFFER9 buff;
struct Vertex
{
int x,y,z;
DWORD color;
};
void Matrix()
{
// Ìàòðèöà âèäà
D3DXMATRIX MatrixView;
//ìàòðèöà ïðîåêöèè
D3DXMATRIX MatrixProjection;
D3DXMATRIX MatrixWorld;
Device->SetTransform(D3DTS_WORLD, &MatrixWorld);
// Èçìåíÿåì âèäîâóþ ìàòðèöó
D3DXMatrixLookAtLH(&MatrixView, // ïîëó÷åííàÿ â èòîãå âèäîâàÿ ìàòðèöà
&D3DXVECTOR3(0.0f, 0.0f, -100.0f), // òî÷êà, èç êîòîðîé ñìîòðèì
&D3DXVECTOR3(0.0f, 0.0f, 0.0f), // êóäà ñìîòðèì
&D3DXVECTOR3(0.0f, 1.0f, 0.0f)); // íàïðàâëåíèå âåðõà
// Óñòàíàâëèâàåì âèäîâóþ ìàòðèöó
Device->SetTransform(D3DTS_VIEW, &MatrixView);
// Èçìåíÿåì ìàòðèöó ïðîåêöèè
D3DXMatrixPerspectiveFovLH(&MatrixProjection, // ïîëó÷åííàÿ èòîãîâàÿ ìàòðèöà ïðîåêöèè
D3DX_PI/4, // ïîëå çðåíèÿ â íàïðàâëåíèè îñè Y â ðàäèàíàõ
1.54f, // ñîîòíîøåíèÿ ñòîðîí ýêðàíà 770/500=1.54
10.0f, // ïåðåäíèé ïëàí îòñå÷åíèÿ ñöåíû
200.0f); // çàäíèé ïëàí îòñå÷åíèÿ ñöåíû
// Óñòàíàâëèâàåì ìàòðèöó ïðîåêöèè
Device->SetTransform(D3DTS_PROJECTION, &MatrixProjection);
}
Vertex cube[]=
{
{200,100,0, D3DCOLOR_XRGB(0,0,0)},
{100,200,0, D3DCOLOR_XRGB(0,0,0)},
{300,300,0, D3DCOLOR_XRGB(0,0,0)}
};
#define D3D_FVF (D3DFVF_XYZ)
HRESULT InitD3D( HWND hWnd )
{
if( NULL == ( g_pD3D = Direct3DCreate9( D3D_SDK_VERSION ) ) )
return E_FAIL;
D3DPRESENT_PARAMETERS d3dpp = {(0)};
d3dpp.Windowed = TRUE;
d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
d3dpp.BackBufferFormat = D3DFMT_UNKNOWN;
if( FAILED( g_pD3D->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd,
D3DCREATE_SOFTWARE_VERTEXPROCESSING,
&d3dpp, &Device ) ) )
{
return E_FAIL;
}
// ???? ?????????? ??????? - ?????????? S_OK
return S_OK;
}
//-----------------------------------------------------------------------------
// ???: Cleanup()
// ????????: ??????????? ??? ????? ???????????????? ???????
//-----------------------------------------------------------------------------
VOID Cleanup()
{
if( Device != NULL)
Device->Release();
if( g_pD3D != NULL)
g_pD3D->Release();
}
//-----------------------------------------------------------------------------
// ???: Render()
// ????????: ????????? ?????
//-----------------------------------------------------------------------------
VOID Render()
{
Device->Clear( 0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0,0,255), 1.0f, 0 );
Device->CreateVertexBuffer(sizeof(Vertex)*3,0,D3D_FVF,D3DPOOL_SYSTEMMEM,&buff,NULL);
VOID * pv=NULL;
buff->Lock(0,sizeof(cube),(void**)&pv,0);
memcpy(pv,cube,sizeof(cube));
buff->Unlock();
Matrix();
Device->BeginScene();
Device->SetStreamSource(0,buff,0,sizeof(Vertex));
Device->SetFVF(D3D_FVF);
Device->DrawPrimitive(D3DPT_TRIANGLELIST,0,1);
Device->EndScene();
Device->Present( NULL, NULL, NULL, NULL );
}
//-----------------------------------------------------------------------------
// ???: MsgProc()
// ????????: ????????? ?????????
//-----------------------------------------------------------------------------
LRESULT WINAPI MsgProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam )
{
switch( msg )
{
case WM_DESTROY:
Cleanup();
PostQuitMessage( 0 );
return 0;
case WM_QUIT:
Cleanup();
PostQuitMessage(0);
return 0;
}
return DefWindowProc( hWnd, msg, wParam, lParam );
}
//-----------------------------------------------------------------------------
// ???: WinMain()
// ????????: ????? ????? ??????????
//-----------------------------------------------------------------------------
INT WINAPI WinMain( HINSTANCE hInst, HINSTANCE, LPSTR, INT )
{
// ???????????? ????? ????
WNDCLASSEX wc = { sizeof(WNDCLASSEX), CS_CLASSDC, MsgProc, 0L, 0L,
GetModuleHandle(NULL), NULL, NULL, NULL, NULL,
"Win main", NULL };
RegisterClassEx( &wc );
// ???????? ???? ?????????
HWND hWnd = CreateWindow( "Win main", "DirectX 9.0 C",
WS_OVERLAPPEDWINDOW, 0, 0, 1024, 768,
GetDesktopWindow(), NULL, wc.hInstance, NULL );
// ????????????? Direct3D
if( SUCCEEDED( InitD3D( hWnd ) ) )
{
// ?????????? ????
ShowWindow( hWnd, SW_SHOWDEFAULT );
UpdateWindow( hWnd );
// ???? ????? ?????????
MSG msg;
while( msg.message!=WM_QUIT )
{
if(PeekMessage(&msg,NULL,0,0,PM_REMOVE))
{
TranslateMessage( &msg );
DispatchMessage( &msg );
}
else
{
Render();
};
}
}
UnregisterClass( "D3D Tutorial", wc.hInstance );
return 0;
}
Трабл с матрицами
Народ!У меня большой трабл с матрицами!При их использовании не отображается треугольник((