Esempio n. 1
0
int KRLLocalCharacter::GetSlopeRotation(D3DXQUATERNION& qRotationLH, RL_TERRAIN_SLOPE nTerrainSlope)
{
    static float const PITCH_STEP = D3DX_PI / 600.0f;
    static float const ROLL_STEP = D3DX_PI / 600.0f;

    int nRetCode = false;
    int nResult = false;
    float fTerrainYawLH = m_fFootYaw;
    float fTerrainRollLH = 0.0f;
    float fTerrainPitchLH = 0.0f;
    IKG3DScene* p3DScene = NULL;
    D3DXVECTOR3 vPosition;

    KG_PROCESS_ERROR(m_RLCharacter.m_FrameData.m_Current.nMoveState != cmsOnJump);
    KG_PROCESS_ERROR(m_RLCharacter.m_FrameData.m_Current.nMoveState != cmsOnSwimJump);

    p3DScene = m_RLCharacter.m_RenderData.GetRLScene()->m_p3DScene;
    KGLOG_PROCESS_ERROR(p3DScene);

    m_RLCharacter.GetPosition(vPosition);

    nRetCode = GetTerrainSlopeLean(p3DScene, vPosition, fTerrainYawLH, nTerrainSlope, fTerrainPitchLH, fTerrainRollLH);
    KG_PROCESS_ERROR(nRetCode);

    if (abs(m_fFootExpectPitch - fTerrainPitchLH) > D3DX_PI / 1800.0f)
        m_fFootExpectPitch = fTerrainPitchLH;
    m_fFootPitch = CloseToPitch(m_fFootExpectPitch, m_fFootPitch, PITCH_STEP);

    if (abs(m_fFootExpectRoll - fTerrainRollLH) > D3DX_PI / 1800.0f)
        m_fFootExpectRoll = fTerrainRollLH;
    m_fFootRoll = CloseToRoll(m_fFootExpectRoll, m_fFootRoll, ROLL_STEP);

    switch (nTerrainSlope)
    {
    case RL_TERRAIN_SLOPE_NONE:
        YawToQuatLH(qRotationLH, YawLHToModelLH(fTerrainYawLH));
        break;
    case RL_TERRAIN_SLOPE_LENGTHWAYS:
        D3DXQuaternionRotationYawPitchRoll(&qRotationLH, YawLHToModelLH(fTerrainYawLH), m_fFootPitch, 0.0f);
        break;
    case RL_TERRAIN_SLOPE_TRANSVERSE:
        D3DXQuaternionRotationYawPitchRoll(&qRotationLH, YawLHToModelLH(fTerrainYawLH), 0.0f, m_fFootRoll);
        break;
    case RL_TERRAIN_SLOPE_ALL:
        D3DXQuaternionRotationYawPitchRoll(&qRotationLH, YawLHToModelLH(fTerrainYawLH), m_fFootPitch, m_fFootRoll);
        break;
    }

    nResult = true;
Exit0:
    return nResult;
}
Esempio n. 2
0
//Gets the camera matrix (at given distance from target).
//Note that distance is ignored when in first person mode. 
Matrix Camera::GetLookAtMatrix( Real Distance )
{
	if( Pos == NULL )
		return Matrix::MakeIdentity();

	Matrix Result;
	D3DXMATRIX matR, matPosition, matView, matT, matMoveBack;
	D3DXQUATERNION qR;

	D3DXMatrixIdentity(&matView);
	D3DXMatrixIdentity(&matPosition);

	if(CS == CS_FIRST_PERSON)
		Distance=0;
	D3DXMatrixTranslation(&matMoveBack, 0, 0, -Distance);

	if( CS == CS_FOLLOW_MATRIX )
	{
		D3DXMATRIX tempMat;
		
		D3DXQuaternionRotationYawPitchRoll(&qR, Yaw, Pitch, Roll);
	    D3DXMatrixRotationQuaternion(&matR, &qR);

		D3DXMatrixMultiply(&matR, &matMoveBack, &matR);
		D3DXMatrixMultiply (&tempMat, &matR,&FollowMat->Mat );
		D3DXMatrixInverse(&matView, NULL, &tempMat );
		Result.Mat = matView;
		return Result;
	}
	

	if(CS == CS_FIRST_PERSON)
		D3DXMatrixTranslation(&matT, Pos->x + OffsetX, Pos->y + OffsetY + HeightDisplacement, Pos->z + OffsetZ);
	else
		D3DXMatrixTranslation(&matT, Pos->x + OffsetX, Pos->y + OffsetY, Pos->z + OffsetZ);

    D3DXMatrixMultiply(&matPosition, &matT, &matPosition);


	D3DXQuaternionRotationYawPitchRoll(&qR, Yaw, Pitch, Roll);
    D3DXMatrixRotationQuaternion(&matR, &qR);


	D3DXMatrixMultiply(&matR, &matMoveBack, &matR);
    D3DXMatrixMultiply(&matPosition, &matR, &matPosition);
	D3DXMatrixInverse(&matView, NULL, &matPosition);

	Result.Mat = matView;
	return Result;
}
Esempio n. 3
0
//--------------------------------------------------------
 void TSprite::RenderEx(	const RECT *pSrc, const D3DXVECTOR3 &inPos,
							const D3DXVECTOR3 &inScale, const D3DXVECTOR3 &inRotation,
							D3DXVECTOR3 *pCenter , const FLOAT inAlpha, const D3DCOLOR inColor)
 {
	// set color

	const D3DCOLOR COLOR = ((DWORD)(inAlpha*255.f) << 24) | 0x00FFFFFF;
	//const D3DCOLOR COLOR = ((DWORD)(inAlpha * 255.f) << 24 )| (inColor & 0x00FFFFFF);

	// rotation vector change to quaternion
	D3DXQUATERNION qt;
	
	D3DXQuaternionRotationYawPitchRoll(	&qt,
										D3DXToRadian(inRotation.y),
										D3DXToRadian(inRotation.x),
										D3DXToRadian(inRotation.z));
	
	// set matrix
	D3DXMATRIX mat;
	D3DXMatrixTransformation(&mat, NULL, NULL, &inScale, NULL, &qt, &inPos);
	FpD3DXSprite->SetTransform(&mat);

	// draw
	FpD3DXSprite->Draw( FpTexture9, pSrc, pCenter, NULL, COLOR);
 }
Esempio n. 4
0
Matrix Camera::GetRotationMatrix()
{
	D3DXQUATERNION qR;
	D3DXMATRIX matView, matR, matR2;

	D3DXMatrixIdentity(&matView);
	D3DXMatrixIdentity(&matR);

	D3DXQuaternionRotationYawPitchRoll(&qR, Yaw, Pitch, Roll);
    D3DXMatrixRotationQuaternion(&matR, &qR);

	if(CS == CS_FOLLOW_MATRIX )
	{
		D3DXMatrixRotationQuaternion(&matR2, &qR);
		D3DXMatrixMultiply ( &matR,  &matR2, &FollowMatSky->Mat );
	}
	else
	{
		D3DXMatrixRotationQuaternion(&matR, &qR);
	}

	D3DXMatrixInverse(&matView, NULL, &matR);

	Matrix M;
	M.Mat = matView;

	return M;

}
Esempio n. 5
0
void Enemy::Move(D3DXVECTOR3 dir, float dT)
{
	PxRaycastHit* hit = physics->RaycastMultiple(actor->getGlobalPose().p,PxVec3(0,-1,0).getNormalized(),colCapsuleHeight+0.1f,NULL,PxQueryFlag::eSTATIC);
	if(hit[0].actor == NULL)
	{
		dir.y = -9.8f*dT*2.5f;
		if(obj->position.y-colCapsuleHeight < -13.6f)
		{
			obj->position.y = -13.6f+colCapsuleHeight;
		}
	}
	else if(hit[1].actor == NULL && hit[0].actor == actor)
	{
		dir.y = -9.8f*dT*2.5f;
		if(obj->position.y-colCapsuleHeight < -13.6f)
		{
			obj->position.y = -13.6f+colCapsuleHeight;
		}
	}
	obj->position += dir*dT;

	D3DXQUATERNION rotQuat;
	D3DXQuaternionRotationYawPitchRoll(&rotQuat,0,0,90*ToRadian);

	if(actor->isRigidDynamic() != NULL) actor->isRigidDynamic()->setKinematicTarget(PxTransform(PxVec3(obj->position.x,obj->position.y,obj->position.z),PxQuat(rotQuat.x,rotQuat.y,rotQuat.z,rotQuat.w)));
}
Esempio n. 6
0
void Mesh::Rotate(D3DXVECTOR3 radians)
{
	D3DXQUATERNION quaternion;
	D3DXQuaternionRotationYawPitchRoll(&quaternion, radians.y, radians.x, radians.z);
	D3DXQuaternionMultiply(&this->rotQuat, &this->rotQuat, &quaternion);
	this->RecreateWorldMatrix();
}
/**
* 
* Calls the ProcessControls function to update the camera's position etc.
* Rotates the camera by the current yaw, pitch and roll values, and then 
*	re-calculates the view matrix by calling the SetView3D function. 
* 
* @author Rebeccah Cox
* @param float32 _fDeltaTick
* @return void
*/
void 
CDebugCamera::Process(float32 _fDeltaTick)
{
	ProcessControls(_fDeltaTick);

	D3DXQUATERNION quatRotation;
	D3DXQuaternionRotationYawPitchRoll(&quatRotation, m_fYaw, m_fPitch, m_fRoll);

	SetViewQuaternion(m_vec3Position, quatRotation);
}
Esempio n. 8
0
int KRLLocalCharacter::SetFaceFootDirection(float fFaceYaw, float fFacePitch, float fFootYaw)
{
    int nRetCode = false;
    int nResult = false;
    KRLRides* pRides = NULL;
    D3DXQUATERNION qRotationLH;
    float fDelta = 0.0f;

    if (m_RLCharacter.m_pRLHorse)
        pRides = m_RLCharacter.m_pRLHorse;
    else if (m_RLCharacter.m_pRLVehicle)
        pRides = m_RLCharacter.m_pRLVehicle;

    if (pRides)
    {
        m_RLCharacter.KRLSceneObject::SetDirectionLH(D3DX_PI);
        if (GetSlopeRotation(qRotationLH, (RL_TERRAIN_SLOPE)pRides->m_dwSlopeType))
        {
            pRides->SetDirectionLH(qRotationLH);
        }
    }
    else
    {
        if (m_RLCharacter.m_FrameData.m_Current.nMoveState == cmsOnSwim)
        {
            if (!m_RLCharacter.m_FrameData.m_Current.bUnderWater)
                fFacePitch = 0.0f;


            D3DXQuaternionRotationYawPitchRoll(&qRotationLH, YawLHToModelLH(fFaceYaw), fFacePitch, 0.0f);
            m_RLCharacter.m_RenderData.SetRotation(qRotationLH);
        }
        else
        {
            if (m_RLCharacter.m_EquipResource.bSplitModel)
            {
                m_RLCharacter.KRLSceneObject::SetDirectionLH(fFootYaw);
            }
            else
            {
                if (GetSlopeRotation(qRotationLH, m_RLCharacter.m_EquipResource.nTerrainSlope))
                {
                    m_RLCharacter.SetDirectionLH(qRotationLH);
                }
            }
        }
    }

    fDelta = NormalizeDirectionNegativeHalfToPositiveHalf(fFaceYaw - fFootYaw);
    m_RLCharacter.TurnFace(fDelta);

    nResult = true;
    return nResult;
}
Esempio n. 9
0
void CArea::__LoadAttribute(TObjectInstance * pObjectInstance, const char * c_szAttributeFileName)
{
	// OBB를 사용한 충돌 정보 자동 생성.
	const bool bFileExist = CResourceManager::Instance().IsFileExist(c_szAttributeFileName);
	
	CAttributeData * pAttributeData = (CAttributeData *) CResourceManager::Instance().GetResourcePointer(c_szAttributeFileName);

	CAttributeInstance * pAttrInstance = ms_AttributeInstancePool.Alloc();
	pAttrInstance->Clear();
	pAttrInstance->SetObjectPointer(pAttributeData);

	if (false == bFileExist)
	{
		std::string attrFileName(c_szAttributeFileName);
		boost::algorithm::to_lower(attrFileName);
		const bool bIsDungeonObject = (std::string::npos != attrFileName.find("/dungeon/")) || (std::string::npos != attrFileName.find("\\dungeon\\"));

		// NOTE: dungeon 오브젝트는 Dummy Collision을 자동으로 생성하지 않도록 함 (던전의 경우 더미 컬리전때문에 문제가 된 경우가 수차례 있었음. 이렇게 하기로 그래픽 팀과 협의 완료)
		if (pAttributeData->IsEmpty() && false == bIsDungeonObject)
		{
			if (NULL != pObjectInstance && NULL != pObjectInstance->pThingInstance)
			{
				CGraphicThingInstance* object = pObjectInstance->pThingInstance;

				D3DXVECTOR3 v3Min, v3Max;

				object->GetBoundingAABB(v3Min, v3Max);
				
				CStaticCollisionData collision;
				collision.dwType = COLLISION_TYPE_OBB;
				D3DXQuaternionRotationYawPitchRoll(&collision.quatRotation, object->GetYaw(), object->GetPitch(), object->GetRoll());
				strcpy(collision.szName, "DummyCollisionOBB");
				collision.v3Position = (v3Min + v3Max) * 0.5f;

				D3DXVECTOR3 vDelta = (v3Max - v3Min);
				collision.fDimensions[0] = vDelta.x * 0.5f;
				collision.fDimensions[1] = vDelta.y * 0.5f;
				collision.fDimensions[2] = vDelta.z * 0.5f;

				pAttributeData->AddCollisionData(collision);
			}
		}
	}

	if (!pAttributeData->IsEmpty())
	{
		pObjectInstance->pAttributeInstance = pAttrInstance;
	}
	else
	{
		pAttrInstance->Clear();
		ms_AttributeInstancePool.Free(pAttrInstance);
	}
}
Esempio n. 10
0
int GetTerrainSlopeRotation(IKG3DScene* p3DScene, D3DXVECTOR3 vPosition, float fYawLH, RL_TERRAIN_SLOPE nTerrainSlope, D3DXQUATERNION& qRotationLH)
{
    int nRetCode = false;
    int nResult = false;
    float fTerrainYawLH = fYawLH;
    float fTerrainRollLH = 0.0f;
    float fTerrainPitchLH = 0.0f;

    switch (nTerrainSlope)
    {
    case RL_TERRAIN_SLOPE_NONE:
        YawToQuatLH(qRotationLH, YawLHToModelLH(fYawLH));
        break;
    case RL_TERRAIN_SLOPE_LENGTHWAYS:
        nRetCode = GetTerrainSlopeLean(p3DScene, vPosition, fTerrainYawLH, nTerrainSlope, fTerrainPitchLH, fTerrainRollLH);
        KG_PROCESS_ERROR(nRetCode);

        D3DXQuaternionRotationYawPitchRoll(&qRotationLH, YawLHToModelLH(fTerrainYawLH), fTerrainPitchLH, 0.0f);
        break;
    case RL_TERRAIN_SLOPE_TRANSVERSE:
        nRetCode = GetTerrainSlopeLean(p3DScene, vPosition, fTerrainYawLH, nTerrainSlope, fTerrainPitchLH, fTerrainRollLH);
        KG_PROCESS_ERROR(nRetCode);

        D3DXQuaternionRotationYawPitchRoll(&qRotationLH, YawLHToModelLH(fTerrainYawLH), 0.0f, fTerrainRollLH);
        break;
    case RL_TERRAIN_SLOPE_ALL:
        nRetCode = GetTerrainSlopeLean(p3DScene, vPosition, fTerrainYawLH, nTerrainSlope, fTerrainPitchLH, fTerrainRollLH);
        KG_PROCESS_ERROR(nRetCode);

        D3DXQuaternionRotationYawPitchRoll(&qRotationLH, YawLHToModelLH(fTerrainYawLH), fTerrainPitchLH, fTerrainRollLH);
        break;
    default:
        ASSERT(0);
        break;
    }

    nResult = true;
Exit0:
    return nResult;
}
Esempio n. 11
0
void	cObjBase::Translation()
{
	D3DXMATRIXA16	matScale, matRot, matTrans;
	D3DXMatrixScaling(&matScale, m_vScale.x, m_vScale.y, m_vScale.z);
	{
		D3DXQUATERNION	q;
		D3DXQuaternionRotationYawPitchRoll(&q, m_vRotation.y, m_vRotation.x, m_vRotation.z);
		D3DXMatrixRotationQuaternion(&matRot, &q);

	}
	D3DXMatrixTranslation(&matTrans, m_vPosition.x, m_vPosition.y, m_vPosition.z);

	m_matWorld = matScale	*	matRot	*	matTrans;
}
//----------------------------------------------------------------------------//
void Direct3D8GeometryBuffer::updateMatrix() const
{
    const D3DXVECTOR3 p(d_pivot.d_x, d_pivot.d_y, d_pivot.d_z);
    const D3DXVECTOR3 t(d_translation.d_x, d_translation.d_y, d_translation.d_z);

    D3DXQUATERNION r;
    D3DXQuaternionRotationYawPitchRoll(&r,
        D3DXToRadian(d_rotation.d_y),
        D3DXToRadian(d_rotation.d_x),
        D3DXToRadian(d_rotation.d_z));

    D3DXMatrixTransformation(&d_matrix, 0, 0, 0, &p, &r, &t);
    d_matrixValid = true;
}
D3DXMATRIXA16 Transform::MatrixTransform()
{
	D3DXQUATERNION	qRotation;
	D3DXMATRIXA16	tempMatrix;

	D3DXMatrixIdentity( &tempMatrix );

	// rotation에서 쿼터니언 생성, yaw ptich roll 은 y, x, z 순서임
	D3DXQuaternionRotationYawPitchRoll( &qRotation, D3DXToRadian( m_Rotation.y ), D3DXToRadian( m_Rotation.x ), D3DXToRadian( m_Rotation.z ) );

	// matrix를 affine변환이 적용된 형태로 변환	
	D3DXMatrixTransformation( &tempMatrix, NULL, NULL, &m_Scale, NULL, &qRotation, &m_Position );

	return tempMatrix;
}
Esempio n. 14
0
void CToolCamera::Transform( LPDIRECT3DDEVICE9 pd3dDevice, CWorld* pWorld )
{
    D3DXQUATERNION qR;
    D3DXQuaternionRotationYawPitchRoll( &qR, m_fYaw, m_fPitch, 0.0f );
    //D3DXMatrixAffineTransformation( &m_matOrientation, 1.25f, NULL, &qR, &m_vPos );
	D3DXMatrixAffineTransformation( &m_matOrientation, 1.0f, NULL, &qR, &m_vPos );
    D3DXMatrixInverse( &m_matView, NULL, &m_matOrientation );
    pd3dDevice->SetTransform( D3DTS_VIEW, &m_matView );
	pWorld->UpdateCullInfo( &m_matView, &pWorld->m_matProj );
	D3DXMatrixInverse( &m_matInvView, NULL, &m_matView );
	m_matInvView._41 = 0.0f; m_matInvView._42 = 0.0f; m_matInvView._43 = 0.0f;
	g_matInvView = m_matInvView;
	g_matView = m_matView;
	
}
D3DXVECTOR3 PlayerCharacter::GetViewDirection()
{
	D3DXQUATERNION	qRotation;
	D3DXMATRIXA16 tempMatrix;

	D3DXMatrixIdentity( &tempMatrix );

	// rotation에서 쿼터니언 생성, yaw ptich roll 은 y, x, z 순서임
	D3DXQuaternionRotationYawPitchRoll( &qRotation, D3DXToRadian( m_Rotation.y ), D3DXToRadian( m_Rotation.x ), D3DXToRadian( m_Rotation.z ) );

	// matrix를 affine변환이 적용된 형태로 변환 - 생략 가능?
	D3DXMatrixTransformation( &tempMatrix, NULL, NULL, &m_Scale, NULL, &qRotation, &m_Position );

	return D3DXVECTOR3( tempMatrix._31, tempMatrix._32, tempMatrix._33 );
}
Esempio n. 16
0
void	vrObject::Rotate( )
{
/*
if ( v_rot.x > v_maxrot.x ) v_rot.x = v_maxrot.x;
if ( v_rot.y > v_maxrot.y ) v_rot.y = v_maxrot.y;
if ( v_rot.z > v_maxrot.z ) v_rot.z = v_maxrot.z;
*/

vrQuaternion	temp_quat;

//D3DXQuaternionRotationYawPitchRoll( &temp_quat, v_rot.x*pi/2, v_rot.y*pi/2, v_rot.z*pi/2 );
D3DXQuaternionRotationYawPitchRoll( &temp_quat, f_yaw*pi/2, f_pitch*pi/2, f_yaw*pi/2 );

D3DXQuaternionMultiply( &q_orient, &q_orient, &temp_quat );


}
Esempio n. 17
0
void vrObject::MoveX()
{

    vrQuaternion TempQuat;
    vrQuaternion TempQuat2;

	D3DXQuaternionRotationYawPitchRoll( &TempQuat2, 0.0, -90.0*(pi/180), 0.0 );

	D3DXQuaternionMultiply( &TempQuat, &q_orient, &TempQuat2 );
	vrVector3 dir;
	TempQuat.GetDirectionVector( &dir );

    v_pos.x += dir.x * v_speed.x;
    v_pos.y += dir.y * v_speed.x;
    v_pos.z += dir.z * v_speed.x;


}
Esempio n. 18
0
//******************************************************************
//		更新・描画
//******************************************************************
bool sceneMain::Update()
{
	PlayerCtrl();
	PlayerUpdate();
	SoundManager::Update();

	if (KeyBoard(KB_F))cameraPos.z += 1;
	if (KeyBoard(KB_G))cameraPos.z -= 1;
	tdnView::Set(cameraPos, VECTOR_ZERO);

	// メッシュテスト
	shader3D->SetValue( "viewPosition", cameraPos );
	static float meshAngle = 0;
	meshAngle += 0.01f;
	TestMesh->Rot( *D3DXQuaternionRotationYawPitchRoll( &Quaternion(), meshAngle, meshAngle * 0.9f, meshAngle * 0.7f ) );
	TestMesh->Pos( Vector3( 0.0f, 0.0f, 0.0f ) );
	TestMesh->UpdateWorldMatrix();

	return true;	
}
Esempio n. 19
0
Scene * D3D::BuildScene(IDirect3DDevice9 *d3dDevice)
 {
	 // Setup some materials - we'll use these for 
	 // making the same mesh appear in multiple
	 // colors

	 D3DMATERIAL9 colors[8];
	 D3DUtil_InitMaterial( colors[0], 1.0f, 1.0f, 1.0f, 1.0f );	// white
	 D3DUtil_InitMaterial( colors[1], 0.0f, 1.0f, 1.0f, 1.0f );	// cyan
	 D3DUtil_InitMaterial( colors[2], 1.0f, 0.0f, 0.0f, 1.0f );	// red
	 D3DUtil_InitMaterial( colors[3], 0.0f, 1.0f, 0.0f, 1.0f );	// green
	 D3DUtil_InitMaterial( colors[4], 0.0f, 0.0f, 1.0f, 1.0f );	// blue
	 D3DUtil_InitMaterial( colors[5], 0.4f, 0.4f, 0.4f, 0.4f );	// 40% grey
	 D3DUtil_InitMaterial( colors[6], 0.25f, 0.25f, 0.25f, 0.25f );	// 25% grey
	 D3DUtil_InitMaterial( colors[7], 0.65f, 0.65f, 0.65f, 0.65f );	// 65% grey

	 // The identity matrix is always useful
	 D3DXMATRIX ident;
	 D3DXMatrixIdentity(&ident);

	 // We'll use these rotations for some teapots and grid objects
	 D3DXMATRIX rotateX, rotateY, rotateZ;

	 // Create the root, and the camera.
	 // Remeber how to use smart pointers?? I hope so!

	 boost::shared_ptr<TransformNode> root(new TransformNode(&ident));

	 boost::shared_ptr<CameraNode> camera(new CameraNode(&ident));
	 root->m_children.push_back(camera);



	 // We'll put the camera in the scene at (20,20,20) looking back at the Origin

	 D3DXMATRIX rotOnly, result, inverse;
	 float cameraYaw = - (3.0f * D3DX_PI) / 4.0f;
	 float cameraPitch = D3DX_PI / 4.0f;
	 D3DXQUATERNION q;
	 D3DXQuaternionIdentity(&q);
	 D3DXQuaternionRotationYawPitchRoll(&q, cameraYaw, cameraPitch, 0.0);
	 D3DXMatrixRotationQuaternion(&rotOnly, &q);

	 D3DXMATRIX trans;
	 D3DXMatrixTranslation(&trans, 15.0f, 15.0f, 15.0f);

	 D3DXMatrixMultiply(&result, &rotOnly, &trans);

	 D3DXMatrixInverse(&inverse, NULL, &result);
	 camera->VSetTransform(&result, &inverse);

	 D3DXMatrixRotationZ(&rotateZ, D3DX_PI / 2.0f);
	 D3DXMatrixRotationX(&rotateX, -D3DX_PI / 2.0f);
	 D3DXVECTOR3 target(30, 2, 15);

	 //

	

	 ID3DXMesh *teapot;
	 if( SUCCEEDED( D3DXCreateTeapot( d3dDevice, &teapot, NULL ) ) )
	 {
		 // Teapot #1 - a white one at (x=6,y=2,z=4)
		 D3DXMatrixTranslation(&trans,6,2,4);

		 boost::shared_ptr<SceneNode> mesh1(new MeshNode(teapot, &trans, colors[2]));
		 root->m_children.push_back(mesh1);

		 // Teapot #2 - a cyan one at (x=3,y=2,z=1)
		 //   with a 
		 D3DXMatrixTranslation(&trans, 3,2,1);
		 D3DXMATRIX result;
		 D3DXMatrixMultiply(&result, &rotateZ, &trans);

		 boost::shared_ptr<SceneNode> mesh2(new MeshNode(teapot, &result, colors[1]));
		 root->m_children.push_back(mesh2);

		 // Teapot #3 - another white one at (x=30, y=2, z=15)
		 D3DXMATRIX rotateY90;
		 D3DXMatrixRotationY(&rotateY90, D3DX_PI / 2.0f);
		 D3DXMatrixTranslation(&trans, target.x, target.y, target.z);
		 D3DXMatrixMultiply(&result, &rotateY90, &trans);
		 boost::shared_ptr<SceneNode> mesh3(new MeshNode(teapot, &result, colors[0]));
		 root->m_children.push_back(mesh3);

		 // We can release the teapot now, mesh1 and mesh2 AddRef'd it.
		 SAFE_RELEASE(teapot);
	 }

	 ID3DXMesh *sphere;
	 if ( SUCCEEDED( 
		 D3DXCreateSphere( 
		 d3dDevice, .25, 16, 16, &sphere, NULL) ) )
	 {
		 // We're going to create a spiral of spheres...
		 // starting at (x=3, y=0, z=3), and spiraling
		 // upward about a local Y axis.

		 D3DXMatrixTranslation(&trans, 3,0,3);

		 boost::shared_ptr<SceneNode> sphere1(new MeshNode(sphere, &trans, colors[4]) );
		 root->m_children.push_back(sphere1);

		 // Here's the local rotation and translation.
		 // We'll rotate about Y, and then translate
		 // up (along Y) and forward (along Z).
		 D3DXMatrixRotationY(&rotateY, D3DX_PI / 8.0f);
		 D3DXMATRIX trans2;
		 D3DXMatrixTranslation(&trans2, 0, 0.5, 0.5);
		 D3DXMatrixMultiply(&result, &trans2, &rotateY);

		 for (int i=0; i<25; i++)
		 {
			 // If you didn't think smart pointers were cool - 
			 // watch this! No leaked memory....

			 // Notice this is a heirarchy....
			 boost::shared_ptr<SceneNode> sphere2(new MeshNode(sphere, &result, colors[i%5]) );
			 sphere1->m_children.push_back(sphere2);
			 sphere1 = sphere2;
		 }

		 // We can release the sphere now, all the cylinders AddRef'd it.
		 SAFE_RELEASE(sphere);
	 }
	 

	 //
	 D3DXMatrixTranslation(&trans,-25,20,20);
	 //D3DXMatrixScaling(&trans, -10, -10, -10);
	 ScaleMtrl scale;
	 scale.x = -50.0f;
	 scale.y = -50.0f;
	 scale.z = -50.0f;
	 boost::shared_ptr<SceneNode> xmesh1(new XMeshNode(L"gf3.x", d3dDevice, &trans, &scale));
	 root->m_children.push_back(xmesh1);

	 
	  root->m_children.push_back(xmesh1);

	 /*D3DXMatrixTranslation(&trans,-45,20,20);
	 boost::shared_ptr<SceneNode> xmesh11(new XMeshNode(L"gf3.x", d3dDevice, &trans, &scale));
	 root->m_children.push_back(xmesh11);*/


	  XMeshNode *mm = new XMeshNode(L"gow_m1.x", d3dDevice, &trans, &scale);

	 D3DXMatrixTranslation(&trans,10,10,10);
	 //D3DXMatrixScaling(&trans, -10, -10, -10);
	 //ScaleMtrl scale;
	 scale.x = 100.0f;
	 scale.y = 100.0f;
	 scale.z = 100.0f;
	 boost::shared_ptr<SceneNode> xmesh2( new XMeshNode(L"gow_m1.x", d3dDevice, &trans, &scale));
	 root->m_children.push_back(xmesh2);

	 
	 
	 /*D3DXMatrixTranslation(&trans,20,20,20);
	 boost::shared_ptr<SceneNode> xmesh3(new XMeshNode(mm->m_mesh, mm->Mtrls, mm->Textures, &trans, 0));
	 root->m_children.push_back(xmesh3);*/

	 int col = 10;
	 int row= 10;
	 int zoom = 10;
	 const int COUNT = 13;
	 for(int i = 0; i < COUNT; i++)
	 {
		 for (int j = 0; j< COUNT; j++)
		 {
			 for(int z = 0; z< COUNT; z++)
			 {
				 D3DXMatrixTranslation(&trans, col + i, row + j , zoom + z);
				 boost::shared_ptr<SceneNode> xmeshNew(new XMeshNode(mm->m_mesh, mm->Mtrls, mm->Textures, &trans, 0));
				 root->m_children.push_back(xmeshNew);
			 }
		 }
	 }


	 //D3DXMatrixScaling(&trans, 10, 10, 10);

	 // Here are the grids...they make it easy for us to 
	 // see where the coordinates are in 3D space.
	 boost::shared_ptr<SceneNode> grid1(new Grid(40, 0x00404040, L"Textures\\grid.dds", &ident));
	 root->m_children.push_back(grid1);
	 boost::shared_ptr<SceneNode> grid2(new Grid(40, 0x00004000, L"Textures\\grid.dds", &rotateX));
	 root->m_children.push_back(grid2);
	 boost::shared_ptr<SceneNode> grid3(new Grid(40, 0x00000040, L"Textures\\grid.dds", &rotateZ));
	 root->m_children.push_back(grid3);

	 //  Here's the sky node that never worked!!!!
	 boost::shared_ptr<SkyNode> sky(new SkyNode(_T("Sky2"), camera));
	 root->m_children.push_back(sky);

	 D3DXMatrixTranslation(&trans,15,2,15);
	 D3DXMatrixRotationY(&rotateY, D3DX_PI / 4.0f);
	 D3DXMatrixMultiply(&result, &rotateY, &trans);

	 boost::shared_ptr<SceneNode> arrow1(new ArrowNode(2, &result, colors[0], d3dDevice));
	 root->m_children.push_back(arrow1);

	 D3DXMatrixRotationY(&rotateY, D3DX_PI / 2.0f);
	 D3DXMatrixMultiply(&result, &rotateY, &trans);
	 boost::shared_ptr<SceneNode> arrow2(new ArrowNode(2, &result, colors[5], d3dDevice));
	 root->m_children.push_back(arrow2);

	 D3DXMatrixMultiply(&result, &rotateX, &trans);
	 boost::shared_ptr<SceneNode> arrow3(new ArrowNode(2, &result, colors[0], d3dDevice));
	 root->m_children.push_back(arrow3);


	 // Everything has been attached to the root. Now
	 // we attach the root to the scene.

	 Scene *scene = new Scene(d3dDevice, root);
	 scene->Restore();

	 // A movement controller is going to control the camera, 
	 // but it could be constructed with any of the objects you see in this
	 // function. You can have your very own remote controlled sphere. What fun...
	 boost::shared_ptr<MovementController> m_pMovementController(new MovementController(camera, cameraYaw, cameraPitch));

	 scene->m_pMovementController = m_pMovementController;
	 return scene;
 }
Esempio n. 20
0
//--------------------------------------------------------------------------------------
// This callback function will be called immediately after the Direct3D device has been 
// created, which will happen during application initialization and windowed/full screen 
// toggles. This is the best location to create D3DPOOL_MANAGED resources since these 
// resources need to be reloaded whenever the device is destroyed. Resources created  
// here should be released in the OnDestroyDevice callback. 
//--------------------------------------------------------------------------------------
HRESULT CALLBACK OnCreateDevice( IDirect3DDevice9* pd3dDevice, const D3DSURFACE_DESC* pBackBufferSurfaceDesc,
                                 void* pUserContext )
{
    HRESULT hr;


    V_RETURN( g_DialogResourceManager.OnD3D9CreateDevice( pd3dDevice ) );
    V_RETURN( g_SettingsDlg.OnD3D9CreateDevice( pd3dDevice ) );
    // Initialize the font
    V_RETURN( D3DXCreateFont( pd3dDevice, 15, 0, FW_BOLD, 1, FALSE, DEFAULT_CHARSET,
                              OUT_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH | FF_DONTCARE,
                              L"Arial", &g_pFont ) );

    // Define DEBUG_VS and/or DEBUG_PS to debug vertex and/or pixel shaders with the 
    // shader debugger. Debugging vertex shaders requires either REF or software vertex 
    // processing, and debugging pixel shaders requires REF.  The 
    // D3DXSHADER_FORCE_*_SOFTWARE_NOOPT flag improves the debug experience in the 
    // shader debugger.  It enables source level debugging, prevents instruction 
    // reordering, prevents dead code elimination, and forces the compiler to compile 
    // against the next higher available software target, which ensures that the 
    // unoptimized shaders do not exceed the shader model limitations.  Setting these 
    // flags will cause slower rendering since the shaders will be unoptimized and 
    // forced into software.  See the DirectX documentation for more information about 
    // using the shader debugger.
    DWORD dwShaderFlags = D3DXFX_NOT_CLONEABLE;

#if defined( DEBUG ) || defined( _DEBUG )
    // Set the D3DXSHADER_DEBUG flag to embed debug information in the shaders.
    // Setting this flag improves the shader debugging experience, but still allows 
    // the shaders to be optimized and to run exactly the way they will run in 
    // the release configuration of this program.
    dwShaderFlags |= D3DXSHADER_DEBUG;
    #endif

#ifdef DEBUG_VS
    dwShaderFlags |= D3DXSHADER_FORCE_VS_SOFTWARE_NOOPT;
#endif
#ifdef DEBUG_PS
    dwShaderFlags |= D3DXSHADER_FORCE_PS_SOFTWARE_NOOPT;
#endif

    // Determine which LDPRT texture and SH coefficient cubemap formats are supported
    IDirect3D9* pD3D = DXUTGetD3D9Object();
    D3DCAPS9 Caps;
    pd3dDevice->GetDeviceCaps( &Caps );
    D3DDISPLAYMODE DisplayMode;
    pd3dDevice->GetDisplayMode( 0, &DisplayMode );

    GetSupportedTextureFormat( pD3D, &Caps, DisplayMode.Format, &g_fmtTexture, &g_fmtCubeMap );
    if( D3DFMT_UNKNOWN == g_fmtTexture || D3DFMT_UNKNOWN == g_fmtCubeMap )
        return E_FAIL;

    // Create the skybox
    g_Skybox.OnCreateDevice( pd3dDevice, 50, L"Light Probes\\rnl_cross.dds", L"SkyBox.fx" );
    V( D3DXSHProjectCubeMap( 6, g_Skybox.GetEnvironmentMap(), g_fSkyBoxLightSH[0], g_fSkyBoxLightSH[1],
                             g_fSkyBoxLightSH[2] ) );

    // Now compute the SH projection of the skybox...
    LPDIRECT3DCUBETEXTURE9 pSHCubeTex = NULL;
    V( D3DXCreateCubeTexture( pd3dDevice, 256, 1, 0, D3DFMT_A16B16G16R16F, D3DPOOL_MANAGED, &pSHCubeTex ) );

    SHCubeProj projData;
    projData.Init( g_fSkyBoxLightSH[0], g_fSkyBoxLightSH[1], g_fSkyBoxLightSH[2] );

    V( D3DXFillCubeTexture( pSHCubeTex, SHCubeFill, &projData ) );
    g_Skybox.InitSH( pSHCubeTex );

    // Read the D3DX effect file
    WCHAR str[MAX_PATH];
    V_RETURN( DXUTFindDXSDKMediaFileCch( str, MAX_PATH, TEXT( "LocalDeformablePRT.fx" ) ) );

    // If this fails, there should be debug output as to they the .fx file failed to compile
    V_RETURN( D3DXCreateEffectFromFile( pd3dDevice, str, NULL, NULL, dwShaderFlags, NULL, &g_pEffect, NULL ) );

    V_RETURN( LoadTechniqueObjects( "bat" ) );

    V_RETURN( g_LightControl.StaticOnD3D9CreateDevice( pd3dDevice ) );
    g_LightControl.SetRadius( 2.0f );

    // Setup the camera's view parameters
    D3DXVECTOR3 vecEye( 0.0f, 0.0f, -5.0f );
    D3DXVECTOR3 vecAt ( 0.0f, 0.0f, 0.0f );
    g_Camera.SetViewParams( &vecEye, &vecAt );

    // Set the model's initial orientation
    D3DXQUATERNION quatRotation;
    D3DXQuaternionRotationYawPitchRoll( &quatRotation, -0.5f, 0.7f, 0.0f );
    g_Camera.SetWorldQuat( quatRotation );

    return hr;
}
void Quaternion::BuildRotYawPitchRoll(const float yawRadians, const float pitchRadians, const float rollRadians)
{
	D3DXQuaternionRotationYawPitchRoll(this, yawRadians, pitchRadians, rollRadians);
}
Esempio n. 22
0
int KRLRemoteCharacter::GetSlopeRotation(D3DXQUATERNION& qRotationLH, RL_TERRAIN_SLOPE nTerrainSlope, BOOL bForceUpdate)
{
    int nRetCode = false;
    int nResult = false;
    float fTerrainYawLH = m_fFootYaw;
    float fTerrainRollLH = 0.0f;
    float fTerrainPitchLH = 0.0f;
    float fPitchStepSpeed = 0.0f;
    float fRollStepSpeed = 0.0f;
    IKG3DScene* p3DScene = NULL;
    D3DXVECTOR3 vPosition;
    BOOL bVisible = FALSE;
    
    bVisible = m_RLCharacter.m_RenderData.IsVisible();

    if (bVisible && !bForceUpdate)
    {
        KG_PROCESS_ERROR(abs(m_fFootYaw - m_fPreviousFootYaw) > FLT_EPSILON || m_bVisible != bVisible);

        m_bVisible = bVisible;
    }

    if (m_FrameData.m_Current.nMoveState == cmsOnJump || 
        m_FrameData.m_Current.nMoveState == cmsOnSwimJump)
    {
        nTerrainSlope = RL_TERRAIN_SLOPE_NONE;
    }

    p3DScene = m_RLCharacter.m_RenderData.GetRLScene()->m_p3DScene;
    KGLOG_PROCESS_ERROR(p3DScene);

    m_RLCharacter.GetPosition(vPosition);

    nRetCode = GetTerrainSlopeLean(p3DScene, vPosition, fTerrainYawLH, nTerrainSlope, fTerrainPitchLH, fTerrainRollLH);
    KG_PROCESS_ERROR(nRetCode);

    fPitchStepSpeed = GetPitchStepSpeed(m_FrameData.m_Current.nMoveState);
    fRollStepSpeed = GetRollStepSpeed(m_FrameData.m_Current.nMoveState);

    if (abs(m_fFootExpectPitch - fTerrainPitchLH) > D3DX_PI / 1800.0f)
        m_fFootExpectPitch = fTerrainPitchLH;
    m_fFootPitch = CloseToPitch(m_fFootExpectPitch, m_fFootPitch, fPitchStepSpeed);

    if (abs(m_fFootExpectRoll - fTerrainRollLH) > D3DX_PI / 1800.0f)
        m_fFootExpectRoll = fTerrainRollLH;
    m_fFootRoll = CloseToRoll(m_fFootExpectRoll, m_fFootRoll, fRollStepSpeed);

    switch (nTerrainSlope)
    {
    case RL_TERRAIN_SLOPE_NONE:
        YawToQuatLH(qRotationLH, YawLHToModelLH(fTerrainYawLH));
        break;
    case RL_TERRAIN_SLOPE_LENGTHWAYS:
        D3DXQuaternionRotationYawPitchRoll(&qRotationLH, YawLHToModelLH(fTerrainYawLH), m_fFootPitch, 0.0f);
        break;
    case RL_TERRAIN_SLOPE_TRANSVERSE:
        D3DXQuaternionRotationYawPitchRoll(&qRotationLH, YawLHToModelLH(fTerrainYawLH), 0.0f, m_fFootRoll);
        break;
    case RL_TERRAIN_SLOPE_ALL:
        D3DXQuaternionRotationYawPitchRoll(&qRotationLH, YawLHToModelLH(fTerrainYawLH), m_fFootPitch, m_fFootRoll);
        break;
    }

    nResult = true;
Exit0:
    return nResult;
}
Esempio n. 23
0
void GERenderingObject::setRotation(float X, float Y, float Z)
{
    D3DXQuaternionRotationYawPitchRoll(&qRotation, Y, X, Z);
}
Esempio n. 24
0
void GERenderingObject::setRotation(const GEVector& Rotation)
{
    D3DXQuaternionRotationYawPitchRoll(&qRotation, Rotation.Y, Rotation.X, Rotation.Z);
}
Esempio n. 25
0
void Tank::Update(Input* input, float time, QuadTree *m_QuadTree){
	int deltaX, deltaY;
	input->GetMouseDelta(deltaX, deltaY);
	
	m_tankState->SetTime(time);
	
	m_tankState->ApplyForce(D3DXVECTOR3(0.0f, 0.0f, forward));
	forward = 0;

	m_tankState->SetYaw(turn * time);
	yaw -= turn * time;	
	turn    = 0;

	
	D3DXVECTOR3 position = *getTankState()->GetPosition(), vgarbage, normal5;
	float height;	

	m_QuadTree->GetHeightAtPosition(position.x, position.z, height, normal5);
	float netforce = -0.00098f;
	float y = m_tankState->GetPosition()->y;
	if (y-(height+17) < 0) {
		netforce -= (y-(height+17)) * 0.000065f;
	}

	m_tankState->ApplyForce(D3DXVECTOR3(0,netforce,0));

	//m_turretState->SetPitch(deltaY*0.01f);*/

	//m_turretState->SetYaw(rotation);

	D3DXQUATERNION quat = *m_tankState->GetRotation();
	D3DXQUATERNION inverse;
	D3DXQuaternionInverse(&inverse, &quat);
	D3DXQUATERNION temp;
	D3DXVECTOR3 output;
	
	float garbage;

	temp = quat * D3DXQUATERNION(FRONTRIGHT.x, FRONTRIGHT.y, FRONTRIGHT.z, 0.0f) * inverse;
	D3DXQuaternionToAxisAngle(&temp, &output, &garbage);
	m_frontRight = *m_tankState->GetPosition() + output;

	temp = quat * D3DXQUATERNION(FRONTLEFT.x, FRONTLEFT.y, FRONTLEFT.z, 0.0f) * inverse;
	D3DXQuaternionToAxisAngle(&temp, &output, &garbage);
	m_frontLeft = *m_tankState->GetPosition() + output;

	temp = quat * D3DXQUATERNION(REARLEFT.x, REARLEFT.y, REARLEFT.z, 0.0f) * inverse;
	D3DXQuaternionToAxisAngle(&temp, &output, &garbage);
	m_rearLeft = *m_tankState->GetPosition() + output;

	temp = quat * D3DXQUATERNION(REARRIGHT.x, REARRIGHT.y, REARRIGHT.z, 0.0f) * inverse;
	D3DXQuaternionToAxisAngle(&temp, &output, &garbage);
	m_rearRight = *m_tankState->GetPosition() + output;

	temp = quat * D3DXQUATERNION(CENTER.x, CENTER.y, CENTER.z, 0.0f) * inverse;
	D3DXQuaternionToAxisAngle(&temp, &output, &garbage);
	m_center = *m_tankState->GetPosition() + output;

	// Get the height of the triangle that is directly underneath the given tank position.
	//result = m_QuadTree->GetHeightAtPosition(position.x, position.z, height, vgarbage);
	//if(result) {
		// If there was a triangle under the tank then position the tank just above it by one unit.
	//	getTankState()->SetPosition(D3DXVECTOR3(position.x,m_tankState->GetPosition()->y, position.z));
	//}

	int count = 5;
	//D3DXVECTOR3 normal1, normal2, normal3, normal4, normal5;
	//result = m_QuadTree->GetHeightAtPosition(m_frontRight.x, m_frontRight.z, height, normal1);
	//if(!result) {
	//	normal1 = D3DXVECTOR3(0.0f, 0.0f, 0.0f);
	//	count--;
	//}

	//result = m_QuadTree->GetHeightAtPosition(m_frontLeft.x, m_frontLeft.z, height, normal2);
	//if(!result) {
	//	normal2 = D3DXVECTOR3(0.0f, 0.0f, 0.0f);
	//	count--;
	//}

	//result = m_QuadTree->GetHeightAtPosition(m_rearRight.x, m_rearRight.z, height, normal3);
	//if(!result) {
	//	normal3 = D3DXVECTOR3(0.0f, 0.0f, 0.0f);
	//	count--;
	//}

	//result = m_QuadTree->GetHeightAtPosition(m_rearLeft.x, m_rearLeft.z, height, normal4);
	//if(!result) {
	//	normal4 = D3DXVECTOR3(0.0f, 0.0f, 0.0f);
	//	count--;
	//}

	//result = m_QuadTree->GetHeightAtPosition(m_center.x, m_center.z, height, normal5);
	//if(!result) {
	//	normal5 = D3DXVECTOR3(0.0f, 0.0f, 0.0f);
	//	count--;
	//}
		
	D3DXVECTOR3 line3;
	if (count > 0)
		line3 = normal5;
		//line3 = (normal1+normal2+normal3+normal4+normal5)/float(count);
	else
		line3 = *m_tankState->GetUp();

	float angle = acos(D3DXVec3Dot(&line3, m_tankState->GetUp()));// assume normalized vectors /(D3DXVec3Length(&line3)*D3DXVec3Length(m_tankState->getUp())));
	angle /= 15.0f;// * time;

	//if (angle > 0.015f)
	//{
		D3DXVECTOR3 cross;
		D3DXVec3Cross(&cross, &line3, m_tankState->GetUp());

		D3DXVec3Normalize(&cross, &cross);

		D3DXQUATERNION quaternion;
		D3DXQuaternionRotationAxis(&quaternion, &cross, -angle);
		m_tankState->multiplyOrientation(&quaternion);
	//}
	m_tankState->Update();




	D3DXQUATERNION orien;

	D3DXVECTOR3 slope, forward, turretProj;
	D3DXVec3Cross(&slope, m_tankState->GetUp(), &D3DXVECTOR3(0, 1, 0));
	D3DXVec3Normalize(&slope, &slope);
	D3DXVec3Cross(&forward, m_tankState->GetUp(), &slope);
	D3DXVec3Normalize(&forward, &forward);
	D3DXVec3Cross(&turretProj, m_tankState->GetUp(), m_turretState->GetForward());
	D3DXVec3Normalize(&turretProj, &turretProj);
	D3DXVec3Cross(&turretProj, m_tankState->GetUp(), &turretProj);
	D3DXVec3Normalize(&turretProj, &turretProj);

	float projangle = acos(abs(D3DXVec3Dot(&turretProj, &forward)));
	if (D3DXVec3Dot(&turretProj, &forward) < 0) projangle = float(D3DX_PI - projangle);

	float slopeangle = acos(abs(D3DXVec3Dot(&D3DXVECTOR3(0, 1, 0), &forward)));
	slopeangle = float(D3DX_PI/2 - slopeangle);

	float pitchOffset = (1 - cos(projangle)) * slopeangle;

	D3DXQuaternionRotationYawPitchRoll(&orien, yaw, pitch - pitchOffset, 0);
	orien = orien * *m_tankState->GetRotation();
	m_turretState->SetOrientation(&orien);
	m_turretState->Update();

	m_Bullet->Update(time);
}
Esempio n. 26
0
void GERenderingObject::rotate(float RX, float RY, float RZ)
{
    D3DXQUATERNION qAux;
    D3DXQuaternionRotationYawPitchRoll(&qAux, RY, RX, RZ);
    qRotation = qAux * qRotation;
}
Esempio n. 27
0
void GERenderingObject::rotate(const GEVector& Rotate)
{
    D3DXQUATERNION qAux;
    D3DXQuaternionRotationYawPitchRoll(&qAux, Rotate.Y, Rotate.X, Rotate.Z);
    qRotation = qAux * qRotation;
}
Esempio n. 28
0
void State::Update() {
	// TODO: Add decay speed
	m_acceleration -= m_posvel * m_friction;

	// Apply positional velocity
	if(!m_follow) {
		m_pos += (m_right * m_posvel.x) * m_time + (1/2) * (m_right * m_acceleration.x) * pow(m_time, 2);
		m_pos += (m_up * m_posvel.y) * m_time + (1/2) * (m_up * m_acceleration.y) * pow(m_time, 2);
		m_pos += (m_front * m_posvel.z) * m_time + (1/2) * (m_front * m_acceleration.z) * pow(m_time, 2);
	} else {
		m_pos = *m_follow->GetPosition();
		m_pos += (*m_follow->GetRight() * m_offset.x);
		m_pos += (*m_follow->GetUp() * m_offset.y);
		m_pos += (*m_follow->GetForward() * m_offset.z);
	}

	m_posvel += m_acceleration * m_time;

	D3DXQUATERNION rot;
	D3DXQuaternionRotationYawPitchRoll(
		&rot, 
		m_rotvel.x,
		m_rotvel.y,
		m_rotvel.z
	);



	if (m_follow)
	{
		/*D3DXQUATERNION xrot,yrot,zrot;
		D3DXQuaternionRotationAxis(&xrot,&m_right,m_rotvel.y);
		D3DXQuaternionRotationAxis(&yrot,m_follow->GetUp(),m_rotvel.x);
		rot = yrot * xrot;

		static D3DXQUATERNION totalRot = rot;
		totalRot *= rot;
		D3DXQUATERNION temp;
		temp = *m_follow->GetRotation();*/
		//m_rot = temp * totalRot;
	}
	else
	{	
		m_rot *= rot;
	}

	/*

	if(m_rotvel_on) {
		// Apply rotational velocity
		D3DXQUATERNION rot;
		D3DXQuaternionRotationYawPitchRoll(
			&rot, 
			m_rotvel.x, 
			m_rotvel.y, 
			m_rotvel.z
		);

		m_rot *= rot;
	} else {
		D3DXQUATERNION rot, temp;
		D3DXQuaternionRotationYawPitchRoll(
			&rot, 
			m_rotvel.x, 
			m_rotvel.y, 
			m_rotvel.z
		);

		temp = *m_follow->GetRotation();
		//temp.y = 0;
		m_rot = temp;
		D3DXQuaternionNormalize(&m_rot, &m_rot);

		m_rot = rot * m_rot;
	}*/

	D3DXQuaternionNormalize(&m_rot, &m_rot);

	// Calculate up, front, left
	m_up = D3DXVECTOR3(
		2 * (m_rot.x * m_rot.y - m_rot.w * m_rot.z), 
		1 - 2 * (m_rot.x * m_rot.x + m_rot.z * m_rot.z),
		2 * (m_rot.y * m_rot.z + m_rot.w * m_rot.x)
	);

	m_front = D3DXVECTOR3(
		2 * (m_rot.x * m_rot.z + m_rot.w * m_rot.y), 
		2 * (m_rot.y * m_rot.z - m_rot.w * m_rot.x),
		1 - 2 * (m_rot.x * m_rot.x + m_rot.y * m_rot.y)
	);
	
	m_right = D3DXVECTOR3(
		1 - 2 * (m_rot.y * m_rot.y + m_rot.z * m_rot.z),
		2 * (m_rot.x * m_rot.y + m_rot.w * m_rot.z),
		2 * (m_rot.x * m_rot.z - m_rot.w * m_rot.y)
	);;

	m_rotvel = D3DXVECTOR3(0.0f, 0.0f, 0.0f);
	m_acceleration = D3DXVECTOR3(0.0f, 0.0f, 0.0f);

	m_prevRot = m_rot;
}
Esempio n. 29
0
HRESULT InitScene()
{
	HRESULT hr;

	SetWindowText(hwnd, TITLE);

	MYVALID(D3DXLoadMeshFromX("../media/meshes/sky.X", D3DXMESH_MANAGED, device, NULL, NULL, NULL, NULL, &skymesh));
	MYVALID(D3DXLoadMeshFromX("../media/meshes/box.X", D3DXMESH_MANAGED, device, NULL, NULL, NULL, NULL, &mesh));
	MYVALID(D3DXCreateTextureFromFileA(device, "../media/textures/fire.png", &texture1));
	MYVALID(D3DXCreateTextureFromFileA(device, "../media/textures/stones.jpg", &texture2));
	MYVALID(D3DXCreateCubeTextureFromFileA(device, "../media/textures/sky4.dds", &skytex));
	MYVALID(device->CreateTexture(512, 512, 1, 0, D3DFMT_A8R8G8B8, D3DPOOL_MANAGED, &text, NULL));

	MYVALID(DXCreateEffect("../media/shaders/skinning.fx", device, &effect));
	MYVALID(DXCreateEffect("../media/shaders/sky.fx", device, &skyeffect));

	system1.Initialize(device, 500);
	system1.ParticleTexture = texture1;

	// load dwarfs
	dwarfs[0].Effect = effect;
	dwarfs[0].Method = SM_Shader;
	dwarfs[0].Path = "../media/meshes/dwarf/";
	
	MYVALID(dwarfs[0].Load(device, "../media/meshes/dwarf/dwarf.X"));

	dwarfs[0].Clone(dwarfs[1]);
	dwarfs[0].Clone(dwarfs[2]);
	dwarfs[0].Clone(dwarfs[3]);
	dwarfs[0].Clone(dwarfs[4]);
	dwarfs[0].Clone(dwarfs[5]);

	D3DXVECTOR3 scale(0.1f, 0.1f, 0.1f);
	D3DXVECTOR3 trans;
	D3DXQUATERNION rot;

	// dwarf 0
	trans = D3DXVECTOR3(-1, 0, 1);

	D3DXQuaternionRotationYawPitchRoll(&rot, -0.785f, 0, 0);
	D3DXMatrixTransformation(&dwarfmatrices[0], NULL, NULL, &scale, NULL, &rot, &trans);

	// dwarf 1
	trans = D3DXVECTOR3(-1, 0, -1);

	D3DXQuaternionRotationYawPitchRoll(&rot, -2.356f, 0, 0);
	D3DXMatrixTransformation(&dwarfmatrices[1], NULL, NULL, &scale, NULL, &rot, &trans);

	// dwarf 2
	trans = D3DXVECTOR3(1, 0, -1);

	D3DXQuaternionRotationYawPitchRoll(&rot, 2.356f, 0, 0);
	D3DXMatrixTransformation(&dwarfmatrices[2], NULL, NULL, &scale, NULL, &rot, &trans);

	// dwarf 3
	trans = D3DXVECTOR3(1, 0, 1);

	D3DXQuaternionRotationYawPitchRoll(&rot, 0.785f, 0, 0);
	D3DXMatrixTransformation(&dwarfmatrices[3], NULL, NULL, &scale, NULL, &rot, &trans);

	// dwarf 4
	trans = D3DXVECTOR3(-0.2f, 0, -0.2f);

	D3DXQuaternionRotationYawPitchRoll(&rot, -1.57f, 0, 0);
	D3DXMatrixTransformation(&dwarfmatrices[4], NULL, NULL, &scale, NULL, &rot, &trans);

	// dwarf 5
	trans = D3DXVECTOR3(0.2f, 0, 0);

	D3DXQuaternionRotationYawPitchRoll(&rot, -1.57f, 0, 0);
	D3DXMatrixTransformation(&dwarfmatrices[5], NULL, NULL, &scale, NULL, &rot, &trans);

	// skins
	DwarfSkin(0, 1, 1, 3, 1, 1, 3, 0);
	DwarfSkin(1, 2, 0, 1, 3, 3, 1, 1);
	DwarfSkin(2, 3, 2, 2, 2, 1, 2, 3);
	DwarfSkin(3, 1, 3, 2, 1, 3, 0, 2);
	DwarfSkin(4, 1, 1, 3, 3, 2, 2, 1);
	DwarfSkin(5, 0, 0, 1, 2, 3, 0, 1);

	// 0, 1, 2 - dead
	// 3, 5 - stand
	// 4 - jump
	// 6 - cheer with weapon
	// 7 - cheer with one hand
	// 8 - cheer with both hands
	dwarfs[0].SetAnimation(6);
	dwarfs[1].SetAnimation(8);
	dwarfs[2].SetAnimation(7);
	dwarfs[3].SetAnimation(4);
	dwarfs[4].SetAnimation(2);
	dwarfs[5].SetAnimation(1);

	// other
	device->SetRenderState(D3DRS_LIGHTING, false);
	device->SetSamplerState(0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR);
	device->SetSamplerState(0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR);

	DXRenderText(HELP_TEXT, text, 512, 512);
	cameraangle = D3DXVECTOR2(0.785f, 0.785f);

	// sound
	if( SUCCEEDED(InitXAudio2()) )
	{
		firesound = streamer.LoadSound(xaudio2, "../media/sound/fire.ogg");

		// create streaming thread and load music
		worker.Attach<AudioStreamer>(&streamer, &AudioStreamer::Update);
		worker.Start();

		music = streamer.LoadSoundStream(xaudio2, "../media/sound/painkiller.ogg");
	}

	if( music )
	{
		music->GetVoice()->SetVolume(4);
		music->Play();
	}

	if( firesound )
	{
		firesound->GetVoice()->SetVolume(0.7f);
		firesound->Play();
	}

	return S_OK;
}
Esempio n. 30
0
void _X3PCamera::Advance( void )
{
	BOOL changedcamerastatus = FALSE;

	FLOAT abscamvel_x = fabs(m_CameraVelocity.x);
	FLOAT abscamvel_y = fabs(m_CameraVelocity.y);
	FLOAT abscamvel_z = fabs(m_CameraVelocity.z);
	FLOAT abscamvel_dist = fabs(m_ZoominoutVelocity);

	if( abscamvel_x > EPSILON3 || abscamvel_y > EPSILON3 || abscamvel_z > EPSILON3 || abscamvel_dist > EPSILON3 )
	{
		if( abscamvel_x > EPSILON3 )
		{
			m_CameraVelocity.x *= _XDEF_CAMERADECREASERATE;
			
			// add yaw
			mp_fYaw += m_CameraVelocity.x;
			if( mp_fYaw > 360.0f ) mp_fYaw = (FLOAT)fmod(mp_fYaw, 360.0);
		}

		if( abscamvel_y > EPSILON3 )
		{
			m_CameraVelocity.y *= _XDEF_CAMERADECREASERATE;
			
			//add pitch
			mp_fPitch += m_CameraVelocity.y;			
			if(mp_fPitch < mp_fMinPitchLimit) mp_fPitch = mp_fMinPitchLimit;
			else if(mp_fPitch > mp_fMaxPitchLimit) mp_fPitch = mp_fMaxPitchLimit;
		}

		if( abscamvel_z > EPSILON3 )
		{
			m_CameraVelocity.z *= _XDEF_CAMERADECREASERATE;
			
			//add roll
			mp_fRoll += m_CameraVelocity.z;			
			if(mp_fRoll < mp_fMinRollLimit) mp_fRoll = mp_fMinRollLimit;
			else if(mp_fRoll > mp_fMaxRollLimit) mp_fRoll = mp_fMaxRollLimit;
		}

		if( abscamvel_dist > EPSILON3 )
		{

#ifdef _XDWDEBUG
			extern BOOL g_MouseLockFlag;
			if( !g_MouseLockFlag )
			{
#endif
			if( m_MinDistance + m_AdditionalHeightMinDistance < m_TargetDistance )
			{
				m_ZoominoutVelocity *= _XDEF_CAMERAZOOMDECREASERATE;
			}
			else
			{
				m_ZoominoutVelocity *= 0.3f;
			}
			
			m_TargetDistance += m_ZoominoutVelocity;
			if( m_MinDistance > m_TargetDistance )
			{
				m_TargetDistance = m_MinDistance; 
			}
			else if( m_MaxDistance < m_TargetDistance )
			{
				m_TargetDistance = m_MaxDistance;
			}
#ifdef _XDWDEBUG
			}
#endif			
		}

		changedcamerastatus = TRUE;
	}
	
	if( m_QuaterViewChanging )
	{
		D3DXQUATERNION	nextrotquat;
		D3DXQUATERNION	orgrotquat;
		D3DXQUATERNION	targetrotquat;
		
		D3DXQuaternionRotationYawPitchRoll( &orgrotquat, _X_RAD(mp_fYaw), _X_RAD(mp_fPitch), _X_RAD(mp_fRoll) );

		if( m_DefaultViewChanging )
		{
			D3DXQuaternionRotationYawPitchRoll( &targetrotquat, _X_RAD(-45.0f), _X_RAD(30.0f), 0.0f );
		}
		else
		{
			D3DXQuaternionRotationYawPitchRoll( &targetrotquat, _X_RAD(180.0f), _X_RAD(30.0f), 0.0f );
		}
		
		FLOAT fElapsedTime = g_fElapsedFrameMilisecondTime*3.0f;
		if( fElapsedTime > 1.0f ) fElapsedTime = 1.0f;
		D3DXQuaternionSlerp( &nextrotquat, &orgrotquat, &targetrotquat, fElapsedTime );
		
		FLOAT fyaw, fpitch, froll;
		_XMeshMath_QuaternionToEulerAngle( nextrotquat, fyaw, fpitch, froll );
		
		fyaw = _X_DEG(fyaw);
		if( fyaw > 360.0f ) fyaw = (FLOAT)fmod(fyaw, 360.0);	
		
		fpitch	= _X_DEG(fpitch); 
		if(fpitch < mp_fMinPitchLimit) fpitch = mp_fMinPitchLimit; 
		else if(fpitch > mp_fMaxPitchLimit) fpitch = mp_fMaxPitchLimit;
		
		froll	= _X_DEG(froll);
		if(froll < mp_fMinRollLimit) froll = mp_fMinRollLimit;
		else if(froll > mp_fMaxRollLimit) froll = mp_fMaxRollLimit;
		
		if( fabs( mp_fYaw   - fyaw   ) > EPSILON1 ||
			fabs( mp_fPitch - fpitch ) > EPSILON1 ||
			fabs( mp_fRoll  - froll  ) > EPSILON1 )
		{
			mp_fYaw   = fyaw;
			mp_fPitch = fpitch;
			mp_fRoll  = froll;			
			changedcamerastatus = TRUE;
		}
		else
		{
			m_QuaterViewChanging = FALSE;
			m_QuaterViewMode = TRUE;

			m_DefaultViewChanging = FALSE;			
		}

		_XWindow_WorldMinimap* pminimapwindow = (_XWindow_WorldMinimap*)g_MainWindowManager.FindWindow( _XDEF_WTITLE_MINIMAPWINDOW );
		if( pminimapwindow )
		{
			// Set direction to minimap arrow 
			pminimapwindow->SetRotateFrustum( _X_RAD( 180 - mp_fYaw ) );
		}
	}
	else if( m_DefaultViewChanging )
	{
		D3DXQUATERNION	nextrotquat;
		D3DXQUATERNION	orgrotquat;
		D3DXQUATERNION	targetrotquat;
		
		D3DXQuaternionRotationYawPitchRoll( &orgrotquat, _X_RAD(mp_fYaw), _X_RAD(mp_fPitch), _X_RAD(mp_fRoll) );
		D3DXQuaternionRotationYawPitchRoll( &targetrotquat, _X_RAD(180.0f), _X_RAD(mp_fPitch), 0.0f );
		
		FLOAT fElapsedTime = g_fElapsedFrameMilisecondTime*3.0f;
		if( fElapsedTime > 1.0f ) fElapsedTime = 1.0f;
		D3DXQuaternionSlerp( &nextrotquat, &orgrotquat, &targetrotquat, fElapsedTime );
		
		FLOAT fyaw, fpitch, froll;
		_XMeshMath_QuaternionToEulerAngle( nextrotquat, fyaw, fpitch, froll );
		
		fyaw = _X_DEG(fyaw);
		if( fyaw > 360.0f ) fyaw = (FLOAT)fmod(fyaw, 360.0);	
		
		fpitch	= _X_DEG(fpitch); 
		if(fpitch < mp_fMinPitchLimit) fpitch = mp_fMinPitchLimit; 
		else if(fpitch > mp_fMaxPitchLimit) fpitch = mp_fMaxPitchLimit;
		
		froll	= _X_DEG(froll);
		if(froll < mp_fMinRollLimit) froll = mp_fMinRollLimit;
		else if(froll > mp_fMaxRollLimit) froll = mp_fMaxRollLimit;
		
		if( fabs( mp_fYaw   - fyaw   ) > EPSILON1 ||
			fabs( mp_fPitch - fpitch ) > EPSILON1 ||
			fabs( mp_fRoll  - froll  ) > EPSILON1 )
		{
			mp_fYaw   = fyaw;
			mp_fPitch = fpitch;
			mp_fRoll  = froll;			
			changedcamerastatus = TRUE;
		}
		else
		{
			m_DefaultViewChanging = FALSE;
		}

		_XWindow_WorldMinimap* pminimapwindow = (_XWindow_WorldMinimap*)g_MainWindowManager.FindWindow( _XDEF_WTITLE_MINIMAPWINDOW );
		if( pminimapwindow )
		{
			// Set direction to minimap arrow 
			pminimapwindow->SetRotateFrustum( _X_RAD( 180 - mp_fYaw ) );
		}
	}
	else
	{
		if( !gpInput->GetMouseState()->bButton[1] && 
			( g_pLocalUser->GetMotionClass() == _XACTION_MOVE && 
			  ( g_pLocalUser->m_PathNodeCount >= 1 || g_pLocalUser->m_LeftFinalTargetLength > 64.0f ) ) &&
			  (fabs( g_pLocalUser->m_RotateAngle - g_pLocalUser->m_LastRotateAngle ) < EPSILON3) && m_AutoBackTrace )
		{
			D3DXQUATERNION	nextrotquat;	
			D3DXQUATERNION	orgrotquat;
			D3DXQUATERNION	targetrotquat;
					
			D3DXMATRIX  mtxRotate = g_pLocalUser->m_ModelDescriptor.m_Position;		
			mtxRotate._41 = mtxRotate._42 = mtxRotate._43 = 0.0f;
			
			D3DXQuaternionRotationYawPitchRoll( &orgrotquat, 0.0f, _X_RAD(mp_fPitch), 0.0f );

			D3DXQuaternionRotationMatrix( &targetrotquat, &mtxRotate );
			D3DXQuaternionMultiply( &targetrotquat, &orgrotquat, &targetrotquat );

			D3DXQuaternionRotationYawPitchRoll( &orgrotquat, _X_RAD(mp_fYaw), _X_RAD(mp_fPitch), _X_RAD(mp_fRoll) );

			FLOAT fElapsedTime = g_fElapsedFrameMilisecondTime;
			if( fElapsedTime > 1.0f ) fElapsedTime = 1.0f;
			D3DXQuaternionSlerp( &nextrotquat, &orgrotquat, &targetrotquat, fElapsedTime );

			FLOAT fyaw, fpitch, froll;
			_XMeshMath_QuaternionToEulerAngle( nextrotquat, fyaw, fpitch, froll );

			fyaw = _X_DEG(fyaw);
			if( fyaw > 360.0f ) fyaw = (FLOAT)fmod(fyaw, 360.0);	
			
			fpitch	= _X_DEG(fpitch); 
			if(fpitch < mp_fMinPitchLimit) fpitch = mp_fMinPitchLimit; 
			else if(fpitch > mp_fMaxPitchLimit) fpitch = mp_fMaxPitchLimit;
				
			froll	= _X_DEG(froll);
			if(froll < mp_fMinRollLimit) froll = mp_fMinRollLimit;
			else if(froll > mp_fMaxRollLimit) froll = mp_fMaxRollLimit;

			if( fabs( mp_fYaw   - fyaw   ) > EPSILON3 ||
				fabs( mp_fPitch - fpitch ) > EPSILON3 ||
				fabs( mp_fRoll  - froll  ) > EPSILON3 )
			{
				mp_fYaw   = fyaw;
				mp_fPitch = fpitch;
				mp_fRoll  = froll;			
				changedcamerastatus = TRUE;
			}
		}
	}

	if( m_CameraShakeMode )
	{
		if( !m_CameraShakeDelayMode )
		{
			const FLOAT shakeadditionalfactor = 10.0f;
			FLOAT fshakefactor = _XLinearGraph( 0.1f,0.05f,  0.5f,0.0f ).GetValueAt( g_fElapsedFrameMilisecondTime );
			m_CameraShakeFactor.x = fshakefactor * sinf(rand());
			m_CameraShakeFactor.y = fshakefactor * sinf(rand());
			m_CameraShakeFactor.z = fshakefactor * sinf(rand());
			
			D3DXMATRIX matOrientation;
			D3DXMatrixInverse( &matOrientation, NULL, &mp_view_matrix );
			D3DXVec3TransformNormal( &m_CameraShakeFactor, &m_CameraShakeFactor, &matOrientation );
			
			changedcamerastatus = TRUE;
			
			m_fCameraShakeTimer -= g_fElapsedFrameMilisecondTime;
			if( m_fCameraShakeTimer < 0.0f )
			{
				m_CameraShakeMode = FALSE; 
				m_CameraShakeFactor = D3DXVECTOR3( 0.0f,0.0f,0.0f );
			}
			
		}
		else
		{
			if( g_LocalSystemTime - m_fCameraShakeStartTimer < m_fCameraShakeTimer*1000 )
			{
				
				int temp = (int)(((g_LocalSystemTime - m_fCameraShakeStartTimer)/1000.0f)/5.0f)%2;
				if( temp == 0 )
				{					
					if( !m_ChangeCameraShakeAtDelayMode )
					{
						_XPlaySoundEffect(ID_SR_INTERFACE_EARTHQUAKE_WAV, g_pLocalUser->m_Position );
					}
					const FLOAT shakeadditionalfactor = 10.0f;
					FLOAT fshakefactor = _XLinearGraph( 0.05f,0.025f,  0.25f,0.0f ).GetValueAt( g_fElapsedFrameMilisecondTime );
					m_CameraShakeFactor.x = fshakefactor * sinf(rand());
					m_CameraShakeFactor.y = fshakefactor * sinf(rand());
					m_CameraShakeFactor.z = fshakefactor * sinf(rand());
					
					D3DXMATRIX matOrientation;
					D3DXMatrixInverse( &matOrientation, NULL, &mp_view_matrix );
					D3DXVec3TransformNormal( &m_CameraShakeFactor, &m_CameraShakeFactor, &matOrientation );
					
					changedcamerastatus = TRUE;
					m_ChangeCameraShakeAtDelayMode = TRUE;
				}
				else
				{
					if( m_ChangeCameraShakeAtDelayMode )
					{
						_XPlaySoundEffect(ID_SR_INTERFACE_EARTHQUAKE01_WAV, g_pLocalUser->m_Position );
					}
					m_ChangeCameraShakeAtDelayMode = FALSE;
				}
			}
			else
			{
				m_CameraShakeMode = FALSE; 
				m_CameraShakeFactor = D3DXVECTOR3( 0.0f,0.0f,0.0f );
			}
		}
		
	}

	if( changedcamerastatus )
	{
		UpdateViewMatrix( &g_LodTerrain, TRUE );
		g_LodTerrain.m_ObjectQuadTree.UpdateCamera(g_LodTerrain.m_3PCamera);
		g_LodTerrain.RebuildLevel();
	}
}