-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcamera.cpp
More file actions
423 lines (353 loc) · 11.8 KB
/
camera.cpp
File metadata and controls
423 lines (353 loc) · 11.8 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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
/*==========================================================================================================================================================
カメラの処理[camera.cpp]
Author:大宮愛羅 平澤詩苑 石原颯馬
============================================================================================================================================================*/
#include "camera.h"
#include "input.h"
#include "PvP_player.h"
#include "hdr_player.h"
#include "camera_frame.h"
#include "input.h"
//注視点情報
#define POSR_SPEED (10.0f) //注視点の移動速度
#define POSR_WIDTH (0.0f) //注視点の幅
#define POSR_HEIGHT (0.0f) //注視点の高さ
#define POSR_DEPTH (0.0f) //注視点の奥行き
//視点情報
#define POSV_ROTSPEED (0.05f) //視点の回転速度
#define POSV_SPEED (10.0f) //視点の移動速度
#define POSR_ADD (8.0f) //注視点の上下移動
#define POSV_DISTANCE (100.0f) //視点の距離
#define POSV_WIDTH (0.0f) //視点の幅
#define POSV_HEIGHT (1050.0f) //視点の高さ
#define POSV_DEPTH (1000.0f) //視点の奥行き
//上方向ベクトル情報
#define VECU_OVER (D3DXVECTOR3(0.0f, 1.0f, 0.0f)) //上方向ベクトル
//カメラ情報
#define ANGLE_OF_VIEW (45.0f) //画角
//描画範囲
#define MIN_DRAW (10.0f) //最小描画範囲
#define MAX_DRAW (40000.0f) //最大描画範囲
//位置の差分を2乗
#define DIFF_TIMES (2.0f) //2乗
//カメラ単体の場合の視点座標
#define ALONE_CAMERA_POS (D3DXVECTOR3(0.0f, 400.0f, 500.0f))
//3人称視点時のマクロ
#define TPS_LENGTH (150.0f) //注視点~視点間の距離
#define TPS_posV_HEIGHT (100.0f) //視点の高さ
#define TPS_posR_HEIGHT (30.0f) //注視点の高さ
//グローバル変数
Camera g_Camera[NUM_CAMERA]; //カメラの情報
NumCamera g_NumCameraType; //カメラ分割の情報
//=========================================
//カメラの位置設定処理
//Author:石原颯馬
//=========================================
void InitSetCameraPos(D3DXVECTOR3 posV, D3DXVECTOR3 posR, int nNumCamera)
{
//設定
g_Camera[nNumCamera].posV = posV; //視点
g_Camera[nNumCamera].posR = posR; //注視点
//上方向ベクトルだけ固定
g_Camera[nNumCamera].vecU = VECU_OVER; //上方向ベクトル
//それぞれの位置の差分を格納する変数
float PosDiffX, PosDiffZ;
PosDiffX = powf(g_Camera[nNumCamera].posR.x - g_Camera[nNumCamera].posV.x, DIFF_TIMES); //2乗
PosDiffZ = powf(g_Camera[nNumCamera].posR.z - g_Camera[nNumCamera].posV.z, DIFF_TIMES); //2乗
//長さの算出
g_Camera[nNumCamera].fLength = sqrtf(PosDiffX + PosDiffZ);
//視点の位置更新
UpdatePosVCamera(nNumCamera);
}
//=========================================
//カメラの初期化処理
//=========================================
void InitCamera(NumCamera type)
{
//基本情報の初期化処理
for (int nCntCamera = 0; nCntCamera < NUM_CAMERA; nCntCamera++)
{
g_Camera[nCntCamera].rot = ZERO_SET;
}
Set_NumCamera(type); //カメラの数によるカメラ情報の初期化
}
//=========================================
//カメラの終了処理
//=========================================
void UninitCamera(void)
{
}
//=========================================
//カメラの更新処理
//=========================================
void UpdateCamera(void)
{
for (int nCntCamera = 0; nCntCamera < NUM_CAMERA; nCntCamera++)
{
//カメラが使われている
if (g_Camera[nCntCamera].bUse == true)
{
//カメラの移動処理
MoveCamera(nCntCamera);
}
}
}
//=========================================
//カメラの設定処理
//=========================================
void SetCamera(int nIdx)
{
//カメラが使われている
if (g_Camera[nIdx].bUse == true)
{
LPDIRECT3DDEVICE9 pDevice = GetDevice();
//ビューポートの設定
pDevice->SetViewport(&g_Camera[nIdx].viewport);
//プロジェクションマトリックスの初期化
D3DXMatrixIdentity(&g_Camera[nIdx].mtxProjection);
//プロジェクションマトリックスの作成
D3DXMatrixPerspectiveFovLH(&g_Camera[nIdx].mtxProjection, D3DXToRadian(ANGLE_OF_VIEW), (float)g_Camera[nIdx].viewport.Width / (float)g_Camera[nIdx].viewport.Height, MIN_DRAW, g_Camera[nIdx].fMaxLength);
//プロジェクションマトリックスの設定
pDevice->SetTransform(D3DTS_PROJECTION, &g_Camera[nIdx].mtxProjection);
//ビューマトリックスの初期化
D3DXMatrixIdentity(&g_Camera[nIdx].mtxView);
//ビューマトリックスの作成
D3DXMatrixLookAtLH(&g_Camera[nIdx].mtxView, &g_Camera[nIdx].posV, &g_Camera[nIdx].posR, &g_Camera[nIdx].vecU);
//ビューマトリックスの設定
pDevice->SetTransform(D3DTS_VIEW, &g_Camera[nIdx].mtxView);
}
}
//=========================================
//カメラの台数別 設定処理
//=========================================
void Set_NumCamera(NumCamera type)
{
int nCntCamera = 0; //カウンター初期化
Player *pPlayer = GetPlayer(); //プレイヤーの情報取得
g_NumCameraType = type; //カメラの分割情報格納
switch (type)
{
/*----------------------------------------------------------------
俯瞰で全体が見える視点の処理
----------------------------------------------------------------*/
case NumCamera_ONLY:
{
g_Camera[nCntCamera].posV = ALONE_CAMERA_POS; //視点座標初期化
g_Camera[nCntCamera].posR = ZERO_SET; //注視点座標初期化
g_Camera[nCntCamera].fMaxLength = MAX_DRAW; //最大描画距離設定
g_Camera[nCntCamera].viewport.X = (DWORD)0.0f; //原点Ⅹ位置代入
g_Camera[nCntCamera].viewport.Y = (DWORD)0.0f; //原点Y位置代入
g_Camera[nCntCamera].viewport.Width = SCREEN_WIDTH; //画面幅初期化
g_Camera[nCntCamera].viewport.Height = SCREEN_HEIGHT; //画面高さ初期化
g_Camera[nCntCamera].viewport.MinZ = 0.0f;
g_Camera[nCntCamera].viewport.MaxZ = 1.0f;
g_Camera[nCntCamera].bUse = true;
}
break;
/*----------------------------------------------------------------
横2分割の視点の処理
----------------------------------------------------------------*/
case NumCamera_HALF_SIDE:
{
for (nCntCamera; nCntCamera < NUM_CAMERA_HALF; nCntCamera++)
{
g_Camera[nCntCamera].fMaxLength = MAX_DRAW; //最大描画距離設定
g_Camera[nCntCamera].viewport.X = (DWORD)(SCREEN_WIDTH / 2) * (nCntCamera % 2); //原点Ⅹ位置代入
g_Camera[nCntCamera].viewport.Y = (DWORD)NIL_F; //原点Y位置代入
g_Camera[nCntCamera].viewport.Width = SCREEN_WIDTH / 2; //画面幅初期化
g_Camera[nCntCamera].viewport.Height = SCREEN_HEIGHT; //画面高さ初期化
g_Camera[nCntCamera].viewport.MinZ = 0.0f;
g_Camera[nCntCamera].viewport.MaxZ = 1.0f;
g_Camera[nCntCamera].bUse = true;
//注視点の位置更新
UpdatePosVCamera(nCntCamera);
}
}
break;
/*----------------------------------------------------------------
縦2分割の視点の処理
----------------------------------------------------------------*/
case NumCamera_HALF_HIGH_row:
{
for (nCntCamera; nCntCamera < NUM_CAMERA_HALF; nCntCamera++)
{
g_Camera[nCntCamera].fMaxLength = MAX_DRAW; //最大描画距離設定
g_Camera[nCntCamera].viewport.X = (DWORD)NIL_F; //原点Ⅹ位置代入
g_Camera[nCntCamera].viewport.Y = (DWORD)(SCREEN_HEIGHT / 2) * (nCntCamera % 2); //原点Y位置代入
g_Camera[nCntCamera].viewport.Width = SCREEN_WIDTH; //画面幅初期化
g_Camera[nCntCamera].viewport.Height = SCREEN_HEIGHT / 2; //画面高さ初期化
g_Camera[nCntCamera].viewport.MinZ = 0.0f;
g_Camera[nCntCamera].viewport.MaxZ = 1.0f;
g_Camera[nCntCamera].bUse = true;
//注視点の位置更新
UpdatePosVCamera(nCntCamera);
}
}
break;
/*----------------------------------------------------------------
各プレイヤー専用のカメラの処理
----------------------------------------------------------------*/
case NumCamera_FOUR_Separate:
{
for (nCntCamera; nCntCamera < NUM_CAMERA; nCntCamera++)
{
g_Camera[nCntCamera].fMaxLength = MAX_DRAW; //最大描画距離設定
g_Camera[nCntCamera].viewport.X = (SCREEN_WIDTH / 2) * (nCntCamera % 2); //原点Ⅹ位置代入
g_Camera[nCntCamera].viewport.Y = (SCREEN_HEIGHT / 2) * (nCntCamera / 2); //原点Y位置代入
g_Camera[nCntCamera].viewport.Width = SCREEN_WIDTH / 2; //画面幅初期化
g_Camera[nCntCamera].viewport.Height = SCREEN_HEIGHT / 2; //画面高さ初期化
g_Camera[nCntCamera].viewport.MinZ = 0.0f;
g_Camera[nCntCamera].viewport.MaxZ = 1.0f;
g_Camera[nCntCamera].bUse = true;
//注視点の位置更新
UpdatePosVCamera(nCntCamera);
}
}
break;
/*----------------------------------------------------------------
各プレイヤー専用のカメラの処理
----------------------------------------------------------------*/
case NumCamera_FOUR_SIDE:
{
for (nCntCamera; nCntCamera < NUM_CAMERA; nCntCamera++)
{
g_Camera[nCntCamera].fMaxLength = MAX_DRAW; //最大描画距離設定
g_Camera[nCntCamera].viewport.X = (DWORD)(SCREEN_WIDTH / 4) * nCntCamera; //原点Ⅹ位置代入
g_Camera[nCntCamera].viewport.Y = (DWORD)NIL_F; //原点Y位置代入
g_Camera[nCntCamera].viewport.Width = SCREEN_WIDTH / 4; //画面幅初期化
g_Camera[nCntCamera].viewport.Height = SCREEN_HEIGHT; //画面高さ初期化
g_Camera[nCntCamera].viewport.MinZ = 0.0f;
g_Camera[nCntCamera].viewport.MaxZ = 1.0f;
g_Camera[nCntCamera].bUse = true;
//注視点の位置更新
UpdatePosVCamera(nCntCamera);
}
}
}
//画面分割の枠を設定
SetUseFrame(type);
//各カメラの設定
for (int nCntUse = 0; nCntUse < NUM_CAMERA; nCntUse++)
{
//上のスイッチ文で使われることになったカメラ
if (nCntUse <= nCntCamera)
{
//注視点の位置設定
SetPosRCamera(nCntCamera);
//視点の位置設定
UpdatePosVCamera(nCntUse);
}
//設定されたカメラ以外のカメラを使用していないようにする
else
{
g_Camera[nCntUse].bUse = false;
}
}
}
//カメラの移動処理
void MoveCamera(int nCntCamera)
{
#ifdef _DEBUG
//----------------------
// 視点の移動
//----------------------
//視点の上下
if (GetKeyboardPress(DIK_T) == true)
{
g_Camera[nCntCamera].posV.y += POSV_SPEED;
}
if (GetKeyboardPress(DIK_B) == true)
{
g_Camera[nCntCamera].posV.y -= POSV_SPEED;
}
//視点の前後
if (GetKeyboardPress(DIK_N) == true)
{
g_Camera[nCntCamera].fLength += POSV_SPEED;
}
if (GetKeyboardPress(DIK_Y) == true)
{
g_Camera[nCntCamera].fLength -= POSV_SPEED;
}
//視点の左右
if (GetKeyboardPress(DIK_Z) == true)
{
g_Camera[nCntCamera].rot.y -= POSV_ROTSPEED;
}
if (GetKeyboardPress(DIK_C) == true)
{
g_Camera[nCntCamera].rot.y += POSV_ROTSPEED;
}
//------------------------
// 注視点の移動
//------------------------
//注視点の上下
if (GetKeyboardPress(DIK_I) == true)
{
g_Camera[nCntCamera].posR.y += POSR_ADD;
}
if (GetKeyboardPress(DIK_K) == true)
{
g_Camera[nCntCamera].posR.y -= POSR_ADD;
}
#endif // _DEBUG
//注視点の位置設定
SetPosRCamera(nCntCamera);
//視点カメラ更新
UpdatePosVCamera(nCntCamera);
}
//3人称視点
void TPS_ChaseCamera(int nCntCamera, D3DXVECTOR3 rot)
{
//プレイヤーの角度の逆に設定
g_Camera[nCntCamera].rot.y = (D3DX_PI - rot.y);
//角度修正
FIX_ROT(g_Camera[nCntCamera].rot.y);
//視点の高さ設定
g_Camera[nCntCamera].posV.y = g_Camera[nCntCamera].posR.y + TPS_posV_HEIGHT;
g_Camera[nCntCamera].posR.y += TPS_posR_HEIGHT;
//注視点~視点 間の距離設定
g_Camera[nCntCamera].fLength = TPS_LENGTH;
//視点位置更新
UpdatePosVCamera(nCntCamera);
}
//視点の位置更新
void UpdatePosVCamera(int nCntCamera)
{
//視点の位置更新
g_Camera[nCntCamera].posV.x = g_Camera[nCntCamera].posR.x + sinf(D3DX_PI - g_Camera[nCntCamera].rot.y) * g_Camera[nCntCamera].fLength;
g_Camera[nCntCamera].posV.z = g_Camera[nCntCamera].posR.z + cosf(D3DX_PI - g_Camera[nCntCamera].rot.y) * g_Camera[nCntCamera].fLength;
}
//注視点の位置設定
void SetPosRCamera(int nCntCamera)
{
//プレイヤー情報取得
Player *pPlayer = GetPlayer();
//対象のプレイヤーに注視点を合わせる
g_Camera[nCntCamera].posR = pPlayer[nCntCamera].pos;
//3人称視点設定
TPS_ChaseCamera(nCntCamera, pPlayer[nCntCamera].rot);
}
//カメラの取得
Camera *GetCamera(void)
{
return &g_Camera[0];
}
//*********************************************
//タイトル用カメラ処理
//MEMO : カメラは1番目のカメラのみを使用する
//*********************************************
void CameraForTitle(void)
{
//カメラの向きY のポインタを取得
float *pRot_Y = &g_Camera[0].rot.y;
//回転
*pRot_Y += 0.04f;
//向きが3.14を超えた
if (D3DX_PI <= *pRot_Y)
{
FIX_ROT(*pRot_Y);
}
//視点の位置更新
UpdatePosVCamera(0);
}