-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcamera.cpp
More file actions
174 lines (126 loc) · 4.93 KB
/
camera.cpp
File metadata and controls
174 lines (126 loc) · 4.93 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
#include "camera.h"
#include "input.h"
#include <iostream>
glm::mat4 camera::getMatrix()
{
glm::mat4 rtrn(1.0f);
rtrn = glm::translate(rtrn, position);
rtrn = glm::rotate(rtrn, glm::radians(yaw), glm::vec3(0,1,0));
rtrn = glm::rotate(rtrn, glm::radians(pitch), glm::vec3(1, 0, 0));
rtrn = glm::rotate(rtrn, glm::radians(roll), glm::vec3(0, 0, 1));
return rtrn;
}
//glm::normalize
void camera::processInputs(input inp)
{
//if (inp.getKey_e())
//{
// roll -= 1.0f;
//}
//if (inp.getKey_q())
//{
// roll += 1.0f;
//}
// mouse inputs and rotation
if (inp.getKey_f())
{
float xoff = -inp.getMouseDelta().x * mouseSensitivity;
float yoff = -inp.getMouseDelta().y * mouseSensitivity;
yaw += xoff; pitch += yoff;
glm::vec3 frnt;
frnt.x = cos(glm::radians(yaw)) * cos(glm::radians(pitch));
frnt.y = sin(glm::radians(pitch));
frnt.z = sin(glm::radians(yaw)) * cos(glm::radians(pitch));
front = glm::normalize(frnt);
right = glm::cross(frnt, up);
glm::mat4 CameraModel(1.0f);
CameraModel = glm::rotate(CameraModel, glm::radians(yaw), glm::vec3(0, 1, 0));
CameraModel = glm::rotate(CameraModel, glm::radians(pitch), glm::vec3(1, 0, 0));
CameraModel = glm::rotate(CameraModel, glm::radians(roll), glm::vec3(0, 0, 1));
front = glm::vec3(CameraModel * glm::vec4(0.0f, 0.0f, -1.0f, 1.0f));
right = glm::vec3(CameraModel * glm::vec4(1.0f, 0.0f, 0.0f, 1.0f));
up = glm::vec3(CameraModel * glm::vec4(0.0f, 1.0f, 0.0f, 1.0f));
// keyboard inputs and position
if (inp.getKey_w())
{
position += front * cameraSpeed;
}
if (inp.getKey_s())
{
position += -(front * cameraSpeed);
}
if (inp.getKey_a())
{
position += -(right * cameraSpeed);
}
if (inp.getKey_d())
{
position += right * cameraSpeed;
}
if (inp.getKey_space())
{
position += up * cameraSpeed;
}
if (inp.getKey_lControl())
{
position += -(up * cameraSpeed);
}
}
}
void camera::setTransform(glm::vec3 _position, glm::vec3 _rotation)
{
position = _position;
yaw = _rotation.x;
pitch = _rotation.y;
roll = _rotation.z;
}
/*glm::mat4 view(1.0f);
float xoffset = -inp.getMouseDelta().x;
float yoffset = inp.getMouseDelta().y;
xoffset *= mouseSensitivity;
yoffset *= mouseSensitivity;
yaw += xoffset;
pitch += yoffset;
glm::vec3 tmpdirection;
tmpdirection.x = cos(glm::radians(yaw)) * cos(glm::radians(pitch));
tmpdirection.y = sin(glm::radians(pitch));
tmpdirection.z = sin(glm::radians(yaw)) * cos(glm::radians(pitch));
glm::vec3 forward = glm::normalize(tmpdirection);
glm::vec3 direction = glm::vec3(cos(glm::radians(yaw)) , 0 , sin(glm::radians(yaw)));
glm::vec3 right = glm::cross(direction, glm::vec3(0.0f,1.0f,0.0f));
glm::vec3 up = glm::cross(right, direction);
if (inp.getKey_w()) { position += glm::normalize(right) * cameraSpeed; }
else if (inp.getKey_s()) { position += glm::normalize(right) * -cameraSpeed; }
else if (inp.getKey_a()) { position += direction * cameraSpeed; }
else if (inp.getKey_d()) { position += direction * -cameraSpeed; }
else if (inp.getKey_space()) { position += glm::normalize(up) * -cameraSpeed; }
else if (inp.getKey_lControl()) { position += glm::normalize(up) * cameraSpeed; }
view = glm::lookAt(position, position + forward, up);*/
/*glm::vec3 forward = rotation;
glm::vec3 right = glm::cross(forward, glm::vec3(0.0f, 1.0f, 0.0f));
glm::vec3 up = glm::cross(forward, right);*/
/*view = glm::rotate(view, glm::radians(rotation.x), glm::vec3(0,1,0) );
view = glm::rotate(view, glm::radians(-rotation.y), glm::vec3(1, 0, 0) );
if (inp.getKey_w()) { position += glm::normalize(right) * cameraSpeed; }
else if (inp.getKey_s()) { position += glm::normalize(right) * -cameraSpeed; }
else if (inp.getKey_a()) { position += glm::normalize(forward) * cameraSpeed; }
else if (inp.getKey_d()) { position += glm::normalize(forward) * -cameraSpeed; }
else if (inp.getKey_space()) { position += glm::normalize(up) * -cameraSpeed; }
else if (inp.getKey_lControl()) { position += glm::normalize(up) * cameraSpeed; }
view = glm::translate(view, position);*/
/*glm::vec3 direction = glm::vec3(
glm::cos(glm::radians(rotation.x)) * glm::cos(glm::radians(rotation.y)),
glm::sin(glm::radians(rotation.y)),
glm::sin(glm::radians(rotation.x)) * glm::cos(glm::radians(rotation.y)));
glm::vec3 forward = glm::normalize(direction);
glm::vec3 right = glm::cross(forward, glm::vec3(0.0f,1.0f,0.0f));
glm::vec3 up = glm::cross(forward, right);
if (inp.getKey_w()) { position += glm::normalize(forward) * cameraSpeed; }
else if (inp.getKey_s()) { position += -glm::normalize(forward) * cameraSpeed; }
else if (inp.getKey_a()) { position += -glm::normalize(right) * cameraSpeed; }
else if (inp.getKey_d()) { position += glm::normalize(right) * cameraSpeed; }
else if (inp.getKey_space()) { position += glm::normalize(up) * cameraSpeed; }
else if (inp.getKey_lControl()) { position += -glm::normalize(up) *cameraSpeed; }
view = glm::lookAt(position, position + forward, up);
*/
//return view;