-
Notifications
You must be signed in to change notification settings - Fork 1
/
camera.cpp
157 lines (119 loc) · 3.24 KB
/
camera.cpp
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
/*
Author: Lucas Parzych
Implements First person camera with complete freedom of movement
WASD to move foreward/backwards and strafe left/right in relation to the look-at direction
mouse to change look-at direction
Not the most natural feeling thing in the world
*/
using namespace std;
class camera
{
public:
float yaw, pitch;
glm::vec3 move_signal;
float move_signal_speed;
float sensitivity;
float scroll_speed, scroll_boundary;
glm::vec3 position;
glm::vec3 direction;
glm::vec3 head;
glm::mat4 Model;
glm::mat4 View;
glm::mat4 Projection;
glm::mat4 MVP;
float fov_y;
camera(void);
void calculateMovementSpeed(void);
void setupMVP(void);
void update(void);
};
camera::camera(void)
{
yaw=-90.0f;
pitch=0.0f;
scroll_boundary=25;
sensitivity = 0.05f;
fov_y=45.0f;
float Z = window_height / (2 * tan(glm::radians(fov_y/2.0)));
position= glm::vec3(window_width/2, window_height/2, Z);
direction= glm::vec3(0,0,-1.0);
head = glm::vec3(0,1.0,0);
Model = glm::mat4(1.0f);
View = glm::mat4(1.0f);
Projection = glm::mat4(1.0f);
MVP = glm::mat4(1.0f);
move_signal=glm::vec3(0.0,0.0,0.0);
setupMVP();
}
void camera::setupMVP(void)
{
//setup view, projection and model matrices
//model
Model = glm::rotate(
Model,
glm::radians(-90.0f),
glm::vec3(1.0f, 0.0f, 0.0f)
);
//view
View = glm::lookAt(
position,
position + direction,
head
);
//projection
float aspect_ratio=(float) window_width / (float) window_height;
Projection = glm::perspective(glm::radians(fov_y), aspect_ratio, 0.1f, 10000.0f);
MVP = Projection * View * Model;
}
void camera::calculateMovementSpeed(void)
{
scroll_speed=((window_width + window_height)/2)/10;
move_signal_speed=scroll_speed;
}
void camera::update(void)
{
if(mouseX>window_width-scroll_boundary)
{
yaw += scroll_speed * sensitivity;
}
else if(mouseX < scroll_boundary)
{
yaw -= scroll_speed * sensitivity;
}
if(mouseY>window_height-scroll_boundary)
{
pitch += scroll_speed * sensitivity;
}
else if(mouseY < scroll_boundary)
{
pitch -= scroll_speed * sensitivity;
}
if(pitch > 89.0f)
{
pitch = 89.0f;
}
if(pitch < -89.0f)
{
pitch = -89.0f;
}
direction.x = cos(glm::radians(pitch)) * cos(glm::radians(yaw));
direction.y = sin(glm::radians(pitch));
direction.z = cos(glm::radians(pitch)) * sin(glm::radians(yaw));
direction = glm::normalize(direction);
if(move_signal.z!=0.0)
{
position -= (move_signal.z * move_signal_speed * direction);
}
if(move_signal.x!=0.0)
{
position -= (move_signal.x * move_signal_speed) * glm::normalize(glm::cross(head, direction));
}
View = glm::lookAt(
position,
position + direction,
head
);
MVP = Projection * View * Model;
matrixID = glGetUniformLocation(programID, "MVP");
glUniformMatrix4fv(matrixID, 1, GL_FALSE, &MVP[0][0]);
}