-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcamera.h
216 lines (172 loc) · 5.11 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
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
#pragma once
#include "vector.h"
#include "model.h"
enum CameraMovement
{
FORWARD,
BACKWARD,
LEFT,
RIGHT,
ZOOMIN,
ZOOMOUT,
WIRE,
PLAIN,
GOURAUD,
ROTATEX,
ROTATEY,
ROTATEZ,
};
// Default camera values
const float YAW = -90.0f;
const float PITCH = 0.0f;
const float SPEED = 100.0f;
const float SENSITIVITY = 0.02f;
const float ZOOM = 20.0f;
bool wireframe = false;
bool plainshade = false;
bool gouraudshade = false;
bool rotatex = false;
mat4x4 lookAt(vec4f eye, vec4f target, vec4f vUp);
class Camera
{
private:
// calculates the front vector from the Camera's (updated) Euler Angles
void updateCameraVectors();
public:
// camera Attributes
vec4f Position;
vec4f Front;
vec4f Up;
vec4f Right;
vec4f WorldUp;
float Yaw;
float Pitch;
float MovementSpeed;
float MouseSensitivity;
float Zoom;
Camera(vec4f position = vec4f{0, 0, 0}, vec4f up = vec4f{0, 1, 0}, float yaw = YAW, float pitch = PITCH);
Camera(float posX, float posY, float posZ, float upX, float upY, float upZ, float yaw, float pitch);
mat4x4 getViewMatrix();
void processKeyboard(CameraMovement direction, float deltaTime);
void processMouseMovement(float xoffset, float yoffset, bool constrainPitch = true);
// void processMouseScroll(float yoffset);
};
Camera::Camera(vec4f position, vec4f up, float yaw, float pitch) : Front(vec4f{0, 0, -1}), MovementSpeed(SPEED), MouseSensitivity(SENSITIVITY), Zoom(ZOOM)
{
Position = position;
WorldUp = up;
Yaw = yaw;
Pitch = pitch;
updateCameraVectors();
}
Camera::Camera(float posX, float posY, float posZ, float upX, float upY, float upZ, float yaw, float pitch) : Front(vec4f{0, 0, -1}), MovementSpeed(SPEED), MouseSensitivity(SENSITIVITY), Zoom(ZOOM)
{
std::cout << "Hello Camera 2\n";
Position = vec4f{posX, posY, posZ};
WorldUp = vec4f{upX, upY, upZ};
Yaw = yaw;
Pitch = pitch;
updateCameraVectors();
}
mat4x4 Camera::getViewMatrix()
{
return lookAt(Position, Position + Front, Up);
}
void Camera::processKeyboard(CameraMovement direction, float deltaTime)
{
float velocity = MovementSpeed * deltaTime;
if (direction == FORWARD)
Position = (Up * velocity) + Position;
if (direction == BACKWARD)
Position = (Up * velocity).inverse() + Position;
if (direction == LEFT)
Position = (Right * velocity).inverse() + Position;
if (direction == RIGHT)
Position = (Right * velocity) + Position;
if (direction == ZOOMIN)
{
// Zoom += MovementSpeed * deltaTime;
Zoom += 10 * deltaTime;
if (Zoom < 5.0f)
Zoom = 5.0f;
if (Zoom > 150.0f)
Zoom = 150.0f;
}
if (direction == ZOOMOUT)
{
// Zoom -= MovementSpeed * deltaTime;
Zoom -= 10 * deltaTime;
if (Zoom < 5.0f)
Zoom = 5.0f;
if (Zoom > 45.0f)
Zoom = 45.0f;
}
if (direction == WIRE)
{
wireframe = true;
plainshade = false;
gouraudshade =false;
}
if (direction == PLAIN )
{
plainshade = true;
wireframe = false;
gouraudshade = false;
}
if (direction == GOURAUD)
{
gouraudshade = true;
wireframe = false;
plainshade = false;
}
if (direction == ROTATEX)
{
rotatex = true;
}
}
void Camera::processMouseMovement(float xoffset, float yoffset, bool constrainPitch /*= true*/)
{
xoffset *= MouseSensitivity;
yoffset *= MouseSensitivity;
Yaw -= xoffset;
Pitch -= yoffset;
// make sure that when pitch is out of bounds, screen doesn't get flipped
if (constrainPitch)
{
if (Pitch > 89.0f)
Pitch = 89.0f;
if (Pitch < -89.0f)
Pitch = -89.0f;
}
// update Front, Right and Up Vectors using the updated Euler angles
updateCameraVectors();
}
void Camera::updateCameraVectors()
{
vec4f front;
float val = cos(converttoRad(Yaw));
// std::cout<<val;
front.x = cos(converttoRad(Yaw)) * cos(converttoRad(Pitch));
front.y = sin(converttoRad(Pitch));
front.z = sin(converttoRad(Yaw)) * cos(converttoRad(Pitch));
Front = front.normalize();
Right = (Front.crossProduct(WorldUp)).normalize();
// normalize the vectors, because their length gets closer to 0 the more you look up or down which results in slower movement.
Up = (Right.crossProduct(Front)).normalize();
}
//lOokat matrix//
mat4x4 lookAt(vec4f eye, vec4f target, vec4f vUp = {0, 1, 0})
{
// Calculate new forward direction
vec4f forward = (eye-target).normalize();
vec4f right = (vUp.crossProduct(forward)).normalize();
// vec4f right = vUp.normalize().crossProduct(forward);
// Calculate new Up direction
vec4f up = forward.crossProduct(right);
// vec4f c = target.inverse();
mat4x4 view = {{{right.x, right.y, right.z, -dotProduct(right, eye)},
{up.x, up.y, up.z, -dotProduct(up, eye)},
{forward.x, forward.y, forward.z, -dotProduct(forward, eye)},
{0, 0, 0, 1}}};
return view;
}