-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcamera.h
138 lines (105 loc) · 2.99 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
/**************************************************************************
* D3D Ex. 13b
*
* Camera class
*
**************************************************************************/
#pragma once
#include <math.h>
#include <d3dx9.h>
class Camera
{
public:
Camera(D3DXVECTOR3 pos, float pitch, float yaw, float roll);
void rotate(float dPitch, float dYaw, float dRoll);
void moveUp(float scale);
void moveDown(float scale);
void moveLeft(float scale);
void moveRight(float scale);
void moveForward(float scale);
void moveBackward(float scale);
void setView(IDirect3DDevice9 * pDevice);
void setViewOrientation(IDirect3DDevice9 * pDevice, D3DXVECTOR3 pos);
void setViewBehind(IDirect3DDevice9 * pDevice, float scale);
// public for easy access
float pitch, yaw, roll;
D3DXVECTOR3 pos;
D3DXVECTOR3 dir, right, up;
};
Camera::Camera(D3DXVECTOR3 pos, float pitch, float yaw, float roll)
{
this->pitch = pitch;
this->yaw = yaw;
this->roll = roll;
this->pos = pos;
rotate(0, 0, 0);
}
void Camera::rotate(float dPitch, float dYaw, float dRoll)
{
this->pitch += dPitch;
this->yaw += dYaw;
this->roll += dRoll;
const float HALF_PI = 0.99f * (3.1415f / 2.0f);
if (pitch > HALF_PI)
pitch = HALF_PI;
if (pitch < -HALF_PI)
pitch = -HALF_PI;
D3DXMATRIX matY, matX, matZ, matRot;
D3DXMatrixRotationY( &matY, yaw );
D3DXMatrixRotationX( &matX, pitch );
D3DXMatrixRotationZ( &matZ, roll );
matRot = matY * matX * matZ;
dir = D3DXVECTOR3( matRot._13, matRot._23, matRot._33 );
D3DXMATRIX view;
D3DXMatrixLookAtLH( &view, &pos, &(pos + dir), &D3DXVECTOR3( 0, 1, 0 ) );
right = D3DXVECTOR3( view._11, view._21, view._31 );
up = D3DXVECTOR3( view._12, view._22, view._32 );
dir = D3DXVECTOR3( view._13, view._23, view._33 );
}
void Camera::moveLeft(float scale = 1.0f)
{
pos -= scale * right;
}
void Camera::moveRight(float scale = 1.0f)
{
pos += scale * right;
}
void Camera::moveForward(float scale = 1.0f)
{
pos += scale * dir;
}
void Camera::moveBackward(float scale = 1.0f)
{
pos -= scale * dir;
}
void Camera::moveUp(float scale = 1.0f)
{
pos += scale * up;
}
void Camera::moveDown(float scale = 1.0f)
{
pos -= scale * up;
}
void Camera::setView(IDirect3DDevice9 * pDevice)
{
D3DXMATRIX view;
D3DXMatrixLookAtLH( &view, &pos, &(pos + dir), &up );
pDevice->SetTransform( D3DTS_VIEW, &view );
}
void Camera::setViewOrientation(IDirect3DDevice9 * pDevice, D3DXVECTOR3 pos = D3DXVECTOR3(0, 0, 0))
{
D3DXMATRIX view;
D3DXMatrixLookAtLH( &view, &pos, &(pos + dir), &up );
pDevice->SetTransform( D3DTS_VIEW, &view );
}
void Camera::setViewBehind(IDirect3DDevice9 * pDevice, float scale = 10.0f)
{
D3DXMATRIX view;
D3DXMatrixLookAtLH( &view, &pos, &(pos + dir), &up );
right = D3DXVECTOR3( view._11, view._21, view._31 );
up = D3DXVECTOR3( view._12, view._22, view._32 );
dir = D3DXVECTOR3( view._13, view._23, view._33 );
D3DXVECTOR3 behind = pos - (scale * dir);
D3DXMatrixLookAtLH( &view, &behind, &pos, &up );
pDevice->SetTransform( D3DTS_VIEW, &view );
}