zfxCamera.cpp
资源名称:Direct3D.rar [点击查看]
上传用户:junlon
上传日期:2022-01-05
资源大小:39075k
文件大小:3k
源码类别:
DirextX编程
开发平台:
Visual C++
- #include "stdafx.h"
- #include "zfxCamera.h"
- CZFXCamera::CZFXCamera()
- {
- m_x = m_y = m_z = 0;
- m_pitchAngle = 0;
- m_yawAngle = 0;
- m_fovy = D3DX_PI/4;
- m_Aspect = 1;
- m_zn = 1;
- m_zf = 20;
- }
- CZFXCamera::~CZFXCamera()
- {
- }
- void CZFXCamera::SetPosX(float x)
- {
- m_x = x;
- }
- void CZFXCamera::SetPosY(float y)
- {
- m_y = y;
- }
- void CZFXCamera::SetPosZ(float z)
- {
- m_z = z;
- }
- // 设置摄像机的镜头属性
- void CZFXCamera::SetProjProp(float zn, float zf, float fovy, float Aspect)
- {
- m_zn = zn;
- m_zf = zf;
- m_fovy = fovy;
- m_Aspect = Aspect;
- }
- // 改变摄像机镜头的广角属性,默认为增大广角(increase == ture, 当该值设为false时为缩小广角)
- // 默认每次增大为1度(当然这个值也可通过变量increment设置)
- void CZFXCamera::Change_fovy(bool increase, float increment)
- {
- m_fovy += (increase?increment:-increment);
- }
- D3DXVECTOR3* CZFXCamera::GetLookVec(D3DXVECTOR3* lookAtVec)
- {
- float cosPitch = cos(m_pitchAngle);
- float sinPitch = sin(m_pitchAngle);
- float cosYaw = cos(m_yawAngle);
- float sinYaw = sin(m_yawAngle);
- lookAtVec->x = cosPitch * sinYaw;
- lookAtVec->y = sinPitch;
- lookAtVec->z = cosPitch * cosYaw;
- return lookAtVec;
- }
- // 获取摄像机的视口矩阵
- D3DXMATRIX* CZFXCamera::GetViewMatrix(D3DXMATRIX* pMatView)
- {
- /* D3DXMatrixLookAtLH Function in SDK interpretation
- zaxis = normal(At - Eye)
- xaxis = normal(cross(Up, zaxis))
- yaxis = cross(zaxis, xaxis)
- xaxis.x yaxis.x zaxis.x 0
- xaxis.y yaxis.y zaxis.y 0
- xaxis.z yaxis.z zaxis.z 0
- -dot(xaxis, eye) -dot(yaxis, eye) -dot(zaxis, eye) 1
- */
- D3DXVECTOR3 zAxis, up, xAxis, yAxis;
- float cosPitch = cos(m_pitchAngle);
- float sinPitch = sin(m_pitchAngle);
- float cosYaw = cos(m_yawAngle);
- float sinYaw = sin(m_yawAngle);
- // 构造zAxis,构造时已是单位向量
- zAxis.x = cosPitch * sinYaw;
- zAxis.y = sinPitch;
- zAxis.z = cosPitch * cosYaw;
- // 构造up,构造时已是单位向量
- up.x = -sinPitch * sinYaw;
- up.y = cosPitch;
- up.z = -sinPitch * cosYaw;
- // 构造xAxis和yAxis
- D3DXVec3Cross( &xAxis, &up, &zAxis );
- D3DXVec3Cross( &yAxis, &zAxis, &xAxis );
- // Build the view matrix:
- D3DXVECTOR3 pos( m_x, m_y, m_z );
- float x = -D3DXVec3Dot(&xAxis, &pos);
- float y = -D3DXVec3Dot(&yAxis, &pos);
- float z = -D3DXVec3Dot(&zAxis, &pos);
- (*pMatView)(0,0) = xAxis.x; (*pMatView)(0, 1) = yAxis.x; (*pMatView)(0, 2) = zAxis.x; (*pMatView)(0, 3) = 0.0f;
- (*pMatView)(1,0) = xAxis.y; (*pMatView)(1, 1) = yAxis.y; (*pMatView)(1, 2) = zAxis.y; (*pMatView)(1, 3) = 0.0f;
- (*pMatView)(2,0) = xAxis.z; (*pMatView)(2, 1) = yAxis.z; (*pMatView)(2, 2) = zAxis.z; (*pMatView)(2, 3) = 0.0f;
- (*pMatView)(3,0) = x; (*pMatView)(3, 1) = y; (*pMatView)(3, 2) = z; (*pMatView)(3, 3) = 1.0f;
- return pMatView;
- }
- // 获取摄像机的投影矩阵
- D3DXMATRIX* CZFXCamera::GetProjMatrix(D3DXMATRIX* pMatProj)
- {
- return D3DXMatrixPerspectiveFovLH(pMatProj, m_fovy, m_Aspect, m_zn, m_zf);;
- }
- // +z代表前进方向,+x代表向右方向
- void CZFXCamera::Walk(float x, float z)
- {
- float cosPitch = cos(m_pitchAngle);
- float sinPitch = sin(m_pitchAngle);
- float cosYaw = cos(m_yawAngle);
- float sinYaw = sin(m_yawAngle);
- m_z += z * cosPitch * cosYaw - x * cosPitch * sinYaw;
- m_x += z * cosPitch * sinYaw + x * cosPitch * cosYaw;
- m_y += z * sinPitch;
- }
- void CZFXCamera::Pitch(float fAngle)
- {
- m_pitchAngle += fAngle;
- }
- void CZFXCamera::Yaw(float fAngle)
- {
- m_yawAngle += fAngle;
- }