投影矩阵推导视锥体的6个面方程式

I've got a camera which is in targeted mode. This camera has a near clip plane distance of 1.0f

and far clip plane distance of 50000.0f

 

When I look at an object like this, the targeted object is culled, any ideas why?

m_Cam.ChangeTargetView(&D3DXVECTOR3(m_CharObject[5]->GetPos() + D3DXVECTOR3(0, 150, -50)),
                    &m_CharObject[5]->GetPos());
 
#include "CCamera.h"

CCamera::CCamera()
{
    m_vPos = D3DXVECTOR3(0.0f,0.0f,0.0f);    
    m_vVel = D3DXVECTOR3(0.0f,0.0f,0.0f);
    m_vLookAt = D3DXVECTOR3(0.0f,0.0f,0.0f);
    m_vLookAtVel = D3DXVECTOR3(0.0f,0.0f,0.0f);
    m_vUp = D3DXVECTOR3(0, 1, 0);
    m_vTarget = D3DXVECTOR3(0, 0, 0);
    D3DXMatrixIdentity(&m_matOrientation);
    m_bChangingView = false;
    m_fPosS        = 0.0f;
    m_fLookAtS    = 0.0f;
    m_fSpeed    = 2.0f; // half a second it reaches destination
    m_bFreeCamMode = true;
}

void CCamera::Create(float fAspect, float fFar)
{
    D3DXMatrixPerspectiveFovLH(&m_matProj,
        D3DX_PI/4, fAspect, 1.0f, fFar );
}

void CCamera::Update(float dt, bool bUpdateFrustum)
{
    FLOAT fTime;
    //dt = 0.01f;
    if( dt >= 0.0f )
        fTime = dt;
    else                      
        fTime = 0.01f;

    float fSpeed        = 3.0f * fTime;
    float fAngularSpeed = 0.1f * fTime;//1.0f*fTime;

    // Update the position vector
    D3DXVECTOR3 vT = m_vVel * fSpeed;
    D3DXVec3TransformNormal( &vT, &vT, &m_matOrientation );
    m_vPos += vT;

    // Update the yaw-pitch-rotation vector
    m_vLookAt += fAngularSpeed * m_vLookAtVel;

    if(m_bChangingView)
    {
        m_fPosS        += (m_fSpeed * dt);
        m_fLookAtS    += (m_fSpeed * dt);
        D3DXVec3Lerp(&m_vPos, &m_vStartPos, &m_vEndPos, m_fPosS);
        D3DXVec3Lerp(&m_vLookAt, &m_vStartLookAt, &m_vEndLookAt, m_fLookAtS);
        
        if(m_fPosS > 1.0f && m_fLookAtS > 1.0f)
            m_bChangingView = false;
    }
    //m_vPos = m_vStartPos;
    //m_vLookAt = m_vStartLookAt;

    // Set the view matrix
    D3DXQUATERNION qR;
    if (m_bFreeCamMode) {
        D3DXQuaternionRotationYawPitchRoll(&qR, m_vLookAt.x, m_vLookAt.y, m_vLookAt.z);
        D3DXMatrixAffineTransformation(&m_matOrientation, 1.25f, NULL, &qR, &m_vPos);
        D3DXMatrixInverse(&m_matView, NULL, &m_matOrientation);
        
    }
    else {
        //D3DXQuaternionRotationYawPitchRoll(&qR, m_vLookAt.x, m_vLookAt.y, m_vLookAt.z);
        //D3DXMatrixAffineTransformation(&m_matOrientation, 1.25f, NULL, &qR, &m_vPos );    
        D3DXMatrixLookAtLH(&m_matView, &m_vPos, &m_vTarget, &m_vUp);
    }
    

    // Decelerate only the camera's position velocity (for smooth motion)
    m_vVel       *= 0.0f;
    m_vLookAtVel *= 0.0f;

    // Deceleating the yaw/pitch/roll velocities works great for keyboard control,
    // but it gives me motion sickness when used with mouselook, so I commented it out
    // and replaced it with multiplication by zero, which disables the velocities.  
    // Your mileage may vary.

    if(bUpdateFrustum)
        UpdateFrustum();
}

void CCamera::UpdateFrustum()
{
    D3DXMATRIX Matrix;
    D3DXMatrixMultiply(&Matrix, &m_matView, &m_matProj);

    // Calculate the planes
    m_FrustumPlanes[0].a = Matrix._14 + Matrix._13; // Near plane
    m_FrustumPlanes[0].b = Matrix._24 + Matrix._23;
    m_FrustumPlanes[0].c = Matrix._34 + Matrix._33;
    m_FrustumPlanes[0].d = Matrix._44 + Matrix._43;
    D3DXPlaneNormalize(&m_FrustumPlanes[0], &m_FrustumPlanes[0]);

    m_FrustumPlanes[1].a = Matrix._14 - Matrix._13; // Far plane
    m_FrustumPlanes[1].b = Matrix._24 - Matrix._23;
    m_FrustumPlanes[1].c = Matrix._34 - Matrix._33;
    m_FrustumPlanes[1].d = Matrix._44 - Matrix._43;
    D3DXPlaneNormalize(&m_FrustumPlanes[1], &m_FrustumPlanes[1]);

    m_FrustumPlanes[2].a = Matrix._14 + Matrix._11; // Left plane
    m_FrustumPlanes[2].b = Matrix._24 + Matrix._21;
    m_FrustumPlanes[2].c = Matrix._34 + Matrix._31;
    m_FrustumPlanes[2].d = Matrix._44 + Matrix._41;
    D3DXPlaneNormalize(&m_FrustumPlanes[2], &m_FrustumPlanes[2]);

    m_FrustumPlanes[3].a = Matrix._14 - Matrix._11; // Right plane
    m_FrustumPlanes[3].b = Matrix._24 - Matrix._21;
    m_FrustumPlanes[3].c = Matrix._34 - Matrix._31;
    m_FrustumPlanes[3].d = Matrix._44 - Matrix._41;
    D3DXPlaneNormalize(&m_FrustumPlanes[3], &m_FrustumPlanes[3]);

    m_FrustumPlanes[4].a = Matrix._14 - Matrix._12; // Top plane
    m_FrustumPlanes[4].b = Matrix._24 - Matrix._22;
    m_FrustumPlanes[4].c = Matrix._34 - Matrix._32;
    m_FrustumPlanes[4].d = Matrix._44 - Matrix._42;
    D3DXPlaneNormalize(&m_FrustumPlanes[4], &m_FrustumPlanes[4]);

    m_FrustumPlanes[5].a = Matrix._14 + Matrix._12; // Bottom plane
    m_FrustumPlanes[5].b = Matrix._24 + Matrix._22;
    m_FrustumPlanes[5].c = Matrix._34 + Matrix._32;
    m_FrustumPlanes[5].d = Matrix._44 + Matrix._42;
    D3DXPlaneNormalize(&m_FrustumPlanes[5], &m_FrustumPlanes[5]);
}

void CCamera::SetView(D3DXVECTOR3 *vPos, D3DXVECTOR3 *vAt)
{
    m_vPos        = *vPos;
    m_vLookAt   = *vAt;
    m_bFreeCamMode = true;
}

void CCamera::SetTargetView(D3DXVECTOR3* vPos, D3DXVECTOR3* vTarget) {
    m_vPos        =    *vPos;
    m_vTarget    =    *vTarget;
    m_bFreeCamMode = false;
}

void CCamera::ChangeView(D3DXVECTOR3 *vPos, D3DXVECTOR3 *vAt)
{
    m_bChangingView     = true;
    m_vStartPos            = m_vPos;
    m_vStartLookAt        = m_vLookAt;
    m_vEndPos            = *vPos;
    m_vEndLookAt        = *vAt;    
    m_fPosS                = 0.0f;
    m_fLookAtS            = 0.0f;
    m_bFreeCamMode = true;
}

void CCamera::ChangeTargetView(D3DXVECTOR3* vPos, D3DXVECTOR3* vTarget) {
    m_bChangingView = true;
    m_vStartPos = m_vPos;
    m_vStartLookAt = m_vLookAt;
    m_vEndPos = *vPos;    
    m_vTarget = *vTarget;
    m_fPosS = 0.0f;
    m_fLookAtS = 0.0f;
    m_bFreeCamMode = false;
}

bool CCamera::CheckSphere(D3DXVECTOR3 *vCenter, float *fRadius)
{
  int i;

  for(i = 0; i < 6; i++)
    if(D3DXPlaneDotCoord(&m_FrustumPlanes[i], &D3DXVECTOR3(vCenter->x, vCenter->y, vCenter->z)) < -*fRadius)
      return false;  
  return true;
}  

posted on 2019-05-17 17:16  guanxi0808  阅读(610)  评论(0编辑  收藏  举报

导航