Esempio n. 1
0
void LevelParts::setWorld(XMFLOAT4X4 tempLevelPartsWorld)
{


	mLevelPartsWorld = tempLevelPartsWorld;


	XMMATRIX tempWorld = XMLoadFloat4x4(&mLevelPartsWorld);


	XMVECTOR Scale;
	XMVECTOR Position;
	XMVECTOR Rotation;


	XMMatrixDecompose(&Scale, &Rotation, &Position, tempWorld);

	XMStoreFloat3(&mLevelPartsPosition, Position);

	XMStoreFloat3(&mLevelPartsScale, Scale);

	XMStoreFloat4(&mLevelPartsRotation, Rotation);





	XMVECTOR S = XMLoadFloat3(&mLevelPartsScale);
	XMVECTOR P = XMLoadFloat3(&mLevelPartsPosition);
	XMVECTOR Q = XMLoadFloat4(&mLevelPartsRotationQuad);
	XMVECTOR rot = XMLoadFloat4(&mLevelPartsRotation);

	XMStoreFloat4x4(&mLevelPartsStartingWorld, XMMatrixAffineTransformation(S, rot, Q, P));

}
	//! Returns the bounding box in world space. [NOTE] Does not work [TODO].
	AxisAlignedBox AnimatedObject::GetBoundingBox()
	{
		//AxisAlignedBox aabb = mSkinnedModel->GetBoundingBox();	// [WIP] The precalculated AABB for the model.
		SkinnedMeshList* meshList = mSkinnedModel->GetMeshList();
		XNA::AxisAlignedBox aabb = meshList->operator[](0)->GetPrimitive()->GetBoundingBox();
		XMFLOAT3 min = aabb.Center - aabb.Extents;
		XMFLOAT3 max = aabb.Center + aabb.Extents;
		for(int i = 1; i < meshList->size(); i++)
		{
			AxisAlignedBox meshAABB = (*meshList)[i]->GetPrimitive()->GetBoundingBox();
			XMFLOAT3 meshMin = meshAABB.Center - meshAABB.Extents;
			XMFLOAT3 meshMax = meshAABB.Center + meshAABB.Extents;

			XMStoreFloat3(&min, XMVectorMin(XMLoadFloat3(&meshMin), XMLoadFloat3(&min)));
			XMStoreFloat3(&max, XMVectorMax(XMLoadFloat3(&meshMax), XMLoadFloat3(&max)));
		}

		aabb.Center =  (min + max) * 0.5f;
		aabb.Extents = (max - min) * 0.5f;

		// Break up the world matrix into it's components.
		XMVECTOR scale, rotation, translation;
		XMMatrixDecompose(&scale, &rotation, &translation, GetWorldMatrix());

		// Transform the AABB with the components.
		TransformAxisAlignedBoxCustom(&aabb, &aabb, scale, rotation, translation);

		aabb.Center = GetPosition();	// [NOTE] Not like this in StaticObject.

		return aabb;
	}
void Enemies::update(float dt)
{



	DeltaTime = dt;

	for (int i = 0; i < mEnemyInstances.size(); i++)
	{

		enemyclass[i]->update(DeltaTime);

		mEnemyInstances[i].World = enemyclass[i]->GetWorld();

		XMMATRIX temp = XMLoadFloat4x4(&mEnemyInstances[i].World);
		XMVECTOR Scale;
		XMVECTOR Rotation;
		XMVECTOR Position;

		XMFLOAT3 tempPosition;

		XMMatrixDecompose(&Scale, &Rotation, &Position, temp);
		XMStoreFloat3(&tempPosition, Position);



		LevelCollisions[i].Center = tempPosition;


	}

}
void Enemies::CreateBoundingBox()
{
	//
	// Compute scene bounding box.
	//
	XMFLOAT3 minPt(+MathHelper::Infinity, +MathHelper::Infinity, +MathHelper::Infinity);
	XMFLOAT3 maxPt(-MathHelper::Infinity, -MathHelper::Infinity, -MathHelper::Infinity);
	for (UINT i = 0; i < mEnemyInstances.size(); ++i)
	{
		minPt.x = 0.0f;
		minPt.y = 0.0f;
		minPt.z = 0.0f;
		maxPt.x = 0.0f;
		maxPt.y = 0.0f;
		maxPt.z = 0.0f;

		for (UINT j = 0; j < mEnemyInstances[i].Model->BasicVertices.size(); ++j)
		{
			XMFLOAT3 P = mEnemyInstances[i].Model->BasicVertices[j].Pos;
			//////multiply all these by 7
			minPt.x = MathHelper::Min(minPt.x, P.x);
			minPt.y = MathHelper::Min(minPt.y, P.y);
			minPt.z = MathHelper::Min(minPt.z, P.z);
			maxPt.x = MathHelper::Max(maxPt.x, P.x);
			maxPt.y = MathHelper::Max(maxPt.y, P.y);
			maxPt.z = MathHelper::Max(maxPt.z, P.z);
		}

		XMMATRIX temp = XMLoadFloat4x4(&mEnemyInstances[i].World);
		enemyclass[i]->setWorld(mEnemyInstances[i].World);

		XMVECTOR Scale;
		XMVECTOR Position;
		XMVECTOR Rotation;

		XMMatrixDecompose(&Scale, &Rotation, &Position, temp);

		XMFLOAT3 tempPos;
		XMStoreFloat3(&tempPos, Position);

		LevelCollisions[i].Center = tempPos;
		LevelCollisions[i].Extents = XMFLOAT3(0.5f*(maxPt.x - minPt.x),
			0.5f*(maxPt.y - minPt.y),
			0.5f*(maxPt.z - minPt.z));

		LevelCollisions[i].collisionType = enemyclass[i]->getcollisiontype();
		FLOAT scale = enemyclass[i]->getScale();
		LevelCollisions[i].Extents.x = LevelCollisions[i].Extents.x * scale;
		LevelCollisions[i].Extents.y = LevelCollisions[i].Extents.y * scale;
		LevelCollisions[i].Extents.z = LevelCollisions[i].Extents.z * scale;

		EnemyBox.collisionType = 1;

		enemyclass[i]->setAABB(&LevelCollisions[i]);
	}
}
XMMATRIX ModelLoader::ConvertFBXtoDXMatrix( aiMatrix4x4 inMat ) {
	// aiMatrix is transposed
	XMMATRIX xmInMat = XMLoadFloat4x4( &XMFLOAT4X4(
		inMat.a1, inMat.b1, inMat.c1, inMat.d1,
		inMat.a2, inMat.b2, inMat.c2, inMat.d2,
		inMat.a3, inMat.b3, inMat.c3, inMat.d3,
		inMat.a4, inMat.b4, inMat.c4, inMat.d4 ) );
	XMVECTOR translate, scale, rotQuat;
	XMMatrixDecompose( &scale, &rotQuat, &translate, xmInMat );
	scale = scale*100;
	return nullptr;
}
Esempio n. 6
0
//! Returns the camera frustum in world space.
Frustum Camera::GetFrustum()
{
    XMVECTOR scale, rotation, translation, detView;

    // The frustum is in view space, so we need to get the inverse view matrix
    // to transform it to world space.
    XMMATRIX invView = XMMatrixInverse(&detView, XMLoadFloat4x4(&GetViewMatrix()));

    // Decompose the inverse view matrix and transform the frustum with the components.
    XMMatrixDecompose(&scale, &rotation, &translation, invView);
    Frustum worldFrustum;
    TransformFrustum(&worldFrustum, &mFrustum, XMVectorGetX(scale), rotation, translation);

    // Return the transformed frustum that now is in world space.
    return worldFrustum;
}
Esempio n. 7
0
void FrustumCulling::FrustumCull(std::vector<GenericModelInstance>& instances, Camera& camera)
{
    if (!mFrustumCullingEnabled)
        return;

    mNumVisible = 0;

    XMVECTOR detView = XMMatrixDeterminant(camera.GetViewMatrix());
    XMMATRIX invView = XMMatrixInverse(&detView, camera.GetViewMatrix());

    for (UINT i = 0; i < instances.size(); ++i)
    {
        instances[i].isVisible = false;

        XMMATRIX W = XMLoadFloat4x4(&instances[i].world);
        XMMATRIX invWorld = XMMatrixInverse(&XMMatrixDeterminant(W), W);

        // View space to the object's local space.
        XMMATRIX toLocal = XMMatrixMultiply(invView, invWorld);

        // Decompose the matrix into its individual parts.
        XMVECTOR scale;
        XMVECTOR rotQuat;
        XMVECTOR translation;
        XMMatrixDecompose(&scale, &rotQuat, &translation, toLocal);

        // Transform the camera frustum from view space to the object's local space.
        XNA::Frustum localspaceFrustum;
        XNA::TransformFrustum(&localspaceFrustum, &camera.GetFrustum(), XMVectorGetX(scale), rotQuat, translation);

        // Perform the box/frustum intersection test in local space.
        if(XNA::IntersectAxisAlignedBoxFrustum(&instances[i].model->boundingBox, &localspaceFrustum) != 0)
        {
            // Write the instance data to dynamic VB of the visible objects.
            //dataView[mVisibleObjectCount++] = mInstancedData[i];
            mNumVisible++;
            instances[i].isVisible = true;
        }
    }
}
void InstancingAndCullingApp::UpdateScene(float dt)
{
	//
	// Control the camera.
	//
	if( GetAsyncKeyState('W') & 0x8000 )
		mCam.Walk(10.0f*dt);

	if( GetAsyncKeyState('S') & 0x8000 )
		mCam.Walk(-10.0f*dt);

	if( GetAsyncKeyState('A') & 0x8000 )
		mCam.Strafe(-10.0f*dt);

	if( GetAsyncKeyState('D') & 0x8000 )
		mCam.Strafe(10.0f*dt);
		
	if( GetAsyncKeyState('1') & 0x8000 )
		mFrustumCullingEnabled = true;

	if( GetAsyncKeyState('2') & 0x8000 )
		mFrustumCullingEnabled = false;

	//
	// Perform frustum culling.
	//

	mCam.UpdateViewMatrix();
	mVisibleObjectCount = 0;

	if(mFrustumCullingEnabled)
	{
		XMVECTOR detView = XMMatrixDeterminant(mCam.View());
		XMMATRIX invView = XMMatrixInverse(&detView, mCam.View());
	
		D3D11_MAPPED_SUBRESOURCE mappedData; 
		md3dImmediateContext->Map(mInstancedBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedData);

		InstancedData* dataView = reinterpret_cast<InstancedData*>(mappedData.pData);

		for(UINT i = 0; i < mInstancedData.size(); ++i)
		{
			XMMATRIX W = XMLoadFloat4x4(&mInstancedData[i].World);
			XMMATRIX invWorld = XMMatrixInverse(&XMMatrixDeterminant(W), W);

			// View space to the object's local space.
			XMMATRIX toLocal = XMMatrixMultiply(invView, invWorld);
		
			// Decompose the matrix into its individual parts.
			XMVECTOR scale;
			XMVECTOR rotQuat;
			XMVECTOR translation;
			XMMatrixDecompose(&scale, &rotQuat, &translation, toLocal);

			// Transform the camera frustum from view space to the object's local space.
			XNA::Frustum localspaceFrustum;
			XNA::TransformFrustum(&localspaceFrustum, &mCamFrustum, XMVectorGetX(scale), rotQuat, translation);

			// Perform the box/frustum intersection test in local space.
			if(XNA::IntersectAxisAlignedBoxFrustum(&mSkullBox, &localspaceFrustum) != 0)
			{
				// Write the instance data to dynamic VB of the visible objects.
				dataView[mVisibleObjectCount++] = mInstancedData[i];
			}
		}

		md3dImmediateContext->Unmap(mInstancedBuffer, 0);
	}
	else // No culling enabled, draw all objects.
	{
		D3D11_MAPPED_SUBRESOURCE mappedData; 
		md3dImmediateContext->Map(mInstancedBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedData);

		InstancedData* dataView = reinterpret_cast<InstancedData*>(mappedData.pData);

		for(UINT i = 0; i < mInstancedData.size(); ++i)
		{
			dataView[mVisibleObjectCount++] = mInstancedData[i];
		}

		md3dImmediateContext->Unmap(mInstancedBuffer, 0);
	}

	std::wostringstream outs;   
	outs.precision(6);
	outs << L"Instancing and Culling Demo" << 
		L"    " << mVisibleObjectCount << 
		L" objects visible out of " << mInstancedData.size();
	mMainWndCaption = outs.str();
}
void Enemy::move(FLOAT dt)
{


	bool dontUpdate = false;

	XMMATRIX startingworldMatrix = XMLoadFloat4x4(&mEnemyStartingWorld);
	XMVECTOR S;
	XMVECTOR P;
	XMVECTOR Q;

	XMMatrixDecompose(&S, &Q, &P, startingworldMatrix);


	XMVECTOR enemyPosition = XMLoadFloat3(&mEnemyPosition);



	FLOAT newPos = XMVectorGetZ(enemyPosition);
	FLOAT oldPos = XMVectorGetZ(P);

	direction = XMVectorSet(0.0f, 0.0f, 0.0f, 0.0f);



	FLOAT diffX;
	FLOAT diffY;
	FLOAT diffZ;

	diffX = 0;
	diffY = 0;
	diffZ = 0;





	if (travelToPoint == 2)
	{


		diffX = mEnemyPosition.x - mEnemyPositionTwo.x;
		diffY = mEnemyPosition.y - mEnemyPositionTwo.y;
		diffZ = mEnemyPosition.z - mEnemyPositionTwo.z;
		mEnemyPositionThree.x;

	
	
	

		int nothing = 0;
		if (diffX < 0.0f)
		{

			diffX *= -1;
		}
		if (diffY < 0.0f)
		{

			diffY *= -1;
		}
		if (diffZ < 0.0f)
		{

			diffZ *= -1;
		}

		//////////////////////////////////
		if (diffX < 0.01)
		{

			mEnemyPosition.x = mEnemyPositionTwo.x;
		}
		if (diffY < 0.01)
		{

			mEnemyPosition.y = mEnemyPositionTwo.y;
		}
		if (diffZ < 0.01)
		{

			mEnemyPosition.z = mEnemyPositionTwo.z;
		}
		////////////////////////////////


		if (mEnemyPosition.x > mEnemyPositionTwo.x)
		{

			direction += XMVectorSet(-1.0f, 0.0f, 0, 0.0f);


		}
		else if (mEnemyPosition.x < mEnemyPositionTwo.x)
		{

			direction += XMVectorSet(1.0f, 0.0f, 0, 0.0f);

		}



		if (mEnemyPosition.z > mEnemyPositionTwo.z)
		{

			direction += XMVectorSet(0.0f, 0.0f, -1.0f, 0.0f);


		}
		else if (mEnemyPosition.z < mEnemyPositionTwo.z)
		{

			direction += XMVectorSet(0.0f, 0.0f, 1.0f, 0.0f);

		}


		if (mEnemyPosition.y > mEnemyPositionTwo.y)
		{

			direction += XMVectorSet(0.0f, -1.0f, 0.0f, 0.0f);


		}
		else if (mEnemyPosition.y < mEnemyPositionTwo.y)
		{

			direction += XMVectorSet(0.0f, 1.0f, 0.0f, 0.0f);

		}
		if (diffX < 0.05f && diffY < 0.05f && diffZ < 0.05f)
		{

		
			if (timesThrough == 0)
			{
				if (mEnemyPositionThree.x == 0)
				{

					dontUpdate = true;
					travelToPoint = 1;

					lastPoint = mEnemyPositionTwo;

				}
				else if (mEnemyPositionThree.x != 0)
				{
					dontUpdate = true;
					travelToPoint = 3;

					lastPoint = mEnemyPositionTwo;

				}
			}
			else
			{
				dontUpdate = true;

				travelToPoint = 1;
				lastPoint = mEnemyPositionTwo;
			}

			int something = 1;
		}

	}



	/////////////////////////////////////////
	else if (travelToPoint == 1)
	{

		timesThrough = 0;

		diffX = mEnemyPosition.x - mEnemyPositionOne.x;
		diffY = mEnemyPosition.y - mEnemyPositionOne.y;
		diffZ = mEnemyPosition.z - mEnemyPositionOne.z;



		if (diffX < 0.0f)
		{

			diffX *= -1;
		}
		if (diffY < 0.0f)
		{

			diffY *= -1;
		}
		if (diffZ < 0.0f)
		{

			diffZ *= -1;
		}


		//////////////////////////////////
		if (diffX < 0.01)
		{

			mEnemyPosition.x = mEnemyPositionOne.x;
		}
		if (diffY < 0.01)
		{

			mEnemyPosition.y = mEnemyPositionOne.y;
		}
		if (diffZ < 0.01)
		{

			mEnemyPosition.z = mEnemyPositionOne.z;
		}
		////////////////////////////////



		if (mEnemyPosition.x > mEnemyPositionOne.x)
		{

			direction += XMVectorSet(-1.0f, 0.0f, 0, 0.0f);


		}
		if (mEnemyPosition.x < mEnemyPositionOne.x)
		{

			direction += XMVectorSet(1.0f, 0.0f,0, 0.0f);

		}



		if (mEnemyPosition.z > mEnemyPositionOne.z && diffZ > 0.01)
		{

			direction += XMVectorSet(0.0f, 0.0f, -1.0f, 0.0f);


		}
		if (mEnemyPosition.z < mEnemyPositionOne.z && diffZ > 0.01)
		{

			direction += XMVectorSet(0.0f, 0.0f, 1.0f, 0.0f);

		}


		if (mEnemyPosition.y > mEnemyPositionOne.y)
		{

			direction += XMVectorSet(0.0f, -1.0f, 0.0f, 0.0f);


		}
		if (mEnemyPosition.y < mEnemyPositionOne.y)
		{

			direction += XMVectorSet(0.0f, 1.0f, 0.0f, 0.0f);

		}
		if (diffX < 0.05f && diffY < 0.05f && diffZ < 0.05f)
		{
			
			travelToPoint = 2;

			lastPoint = mEnemyPositionOne;

		}
	}



	////////////////////////////////////////////////////////////////////////////////
	else if (travelToPoint == 3 && timesThrough == 0)
	{


		diffX = mEnemyPosition.x - mEnemyPositionThree.x;
		diffY = mEnemyPosition.y - mEnemyPositionThree.y;
		diffZ = mEnemyPosition.z - mEnemyPositionThree.z;


		int nothing;
		if (diffX < 0.0f)
		{

			diffX *= -1;
		}
		if (diffY < 0.0f)
		{

			diffY *= -1;
		}
		if (diffZ < 0.0f)
		{

			diffZ *= -1;
		}


		//////////////////////////////////
		if (diffX < 0.01)
		{

			mEnemyPosition.x = mEnemyPositionThree.x;
		}
		if (diffY < 0.01)
		{

			mEnemyPosition.y = mEnemyPositionThree.y;
		}
		if (diffZ < 0.01)
		{

			mEnemyPosition.z = mEnemyPositionThree.z;
		}
		////////////////////////////////



		if (mEnemyPosition.x > mEnemyPositionThree.x)
		{

			direction += XMVectorSet(-1.0f, 0.0f, 0, 0.0f);


		}
		if (mEnemyPosition.x < mEnemyPositionThree.x)
		{

			direction += XMVectorSet(1.0f, 0.0f, 0, 0.0f);

		}



		if (mEnemyPosition.z > mEnemyPositionThree.z)
		{

			direction += XMVectorSet(0.0f, 0.0f, -1.0f, 0.0f);


		}
		if (mEnemyPosition.z < mEnemyPositionThree.z)
		{

			direction += XMVectorSet(0.0f, 0.0f, 1.0f, 0.0f);

		}


		if (mEnemyPosition.y > mEnemyPositionThree.y)
		{

			direction += XMVectorSet(0.0f, -1.0f, 0.0f, 0.0f);


		}
		if (mEnemyPosition.y < mEnemyPositionThree.y)
		{

			direction += XMVectorSet(0.0f, 1.0f, 0.0f, 0.0f);

		}
		if (diffX < 0.05f && diffY < 0.05f && diffZ < 0.05f)
		{

			if (mEnemyPositionFour.x == NULL)
			{
					


				travelToPoint = 2;
				timesThrough = 1;
			}
			else
			{
				
				travelToPoint = 4;
		
			}
		}
	}


	else if (travelToPoint == 4)
	{


		diffX = mEnemyPosition.x - mEnemyPositionFour.x;
		diffY = mEnemyPosition.y - mEnemyPositionFour.y;
		diffZ = mEnemyPosition.z - mEnemyPositionFour.z;

		if (diffX < 0.0f)
		{

			diffX *= -1;
		}
		if (diffY < 0.0f)
		{

			diffY *= -1;
		}
		if (diffZ < 0.0f)
		{

			diffZ *= -1;
		}

		//////////////////////////////////
		if (diffX < 0.001)
		{

			mEnemyPosition.x = mEnemyPositionFour.x;
		}
		if (diffY < 0.001)
		{

			mEnemyPosition.y = mEnemyPositionFour.y;
		}
		if (diffZ < 0.001)
		{

			mEnemyPosition.z = mEnemyPositionFour.z;
		}
		////////////////////////////////



		if (mEnemyPosition.x > mEnemyPositionFour.x)
		{

			direction += XMVectorSet(-1.0f, 0.0f, 0, 0.0f);


		}
		if (mEnemyPosition.x < mEnemyPositionFour.x)
		{

			direction += XMVectorSet(1.0f, 0.0f,0, 0.0f);

		}



		if (mEnemyPosition.z > mEnemyPositionFour.z && diffZ > 0.01)
		{

			direction += XMVectorSet(0.0f, 0.0f, -1.0f, 0.0f);


		}
		if (mEnemyPosition.z < mEnemyPositionFour.z && diffZ > 0.01)
		{

			direction += XMVectorSet(0.0f, 0.0f, 1.0f, 0.0f);

		}


		if (mEnemyPosition.y > mEnemyPositionFour.y)
		{

			direction += XMVectorSet(0.0f, -1.0f, 0.0f, 0.0f);


		}
		if (mEnemyPosition.y < mEnemyPositionFour.y)
		{

			direction += XMVectorSet(0.0f, 1.0f, 0.0f, 0.0f);

		}
		if (diffX < 0.05f && diffY < 0.05f && diffZ < 0.05f)
		{

			
			travelToPoint = 1;

			lastPoint = mEnemyPositionFour;
		}
	}












	XMMATRIX worldMatrix = XMLoadFloat4x4(&mEnemyWorld);
	XMVECTOR r = XMLoadFloat3(&mEnemyPosition);



	// Normalize our destinated direction vector
	direction = XMVector3Normalize(direction);





	/////character spinning make it more smooth

	if (XMVectorGetX(XMVector3Dot(direction, oldCharDirection)) == -1)
	{
		oldCharDirection += XMVectorSet(1.11f, 1.0f, 0.0f, 0.0f);
	}


	///////get characters position in world space
	charPosition = XMVectorSet(0.0f, 0.0f, 0.0f, 0.0f);
	charPosition = XMVector3TransformCoord(charPosition, worldMatrix);



	///// rotate the character
	float destDirLength = 10.0f * dt;



	currCharDirection = (oldCharDirection)+(direction * destDirLength);
	currCharDirection = XMVector3Normalize(currCharDirection);


	// get the angle 
	float charDirAngle = XMVectorGetX(XMVector3AngleBetweenNormals(XMVector3Normalize(currCharDirection), XMVector3Normalize(EnemyForward)));



	if (XMVectorGetY(XMVector3Cross(currCharDirection, EnemyForward)) > 0.0f)
	{
		charDirAngle = -charDirAngle;
	}



	float Speed = speed * dt;



	direction = direction * Speed;



	charPosition = charPosition + direction;



	XMMATRIX rotationMatrix;





	XMMATRIX Translation = XMMatrixTranslation(XMVectorGetX(charPosition), XMVectorGetY(charPosition), XMVectorGetZ(charPosition));

	rotationMatrix = XMMatrixRotationY(charDirAngle - 3.14159265f);		// Subtract PI from angle so the character doesn't run backwards



	worldMatrix = rotationMatrix * Translation;


	XMMatrixDecompose(&S, &Q, &P, worldMatrix);


	XMStoreFloat3(&mEnemyPosition, P);

	XMStoreFloat4(&mEnemyRotationQuad, Q);




	XMStoreFloat4x4(&mEnemyWorld, worldMatrix);



	oldCharDirection = currCharDirection;



}
void AnimationPlayer::Play(float delta)
{
	//find the appropriate frame
	this->AnimTime += delta * this->AnimRate;
	
	if(this->AnimTime >= this->Animation.second->mDuration)
    {
        this->AnimTime = 0.0f;
		this->CurrentFrame = 0;
    }
	else if(this->AnimTime < 0.0f)
    {
        this->AnimTime = this->Animation.second->mDuration;
		this->CurrentFrame = this->Animation.second->mKeys.size() - 1;
    }

	if(this->AnimTime < 0)
	{
		return; // No time change so don't do anything
	}

	std::size_t prevFrame = this->CurrentFrame;
	if(this->AnimTime < this->Animation.second->mKeys[this->CurrentFrame].mTime)
	{
		do
		{
			this->CurrentFrame -= 1;
		}while(this->AnimTime < this->Animation.second->mKeys[this->CurrentFrame].mTime);
	}
	else if(this->AnimTime > this->Animation.second->mKeys[this->CurrentFrame].mTime)
	{
		do
		{
			this->CurrentFrame += 1;
		}while(this->AnimTime > this->Animation.second->mKeys[this->CurrentFrame].mTime);
	}

	if(prevFrame != this->CurrentFrame)
	{
		this->PreviousFrame = prevFrame;
	}

	if(this->PreviousFrame != this->CurrentFrame)
	{
		float ratio = (this->AnimTime - this->Animation.second->mKeys[this->PreviousFrame].mTime) / 
						(this->Animation.second->mKeys[this->CurrentFrame].mTime - this->Animation.second->mKeys[this->PreviousFrame].mTime);
		
		for(std::size_t i = 0; i < this->CurrentBones.size(); ++i)
		{
			if(this->ChannelMap[i] != -1)
			{
				const cFBXBuffer::JointPose& jntA = this->Animation.second->mKeys[this->PreviousFrame].mBones[this->ChannelMap[i]];
				const cFBXBuffer::JointPose& jntB = this->Animation.second->mKeys[this->CurrentFrame].mBones[this->ChannelMap[i]];

				XMVECTOR lerpedTranslation = XMVectorLerp(XMLoadFloat3(&jntA.translation), XMLoadFloat3(&jntB.translation), ratio);
				XMStoreFloat3(&(this->CurrentBones[i].translation), lerpedTranslation);
				XMVECTOR slerpedRotation = XMQuaternionSlerp(XMLoadFloat4(&jntA.rotation), XMLoadFloat4(&jntB.rotation), ratio);
				XMStoreFloat4(&(this->CurrentBones[i].rotation), slerpedRotation);
				XMVECTOR lerpedScale = XMVectorLerp(XMLoadFloat3(&jntA.scale), XMLoadFloat3(&jntB.scale), ratio);
				XMStoreFloat3(&(this->CurrentBones[i].scale), lerpedScale);
			}
		}
	}

	
	XMMATRIX root = this->CurrentBones[1].GetTransform();
	XMMATRIX modroot = root;
	if(this->PreviousFrame > this->Animation.second->mKeys.size())
    {
        //if we've looped, add on the total root-motion of the animation
        XMVECTOR fdet;
		XMMATRIX invFirstRoot = XMMatrixInverse(&fdet, this->Animation.second->mKeys.back().mBones[1].GetTransform());    
		modroot *= invFirstRoot * this->Animation.second->mKeys.front().mBones[1].GetTransform();
    }

	XMVECTOR det;
	XMMATRIX invPrevRoot = XMMatrixInverse(&det, XMLoadFloat4x4(&mPrevRoot));
	XMMATRIX rootMotion = invPrevRoot * modroot;


	XMVECTOR scale;
	XMVECTOR rotQuat;
	XMVECTOR trans;
	if(XMMatrixDecompose(&scale, &rotQuat, &trans, rootMotion))
	{
		XMStoreFloat3(&mRootTranslation, trans);
		XMStoreFloat4(&mRootRotation, rotQuat);
		if(mRootTranslation.z < -0.1f)
		{
			mRootTranslation.z = mRootTranslation.z;
		}
	}
	
	XMStoreFloat4x4(&mPrevRoot, root);

	CurrentBones[1].translation.x = Animation.second->mKeys[0].mBones[1].translation.x;
	CurrentBones[1].translation.z = Animation.second->mKeys[0].mBones[1].translation.z;
}
void LevelBuilder::CreateBoundingBox()
{


    //
    // Compute scene bounding box.
    //
    XMFLOAT3 minPt(+MathHelper::Infinity, +MathHelper::Infinity, +MathHelper::Infinity);
    XMFLOAT3 maxPt(-MathHelper::Infinity, -MathHelper::Infinity, -MathHelper::Infinity);



    for (UINT i = 0; i < mLevelPartsInstances.size(); ++i)
    {
        minPt.x = 0.0f;
        minPt.y = 0.0f;
        minPt.z = 0.0f;

        maxPt.x = 0.0f;
        maxPt.y = 0.0f;
        maxPt.z = 0.0f;

        for (UINT j = 0; j < mLevelPartsInstances[i].Model->BasicVertices.size(); ++j)
        {

            XMFLOAT3 P = mLevelPartsInstances[i].Model->BasicVertices[j].Pos;




            minPt.x = MathHelper::Min(minPt.x, P.x);
            minPt.y = MathHelper::Min(minPt.y, P.y);
            minPt.z = MathHelper::Min(minPt.z, P.z);

            maxPt.x = MathHelper::Max(maxPt.x, P.x);
            maxPt.y = MathHelper::Max(maxPt.y, P.y);
            maxPt.z = MathHelper::Max(maxPt.z, P.z);


        }

        XMMATRIX temp = XMLoadFloat4x4(&mLevelPartsInstances[i].World);

        LevelPartsclass[i]->setWorld(mLevelPartsInstances[i].World);

        XMVECTOR Scale;
        XMVECTOR Position;
        XMVECTOR Rotation;


        XMMatrixDecompose(&Scale, &Rotation, &Position, temp);

        XMFLOAT3 tempPos;
        XMStoreFloat3(&tempPos, Position);

        LevelCollisions[i].Center = tempPos;


        LevelCollisions[i].Extents = XMFLOAT3(0.5f*(maxPt.x - minPt.x),
                                              0.5f*(maxPt.y - minPt.y),
                                              0.5f*(maxPt.z - minPt.z));

        LevelCollisions[i].collisionType = LevelPartsclass[i]->getCollisionType();

        // sets the scale of the collision box
        int scale = LevelPartsclass[i]->getScale();

        LevelCollisions[i].Extents.x = LevelCollisions[i].Extents.x * scale;
        LevelCollisions[i].Extents.y = LevelCollisions[i].Extents.y * scale;
        LevelCollisions[i].Extents.z = LevelCollisions[i].Extents.z * scale;

        if (LevelPartsclass[i]->getRotationY() != 0)
        {
            FLOAT tempX = LevelCollisions[i].Extents.x;
            FLOAT tempZ = LevelCollisions[i].Extents.z;

            LevelCollisions[i].Extents.x = tempZ;
            LevelCollisions[i].Extents.z = tempX;

        }


        //// this doesn't work, useless atm
        //LevelPartsBox[i]->setCollisionType();


        LevelPartsclass[i]->setAABB(&LevelCollisions[i]);

    }
}
Esempio n. 12
0
void Projekt::UpdateScene(float dt)
{
	mPlayer.Update(dt, mDirectInput);

	for (UINT i = 0; i < mGenSkinnedInstances.size(); ++i)
		mGenSkinnedInstances[i].Update(dt);

	/*
	// Up
	if (mDirectInput->GetKeyboardState()[DIK_W] && 0x80)
	{
		mPlayer.GetCamera()->walk(30.0f*dt);
	}

	// Left
	if (mDirectInput->GetKeyboardState()[DIK_A] & 0x80)
	{
		mPlayer.GetCamera()->strafe(-30.0f*dt);
	}

	// Down
	if (mDirectInput->GetKeyboardState()[DIK_S] & 0x80)
	{
		mPlayer.GetCamera()->walk(-30.0f*dt);
	}

	// Right
	if (mDirectInput->GetKeyboardState()[DIK_D] & 0x80)
	{
		mPlayer.GetCamera()->strafe(30.0f*dt);
	}

	// Mouse has moved in x-axis
	if (mDirectInput->MouseHasMoved())
	{
		// Make each pixel correspond to a quarter of a degree.
		float dx = XMConvertToRadians(0.25f*static_cast<float>(mDirectInput->GetMouseState().lX));
		float dy = XMConvertToRadians(0.25f*static_cast<float>(mDirectInput->GetMouseState().lY));

		mPlayer.GetCamera()->yaw(dx);
		mPlayer.GetCamera()->pitch(dy);
	}
	*/

	//DetectInput(dt);
	// Movement
// 	if(GetAsyncKeyState('W') & 0x8000)
// 		mPlayer.GetCamera()->walk(30.0f*dt);
// 
// 	if(GetAsyncKeyState('S') & 0x8000)
// 		mPlayer.GetCamera()->walk(-30.0f*dt);
// 
// 	if(GetAsyncKeyState('A') & 0x8000)
// 		mPlayer.GetCamera()->strafe(-30.0f*dt);
// 
// 	if(GetAsyncKeyState('D') & 0x8000)
// 		mPlayer.GetCamera()->strafe(30.0f*dt);

	// Change shadow map resolution
// 	if(GetAsyncKeyState('1') & 0x8000)
// 	{
// 		mShadowMapSize = 256;
// 		mShadowMap->setResolution(mDirect3D->GetDevice(), mShadowMapSize, mShadowMapSize);
// 	}
// 
// 	if(GetAsyncKeyState('2') & 0x8000)
// 	{
// 		mShadowMapSize = 512;
// 		mShadowMap->setResolution(mDirect3D->GetDevice(), mShadowMapSize, mShadowMapSize);
// 	}
// 
// 	if(GetAsyncKeyState('3') & 0x8000)
// 	{
// 		mShadowMapSize = 1024;
// 		mShadowMap->setResolution(mDirect3D->GetDevice(), mShadowMapSize, mShadowMapSize);
// 	}
// 
// 	if(GetAsyncKeyState('4') & 0x8000)
// 	{
// 		mShadowMapSize = 2048;
// 		mShadowMap->setResolution(mDirect3D->GetDevice(), mShadowMapSize, mShadowMapSize);
// 	}
// 
// 	if(GetAsyncKeyState('5') & 0x8000)
// 	{
// 		mShadowMapSize = 4096;
// 		mShadowMap->setResolution(mDirect3D->GetDevice(), mShadowMapSize, mShadowMapSize);
// 	}
// 
// 	if(GetAsyncKeyState('6') & 0x8000)
// 	{
// 		mShadowMapSize = 8192;
// 		mShadowMap->setResolution(mDirect3D->GetDevice(), mShadowMapSize, mShadowMapSize);
// 	}

	// Walk/fly mode
// 	if(GetAsyncKeyState('Z') & 0x8000)
// 		mWalkCamMode = true;
// 	if(GetAsyncKeyState('X') & 0x8000)
// 		mWalkCamMode = false;


	// Walk mode
	if (mWalkCamMode)
	{
		XMFLOAT3 camPos = mPlayer.GetCamera()->getPosition();
		float y = mTerrain.GetHeight(camPos.x, camPos.z);
		mPlayer.GetCamera()->setPosition(camPos.x, y + 2.0f, camPos.z);
	}

	// Update particle systems
	mFire.update(dt, mTimer.getTimeElapsedS());

	// Build shadow map transform
	buildShadowTransform();

	// Update camera
	mPlayer.GetCamera()->updateViewMatrix();

	//------------------------------------------------------------------
	// Frustum culling
	//------------------------------------------------------------------
	mVisibleObjectCount = 0;

	if (mFrustumCullingEnabled)
	{
		XMVECTOR detView = XMMatrixDeterminant(mPlayer.GetCamera()->getViewMatrix());
		XMMATRIX invView = XMMatrixInverse(&detView, mPlayer.GetCamera()->getViewMatrix());

		for (UINT i = 0; i < mGenericInstances.size(); ++i)
		{
			mGenericInstances[i].isVisible = false;

			XMMATRIX W = XMLoadFloat4x4(&mGenericInstances[i].world);
			XMMATRIX invWorld = XMMatrixInverse(&XMMatrixDeterminant(W), W);

			// View space to the object's local space.
			XMMATRIX toLocal = XMMatrixMultiply(invView, invWorld);

			// Decompose the matrix into its individual parts.
			XMVECTOR scale;
			XMVECTOR rotQuat;
			XMVECTOR translation;
			XMMatrixDecompose(&scale, &rotQuat, &translation, toLocal);

			// Transform the camera frustum from view space to the object's local space.
			XNA::Frustum localspaceFrustum;
			XNA::TransformFrustum(&localspaceFrustum, &mCamFrustum, XMVectorGetX(scale), rotQuat, translation);

			// Perform the box/frustum intersection test in local space.
			if(XNA::IntersectAxisAlignedBoxFrustum(&mGenericInstances[i].model->boundingBox, &localspaceFrustum) != 0)
			{
				// Write the instance data to dynamic VB of the visible objects.
				//dataView[mVisibleObjectCount++] = mInstancedData[i];
				mVisibleObjectCount++;
				mGenericInstances[i].isVisible = true;
			}
		}
	}
	else
	{
		for (UINT i = 0; i < mGenericInstances.size(); ++i)
		{
			mGenericInstances[i].isVisible = true;
			mVisibleObjectCount++;
		}
	}

	std::wostringstream outs;   
	outs.precision(6);
	outs << L"    " << mVisibleObjectCount << 
		L" objects visible out of " << mGenericInstances.size();
	mMainWndCaption = outs.str();
}