-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCamera.h
148 lines (120 loc) · 3.64 KB
/
Camera.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
//***************************************************************************************
// Camera.h by X_Jun(MKXJun) (C) 2018-2020 All Rights Reserved.
// Licensed under the MIT License.
//
// 提供第一人称(自由视角)和第三人称摄像机
// Provide 1st person(free view) and 3rd person cameras.
//***************************************************************************************
#ifndef CAMERA_H
#define CAMERA_H
#include <d3d11_1.h>
#include <DirectXMath.h>
#include "Transform.h"
class Camera
{
public:
Camera() = default;
virtual ~Camera() = 0;
//
// 获取摄像机位置
//
DirectX::XMVECTOR GetPositionXM() const;
DirectX::XMFLOAT3 GetPosition() const;
//
// 获取摄像机旋转
//
// 获取绕X轴旋转的欧拉角弧度
float GetRotationX() const;
// 获取绕Y轴旋转的欧拉角弧度
float GetRotationY() const;
//
// 获取摄像机的坐标轴向量
//
DirectX::XMVECTOR GetRightAxisXM() const;
DirectX::XMFLOAT3 GetRightAxis() const;
DirectX::XMVECTOR GetUpAxisXM() const;
DirectX::XMFLOAT3 GetUpAxis() const;
DirectX::XMVECTOR GetLookAxisXM() const;
DirectX::XMFLOAT3 GetLookAxis() const;
//
// 获取矩阵
//
DirectX::XMMATRIX GetViewXM() const;
DirectX::XMMATRIX GetProjXM() const;
DirectX::XMMATRIX GetViewProjXM() const;
// 获取视口
D3D11_VIEWPORT GetViewPort() const;
// 设置视锥体
void SetFrustum(float fovY, float aspect, float nearZ, float farZ);
// 设置视口
void SetViewPort(const D3D11_VIEWPORT& viewPort);
void SetViewPort(float topLeftX, float topLeftY, float width, float height, float minDepth = 0.0f, float maxDepth = 1.0f);
protected:
// 摄像机的变换
Transform m_Transform = {};
// 视锥体属性
float m_NearZ = 0.0f;
float m_FarZ = 0.0f;
float m_Aspect = 0.0f;
float m_FovY = 0.0f;
// 当前视口
D3D11_VIEWPORT m_ViewPort = {};
};
class FirstPersonCamera : public Camera
{
public:
FirstPersonCamera() = default;
~FirstPersonCamera() override;
// 设置摄像机位置
void SetPosition(float x, float y, float z);
void SetPosition(const DirectX::XMFLOAT3& pos);
// 设置摄像机的朝向
void LookAt(const DirectX::XMFLOAT3& pos, const DirectX::XMFLOAT3& target,const DirectX::XMFLOAT3& up);
void LookTo(const DirectX::XMFLOAT3& pos, const DirectX::XMFLOAT3& to, const DirectX::XMFLOAT3& up);
// 平移
void Strafe(float d);
// 直行(平面移动)
void Walk(float d);
// 前进(朝前向移动)
void MoveForward(float d);
// 上下观察
// 正rad值向上观察
// 负rad值向下观察
void Pitch(float rad);
// 左右观察
// 正rad值向右观察
// 负rad值向左观察
void RotateY(float rad);
};
class ThirdPersonCamera : public Camera
{
public:
ThirdPersonCamera() = default;
~ThirdPersonCamera() override;
// 获取当前跟踪物体的位置
DirectX::XMFLOAT3 GetTargetPosition() const;
// 获取与物体的距离
float GetDistance() const;
// 绕物体垂直旋转(注意绕x轴旋转欧拉角弧度限制在[0, pi/3])
void RotateX(float rad);
// 绕物体水平旋转
void RotateY(float rad);
// 拉近物体
void Approach(float dist);
// 设置初始绕X轴的弧度(注意绕x轴旋转欧拉角弧度限制在[0, pi/3])
void SetRotationX(float rad);
// 设置初始绕Y轴的弧度
void SetRotationY(float rad);
// 设置并绑定待跟踪物体的位置
void SetTarget(const DirectX::XMFLOAT3& target);
// 设置初始距离
void SetDistance(float dist);
// 设置最小最大允许距离
void SetDistanceMinMax(float minDist, float maxDist);
private:
DirectX::XMFLOAT3 m_Target = {};
float m_Distance = 0.0f;
// 最小允许距离,最大允许距离
float m_MinDist = 0.0f, m_MaxDist = 0.0f;
};
#endif