📄 cubemap.cpp
字号:
}
return S_OK;
}
//-----------------------------------------------------------------------------
// Name: InitDeviceObjects()
// Desc: Initialize scene objects.
//-----------------------------------------------------------------------------
HRESULT CMyD3DApplication::InitDeviceObjects()
{
// Load the mesh from the specified file
LPD3DXBUFFER pD3DXMtrlBuffer;
if( FAILED( D3DXLoadMeshFromX( "room.x", D3DXMESH_SYSTEMMEM,
m_pd3dDevice, NULL,
&pD3DXMtrlBuffer, &g_dwNumMaterials,
&m_pSkyBox ) ) )
{
return E_FAIL;
}
if( FAILED( m_pAirplane->Create( m_pd3dDevice, _T("space station 5.x") ) ) )
return D3DAPPERR_MEDIANOTFOUND;
m_pAirplane->SetFVF( m_pd3dDevice, D3DFVF_XYZ|D3DFVF_NORMAL|D3DFVF_TEX1 );
//SetFVF( m_pd3dDevice, D3DFVF_XYZ|D3DFVF_NORMAL|D3DFVF_TEX2 );
D3DXMATERIAL* d3dxMaterials = (D3DXMATERIAL*)pD3DXMtrlBuffer->GetBufferPointer();
g_pMeshMaterials = new D3DMATERIAL8[g_dwNumMaterials];
g_pMeshTextures = new LPDIRECT3DTEXTURE8[g_dwNumMaterials];
g_pTexture = new LPDIRECT3DTEXTURE8();
g_pTexture2 = new LPDIRECT3DTEXTURE8();
for( DWORD i=0; i<g_dwNumMaterials; i++ )
{
// Copy the material
g_pMeshMaterials[i] = d3dxMaterials[i].MatD3D;
// Set the ambient color for the material (D3DX does not do this)
g_pMeshMaterials[i].Ambient = g_pMeshMaterials[i].Diffuse;
// Create the texture
if( FAILED( D3DXCreateTextureFromFile( m_pd3dDevice,
d3dxMaterials[i].pTextureFilename,
&g_pMeshTextures[i] ) ) )
{
g_pMeshTextures[i] = NULL;
}
}
if( FAILED( D3DXCreateTextureFromFile( m_pd3dDevice,
"banana.bmp",
g_pTexture) ) )
{
g_pTexture = NULL;
}
if( FAILED( D3DXCreateTextureFromFile( m_pd3dDevice,
"caust29.tga",
g_pTexture2) ) )
{
g_pTexture2 = NULL;
}
pD3DXMtrlBuffer->Release();
m_pFont->InitDeviceObjects( m_pd3dDevice );
return S_OK;
}
//-----------------------------------------------------------------------------
// Name: RestoreDeviceObjects()
// Desc: Restore device-memory objects and state after a device is created or
// resized.
//-----------------------------------------------------------------------------
HRESULT CMyD3DApplication::RestoreDeviceObjects()
{
m_pd3dDevice->SetRenderState( D3DRS_LIGHTING, TRUE );
m_pd3dDevice->SetRenderState( D3DRS_AMBIENT,
D3DCOLOR_COLORVALUE( 0.45, 0.45, 0.45, 1.0 ) );
m_pFont->RestoreDeviceObjects();
m_pAirplane->RestoreDeviceObjects( m_pd3dDevice );
// Set the transform matrices
FLOAT fAspect = (FLOAT) m_d3dsdBackBuffer.Width / (FLOAT) m_d3dsdBackBuffer.Height;
D3DXMatrixPerspectiveFovLH( &m_matProject, D3DX_PI * 0.4f, fAspect, 0.5f, 200.0f );
InitMusic(m_hWnd);
PlayMusic(m_hWnd, TRUE);
return S_OK;
}
//-----------------------------------------------------------------------------
// Name: InvalidateDeviceObjects()
// Desc: Called when the device-dependent objects are about to be lost.
//-----------------------------------------------------------------------------
HRESULT CMyD3DApplication::InvalidateDeviceObjects()
{
m_pAirplane->InvalidateDeviceObjects();
m_pFont->InvalidateDeviceObjects();
StopMusic();
//SAFE_RELEASE( m_pCubeMap );
return S_OK;
}
//-----------------------------------------------------------------------------
// Name: DeleteDeviceObjects()
// Desc: Called when the app is exiting, or the device is being changed,
// this function deletes any device dependent objects.
//-----------------------------------------------------------------------------
HRESULT CMyD3DApplication::DeleteDeviceObjects()
{
m_pFont->DeleteDeviceObjects();
m_pAirplane->Destroy();
SAFE_RELEASE( *g_pTexture );
SAFE_RELEASE( *g_pTexture2 );
SAFE_DELETE( g_pMusicSegment );
SAFE_DELETE( g_pMusicManager );
// DemoMusic.StopMySound();
// DemoMusic.ReleaseMySound();
return S_OK;
}
//-----------------------------------------------------------------------------
// Name: FinalCleanup()
// Desc: Called before the app exits, this function gives the app the chance
// to cleanup after itself.
//-----------------------------------------------------------------------------
HRESULT CMyD3DApplication::FinalCleanup()
{
SAFE_DELETE( m_pFont );
return S_OK;
}
//-----------------------------------------------------------------------------
// Name: ConfirmDevice()
// Desc: Called during device intialization, this code checks the device
// for some minimum set of capabilities
//-----------------------------------------------------------------------------
HRESULT CMyD3DApplication::ConfirmDevice( D3DCAPS8* pCaps, DWORD dwBehavior,
D3DFORMAT Format )
{
if( dwBehavior & D3DCREATE_PUREDEVICE )
return E_FAIL;
if( !(pCaps->TextureCaps & D3DPTEXTURECAPS_CUBEMAP) &&
!(dwBehavior & D3DCREATE_SOFTWARE_VERTEXPROCESSING) &&
!(pCaps->VertexShaderVersion >= D3DVS_VERSION(1, 0)) )
{
return E_FAIL;
}
return S_OK;
}
void CMyD3DApplication::SetupPixelFog(DWORD Color, DWORD Mode)
{
float Start = 0.5f, // For linear mode
End = 0.8f,
Density = 0.06; // For exponential modes
// Enable fog blending.
m_pd3dDevice->SetRenderState(D3DRS_FOGENABLE, TRUE);
// Set the fog color.
m_pd3dDevice->SetRenderState(D3DRS_FOGCOLOR, Color);
// Set fog parameters.
if(D3DFOG_LINEAR == Mode)
{
m_pd3dDevice->SetRenderState(D3DRS_FOGTABLEMODE, Mode);
m_pd3dDevice->SetRenderState(D3DRS_FOGSTART, *(DWORD *)(&Start));
m_pd3dDevice->SetRenderState(D3DRS_FOGEND, *(DWORD *)(&End));
}
else
{
m_pd3dDevice->SetRenderState(D3DRS_FOGTABLEMODE, Mode);
m_pd3dDevice->SetRenderState(D3DRS_FOGDENSITY, *(DWORD *)(&Density));
}
}
void CMyD3DApplication::SetView()
{
D3DXMatrixLookAtLH(&m_matView, &VDot, &VAtPoint, &VUp );
}
HRESULT CMyD3DApplication::TurnView(WORD dir, float u)
{
D3DXVECTOR3 pOut;
D3DXVECTOR3 pOut2;
D3DXVECTOR3 ab;
D3DXVECTOR3 ac;
D3DXVECTOR4 Vtemp;
D3DXMATRIX R,RT,R2;
//DOUBLE v,a,b,c;
float s1=0,s2=0;
D3DXVec3Subtract(&ab,&VAtPoint,&VDot);
//D3DXVec3Subtract(&ac,&VUp,&VDot);
ac=VUp;
switch(dir)
{
case 1:D3DXVec3Cross(&pOut,&ab,&ac);
D3DXVec3Normalize(&pOut2,&pOut);
pOut2.x*=u;pOut2.y*=u;pOut2.z*=u;
pOut=VDot;
D3DXVec3Add(&VDot,&pOut,&pOut2);
pOut=VAtPoint;
D3DXVec3Add(&VAtPoint,&pOut,&pOut2);//*/
//pOut=VUp;
//D3DXVec3Add(&VUp,&pOut,&pOut2);
SetView();
break;//左右平移
case 2://前后
D3DXVec3Normalize(&pOut2,&ab);
pOut2.x*=u;pOut2.y*=u;pOut2.z*=u;
pOut=VDot;
D3DXVec3Add(&VDot,&pOut,&pOut2);
pOut=VAtPoint;
D3DXVec3Add(&VAtPoint,&pOut,&pOut2);//*/
SetView();
break;
case 3: //左右移动
D3DXVec3Normalize(&pOut,&ac);
RT = D3DXMATRIX(1, 0, 0, 0,
0, 1, 0, 0,
0, 0, 1, 0,
-VDot.x, -VDot.y, -VDot.z, 1);
D3DXMatrixRotationAxis(
&R2,
&pOut,
u);
R = RT * R2;
RT = D3DXMATRIX(1, 0, 0, 0,
0, 1, 0, 0,
0, 0, 1, 0,
VDot.x, VDot.y, VDot.z, 1);
R = R * RT;
D3DXVec3Transform(&Vtemp, &VAtPoint, &R);
VAtPoint.x = Vtemp.x;
VAtPoint.y = Vtemp.y;
VAtPoint.z = Vtemp.z;
SetView();
break;
case 4: //上下移动
s1 = D3DXVec3Length(&ab)*D3DXVec3Length(&VUp);
s2 = (float)acos(D3DXVec3Dot(&ab, &VUp)/s1);
if(u>0)
{
if(s2<=0.018)
break;
}//一度
else
if(s2>=3.124)
break;
D3DXVec3Cross(&pOut,&ab,&ac);
D3DXVec3Normalize(&pOut2,&pOut);
RT = D3DXMATRIX(1, 0, 0, 0,
0, 1, 0, 0,
0, 0, 1, 0,
-VDot.x, -VDot.y, -VDot.z, 1);
D3DXMatrixRotationAxis(
&R2,
&pOut2,
u
);
R = RT * R2;
RT = D3DXMATRIX(1, 0, 0, 0,
0, 1, 0, 0,
0, 0, 1, 0,
VDot.x, VDot.y, VDot.z, 1);
R = R * RT;
D3DXVec3Transform(&Vtemp, &VAtPoint, &R);
VAtPoint.x = Vtemp.x;
VAtPoint.y = Vtemp.y;
VAtPoint.z = Vtemp.z;
SetView();
break;
default:break;
}
return S_OK;
}
void CMyD3DApplication::LoadSegmentFile( HWND hDlg, TCHAR* strFileName )
{
HRESULT hr;
SAFE_DELETE( g_pMusicSegment );
// Have the loader collect any garbage now that the old
// segment has been released
g_pMusicManager->CollectGarbage();
// Set the media path based on the file name (something like C:\MEDIA)
// to be used as the search directory for finding DirectMusic content
// related to this file.
TCHAR strMediaPath[MAX_PATH];
_tcscpy( strMediaPath, strFileName );
TCHAR* strLastSlash = _tcsrchr(strMediaPath, TEXT('\\'));
*strLastSlash = 0;
g_pMusicManager->SetSearchDirectory( strMediaPath );
BOOL bMidiFile = FALSE;
// Load the file into a DirectMusic segment
// g_pMusicManager->CreateSegmentFromFile( &g_pMusicSegment, strFileName, TRUE, bMidiFile );
if( FAILED( g_pMusicManager->CreateSegmentFromFile( &g_pMusicSegment, strFileName,
TRUE, bMidiFile ) ) )
{
// Not a critical failure, so just update the status
MessageBox( NULL, "No find Music sound.",
"DirectMusic Sample", MB_OK | MB_ICONERROR );
}
}
void CMyD3DApplication::InitMusic(HWND hWnd)
{
g_pMusicManager = new CMusicManager();
g_pMusicManager->Initialize(m_hWnd );
TCHAR strFileName[MAX_PATH];
strcpy( strFileName, DXUtil_GetDXSDKMediaPath() );
strcat( strFileName, "sample.sgt" );
LoadSegmentFile(m_hWnd, strFileName ) ;
}
void CMyD3DApplication::PlayMusic( HWND hDlg, BOOL bLooped )
{
if( bLooped )
{
// Set the segment to repeat many times
g_pMusicSegment->SetRepeats( DMUS_SEG_REPEAT_INFINITE );
}
else
{
// Set the segment to not repeat
g_pMusicSegment->SetRepeats( 0 );
}
// Play the segment and wait. The DMUS_SEGF_BEAT indicates to play on the
// next beat if there is a segment currently playing.
g_pMusicSegment->Play( DMUS_SEGF_BEAT );
}
void CMyD3DApplication::StopMusic()
{
g_pMusicSegment->Stop( DMUS_SEGF_BEAT );
}
//-----------------------------------------------------------------------------
// Name: MsgProc()
// Desc: Message proc function to handle key and menu input
//-----------------------------------------------------------------------------
LRESULT CMyD3DApplication::MsgProc( HWND hWnd, UINT uMsg, WPARAM wParam,
LPARAM lParam )
{
if( WM_KEYDOWN == uMsg )
{
switch((wParam) )
{
case 'W':
TurnView(2, 0.30f + 0.1*sin(alpha));
break;
case 'S':
TurnView(2, -0.30f + 0.1*sin(alpha));
break;
case 'A':
TurnView(1, 0.20f);
break;
case 'D':
TurnView(1, -0.20f);
break;
case VK_UP:
TurnView(4, 0.020f);
break;
case VK_DOWN:
TurnView(4, -0.020f);
break;
case VK_LEFT:
TurnView(3, -0.020f);
break;
case VK_RIGHT:
TurnView(3, 0.020f);
break;
case VK_SPACE:
if (jumpping == true)
break;
else
jumpping = true;
break;
}
}
// Pass remaining messages to default handler
return CD3DApplication::MsgProc( hWnd, uMsg, wParam, lParam );
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -