Ejemplo n.º 1
0
void Camera::buildFrustumPlanes()
{
	D3DXMATRIX VP = m_view * m_proj;

	D3DXVECTOR4 col0(VP(0, 0), VP(1, 0), VP(2, 0), VP(3, 0));
	D3DXVECTOR4 col1(VP(0, 1), VP(1, 1), VP(2, 1), VP(3, 1));
	D3DXVECTOR4 col2(VP(0, 2), VP(1, 2), VP(2, 2), VP(3, 2));
	D3DXVECTOR4 col3(VP(0, 3), VP(1, 3), VP(2, 3), VP(3, 3));

	// Planes face inward.
	mFrustumPlanes[0] = (D3DXPLANE)(col2);        // near
	mFrustumPlanes[1] = (D3DXPLANE)(col3 - col2); // far
	mFrustumPlanes[2] = (D3DXPLANE)(col3 + col0); // left
	mFrustumPlanes[3] = (D3DXPLANE)(col3 - col0); // right
	mFrustumPlanes[4] = (D3DXPLANE)(col3 - col1); // top
	mFrustumPlanes[5] = (D3DXPLANE)(col3 + col1); // bottom

	for (int i = 0; i < 6; i++)
		D3DXPlaneNormalize(&mFrustumPlanes[i], &mFrustumPlanes[i]);
}
Ejemplo n.º 2
0
void FrustumPlanes::Set(const MATRIX &WorldViewProj)
{
	// Left clipping plane
	Planes[0].a = WorldViewProj._14 + WorldViewProj._11;
	Planes[0].b = WorldViewProj._24 + WorldViewProj._21;
	Planes[0].c = WorldViewProj._34 + WorldViewProj._31;
	Planes[0].d = WorldViewProj._44 + WorldViewProj._41;
	D3DXPlaneNormalize( &Planes[0], &Planes[0] );

	// Right clipping plane
	Planes[1].a = WorldViewProj._14 - WorldViewProj._11;
	Planes[1].b = WorldViewProj._24 - WorldViewProj._21;
	Planes[1].c = WorldViewProj._34 - WorldViewProj._31;
	Planes[1].d = WorldViewProj._44 - WorldViewProj._41;
	D3DXPlaneNormalize( &Planes[1], &Planes[1] );

	// Top clipping plane
	Planes[2].a = WorldViewProj._14 - WorldViewProj._12;
	Planes[2].b = WorldViewProj._24 - WorldViewProj._22;
	Planes[2].c = WorldViewProj._34 - WorldViewProj._32;
	Planes[2].d = WorldViewProj._44 - WorldViewProj._42;
	D3DXPlaneNormalize( &Planes[2], &Planes[2] );

	// Bottom clipping plane
	Planes[3].a = WorldViewProj._14 + WorldViewProj._12;
	Planes[3].b = WorldViewProj._24 + WorldViewProj._22;
	Planes[3].c = WorldViewProj._34 + WorldViewProj._32;
	Planes[3].d = WorldViewProj._44 + WorldViewProj._42;
	D3DXPlaneNormalize( &Planes[3], &Planes[3] );

	// Near clipping plane
	Planes[4].a = WorldViewProj._13;
	Planes[4].b = WorldViewProj._23;
	Planes[4].c = WorldViewProj._33;
	Planes[4].d = WorldViewProj._43;
	D3DXPlaneNormalize( &Planes[4], &Planes[4] );

	// Far clipping plane
	Planes[5].a = WorldViewProj._14 - WorldViewProj._13;
	Planes[5].b = WorldViewProj._24 - WorldViewProj._23;
	Planes[5].c = WorldViewProj._34 - WorldViewProj._33;
	Planes[5].d = WorldViewProj._44 - WorldViewProj._43;
	D3DXPlaneNormalize( &Planes[5], &Planes[5] );
}
//反射平面を作ります。
FbxAMatrix* D3DXMatrixReflect_Fixed(__inout FbxAMatrix *pOut, __in const D3DXPLANE *pPlane)
{
    D3DXPLANE P;
    D3DXPlaneNormalize(&P, pPlane);

    pOut->mData[0][0] = -2.0f * P.a * P.a + 1.0f;
    pOut->mData[0][1] = -2.0f * P.b * P.a;
    pOut->mData[0][2] = -2.0f * P.c * P.a;
    pOut->mData[0][3] = 0.0f;
    pOut->mData[1][0] = -2.0f * P.a * P.b;
    pOut->mData[1][1] = -2.0f * P.b * P.b + 1.0f;
    pOut->mData[1][2] = -2.0f * P.c * P.b;
    pOut->mData[1][3] = 0.0f;
    pOut->mData[2][0] = -2.0f * P.a * P.c;
    pOut->mData[2][1] = -2.0f * P.b * P.c;
    pOut->mData[2][2] = -2.0f * P.c * P.c + 1.0f;
    pOut->mData[2][3] = 0.0f;
    pOut->mData[3][0] = -2.0f * P.a * P.d;
    pOut->mData[3][1] = -2.0f * P.b * P.d;
    pOut->mData[3][2] = -2.0f * P.c * P.d;
    pOut->mData[3][3] = 1.0f;

    return pOut;
}
Ejemplo n.º 4
0
void CFrustum::Construct( float depth, Matrix4 &matrixProjectionScreen )
{
	Matrix4 matrix;
	matrix = matrixProjectionScreen; 

	planes[0].a = matrix.n41 - matrix.n11;
	planes[0].b = matrix.n42 - matrix.n12;
	planes[0].c = matrix.n43 - matrix.n13;
	planes[0].d = matrix.n44 - matrix.n14;
	D3DXPlaneNormalize(&planes[0], &planes[0]);

	planes[1].a = matrix.n41 + matrix.n11;
	planes[1].b = matrix.n42 + matrix.n12;
	planes[1].c = matrix.n43 + matrix.n13;
	planes[1].d = matrix.n44 + matrix.n14;
	D3DXPlaneNormalize(&planes[1], &planes[1]);

	planes[2].a = matrix.n41 + matrix.n21;
	planes[2].b = matrix.n42 + matrix.n22;
	planes[2].c = matrix.n43 + matrix.n23;
	planes[2].d = matrix.n44 + matrix.n24;
	D3DXPlaneNormalize(&planes[2], &planes[2]);

	planes[3].a = matrix.n41 - matrix.n21;
	planes[3].b = matrix.n42 - matrix.n22;
	planes[3].c = matrix.n43 - matrix.n23;
	planes[3].d = matrix.n44 - matrix.n24;
	D3DXPlaneNormalize(&planes[3], &planes[3]);

	planes[4].a = matrix.n41 + matrix.n31;
	planes[4].b = matrix.n42 + matrix.n32;
	planes[4].c = matrix.n43 + matrix.n33;
	planes[4].d = matrix.n44 + matrix.n34;
	D3DXPlaneNormalize(&planes[4], &planes[4]);

	planes[5].a = matrix.n41 - matrix.n31;
	planes[5].b = matrix.n42 - matrix.n32;
	planes[5].c = matrix.n43 - matrix.n33;
	planes[5].d = matrix.n44 - matrix.n34;
	D3DXPlaneNormalize(&planes[5], &planes[5]);
}
Ejemplo n.º 5
0
HRESULT KG3DRotationCoordinateOld::RotateBegin()
{
 
	//m_matEntityWorld = m_EntityList.GetWorldMatrix();
	{
		D3DXVECTOR3 vCenter(0,0,0);
		KSF::GetSelectionCenter(m_pAttachScene->GetSelectionTool(), vCenter);
		D3DXMatrixTranslation(&m_matEntityWorld, vCenter.x, vCenter.y, vCenter.z);
	}

	m_vBeginCross.x = m_matEntityWorld._41;
	m_vBeginCross.y = m_matEntityWorld._42;
	m_vBeginCross.z = m_matEntityWorld._43;

	m_vPrevCross = m_vBeginCross;

    HRESULT hResult  = E_FAIL;
    HRESULT hRetCode = E_FAIL;

    D3DXVECTOR3 vOrg;
    D3DXVECTOR3 vDir;

    D3DXMATRIX  matWorldInv;

    D3DXVECTOR3  vCrossXZ;
    D3DXVECTOR3  vCrossYZ;
    D3DXVECTOR3  vCrossXY;

    D3DXPLANE    planeXZ;
    D3DXPLANE    planeXY;
    D3DXPLANE    planeYZ;

    D3DXVECTOR3  vPoint = D3DXVECTOR3(m_matCoord._41, m_matCoord._42, m_matCoord._43);
    D3DXVECTOR3  vNorXZ = D3DXVECTOR3(m_matCoord._21, m_matCoord._22, m_matCoord._23);
    D3DXVECTOR3  vNorXY = D3DXVECTOR3(m_matCoord._31, m_matCoord._32, m_matCoord._33);
    D3DXVECTOR3  vNorYZ = D3DXVECTOR3(m_matCoord._11, m_matCoord._12, m_matCoord._13);

    D3DXVECTOR3 vCross;
    IEKG3DSceneOutputWnd *piCurOutputWnd = NULL;


    KG_PROCESS_ERROR(m_dwCurrSelCoord != 0xFFFFFFFF);
    //KG_PROCESS_ERROR(m_pAttachScene);
    //KG_PROCESS_ERROR(m_EntityList.GetSize());

	_ASSERTE(NULL != m_pAttachScene);
	KG_PROCESS_ERROR(0 != m_pAttachScene->GetSelectionTool().GetSelectionCount());

    hRetCode = m_pAttachScene->GetCurOutputWnd(&piCurOutputWnd);
    KGLOG_COM_PROCESS_ERROR(hRetCode);

    piCurOutputWnd->GetPickRay(&vOrg, &vDir, NULL);

    D3DXPlaneFromPointNormal(
        &planeXZ,
        &vPoint,
        &vNorXZ
        );
    D3DXPlaneFromPointNormal(
        &planeXY,
        &vPoint,
        &vNorXY
        );
    D3DXPlaneFromPointNormal(
        &planeYZ,
        &vPoint,
        &vNorYZ
        );

    D3DXPlaneNormalize(&planeYZ, &planeYZ);
    D3DXPlaneNormalize(&planeXZ, &planeXZ);
    D3DXPlaneNormalize(&planeXY, &planeXY);

    switch (m_dwCurrSelCoord)
    {
    case 0 :    // y
        KG_PROCESS_ERROR(
            D3DXPlaneIntersectLine(
                &vCross, 
                &planeXZ,
                &vOrg,
                &(vOrg + vDir * 10000000.0f)
                )
            );
        m_vPrevCross    = vCross;
        m_currSelPane   = planeXZ;
        m_currSelNormal = vNorXZ;
        break;
    case 1 :    // x
        KG_PROCESS_ERROR(
            D3DXPlaneIntersectLine(
                &vCross, 
                &planeYZ,
                &vOrg,
                &(vOrg + vDir * 10000000.0f)
                )
            );
        m_vPrevCross    = vCross;
        m_currSelPane   = planeYZ;
        m_currSelNormal = vNorYZ;
        break;
    case 2 :    // z
        KG_PROCESS_ERROR(
            D3DXPlaneIntersectLine(
                &vCross, 
                &planeXY,
                &vOrg,
                &(vOrg + vDir * 10000000.0f)
                )
            );
        m_vPrevCross    = vCross;
        m_currSelPane   = planeXY;
        m_currSelNormal = vNorXY;
        break;
    default :
        ASSERT(FALSE);
        break;
    }
    m_vBeginCross = m_vPrevCross;

  
    m_fAngelX = 0.0f;
    m_fAngelY = 0.0f;
    m_fAngelZ = 0.0f;

    m_nMoveFlag = TRUE;
    hResult = S_OK;
Exit0:
    return hResult;
}
Ejemplo n.º 6
0
void DxManager::CalculateCulling()
{
	D3DXMATRIX view = this->camera->GetViewMatrix();
	D3DXMATRIX proj = this->camera->GetProjectionMatrix();

	/*
	float zMinimum = -proj._43 / proj._33;
	float r = this->params.FarClip / (this->params.FarClip - zMinimum);
	proj._33 = r;
	proj._43 = -r * zMinimum;
	*/

	D3DXMATRIX VP;
	D3DXMatrixMultiply(&VP, &view, &proj);


	// Calculate near plane of frustum.
	FrustrumPlanes[0].a = VP._14 + VP._13;
	FrustrumPlanes[0].b = VP._24 + VP._23;
	FrustrumPlanes[0].c = VP._34 + VP._33;
	FrustrumPlanes[0].d = VP._44 + VP._43;
	D3DXPlaneNormalize(&FrustrumPlanes[0], &FrustrumPlanes[0]);

	// Calculate far plane of frustum.
	FrustrumPlanes[1].a = VP._14 - VP._13; 
	FrustrumPlanes[1].b = VP._24 - VP._23;
	FrustrumPlanes[1].c = VP._34 - VP._33;
	FrustrumPlanes[1].d = VP._44 - VP._43;
	D3DXPlaneNormalize(&FrustrumPlanes[1], &FrustrumPlanes[1]);

	// Calculate left plane of frustum.
	FrustrumPlanes[2].a = VP._14 + VP._11; 
	FrustrumPlanes[2].b = VP._24 + VP._21;
	FrustrumPlanes[2].c = VP._34 + VP._31;
	FrustrumPlanes[2].d = VP._44 + VP._41;
	D3DXPlaneNormalize(&FrustrumPlanes[2], &FrustrumPlanes[2]);

	// Calculate right plane of frustum.
	FrustrumPlanes[3].a = VP._14 - VP._11; 
	FrustrumPlanes[3].b = VP._24 - VP._21;
	FrustrumPlanes[3].c = VP._34 - VP._31;
	FrustrumPlanes[3].d = VP._44 - VP._41;
	D3DXPlaneNormalize(&FrustrumPlanes[3], &FrustrumPlanes[3]);

	// Calculate top plane of frustum.
	FrustrumPlanes[4].a = VP._14 - VP._12; 
	FrustrumPlanes[4].b = VP._24 - VP._22;
	FrustrumPlanes[4].c = VP._34 - VP._32;
	FrustrumPlanes[4].d = VP._44 - VP._42;
	D3DXPlaneNormalize(&FrustrumPlanes[4], &FrustrumPlanes[4]);

	// Calculate bottom plane of frustum.
	FrustrumPlanes[5].a = VP._14 + VP._12;
	FrustrumPlanes[5].b = VP._24 + VP._22;
	FrustrumPlanes[5].c = VP._34 + VP._32;
	FrustrumPlanes[5].d = VP._44 + VP._42;
	D3DXPlaneNormalize(&FrustrumPlanes[5], &FrustrumPlanes[5]);
	
	//Terrain
	for(int i = 0; i < this->terrains.size(); i++)
	{
		Terrain* terr = this->terrains.get(i);

		float scale = max(terr->GetScale().x, max(terr->GetScale().y, terr->GetScale().z));
		if(pe.FrustrumVsSphere(this->FrustrumPlanes, terr->GetBoundingSphere(), terr->GetWorldMatrix(), scale))
		{
			terr->SetCulled(false);
		}
		else
		{
			terr->SetCulled(true);
		}
	}

	//Static meshes
	for(int i = 0; i < this->objects.size(); i++)
	{
		StaticMesh* ms = this->objects.get(i);
		MaloW::Array<MeshStrip*>* strips = ms->GetStrips();
		for(int u = 0; u < strips->size(); u++)
		{
			MeshStrip* s = strips->get(u);
			float scale = max(ms->GetScaling().x, max(ms->GetScaling().y, ms->GetScaling().z));
			if(pe.FrustrumVsSphere(this->FrustrumPlanes, s->GetBoundingSphere(), ms->GetWorldMatrix(), scale))
			{
				s->SetCulled(false);
			}
			else
			{
				s->SetCulled(true);
			}
		}
	}

	//Animated meshes
	for(int i = 0; i < this->animations.size(); i++)
	{
		AnimatedMesh* ms = this->animations.get(i);
		MeshStrip* s = ms->GetStrips()->get(0);

		float scale = max(ms->GetScaling().x, max(ms->GetScaling().y, ms->GetScaling().z));
		if(pe.FrustrumVsSphere(this->FrustrumPlanes, s->GetBoundingSphere(), ms->GetWorldMatrix(), scale))
		{
			s->SetCulled(false);
		}
		else
		{
			s->SetCulled(true);
		}
	}
}
Ejemplo n.º 7
0
void Camera::ConstructViewFrustum(ViewFrustum& frustum)
{
	float zMinimum, r;
	D3DXMATRIX matrix;
		
	// Calculate the minimum Z distance in the frustum.
	zMinimum = -this->projectionMatrix._43 / this->projectionMatrix._33;
	r = this->projFar / (this->projFar - zMinimum);
	this->projectionMatrix._33 = r;
	this->projectionMatrix._43 = -r * zMinimum;

	// Create the frustum matrix from the view matrix and updated projection matrix.
	D3DXMatrixMultiply(&matrix, &this->viewMatrix, &this->projectionMatrix);

	// Calculate near plane of frustum.
	frustum.planes[0].a = matrix._14 + matrix._13;
	frustum.planes[0].b = matrix._24 + matrix._23;
	frustum.planes[0].c = matrix._34 + matrix._33;
	frustum.planes[0].d = matrix._44 + matrix._43;
	D3DXPlaneNormalize(&frustum.planes[0], &frustum.planes[0]);

	// Calculate far plane of frustum.
	frustum.planes[1].a = matrix._14 - matrix._13; 
	frustum.planes[1].b = matrix._24 - matrix._23;
	frustum.planes[1].c = matrix._34 - matrix._33;
	frustum.planes[1].d = matrix._44 - matrix._43;
	D3DXPlaneNormalize(&frustum.planes[1], &frustum.planes[1]);

	// Calculate left plane of frustum.
	frustum.planes[2].a = matrix._14 + matrix._11; 
	frustum.planes[2].b = matrix._24 + matrix._21;
	frustum.planes[2].c = matrix._34 + matrix._31;
	frustum.planes[2].d = matrix._44 + matrix._41;
	D3DXPlaneNormalize(&frustum.planes[2], &frustum.planes[2]);

	// Calculate right plane of frustum.
	frustum.planes[3].a = matrix._14 - matrix._11; 
	frustum.planes[3].b = matrix._24 - matrix._21;
	frustum.planes[3].c = matrix._34 - matrix._31;
	frustum.planes[3].d = matrix._44 - matrix._41;
	D3DXPlaneNormalize(&frustum.planes[3], &frustum.planes[3]);

	// Calculate top plane of frustum.
	frustum.planes[4].a = matrix._14 - matrix._12; 
	frustum.planes[4].b = matrix._24 - matrix._22;
	frustum.planes[4].c = matrix._34 - matrix._32;
	frustum.planes[4].d = matrix._44 - matrix._42;
	D3DXPlaneNormalize(&frustum.planes[4], &frustum.planes[4]);

	// Calculate bottom plane of frustum.
	frustum.planes[5].a = matrix._14 + matrix._12;
	frustum.planes[5].b = matrix._24 + matrix._22;
	frustum.planes[5].c = matrix._34 + matrix._32;
	frustum.planes[5].d = matrix._44 + matrix._42;
	D3DXPlaneNormalize(&frustum.planes[5], &frustum.planes[5]);

	//Calculate sphere around frustum for faster culling
	float length = this->projFar - this->projNear;
	float height = length*tan((float)D3DX_PI*0.5f*0.5f);
	float width = height*(800/600);

	D3DXVECTOR3 p(0.0f,0.0f, 1+length*0.5f);
	D3DXVECTOR3 q(width, height, length);

	D3DXVECTOR3 vDiff(p-q);

	frustum.sphere.radius = D3DXVec3Length(&vDiff);
	frustum.sphere.center = D3DXVECTOR3(this->positionX, this->positionY, this->positionZ) + (D3DXVECTOR3(this->GetLookAt().x - this->positionX, this->GetLookAt().y - this->positionY, this->GetLookAt().z - this->positionZ)*(length*0.5f));  
}