-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDebugCamera.cpp
More file actions
96 lines (80 loc) · 2.64 KB
/
Copy pathDebugCamera.cpp
File metadata and controls
96 lines (80 loc) · 2.64 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
// デバッグ用カメラクラス
#include "DebugCamera.h"
using namespace DirectX;
using namespace DirectX::SimpleMath;
const float DebugCamera::DEFAULT_CAMERA_DISTANCE = 5.0f;
// コンストラクタ
DebugCamera::DebugCamera(int w, int h)
: m_yAngle(0.0f), m_yTmp(0.0f), m_xAngle(0.0f), m_xTmp(0.0f), m_x(0), m_y(0), m_scrollWheelValue(0)
{
// 画面サイズに対する相対的なスケールに調整
m_sx = 1.0f / (float)w;
m_sy = 1.0f / (float)h;
m_view = DirectX::SimpleMath::Matrix::Identity;
// マウスの初期化
mouse.reset(new Mouse);
mouseTracker.reset(new Mouse::ButtonStateTracker);
}
//--------------------------------------------------------------------------------------
// 更新
//--------------------------------------------------------------------------------------
void DebugCamera::Update()
{
// マウス情報を取得
mouseState = mouse->GetState();
mouseTracker->Update(mouseState);
// マウスの左ボタンが押された
if (mouseTracker->leftButton == Mouse::ButtonStateTracker::ButtonState::PRESSED)
{
// マウスの座標を取得
m_x = mouseState.x;
m_y = mouseState.y;
}
else if (mouseTracker->leftButton == Mouse::ButtonStateTracker::ButtonState::RELEASED)
{
// 現在の回転を保存
m_xAngle = m_xTmp;
m_yAngle = m_yTmp;
}
// マウスのボタンが押されていたらカメラを移動させる
if (mouseState.leftButton)
{
Motion(mouseState.x, mouseState.y);
}
// マウスのフォイール値を取得
m_scrollWheelValue = mouseState.scrollWheelValue;
if (m_scrollWheelValue > 0)
{
m_scrollWheelValue = 0;
mouse->ResetScrollWheelValue();
}
// ビュー行列を算出する
Matrix rotY = Matrix::CreateRotationY(m_yTmp);
Matrix rotX = Matrix::CreateRotationX(m_xTmp);
Matrix rt = rotY * rotX;
Vector3 eye(0.0f, 0.0f, 1.0f);
Vector3 target(0.0f, 0.0f, 0.0f);
Vector3 up(0.0f, 1.0f, 0.0f);
eye = Vector3::Transform(eye, rt.Invert());
eye *= (DEFAULT_CAMERA_DISTANCE - m_scrollWheelValue / 100);
up = Vector3::Transform(up, rt.Invert());
m_view = Matrix::CreateLookAt(eye, target, up);
}
//--------------------------------------------------------------------------------------
// 行列の生成
//--------------------------------------------------------------------------------------
void DebugCamera::Motion(int x, int y)
{
// マウスポインタの位置のドラッグ開始位置からの変位 (相対値)
float dx = (x - m_x) * m_sx;
float dy = (y - m_y) * m_sy;
if (dx != 0.0f || dy != 0.0f)
{
// Y軸の回転
float yAngle = dx * XM_PI;
// X軸の回転
float xAngle = dy * XM_PI;
m_xTmp = m_xAngle + xAngle;
m_yTmp = m_yAngle + yAngle;
}
}