Example #1
0
void UNIT::SetAnimation(int index)
{
	ID3DXAnimationSet *anim = NULL;
	m_pAnimControl->GetAnimationSet(index, &anim);
	if(anim != NULL)m_pAnimControl->SetTrackAnimationSet(0, anim);
	anim->Release();
}
Example #2
0
float SKINNEDMESH::GetAnimationDuration(int index)
{
	float duration = 0.0f;
	ID3DXAnimationSet *anim = NULL;
	m_pAnimControl->GetAnimationSet(index, &anim);
	if(anim != NULL)duration = (float)anim->GetPeriod();
	anim->Release();

	return duration;
}
void XSkinnedMesh::SetAnimation( int getAnimNum, int setTrackNum )
{
	// set animation
	ID3DXAnimationSet* animSet;

	_animation->GetAnimationSet( getAnimNum, &animSet );

	_animation->SetTrackAnimationSet( setTrackNum, animSet );

	animSet->Release();
}
//-----------------------------------------------------------------------------
// Plays the given animation with the given transition time.
//-----------------------------------------------------------------------------
void AnimatedObject::PlayAnimation( unsigned int animation, float transitionTime, bool loop )
{
	// Ensure object has a valid animation controller.
	if( m_animationController == NULL )
		return;

	// Ensure the transition time is always greater than zero.
	if( transitionTime <= 0.0f )
		transitionTime = 0.000001f;

	// Find which track to play the new animation on.
	unsigned int newTrack = ( m_currentTrack == 0 ? 1 : 0 );

	// Get a pointer to the animation set to play.
	ID3DXAnimationSet *as;
	m_animationController->GetAnimationSet( animation, &as );

	// Set the animation set to the new track.
	m_animationController->SetTrackAnimationSet( newTrack, as );

	// Clear all the events that are currently set on the tracks.
	m_animationController->UnkeyAllTrackEvents( m_currentTrack );
	m_animationController->UnkeyAllTrackEvents( newTrack );

	// Check if this animation should be looped or only played once.
	if( loop == true )
	{
		// Transition the new track in over the specified transition time period.
		m_animationController->KeyTrackEnable( m_currentTrack, false, m_currentTime + transitionTime );
		m_animationController->KeyTrackWeight( m_currentTrack, 0.0f, m_currentTime, transitionTime, D3DXTRANSITION_LINEAR );
		m_animationController->SetTrackEnable( newTrack, true );
		m_animationController->KeyTrackWeight( newTrack, 1.0f, m_currentTime, transitionTime, D3DXTRANSITION_LINEAR );
	}
	else
	{
		// Stop the current track, and start the new track without transitioning.
		m_animationController->SetTrackEnable( m_currentTrack, false );
		m_animationController->SetTrackWeight( m_currentTrack, 0.0f );
		m_animationController->SetTrackEnable( newTrack, true );
		m_animationController->SetTrackWeight( newTrack, 1.0f );
		m_animationController->SetTrackPosition( newTrack, 0.0f );
		m_animationController->KeyTrackEnable( newTrack, false, m_currentTime + as->GetPeriod() );
	}

	// Release the pointer to the animation set.
	as->Release();

	// The new track is now the current track.
	m_currentTrack = newTrack;
}
Example #5
0
TriPickDemo::TriPickDemo(HINSTANCE hInstance, std::string winCaption, D3DDEVTYPE devType, DWORD requestedVP)
	: D3DApp(hInstance, winCaption, devType, requestedVP)
{
	if (!checkDeviceCaps())
	{
		MessageBox(0, "checkDeviceCaps() Failed", 0, 0);
		PostQuitMessage(0);
	}
	//initiate our objects
	HR(D3DXCreateCylinder(g_pDevice, 1, 1, 1, 10, 5, &m_Mesh, 0));
	m_pCharacter = new Character();
	m_pCharacter->Setup(g_pDevice);
	m_pCharacter->Load("resources/meshes/soldier.x");
	m_pCharacter->InitBoneCylinder(m_pCharacter->getPBone(), NULL);
	//set static anime
	m_animController = m_pCharacter->GetController();
	ID3DXAnimationSet *anim = NULL;
	m_animController->GetAnimationSet(2, &anim);
	m_animController->SetTrackAnimationSet(0, anim);
	anim->Release();
	//initiate IK
	m_pIK = new InverseKinematics(m_pCharacter);
	//initiate camera
	D3DXVECTOR3 vFromPt = D3DXVECTOR3(0.0f, 2.0f, -3.0f);
	D3DXVECTOR3 vLookatPt = D3DXVECTOR3(0.0f, 1.0f, 0.0f);
	D3DXVECTOR3 up = D3DXVECTOR3(0.0f, 1.0f, 0.0f);
	g_Camera->lookAt(vFromPt, vLookatPt, up);
	g_Camera->setSpeed(10.0f);

	D3DXMatrixIdentity(&m_matWorld);
	//read shader
	ID3DXBuffer *pErrorMsgs = NULL;
	HR(D3DXCreateEffectFromFile(g_pDevice, "Pick.fx", NULL, NULL, D3DXSHADER_DEBUG, NULL, &m_FX, &pErrorMsgs));

	//initiate lighting
	m_Light.dirW = D3DXVECTOR3(0.0f, 2.0f, -3.0f);
	D3DXVec3Normalize(&m_Light.dirW, &m_Light.dirW);
	m_Light.ambient = D3DXCOLOR(0.5f, 0.5f, 0.5f, 1.0f);
	m_Light.diffuse = D3DXCOLOR(1.0f, 1.0f, 1.0f, 1.0f);
	m_Light.spec = D3DXCOLOR(1.0f, 1.0f, 1.0f, 1.0f);

	L_Hand = (Bone*)D3DXFrameFind(m_pCharacter->getPBone(), "Bone21");
	R_Hand = (Bone*)D3DXFrameFind(m_pCharacter->getPBone(), "Bone18");
	L_Leg = (Bone*)D3DXFrameFind(m_pCharacter->getPBone(), "Bone11");
	R_Leg = (Bone*)D3DXFrameFind(m_pCharacter->getPBone(), "Bone08");
	onResetDevice();
}
Example #6
0
std::vector<std::string> SKINNEDMESH::GetAnimations()
{
	ID3DXAnimationSet *anim = NULL;
	std::vector<std::string> animations;

	for(int i=0;i<(int)m_pAnimControl->GetMaxNumAnimationSets();i++)
	{
		anim = NULL;
		m_pAnimControl->GetAnimationSet(i, &anim);

		if(anim != NULL)
		{
			animations.push_back(anim->GetName());
			anim->Release();
		}
	}

	return animations;
}
Example #7
0
void UNIT::SetAnimation(char name[])
{
	ID3DXAnimationSet *anim = NULL;
	m_animation = 0;

	for(int i=0;i<(int)m_pAnimControl->GetMaxNumAnimationSets();i++)
	{
		anim = NULL;
		m_pAnimControl->GetAnimationSet(i, &anim);

		if(anim != NULL)
		{
			if(strcmp(name, anim->GetName()) == 0)
			{
				m_pAnimControl->ResetTime();
				m_pAnimControl->SetTrackAnimationSet(0, anim);
				m_animation = i;
			}
			anim->Release();
		}
	}
}
Example #8
0
int SKINNEDMESH::SetAnimation(char name[])
{
	ID3DXAnimationSet *anim = NULL;

	for(int i=0;i<(int)m_pAnimControl->GetMaxNumAnimationSets();i++)
	{
		anim = NULL;
		m_pAnimControl->GetAnimationSet(i, &anim);

		if(anim != NULL)
		{
			if(strcmp(name, anim->GetName()) == 0)
			{
				m_pAnimControl->SetTrackAnimationSet(0, anim);
				anim->Release();
				return i;
			}
			anim->Release();
		}
	}

	return -1;
}
Example #9
0
HRESULT Application::Init(HINSTANCE hInstance, bool windowed) {
    g_debug << "Application Started \n";

    //Create Window Class
    WNDCLASS wc;
    memset(&wc, 0, sizeof(WNDCLASS));
    wc.style         = CS_HREDRAW | CS_VREDRAW;
    wc.lpfnWndProc   = (WNDPROC)WndProc;
    wc.hInstance     = hInstance;
    wc.lpszClassName = "D3DWND";

    RECT rc = {0, 0, WINDOW_WIDTH, WINDOW_HEIGHT};
    AdjustWindowRect(&rc, WS_OVERLAPPEDWINDOW, false);

    //Register Class and Create new Window
    RegisterClass(&wc);
    m_mainWindow = ::CreateWindow("D3DWND", "Character Animation with Direct3D: Example 11.2", WS_OVERLAPPEDWINDOW, 0, 0, rc.right - rc.left, rc.bottom - rc.top, 0, 0, hInstance, 0);
    //SetCursor(NULL);
    ::ShowWindow(m_mainWindow, SW_SHOW);
    ::UpdateWindow(m_mainWindow);

    //Create IDirect3D9 Interface
    IDirect3D9* d3d9 = Direct3DCreate9(D3D_SDK_VERSION);

    if (d3d9 == NULL) {
        g_debug << "Direct3DCreate9() - FAILED \n";
        return E_FAIL;
    }

    //Check that the Device supports what we need from it
    D3DCAPS9 caps;
    d3d9->GetDeviceCaps(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, &caps);

    //Check vertex & pixelshader versions
    if (caps.VertexShaderVersion < D3DVS_VERSION(2, 0) || caps.PixelShaderVersion < D3DPS_VERSION(2, 0)) {
        g_debug << "Warning - Your graphic card does not support vertex and pixelshaders version 2.0 \n";
    }

    //Set D3DPRESENT_PARAMETERS
    m_present.BackBufferWidth            = WINDOW_WIDTH;
    m_present.BackBufferHeight           = WINDOW_HEIGHT;
    m_present.BackBufferFormat           = D3DFMT_A8R8G8B8;
    m_present.BackBufferCount            = 2;
    m_present.MultiSampleType            = D3DMULTISAMPLE_NONE;
    m_present.MultiSampleQuality         = 0;
    m_present.SwapEffect                 = D3DSWAPEFFECT_DISCARD;
    m_present.hDeviceWindow              = m_mainWindow;
    m_present.Windowed                   = windowed;
    m_present.EnableAutoDepthStencil     = true;
    m_present.AutoDepthStencilFormat     = D3DFMT_D24S8;
    m_present.Flags                      = 0;
    m_present.FullScreen_RefreshRateInHz = D3DPRESENT_RATE_DEFAULT;
    m_present.PresentationInterval       = D3DPRESENT_INTERVAL_IMMEDIATE;

    //Hardware Vertex Processing
    int vp = 0;
    if ( caps.DevCaps & D3DDEVCAPS_HWTRANSFORMANDLIGHT )
        vp = D3DCREATE_HARDWARE_VERTEXPROCESSING;
    else vp = D3DCREATE_SOFTWARE_VERTEXPROCESSING;

    //Create the IDirect3DDevice9
    if (FAILED(d3d9->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, m_mainWindow, vp, &m_present, &g_pDevice))) {
        g_debug << "Failed to create IDirect3DDevice9 \n";
        return E_FAIL;
    }

    //Release IDirect3D9 interface
    d3d9->Release();

    //Load Application Specific resources here...
    D3DXCreateFont(g_pDevice, 50, 0, FW_BOLD, 1, false,
                   DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, DEFAULT_QUALITY,
                   DEFAULT_PITCH | FF_DONTCARE, "Arial", &g_pFont);

    //Create Sprite
    D3DXCreateSprite(g_pDevice, &g_pSprite);

    //Load Effect
    ID3DXBuffer *pErrorMsgs = NULL;
    HRESULT hRes = D3DXCreateEffectFromFile(g_pDevice, "resources/fx/lighting.fx", NULL, NULL, D3DXSHADER_DEBUG, NULL, &g_pEffect, &pErrorMsgs);

    if (FAILED(hRes) && (pErrorMsgs != NULL)) {      //Failed to create Effect
        g_debug << (char*)pErrorMsgs->GetBufferPointer() << "\n";
        return E_FAIL;
    }

    m_deviceLost = false;

    //Setup mesh
    m_drone.Load("resources/meshes/soldier.x");
    m_animController = m_drone.GetController();

    //Set Still Anim
    ID3DXAnimationSet *anim = NULL;
    m_animController->GetAnimationSet(2, &anim);
    m_animController->SetTrackAnimationSet(0, anim);
    anim->Release();

    //Setup IK
    m_pIK = new InverseKinematics(&m_drone);

    //Set Cursor
    HCURSOR cursor = LoadCursor(NULL, IDC_ARROW);
    SetCursor(cursor);

    return S_OK;
}
Example #10
0
XAsset::XAsset(const char* resourcePath)
{
    // report progress
    if( Engine::instance->progressCallback )
    {
        Engine::instance->progressCallback(
            wstrformat( Gui::iLanguage->getUnicodeString(4), asciizToUnicode(resourcePath).c_str() ).c_str(),
            0.0f,
            Engine::instance->progressCallbackUserData
        );
    }

    IResource* resource = getCore()->getResource( resourcePath, "rb" );
    assert( resource );
    
    // retrieve file size
    FILE* file = resource->getFile();
    fseek( file, 0, SEEK_END );
    int fileSize = ftell( file );
    fseek( file, 0, SEEK_SET );

    // create buffer & load file data in buffer
    char* buffer = new char[fileSize];
    fread( buffer, 1, fileSize, file );
    resource->release();

    // report progress
    if( Engine::instance->progressCallback )
    {
        Engine::instance->progressCallback(
            wstrformat( Gui::iLanguage->getUnicodeString(4), asciizToUnicode(resourcePath).c_str() ).c_str(),
            0.25f,
            Engine::instance->progressCallbackUserData
        );
    }

    // create hierarchy allocation interface
    XAllocateHierarchy xAlloc;

    // load hierarchy from file
    D3DXFRAME*                rootFrame;
    ID3DXAnimationController* animController;
    _dxCR( D3DXLoadMeshHierarchyFromXInMemory(
        buffer,
        fileSize,
        D3DXMESH_MANAGED, 
        iDirect3DDevice, 
        &xAlloc, 
        NULL, 
        &rootFrame, 
        &animController
    ) );

    delete[] buffer;

    // report progress
    if( Engine::instance->progressCallback )
    {
        Engine::instance->progressCallback(
            wstrformat( Gui::iLanguage->getUnicodeString(4), asciizToUnicode(resourcePath).c_str() ).c_str(),
            0.5f,
            Engine::instance->progressCallbackUserData
        );
    }

    // create D3 animation objects
    Clump* clump = new Clump( "SCENE_ROOT" );
    clump->setFrame( static_cast<Frame*>( rootFrame ) );

    if( animController )
    {        
	    ID3DXAnimationSet* animationSet;
        const char*        animationName;
    
	    animController->GetAnimationSet( 0, &animationSet );
	    unsigned int numAnimations = animationSet->GetNumAnimations();
        float period = float( animationSet->GetPeriod() );
        float numFramesPerSecond = 100.0f/3.0f;
        float frameTime = 1.0f / numFramesPerSecond;
        unsigned int numFrames = unsigned int( period * numFramesPerSecond );
        if( !numFrames ) numFrames++;

        AnimationSet* as = new AnimationSet( numAnimations );
	
	    for( unsigned int i=0; i<numAnimations; i++ )
	    {
		    SRT srt;
	
		    animationSet->GetAnimationNameByIndex( i, &animationName );

            Animation* animation = new Animation( animationName, numFrames );

		    for( unsigned int j=0; j<numFrames; j++ )
		    {
                srt.time = j * frameTime;
			    _dxCR( animationSet->GetSRT( 
				    srt.time,
				    i,
				    &srt.scale,
				    &srt.rotation,
				    &srt.translation
                ) );            
                animation->setKey( j, &srt );
            }

            assert( animation->validateKeys() );
        
            as->setAnimation( i, animation );
        }
        clump->setAnimation( as );    

        animationSet->Release();
        animController->Release();
    }

    // report progress
    if( Engine::instance->progressCallback )
    {
        Engine::instance->progressCallback(
            wstrformat( Gui::iLanguage->getUnicodeString(4), asciizToUnicode(resourcePath).c_str() ).c_str(),
            0.75f,
            Engine::instance->progressCallbackUserData
        );
    }

    // create D3 classes for hierarchy
    createD3HierarchyClasses( clump, rootFrame );

    // resolve nameless phenomena
    resolveNamelessFrames( clump );

    _clumps.push_back( clump );
    
    static_cast<Frame*>( rootFrame )->dirty();

    // report progress
    if( Engine::instance->progressCallback )
    {
        Engine::instance->progressCallback(
            wstrformat( Gui::iLanguage->getUnicodeString(4), asciizToUnicode(resourcePath).c_str() ).c_str(),
            1.0f,
            Engine::instance->progressCallbackUserData
        );
    }
}