📄 main.cpp
字号:
nFPS++;
}
/**-----------------------------------------------------------------------------
* 鼠标输入处理
*------------------------------------------------------------------------------
*/
void ProcessMouse( void )
{
POINT pt;
float fDelta = 0.001f; // 鼠标的灵敏度,该值越大移动越多.
GetCursorPos( &pt );
int dx = pt.x - g_dwMouseX; // 鼠标的变化值
int dy = pt.y - g_dwMouseY; // 鼠标的变化值
g_pCamera->RotateLocalX( dy * fDelta ); // 鼠标的Y轴旋转值为 3D world的X轴旋转值
g_pCamera->RotateLocalY( dx * fDelta ); // 鼠标的X轴旋转值为 3D world的Y轴旋转值
D3DXMATRIXA16* pmatView = g_pCamera->GetViewMatrix(); // 获得摄像机矩阵.
g_pd3dDevice->SetTransform( D3DTS_VIEW, pmatView ); // 设置摄像机矩阵
g_matView = *pmatView;
// 鼠标在窗口中央初始化
// SetCursor( NULL ); // 鼠标消失.
RECT rc;
GetClientRect( g_hwnd, &rc );
pt.x = (rc.right - rc.left) / 2;
pt.y = (rc.bottom - rc.top) / 2;
ClientToScreen( g_hwnd, &pt );
SetCursorPos( pt.x, pt.y );
g_dwMouseX = pt.x;
g_dwMouseY = pt.y;
}
/**-----------------------------------------------------------------------------
* 键盘输入处理
*------------------------------------------------------------------------------
*/
void ProcessKey( void )
{
if( GetAsyncKeyState( 'A' ) ) g_pCamera->MoveLocalZ( 0.5f ); // 摄像机前进!
if( GetAsyncKeyState( 'Z' ) ) g_pCamera->MoveLocalZ( -0.5f ); // 摄像机后退!
}
/**-----------------------------------------------------------------------------
* 输入处理
*------------------------------------------------------------------------------
*/
void ProcessInputs( void )
{
ProcessMouse();
ProcessKey();
}
/**-----------------------------------------------------------------------------
* 创建动画
*------------------------------------------------------------------------------
*/
VOID Animate()
{
D3DXMATRIXA16 matTM;
D3DXMATRIXA16 matX;
D3DXMATRIXA16 matY;
D3DXMATRIXA16 matScale;
SetupLights(); // 创建光源
ProcessInputs(); // 输入处理
D3DXMatrixRotationX( &matX, g_xRot );
D3DXMatrixRotationY( &matY, g_yRot );
D3DXMatrixScaling( &matScale, SCALE_MESH, SCALE_MESH, SCALE_MESH );
g_matWorld = matScale * matX * matY;
g_matWorld._41 = POS_X_MESH; g_matWorld._42 = POS_Y_MESH; g_matWorld._41 = POS_Z_MESH;
D3DXMatrixLookAtLH( &g_matWorld2Light, &g_vLightPos, &D3DXVECTOR3(0,0,0), &D3DXVECTOR3(0,1,0) );
D3DXMatrixPerspectiveFovLH( &g_matTexProj, D3DX_PI/4, 1.0f, 1.0f, 100.0f );
D3DXMATRIXA16 mVP = g_matWorld2Light * g_matTexProj;
g_pFrustum->Make( &mVP );
LogFPS(); // loging
}
void SetupVSConst( D3DXMATRIXA16 &matWorld )
{
D3DXMATRIXA16 m;
/// 在顶点着色器常数table设置矩阵值(c0 ~ c7)
m = g_matProj;
D3DXMatrixTranspose( &m, &m );
g_pd3dDevice->SetVertexShaderConstantF( 0, (float*)&m, 4 ); // matWorld
m = matWorld * g_matView;
D3DXMatrixTranspose( &m, &m );
g_pd3dDevice->SetVertexShaderConstantF( 4, (float*)&m, 3 ); // matProj
m = matWorld * g_matWorld2Light * g_matTexProj;
D3DXMatrixTranspose( &m, &m );
g_pd3dDevice->SetVertexShaderConstantF( 7, (float*)&m, 3 ); // matView
}
void DrawFloor()
{
D3DXMATRIXA16 matWorld;
g_matTexProj._11 = 2.0f/100;
g_matTexProj._22 = 2.0f/100;
D3DXMatrixIdentity( &matWorld );
SetupVSConst( matWorld );
g_pd3dDevice->SetTexture( 0, g_pTexFloor );
g_pd3dDevice->SetTexture( 1, g_pTexProj );
g_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLOROP, D3DTOP_SELECTARG1 );
g_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLORARG1, D3DTA_TEXTURE );
g_pd3dDevice->SetTextureStageState( 0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1 );
g_pd3dDevice->SetTextureStageState( 0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE );
g_pd3dDevice->SetTextureStageState( 1, D3DTSS_COLOROP, D3DTOP_MODULATE );
g_pd3dDevice->SetTextureStageState( 1, D3DTSS_COLORARG1, D3DTA_TEXTURE );
g_pd3dDevice->SetTextureStageState( 1, D3DTSS_COLORARG2, D3DTA_CURRENT );
g_pd3dDevice->SetTextureStageState( 1, D3DTSS_ALPHAOP, D3DTOP_DISABLE );
g_pd3dDevice->SetTextureStageState( 2, D3DTSS_COLOROP, D3DTOP_DISABLE );
g_pd3dDevice->SetTextureStageState( 2, D3DTSS_ALPHAOP, D3DTOP_DISABLE );
g_pd3dDevice->SetSamplerState( 0, D3DSAMP_ADDRESSU, D3DTADDRESS_CLAMP );
g_pd3dDevice->SetSamplerState( 0, D3DSAMP_ADDRESSV, D3DTADDRESS_CLAMP );
g_pd3dDevice->SetSamplerState( 1, D3DSAMP_ADDRESSU, D3DTADDRESS_CLAMP );
g_pd3dDevice->SetSamplerState( 1, D3DSAMP_ADDRESSV, D3DTADDRESS_CLAMP );
g_pd3dDevice->SetVertexDeclaration( g_pDecl );
g_pd3dDevice->SetVertexShader( g_pVS );
g_pd3dDevice->SetStreamSource( 0, g_pVB, 0, sizeof(MYVERTEX) );
g_pd3dDevice->DrawPrimitive( D3DPT_TRIANGLESTRIP, 0, 2 );
g_pd3dDevice->SetVertexShader( NULL );
}
void DrawMesh()
{
D3DXMATRIXA16 matWorld;
D3DXMatrixIdentity( &matWorld );
g_matTexProj._11 = 2.0f/100;
g_matTexProj._22 = 2.0f/100;
SetupVSConst( g_matWorld );
g_pd3dDevice->SetTexture( 0, NULL );
g_pd3dDevice->SetTexture( 1, g_pTexProj );
g_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLOROP, D3DTOP_SELECTARG1 );
g_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLORARG1, D3DTA_TEXTURE );
g_pd3dDevice->SetTextureStageState( 0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1 );
g_pd3dDevice->SetTextureStageState( 0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE );
g_pd3dDevice->SetTextureStageState( 1, D3DTSS_COLOROP, D3DTOP_MODULATE );
g_pd3dDevice->SetTextureStageState( 1, D3DTSS_COLORARG1, D3DTA_TEXTURE );
g_pd3dDevice->SetTextureStageState( 1, D3DTSS_COLORARG2, D3DTA_CURRENT );
g_pd3dDevice->SetTextureStageState( 1, D3DTSS_ALPHAOP, D3DTOP_DISABLE );
g_pd3dDevice->SetTextureStageState( 2, D3DTSS_COLOROP, D3DTOP_DISABLE );
g_pd3dDevice->SetTextureStageState( 2, D3DTSS_ALPHAOP, D3DTOP_DISABLE );
g_pd3dDevice->SetSamplerState( 0, D3DSAMP_ADDRESSU, D3DTADDRESS_CLAMP );
g_pd3dDevice->SetSamplerState( 0, D3DSAMP_ADDRESSV, D3DTADDRESS_CLAMP );
g_pd3dDevice->SetSamplerState( 1, D3DSAMP_ADDRESSU, D3DTADDRESS_CLAMP );
g_pd3dDevice->SetSamplerState( 1, D3DSAMP_ADDRESSV, D3DTADDRESS_CLAMP );
g_pd3dDevice->SetVertexDeclaration( g_pDecl );
g_pd3dDevice->SetVertexShader( g_pVS );
g_pMesh->Render( g_pd3dDevice );
g_pd3dDevice->SetVertexShader( NULL );
}
void DrawTexture()
{
struct VTX
{
enum { FVF=D3DFVF_XYZRHW|D3DFVF_TEX1 };
float px, py, pz, pw;
float tx, ty;
};
VTX vtx[4] =
{
{ 0, 127, 0, 1, 0, 1 },
{ 0, 0, 0, 1, 0, 0 },
{ 127, 127, 0, 1, 1, 1 },
{ 127, 0, 0, 1, 1, 0 }
};
g_pd3dDevice->SetTexture( 0, g_pTexProj );
g_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLORARG1, D3DTA_TEXTURE );
g_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLOROP, D3DTOP_SELECTARG1 );
g_pd3dDevice->SetFVF( VTX::FVF );
g_pd3dDevice->DrawPrimitiveUP( D3DPT_TRIANGLESTRIP, 2, vtx, sizeof(VTX) );
g_pd3dDevice->SetTexture( 0, NULL );
}
/**-----------------------------------------------------------------------------
* 绘图
*------------------------------------------------------------------------------
*/
VOID Render()
{
/// 后置缓冲和Z-缓冲初始化
g_pd3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(200,200,200), 1.0f, 0 );
g_pd3dDevice->SetRenderState( D3DRS_FILLMODE, g_bWireframe ? D3DFILL_WIREFRAME : D3DFILL_SOLID );
/// 创建动画矩阵
Animate();
/// 开始渲染
if( SUCCEEDED( g_pd3dDevice->BeginScene() ) )
{
DrawTexture();
DrawFloor();
DrawMesh();
if( g_bFrustum ) g_pFrustum->Draw( g_pd3dDevice );
g_pd3dDevice->EndScene();
}
/// 显示后置缓冲的画面!
g_pd3dDevice->Present( NULL, NULL, NULL, NULL );
}
/**-----------------------------------------------------------------------------
* 窗口过程
*------------------------------------------------------------------------------
*/
LRESULT WINAPI MsgProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam )
{
switch( msg )
{
case WM_DESTROY :
Cleanup();
PostQuitMessage( 0 );
return 0;
case WM_KEYDOWN :
switch( wParam )
{
case VK_ESCAPE :
PostMessage( hWnd, WM_DESTROY, 0, 0L );
break;
case VK_LEFT : g_yRot -= 0.1f; break;
case VK_RIGHT : g_yRot += 0.1f; break;
case VK_UP : g_xRot -= 0.1f; break;
case VK_DOWN : g_xRot += 0.1f; break;
case '1' :
g_bWireframe = !g_bWireframe;
break;
case '2' :
g_bLight = !g_bLight;
break;
case '3' :
g_bFrustum= !g_bFrustum;
break;
}
break;
}
return DefWindowProc( hWnd, msg, wParam, lParam );
}
/**-----------------------------------------------------------------------------
* 程序的起始地址
*------------------------------------------------------------------------------
*/
INT WINAPI WinMain( HINSTANCE hInst, HINSTANCE, LPSTR, INT )
{
/// 注册窗口类
WNDCLASSEX wc = { sizeof(WNDCLASSEX), CS_CLASSDC, MsgProc, 0L, 0L,
GetModuleHandle(NULL), NULL, NULL, NULL, NULL,
"BasicFrame", NULL };
RegisterClassEx( &wc );
/// 创建窗口
HWND hWnd = CreateWindow( "BasicFrame", WINDOW_TITLE,
WS_OVERLAPPEDWINDOW, 100, 100, WINDOW_W, WINDOW_H,
GetDesktopWindow(), NULL, wc.hInstance, NULL );
g_hwnd = hWnd;
/// Direct3D初始化
if( SUCCEEDED( InitD3D( hWnd ) ) )
{
if( SUCCEEDED( InitObjects() ) )
{
if( SUCCEEDED( InitGeometry() ) )
{
/// 显示窗口
ShowWindow( hWnd, SW_SHOWDEFAULT );
UpdateWindow( hWnd );
/// 消息循环
MSG msg;
ZeroMemory( &msg, sizeof(msg) );
while( msg.message!=WM_QUIT )
{
/// 消息队列中有消息时,调用相应的处理过程
if( PeekMessage( &msg, NULL, 0U, 0U, PM_REMOVE ) )
{
TranslateMessage( &msg );
DispatchMessage( &msg );
}
else /// 如果没有需要处理的消息,调用Render()函数
{
if( g_bActive ) Render();
}
}
}
}
}
DeleteObjects();
UnregisterClass( "BasicFrame", wc.hInstance );
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -