-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCamera.cpp
More file actions
executable file
·237 lines (187 loc) · 4.63 KB
/
Camera.cpp
File metadata and controls
executable file
·237 lines (187 loc) · 4.63 KB
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
#include "Camera.h"
Camera::Camera():mPosition(0.0f,0.0f,0.0f),mRight(1.0f,0.0f,0.0f),mUp(0.0f,1.0f,0.0f),mLook(0.0f,0.0f,1.0f)
{
SetLens(0.25f*MathHelper::Pi,1.0f,1.0f,1000.0f);
}
Camera::~Camera(void)
{
}
XMVECTOR Camera::GetPositionXM()const
{
return XMLoadFloat3(&mPosition);
}
XMFLOAT3 Camera::GetPosition()const
{
return mPosition;
}
void Camera::SetPosition(float x, float y, float z)
{
mPosition = XMFLOAT3(x,y,z);
}
void Camera::SetPosition(const XMFLOAT3& v)
{
mPosition = v;
}
XMVECTOR Camera::GetRightXM()const
{
return XMLoadFloat3(&mRight);
}
XMVECTOR Camera::GetUpXM()const
{
return XMLoadFloat3(&mUp);
}
XMFLOAT3 Camera::GetUp()const
{
return mUp;
}
XMVECTOR Camera::GetLookXM()const
{
return XMLoadFloat3(&mLook);
}
XMFLOAT3 Camera::GetLook()const
{
return mLook;
}
float Camera::GetNearZ()const
{
return mNearZ;
}
float Camera::GetFarZ()const
{
return mFarZ;
}
float Camera::GetAspect()const
{
return mAspect;
}
float Camera::GetFovY()const
{
return mFovY;
}
float Camera::GetFovX()const
{
float halfWidth = 0.5f*GetNearWindowWidth();
return 2.0f*atan(halfWidth/mNearZ);
}
float Camera::GetNearWindowWidth()const
{
return mAspect*mNearWindowHeight;
}
float Camera::GetNearWindowHeight()const
{
return mNearWindowHeight;
}
float Camera::GetFarWindowWidth()const
{
return mAspect*mFarWindowHeight;
}
float Camera::GetFarWindowHeight()const
{
return mFarWindowHeight;
}
void Camera::SetLens(float fovY,float aspect,float zn,float zf)
{
mFovY = fovY;
mAspect = aspect;
mNearZ = zn;
mFarZ = zf;
mNearWindowHeight = 2.0f*mNearZ*tanf(0.5f*mFovY);
mFarWindowHeight = 2.0f*mFarZ*tanf(0.5f*mFovY);
//投影矩阵需要的四个参数
XMMATRIX P = XMMatrixPerspectiveFovLH(mFovY,mAspect,mNearZ,mFarZ);
XMStoreFloat4x4(&mProj,P);
}
void Camera::LookAt(FXMVECTOR pos, FXMVECTOR target, FXMVECTOR worldUp)
{
XMVECTOR L = XMVector3Normalize(XMVectorSubtract(target,pos));
XMVECTOR R = XMVector3Normalize(XMVector3Cross(worldUp,L));
XMVECTOR U = XMVector3Cross(L,R);
XMStoreFloat3(&mPosition,pos);
XMStoreFloat3(&mLook,L);
XMStoreFloat3(&mRight,R);
XMStoreFloat3(&mUp,U);
}
void Camera::LookAt(const XMFLOAT3& pos,const XMFLOAT3& target, const XMFLOAT3& up)
{
XMVECTOR P = XMLoadFloat3(&pos);
XMVECTOR T = XMLoadFloat3(&target);
XMVECTOR U = XMLoadFloat3(&up);
LookAt(P,T,U);
}
XMMATRIX Camera::View()const
{
return XMLoadFloat4x4(&mView);
}
XMMATRIX Camera::Proj()const
{
return XMLoadFloat4x4(&mProj);
}
XMMATRIX Camera::ViewProj()const
{
return XMMatrixMultiply(View(),Proj());
}
//横向移动镜头
void Camera::Strafe(float d)
{
// mPosition += d*mRight
XMVECTOR s = XMVectorReplicate(d);
XMVECTOR r = XMLoadFloat3(&mRight);
XMVECTOR p = XMLoadFloat3(&mPosition);
XMStoreFloat3(&mPosition,XMVectorMultiplyAdd(s,r,p));
}
void Camera::Walk(float d)
{
// mPosition += d*mLook
XMVECTOR s = XMVectorReplicate(d);
XMVECTOR l = XMLoadFloat3(&mLook);
XMVECTOR p = XMLoadFloat3(&mPosition);
XMStoreFloat3(&mPosition,XMVectorMultiplyAdd(s,l,p));
}
void Camera::Pitch(float angle)
{
//根据right向量旋转up和look向量
XMMATRIX R = XMMatrixRotationAxis(XMLoadFloat3(&mRight),angle);
XMStoreFloat3(&mUp,XMVector3TransformNormal(XMLoadFloat3(&mUp),R));
XMStoreFloat3(&mLook, XMVector3TransformNormal(XMLoadFloat3(&mLook),R));
}
void Camera::RotateY(float angle)
{
XMMATRIX R = XMMatrixRotationY(angle);
XMStoreFloat3(&mRight,XMVector3TransformNormal(XMLoadFloat3(&mRight),R));
XMStoreFloat3(&mUp,XMVector3TransformNormal(XMLoadFloat3(&mLook),R));
XMStoreFloat3(&mLook,XMVector3TransformNormal(XMLoadFloat3(&mLook),R));
}
void Camera::UpdateViewMatrix()
{
XMVECTOR R = XMLoadFloat3(&mRight);
XMVECTOR U = XMLoadFloat3(&mUp);
XMVECTOR L = XMLoadFloat3(&mLook);
XMVECTOR P = XMLoadFloat3(&mPosition);
//保持镜头的各个轴都垂直,并且是单位长度
L = XMVector3Normalize(L);
U = XMVector3Normalize(XMVector3Cross(L,R));
R = XMVector3Cross(U,L);
//获得视图矩阵的原点
float x = -XMVectorGetX(XMVector3Dot(P,R));
float y = -XMVectorGetX(XMVector3Dot(P,U));
float z = -XMVectorGetX(XMVector3Dot(P,L));
XMStoreFloat3(&mRight,R);
XMStoreFloat3(&mUp,U);
XMStoreFloat3(&mLook,L);
mView(0,0) = mRight.x;
mView(1,0) = mRight.y;
mView(2,0) = mRight.z;
mView(3,0) = x;
mView(0,1) = mUp.x;
mView(1,1) = mUp.y;
mView(2,1) = mUp.z;
mView(3,1) = y;
mView(0,2) = mLook.x;
mView(1,2) = mLook.y;
mView(2,2) = mLook.z;
mView(3,2) = z;
mView(0,3) = 0.0f;
mView(1,3) = 0.0f;
mView(2,3) = 0.0f;
mView(3,3) = 1.0f;
}