前田稔(Maeda Minoru)の超初心者のプログラム入門
C:\Program Files\Microsoft DirectX SDK (August 2007)\Samples\C++\Direct3D\MultiAnimation\ |
tiny_4anim.x Tiny_skin.dds |
//☆Global variables☆ CFirstPersonCamera g_Camera; // A model viewing camera CMultiAnim g_MultiAnim; // the MultiAnim class for holding Tiny's mesh and frame hierarchy double g_fLastAnimTime = 0.0; // Time for the animations WCHAR g_Path[MAX_PATH] = L"C:\\Data\\Model\\Tiny"; WCHAR X_file[MAX_PATH] = L"tiny_4anim.x"; CTiny* g_Tiny = NULL; int g_Setnum = 0; // Animation Set Number bool g_Stop = FALSE; // アニメーションの停止 // Object Class で共通に使う領域 LPDIRECT3DDEVICE9 g_pDevice = NULL; ID3DXAnimationController* g_pAnimController = NULL; CAnimInstance* g_v_pAnimInstances = NULL; float g_fBoundingRadius; // Radius of bounding sphere of object MultiAnimFrame * g_pFrameRoot = NULL; // shared between all instances CMultiAnimAllocateHierarchy AH; D3DXMATRIXA16* g_pBoneMatrices = NULL; UINT g_NumBoneMatricesMax = 0; // Tiny の座標と回転の領域 D3DXVECTOR3 g_vObjectCenter = D3DXVECTOR3(0,0,0); // Center of bounding sphere of object CD3DArcBall g_ArcBall; // Arcball for model control |
struct MultiAnimFrame : public D3DXFRAME { }; |
HRESULT CALLBACK OnCreateDevice( IDirect3DDevice9* pd3dDevice, const D3DSURFACE_DESC* pBackBufferSurfaceDesc, void* pUserContext ) { g_pDevice = pd3dDevice; g_pDevice->AddRef(); return S_OK; } |
HRESULT CALLBACK OnResetDevice( IDirect3DDevice9* pd3dDevice, const D3DSURFACE_DESC* pBackBufferSurfaceDesc, void* pUserContext ) { HRESULT hr; WCHAR strCWD[MAX_PATH]; GetCurrentDirectory(MAX_PATH,strCWD); SetCurrentDirectory(g_Path); V_RETURN( g_MultiAnim.Setup( pd3dDevice ) ); // Restore steps for tiny instances g_Tiny = new CTiny; if( g_Tiny == NULL ) return E_OUTOFMEMORY; hr = g_Tiny->Setup( 0.f ); // Setup render state pd3dDevice->SetRenderState( D3DRS_LIGHTING, TRUE ); pd3dDevice->SetRenderState( D3DRS_AMBIENT, D3DCOLOR_ARGB( 255, 255, 255, 255 ) ); pd3dDevice->LightEnable( 0, TRUE ); pd3dDevice->SetRenderState( D3DRS_NORMALIZENORMALS, TRUE ); D3DXVECTOR3 vEye( 0.0f, -100.0f, -700.f ); D3DXVECTOR3 vAt( 0.0f, 150.0f, 0.0f ); g_Camera.SetViewParams( &vEye, &vAt ); float fAspectRatio = pBackBufferSurfaceDesc->Width / (FLOAT)pBackBufferSurfaceDesc->Height; g_Camera.SetProjParams( D3DX_PI/3, fAspectRatio, 1.0f, 10000.0f ); // Setup the arcball parameters g_ArcBall.Reset(); g_ArcBall.SetWindow( pBackBufferSurfaceDesc->Width, pBackBufferSurfaceDesc->Height, 0.85f ); g_ArcBall.SetTranslationRadius( g_fBoundingRadius ); //☆モデルの向きの初期値を設定☆ g_ArcBall.OnBegin( 330, 130 ); g_ArcBall.OnMove( 580, 260 ); g_ArcBall.OnEnd(); g_fLastAnimTime = DXUTGetGlobalTimer()->GetTime();; SetCurrentDirectory(strCWD); return S_OK; } |
HRESULT CMultiAnim::Setup( LPDIRECT3DDEVICE9 pDevice, LPD3DXLOADUSERDATA pLUD ) { assert( pDevice != NULL ); HRESULT hr = S_OK; WCHAR strCWD[MAX_PATH]; GetCurrentDirectory(MAX_PATH,strCWD); SetCurrentDirectory(g_Path); hr= D3DXLoadMeshHierarchyFromX( X_file, 0, pDevice, &AH, pLUD, (LPD3DXFRAME *) &g_pFrameRoot, &g_pAnimController ); if (FAILED(hr)) goto e_Exit; if( !g_pAnimController ) { MessageBox( NULL, L"The sample is attempting to load a mesh without animation or incompatible animation. This sample requires tiny_4anim.x or a mesh with identical animation sets. The program will now exit.", L"Mesh Load Error", MB_OK ); goto e_Exit; } // set up bone pointers hr = SetupBonePtrs( g_pFrameRoot ); if( FAILED( hr ) ) goto e_Exit; // get bounding radius hr = D3DXFrameCalculateBoundingSphere( g_pFrameRoot, &g_vObjectCenter, &g_fBoundingRadius ); if( FAILED( hr ) ) goto e_Exit; e_Exit: if( FAILED( hr ) ) Cleanup(); SetCurrentDirectory(strCWD); return hr; } |
void CALLBACK OnFrameRender( IDirect3DDevice9* pd3dDevice, double fTime, float fElapsedTime, void* pUserContext ) { HRESULT hr; pd3dDevice->Clear( 0L, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DCOLOR_ARGB( 0, 0x3F, 0xAF, 0xFF ), 1.0f, 0L ); if( SUCCEEDED( pd3dDevice->BeginScene() ) ) { D3DXMATRIXA16 mx, mxView, mxProj; D3DXVECTOR3 vEye; mxView = *g_Camera.GetViewMatrix(); mxProj = *g_Camera.GetProjMatrix(); V( pd3dDevice->SetTransform( D3DTS_VIEW, & mxView ) ); V( pd3dDevice->SetTransform( D3DTS_PROJECTION, & mxProj ) ); vEye = *g_Camera.GetEyePt(); // Setup the projection matrix D3DXMatrixPerspectiveFovLH( &mxProj, D3DX_PI/4, 640.0f/480.0f, g_fBoundingRadius/64.0f, g_fBoundingRadius*200.0f ); pd3dDevice->SetTransform( D3DTS_PROJECTION, &mxProj ); //☆モデルのアニメーション☆ if (!g_Stop) g_Tiny->AdvanceTime( fElapsedTime ); g_Tiny->Draw(); pd3dDevice->EndScene(); } } |
void CTiny::Animate( double dTimeDelta ) { //☆g_vObjectCenter, g_ArcBall でモデルの座標を World に設定☆ D3DXMATRIXA16 mxWorld,mxwk; D3DXMatrixTranslation( &mxwk, g_vObjectCenter.x, g_vObjectCenter.y, g_vObjectCenter.z ); D3DXMatrixIdentity( &mxWorld ); D3DXMatrixMultiply( &mxWorld, &mxWorld, g_ArcBall.GetRotationMatrix() ); D3DXMatrixMultiply( &mxWorld, &mxWorld, g_ArcBall.GetTranslationMatrix() ); D3DXMatrixMultiply( &mxWorld, &mxWorld, &mxwk ); g_v_pAnimInstances->SetWorldTransform( & mxWorld ); } |
//☆アニメーションセットの切り替☆ switch(Setnum) { case 0: g_pAnimController->GetAnimationSet( m_dwAnimIdxWalk, & pAS ); break; case 1: g_pAnimController->GetAnimationSet( m_dwAnimIdxJog, & pAS ); break; case 2: g_pAnimController->GetAnimationSet( m_dwAnimIdxLoiter, & pAS ); } |
前田稔(Maeda Minoru)の超初心者のプログラム入門