-
Notifications
You must be signed in to change notification settings - Fork 89
/
Copy pathcamera.cpp
39 lines (32 loc) · 1.11 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
#include "camera.h"
Camera::Camera()
{
position = Vec3d(0.f, 0.f, 0.f);
direction = Vec3d(0.f, 0.f, 1.f);
forwardVelocity = Vec3d(0.f, 0.f, 0.f);
yaw = 0.f;
pitch = 0.f;
}
void Camera::rotateYaw(const float& angle)
{
this->yaw += angle;
}
void Camera::rotatePitch(const float& angle)
{
this->pitch += angle;
}
Vec3d Camera::lookAtTarget()
{
Mat4 cameraPitchRotation = Mat4::rotateX(this->pitch);
Mat4 cameraYawRotation = Mat4::rotateY(this->yaw);
// create a camera rotation matrix based on Yaw and Pitch
Mat4 cameraRotation = Mat4::eye();
cameraRotation = cameraPitchRotation * cameraRotation;
cameraRotation = cameraYawRotation * cameraRotation;
// update camera direction based on the rotation
Vec3d target(0, 0, 1); // target is looking at the positive Z-axis
this->direction = Vec4d::toVec3d( Vec3d::toVec4d(target) * cameraRotation );
// offset the camera position in the direction where the camera is looking at
target = this->position + this->direction;
return target;
}