📄 textures.cpp
字号:
pVertices[2*i+0].tv = 1.0f;
#endif
pVertices[2*i+1].position = D3DMXVECTOR3( (float)sin(theta), 1.0f, (float)cos(theta) );
pVertices[2*i+1].color = 0xff808080;
#ifndef SHOW_HOW_TO_USE_TCI
pVertices[2*i+1].tu = ((float)i)/(50-1);
pVertices[2*i+1].tv = 0.0f;
#endif
}
g_pVB->Unlock();
return S_OK;
}
//-----------------------------------------------------------------------------
// Name: Cleanup()
// Desc: Releases all previously initialized objects
//-----------------------------------------------------------------------------
VOID Cleanup()
{
if( g_pTexture != NULL )
g_pTexture->Release();
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 leave it as the identity
D3DMXMATRIX matWorld;
D3DMXMatrixIdentity( &matWorld );
D3DMXMatrixRotationX( &matWorld, GetTickCount()/1000.0f );
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()
{
// Clear the backbuffer and the zbuffer
g_pd3dmDevice->Clear( 0, NULL, D3DMCLEAR_TARGET | D3DMCLEAR_ZBUFFER,
D3DMCOLOR_XRGB(0,0,255), 1.0f, 0 );
// Begin the scene
if( SUCCEEDED( g_pd3dmDevice->BeginScene() ) )
{
// Setup the world, view, and projection matrices
SetupMatrices();
// Setup our texture. Using textures introduces the texture stage states,
// which govern how textures get blended together (in the case of multiple
// textures) and lighting information. In this case, we are modulating
// (blending) our texture with the diffuse color of the vertices.
g_pd3dmDevice->SetTexture( 0, g_pTexture );
g_pd3dmDevice->SetTextureStageState( 0, D3DMTSS_COLOROP, D3DMTOP_MODULATE );
g_pd3dmDevice->SetTextureStageState( 0, D3DMTSS_COLORARG1, D3DMTA_TEXTURE );
g_pd3dmDevice->SetTextureStageState( 0, D3DMTSS_COLORARG2, D3DMTA_DIFFUSE );
g_pd3dmDevice->SetTextureStageState( 0, D3DMTSS_ALPHAOP, D3DMTOP_DISABLE );
#ifdef SHOW_HOW_TO_USE_TCI
// Note: to use D3DM texture coordinate generation, use the stage state
// D3DMTSS_TEXCOORDINDEX, as shown below. In this example, we are using
// the position of the vertex in camera space to generate texture
// coordinates. The tex coord index (TCI) parameters are passed into a
// texture transform, which is a 4x4 matrix which transforms the x,y,z
// TCI coordinates into tu, tv texture coordinates.
// In this example, the texture matrix is setup to
// transform the texture from (-1,+1) position coordinates to (0,1)
// texture coordinate space:
// tu = 0.5*x + 0.5
// tv = -0.5*y + 0.5
D3DMXMATRIX mat;
mat._11 = 0.25f; mat._12 = 0.00f; mat._13 = 0.00f; mat._14 = 0.00f;
mat._21 = 0.00f; mat._22 =-0.25f; mat._23 = 0.00f; mat._24 = 0.00f;
mat._31 = 0.00f; mat._32 = 0.00f; mat._33 = 1.00f; mat._34 = 0.00f;
mat._41 = 0.50f; mat._42 = 0.50f; mat._43 = 0.00f; mat._44 = 1.00f;
g_pd3dmDevice->SetTransform( D3DMTS_TEXTURE0, (D3DMMATRIX*)&mat, D3DMFMT_D3DMVALUE_FLOAT );
g_pd3dmDevice->SetTextureStageState( 0, D3DMTSS_TEXTURETRANSFORMFLAGS, D3DMTTFF_COUNT2 );
g_pd3dmDevice->SetTextureStageState( 0, D3DMTSS_TEXCOORDINDEX, D3DMTSS_TCI_CAMERASPACEPOSITION );
#endif
// Render the vertex buffer contents
g_pd3dmDevice->SetStreamSource( 0, g_pVB, sizeof(CUSTOMVERTEX) );
g_pd3dmDevice->DrawPrimitive( D3DMPT_TRIANGLESTRIP, 0, 2*50-2 );
// 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 screen rotation
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 05: Textures"),
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( InitGeometry() ) )
{
// 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 + -