📄 main.cpp
字号:
vDraw3DObject( D3DXVECTOR3( 0.0, 0.0, 0.0 ) ,
D3DXVECTOR3( 200.0, 200.0, 200.0 ),
D3DXVECTOR3( 90.0, 0.0, 0.0 ),
0 );
//
// Render the 3d object( s )
//
if( animTest.m_iNumFrames && animTest.m_iNumObjects ) {
for( int i = 0; i < animTest.m_iNumObjects; i++ ) {
// Render the current object in filled mode. Render
// everything else in wireframe.
//
// *FOR YOU TO DO* Add a button to the toolbar that
// lets you turn this feature on or off.
// Not current, render in wireframe
if( i != g_iCurObj ) {
g_pd3dDevice->SetRenderState( D3DRS_FILLMODE, D3DFILL_WIREFRAME );
}
// Current, render as solid
else {
g_pd3dDevice->SetRenderState( D3DRS_FILLMODE, D3DFILL_SOLID );
}
// Set current frame
iCFrame = animTest.m_iCurFrame;
// Render the object using the frame information
// stored in the animation object.
animTest.m_objObject[ i ]->vDisplayXYZ(
animTest.m_keyFrames[ i ][iCFrame]->m_vecTrans.x,
animTest.m_keyFrames[ i ][iCFrame]->m_vecTrans.y,
animTest.m_keyFrames[ i ][iCFrame]->m_vecTrans.z,
animTest.m_keyFrames[ i ][iCFrame]->m_vecRot.x,
animTest.m_keyFrames[ i ][iCFrame]->m_vecRot.y,
animTest.m_keyFrames[ i ][iCFrame]->m_vecRot.z,
animTest.m_keyFrames[ i ][iCFrame]->m_vecScale.x,
animTest.m_keyFrames[ i ][iCFrame]->m_vecScale.y,
animTest.m_keyFrames[ i ][iCFrame]->m_vecScale.z );
// Animate the object
if( g_iAnimActive ) {
animTest.m_lCurTime++;
// Advance to next frame
if( animTest.m_lCurTime >= animTest.m_keyFrames[ i ][iCFrame]->m_lTimeDelay ) {
animTest.m_iCurFrame++;
bFrameChanged = 1;
animTest.m_lCurTime = 0;
// Reset frame to zero
if( animTest.m_iCurFrame >= animTest.m_iNumFrames ) {
animTest.m_iCurFrame = 0;
}
}
}
}
}
// Output camera position
rectText.bottom = 480;
rectText.left = 0;
rectText.right = 640;
rectText.top = 0;
sprintf( szOutput, "CAM: (%.2f, %.2f, %.2f), Cur Obj: %d",
g_vecCameraOrigin.x,
g_vecCameraOrigin.y,
g_vecCameraOrigin.z,
g_iCurObj );
pD3DXFont->DrawText( szOutput, -1, &rectText,
DT_LEFT,
D3DCOLOR_RGBA(0, 0, 0, 255));
// Frame count
rectText.top = 15;
sprintf( szOutput, "OBJ: Frame %d of %d (%d Objects)",
animTest.m_iCurFrame+1,
animTest.m_iNumFrames,
animTest.m_iNumObjects );
pD3DXFont->DrawText( szOutput, -1, &rectText,
DT_LEFT,
D3DCOLOR_RGBA(0, 0, 0, 255));
// Output frame stats
if( animTest.m_iNumFrames && animTest.m_iNumObjects ) {
// Frame translation
rectText.top = 25;
sprintf( szOutput, " (%.2f, %.2f, %.2f) Trans",
animTest.m_keyFrames[ g_iCurObj ][animTest.m_iCurFrame]->m_vecTrans.x,
animTest.m_keyFrames[ g_iCurObj ][animTest.m_iCurFrame]->m_vecTrans.y,
animTest.m_keyFrames[ g_iCurObj ][animTest.m_iCurFrame]->m_vecTrans.z );
pD3DXFont->DrawText( szOutput, -1, &rectText,
DT_LEFT,
D3DCOLOR_RGBA(0, 0, 0, 255));
// Frame rotation
rectText.top = 35;
sprintf( szOutput, " (%.2f, %.2f, %.2f) Rot",
animTest.m_keyFrames[ g_iCurObj ][animTest.m_iCurFrame]->m_vecRot.x,
animTest.m_keyFrames[ g_iCurObj ][animTest.m_iCurFrame]->m_vecRot.y,
animTest.m_keyFrames[ g_iCurObj ][animTest.m_iCurFrame]->m_vecRot.z );
pD3DXFont->DrawText( szOutput, -1, &rectText,
DT_LEFT,
D3DCOLOR_RGBA(0, 0, 0, 255));
// Frame scale
rectText.top = 45;
sprintf( szOutput, " (%.2f, %.2f, %.2f) Scale",
animTest.m_keyFrames[ g_iCurObj ][animTest.m_iCurFrame]->m_vecScale.x,
animTest.m_keyFrames[ g_iCurObj ][animTest.m_iCurFrame]->m_vecScale.y,
animTest.m_keyFrames[ g_iCurObj ][animTest.m_iCurFrame]->m_vecScale.z );
pD3DXFont->DrawText( szOutput, -1, &rectText,
DT_LEFT,
D3DCOLOR_RGBA(0, 0, 0, 255));
}
// End the scene
g_pd3dDevice->EndScene();
// Present the backbuffer contents to the display
g_pd3dDevice->Present( NULL, NULL, NULL, NULL );
// Update toolbar stats in-case frame advanced
if( bFrameChanged ) {
vUpdateToolbarStats();
}
}
//--------------------------------------------------------------------------------------
//
// Function to initialize the global interface object vertex buffer
//
// Uses the following global variables:
//
// g_pd3dDevice The Direct3D Device
// g_pVBInterface The Vertex buffer for the interface objects
// CUSTOMVERTEX The Vertex structure
// D3DFVF_CUSTOMVERTEX The Vertex format
// g_pTexture The Texture array to hold textures
//
//--------------------------------------------------------------------------------------
void vInitInterfaceObjects( void )
{
CUSTOMVERTEX *pVertices;
DWORD dwColor = D3DCOLOR_XRGB(255,255,255);
// Create the interface object buffer
if( FAILED( g_pd3dDevice->CreateVertexBuffer( 4*sizeof(CUSTOMVERTEX),
0, D3DFVF_CUSTOMVERTEX,
D3DPOOL_DEFAULT, &g_pVBInterface, NULL ) ) )
{
return;
}
// Lock the buffer for editing
if( FAILED( g_pVBInterface->Lock( 0, 0, (void**)&pVertices, 0 ) ) )
return;
//
// Create a quad made up of 3 vertices (triangle strips)
//
// 1 3
// X--------X
// |x |
// | x |
// | x |
// | * |
// | x |
// | x |
// | x |
// | x|
// X--------X
// 0 2
//
pVertices[0].position = D3DXVECTOR3( -0.5f, -0.5f, 0.0f );
pVertices[0].tu = 0.0f;
pVertices[0].tv = 1.0f;
pVertices[0].vecNorm = D3DXVECTOR3(0.0f,0.0f,1.0f);
pVertices[0].color = dwColor;
pVertices[1].position = D3DXVECTOR3( -0.5f, 0.5f, 0.0f );
pVertices[1].tu = 0.0f;
pVertices[1].tv = 0.0f;
pVertices[1].vecNorm = D3DXVECTOR3(0.0f,0.0f,1.0f);
pVertices[1].color = dwColor;
pVertices[2].position = D3DXVECTOR3( 0.5f, -0.5f, 0.0f );
pVertices[2].tu = 1.0f;
pVertices[2].tv = 1.0f;
pVertices[2].vecNorm = D3DXVECTOR3(0.0f,0.0f,1.0f);
pVertices[2].color = dwColor;
pVertices[3].position = D3DXVECTOR3( 0.5f, 0.5f, 0.0f );
pVertices[3].tu = 1.0f;
pVertices[3].tv = 0.0f;
pVertices[3].vecNorm = D3DXVECTOR3(0.0f,0.0f,1.0f);
pVertices[3].color = dwColor;
// Done editing, unlock the buffer
g_pVBInterface->Unlock();
//---------------------------------------------
// LOAD TEXTURES
//---------------------------------------------
// Load the ground texture
if( FAILED( D3DXCreateTextureFromFile(
g_pd3dDevice,
"Data\\Textures\\grid.tga",
&g_pTexture[ 0 ] ) ) ) {
return;
}
// Text Font
hFont = CreateFont(
16, 0, 0, 0,
0, 0, 0, 0, 0,
0, 0, PROOF_QUALITY,
0, "fixedsys" );
// Create the Direct3D Font
D3DXCreateFont( g_pd3dDevice, hFont, &pD3DXFont );
};
//--------------------------------------------------------------------------------------
//
// Function to draw a 3D object in 2D space
//
// iXPos = x-position in screen-space of the top-left corner of the object
// iYPos = y-position in screen-space of the top-left corner of the object
// fXSize = Width of the object in screen-space (pixels)
// fYSize = Height of the object in screen-space (pixels)
// iTexture = Index into the texture array of object to draw
//
//--------------------------------------------------------------------------------------
void vDraw3DObject( D3DXVECTOR3 fPos, D3DXVECTOR3 fSize, D3DXVECTOR3 fRot, int iTexture )
{
D3DXMATRIX matWorld, matRotation, matTranslation, matScale;
// Set default position,scale,rotation
D3DXMatrixIdentity( &matTranslation );
D3DXMatrixTranslation( &matScale, fPos.x, fPos.y, fPos.z );
// Scale the object
D3DXMatrixScaling( &matScale, fSize.x, fSize.y, fSize.z );
D3DXMatrixMultiply( &matTranslation, &matTranslation, &matScale );
// Rotate the object
D3DXMatrixRotationX( &matRotation, (float)DegToRad( fRot.x ) );
D3DXMatrixMultiply( &matWorld, &matTranslation, &matRotation );
D3DXMatrixRotationY( &matRotation, (float)DegToRad( fRot.y ) );
D3DXMatrixMultiply( &matWorld, &matWorld, &matRotation );
D3DXMatrixRotationZ( &matRotation, (float)DegToRad( fRot.z ) );
D3DXMatrixMultiply( &matWorld, &matWorld, &matRotation );
// Set default material
g_pd3dDevice->SetMaterial( &g_mtrlDefault );
// Set matrix
g_pd3dDevice->SetTransform( D3DTS_WORLD, &matWorld );
g_pd3dDevice->SetTexture( 0, g_pTexture[ iTexture ] );
g_pd3dDevice->SetStreamSource( 0, g_pVBInterface, 0, sizeof( CUSTOMVERTEX ) );
g_pd3dDevice->SetFVF( D3DFVF_CUSTOMVERTEX );
g_pd3dDevice->DrawPrimitive( D3DPT_TRIANGLESTRIP, 0, 2 );
// Dereference texture
g_pd3dDevice->SetTexture(0, NULL);
}
//-----------------------------------------------------------------------------
//
// Initializes the DirectInput system
//
//-----------------------------------------------------------------------------
int iInitDirectInput(void)
{
HRESULT hReturn;
// Do not try to create Direct Input if already created
if( !pDI ) {
// Create a DInput object
if( FAILED( hReturn = DirectInput8Create(
g_hInstance, DIRECTINPUT_VERSION,
IID_IDirectInput8, (VOID**)&pDI, NULL ) ) ) {
return( INPUTERROR_NODI );
}
}
else {
return( INPUTERROR_KEYBOARDEXISTS );
}
return( INPUTERROR_SUCCESS );
}
//-----------------------------------------------------------------------------
//
// Reads the keyboard data buffer for input actions
//
//-----------------------------------------------------------------------------
int iInitKeyboard(HWND hWnd)
{
HRESULT hReturn = 0;
DIPROPDWORD dipdw;
// Don't try to create the keyboard twice
if( pKeyboard ) {
return( INPUTERROR_KEYBOARDEXISTS );
}
// Exit out if no Direct Input interface found
else if ( !pDI ) {
return( INPUTERROR_NODI );
}
// Obtain an interface to the system keyboard device
if( FAILED( hReturn = pDI->CreateDevice(
GUID_SysKeyboard,
&pKeyboard,
NULL ) ) ) {
return( INPUTERROR_NOKEYBOARD );
}
// Create buffer to hold keyboard data
ZeroMemory( &dipdw, sizeof( DIPROPDWORD ) );
dipdw.diph.dwSize = sizeof( DIPROPDWORD );
dipdw.diph.dwHeaderSize = sizeof( DIPROPHEADER );
dipdw.diph.dwObj = 0;
dipdw.diph.dwHow = DIPH_DEVICE;
dipdw.dwData = KEYBOARD_BUFFERSIZE;
// Set the size of the buffer
if( FAILED( hReturn = pKeyboard->SetProperty(
DIPROP_BUFFERSIZE,
&dipdw.diph ) ) ) {
return( INPUTERROR_NOKEYBOARD );
}
// Set the format of the keyboard
if( FAILED( hReturn = pKeyboard->SetDataFormat(
&c_dfDIKeyboard ) ) ) {
return( INPUTERROR_NOKEYBOARD );
}
// Set the co-operative level to exclusive access
if( FAILED( hReturn = pKeyboard->SetCooperativeLevel(
hWnd,
DISCL_NONEXCLUSIVE | DISCL_FOREGROUND
) ) ) {
return( INPUTERROR_NOKEYBOARD );
}
// Acquire the keyboard device
pKeyboard->Acquire();
// Get the keyboard layout, this is required
// for converting scan codes to ascii codes.
g_Layout = GetKeyboardLayout( 0 );
return( INPUTERROR_SUCCESS );
}
//-----------------------------------------------------------------------------
//
// Reads the keyboard data buffer for input actions
//
//-----------------------------------------------------------------------------
int iReadKeyboard( void )
{
HRESULT hr;
DWORD dwCurBuffer;
DIDEVICEOBJECTDATA didKeyboardBuffer[KEYBOARD_BUFFERSIZE];
DWORD dwItems = KEYBOARD_BUFFERSIZE;
BYTE byteASCII;
// Dont try to read the keyboard if the interface or device is invalid
if( !pKeyboard || !pDI ) {
return( INPUTERROR_NOKEYBOARD );
}
// Read the buffered data
hr = pKeyboard->GetDeviceData(
sizeof( DIDEVICEOBJECTDATA ),
didKeyboardBuffer,
&dwItems,
0 );
// Keyboard may have been lost, reacquire it
if( FAILED( hr ) ) {
pKeyboard->Acquire();
return( INPUTERROR_SUCCESS );
}
// Process data if there is data to read
if ( dwItems ) {
// Process the data
for( dwCurBuffer = 0; dwCurBuffer < dwItems; dwCurBuffer++ ) {
// Default all keys to being "up"
for( int j = 0; j < 256; j++ ) {
ascKeys[ j ][ dwCurBuffer ] = 0;
diks[ j ][ dwCurBuffer ] = 0;
}
// Map scan-code to ascii code
byteASCII = Scan2Ascii( didKeyboardBuffer[dwCurBuffer].dwOfs );
// Set key to be down (depressed)
if( didKeyboardBuffer[ dwCurBuffer ].dwData & 0x80 ) {
ascKeys[ byteASCII ][ dwCurBuffer ] = 1;
diks[ didKeyboardBuffer[ dwCurBuffer ].dwOfs ]
[ dwCurBuffer ] = 1;
// Check if SHIFT key changed
if( didKeyboardBuffer[ dwCurBuffer ].dwOfs == DIK_LSHIFT ||
didKeyboardBuffer[ dwCurBuffer ].dwOfs == DIK_RSHIFT ) {
g_bShift = 1;
}
}
// Set key to be up
else {
ascKeys[ byteASCII ][ dwCurBuffer ] = 0;
diks[ didKeyboardBuffer[ dwCurBuffer ].dwOfs ]
[ dwCurBuffer ] = 0;
// Check if SHIFT key changed
if( didKeyboardBuffer[ dwCurBuffer ].dwOfs == DIK_LSHIFT ||
didKeyboardBuffer[ dwCurBuffer ].dwOfs == DIK_RSHIFT ) {
g_bShift = 0;
}
}
}
}
// Return # of items read
return ( dwItems );
}
//-----------------------------------------------------------------------------
//
// Converts scan codes to ASCII codes
//
//-----------------------------------------------------------------------------
BYTE Scan2Ascii( DWORD scancode )
{
UINT vk;
// Map the scancode to an ascii code
vk = MapVirtualKeyEx( scancode, 1, g_Layout);
// Map to lower-case
vk = tolower( vk );
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -