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;
- }
- 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::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;
- }
- void 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;
- }
- // +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;
- }