📄 matrices.cpp
字号:
if( FAILED( g_pVB->Lock( 0, sizeof(vertices), &pVertices, 0 ) ) )
return E_FAIL;
memcpy( pVertices, vertices, sizeof(vertices) );
g_pVB->Unlock();
return S_OK;
}
//-----------------------------------------------------------------------------
// Name: Cleanup()
// Desc: Releases all previously initialized objects
//-----------------------------------------------------------------------------
VOID Cleanup()
{
if( g_pVB != NULL )
g_pVB->Release();
if( g_pd3dmDevice != NULL)
g_pd3dmDevice->Release();
if( g_pD3DM != NULL)
{
if (g_hRefDLL)
{
g_pD3DM->RegisterSoftwareDevice(NULL);
FreeLibrary(g_hRefDLL);
}
g_pD3DM->Release();
}
}
//-----------------------------------------------------------------------------
// Name: SetupMatrices()
// Desc: Sets up the world, view, and projection transform matrices.
//-----------------------------------------------------------------------------
VOID SetupMatrices()
{
// For our world matrix, we will just rotate the object about the y-axis.
D3DMXMATRIX matWorld;
// Set up the rotation matrix to generate 1 full rotation (2*PI radians)
// every 1000 ms. To avoid the loss of precision inherent in very high
// floating point numbers, the system time is modulated by the rotation
// period before conversion to a radian angle.
UINT iTime = GetTickCount() % 1000;
float fAngle = iTime * (2.0f * D3DMX_PI) / 1000.0f;
D3DMXMatrixRotationY( &matWorld, fAngle );
g_pd3dmDevice->SetTransform( D3DMTS_WORLD, (D3DMMATRIX*)&matWorld, D3DMFMT_D3DMVALUE_FLOAT );
// Set up our view matrix. A view matrix can be defined given an eye point,
// a point to lookat, and a direction for which way is up. Here, we set the
// eye five units back along the z-axis and up three units, look at the
// origin, and define "up" to be in the y-direction.
D3DMXVECTOR3 vEyePt( 0.0f, 3.0f, -5.0f );
D3DMXVECTOR3 vLookatPt( 0.0f, 0.0f, 0.0f );
D3DMXVECTOR3 vUpVec( 0.0f, 1.0f, 0.0f );
D3DMXMATRIX matView;
D3DMXMatrixLookAtLH( &matView, &vEyePt, &vLookatPt, &vUpVec );
g_pd3dmDevice->SetTransform( D3DMTS_VIEW, (D3DMMATRIX*)&matView, D3DMFMT_D3DMVALUE_FLOAT );
// For the projection matrix, we set up a perspective transform (which
// transforms geometry from 3D view space to 2D viewport space, with
// a perspective divide making objects smaller in the distance). To build
// a perpsective transform, we need the field of view (1/4 pi is common),
// the aspect ratio, and the near and far clipping planes (which define at
// what distances geometry should be no longer be rendered).
D3DMXMATRIX matProj;
D3DMXMatrixPerspectiveFovLH( &matProj, D3DMX_PI/4.0f, 1.0f, 1.0f, 100.0f );
g_pd3dmDevice->SetTransform( D3DMTS_PROJECTION, (D3DMMATRIX*)&matProj, D3DMFMT_D3DMVALUE_FLOAT );
}
//-----------------------------------------------------------------------------
// Name: Render()
// Desc: Draws the scene
//-----------------------------------------------------------------------------
VOID Render()
{
if( NULL == g_pd3dmDevice )
return;
// Clear the backbuffer to a blue color
g_pd3dmDevice->Clear( 0, NULL, D3DMCLEAR_TARGET, D3DMCOLOR_XRGB(0,0,0), 1.0f, 0 );
// Begin the scene
if( SUCCEEDED( g_pd3dmDevice->BeginScene() ) )
{
// Setup the world, view, and projection matrices
SetupMatrices();
// Render the vertex buffer contents
g_pd3dmDevice->SetStreamSource( 0, g_pVB, sizeof(CUSTOMVERTEX) );
g_pd3dmDevice->DrawPrimitive( D3DMPT_TRIANGLELIST, 0, 1 );
// End the scene
g_pd3dmDevice->EndScene();
}
// Present the backbuffer contents to the display
g_pd3dmDevice->Present( NULL, NULL, NULL, NULL );
}
//-----------------------------------------------------------------------------
// Name: MsgProc()
// Desc: The window's message handler
//-----------------------------------------------------------------------------
LRESULT WINAPI MsgProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam )
{
switch( msg )
{
case WM_LBUTTONUP:
PostMessage(hWnd, WM_CLOSE, 0, 0);
break;
case WM_KEYDOWN:
if (VK_ESCAPE == wParam)
{
PostMessage(hWnd, WM_CLOSE, 0, 0);
}
break;
case WM_CLOSE:
Cleanup();
break;
case WM_DESTROY:
PostQuitMessage( 0 );
return 0;
case WM_SETTINGCHANGE:
//we don't support screen rotation
if (IsScreenRotated())
{
PostMessage(hWnd, WM_CLOSE, 0, 0);
}
break;
}
return DefWindowProc( hWnd, msg, wParam, lParam );
}
//-----------------------------------------------------------------------------
// Name: WinMain()
// Desc: The application's entry point
//-----------------------------------------------------------------------------
INT WINAPI WinMain( HINSTANCE hInst, HINSTANCE, LPTSTR szCmd, INT )
{
// Parse command line to determine if user wants to use
// the D3DM reference driver instead of the default system driver
if (0 == lstrcmp(szCmd, TEXT("-ref")))
g_bUseRef = true;
//We don't support a rotated screen
if (IsScreenRotated())
{
return 0;
}
// Register the window class
WNDCLASS wc = { 0L, MsgProc, 0L, 0L,
hInst, NULL, NULL, NULL, NULL,
TEXT("D3DM Tutorial") };
RegisterClass( &wc );
int iScreenWidth = GetSystemMetrics(SM_CXSCREEN);
int iScreenHeight = GetSystemMetrics(SM_CYSCREEN);
// Create the application's window
HWND hWnd = CreateWindow( TEXT("D3DM Tutorial"),
TEXT("D3DM Tutorial 03: Matrices"),
WS_VISIBLE,
0, 0, iScreenWidth, iScreenHeight,
NULL, NULL, wc.hInstance, NULL );
SHFullScreen(hWnd, SHFS_HIDESIPBUTTON | SHFS_HIDETASKBAR);
// Initialize Direct3D Mobile
if( SUCCEEDED( InitD3DM( hWnd ) ) )
{
// Create the vertex buffer
if( SUCCEEDED( InitVB() ) )
{
// Show the window
ShowWindow( hWnd, SW_SHOWNORMAL );
UpdateWindow( hWnd );
// Enter the message loop
MSG msg;
memset( &msg, 0, sizeof(msg) );
while( msg.message!=WM_QUIT )
{
if( PeekMessage( &msg, NULL, 0U, 0U, PM_REMOVE ) )
{
TranslateMessage( &msg );
DispatchMessage( &msg );
}
else
Render();
}
}
}
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -