-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcamera.h
85 lines (75 loc) · 1.9 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
/**
* @file camera.h
* @author Jerry Z
* @brief Arcball camera cpp header class
* @version 0.1
* @date 2022-03-16
*
* An arcball camera for OpenGL
* Assuming the world space is in y up, z out, x right coordinate system.
* @copyright Copyright (c) 2022
*
*/
#ifndef CAMERA_H
#define CAMERA_H
#include "cyCodeBase/cyVector.h"
#define _USE_MATH_DEFINES
#include "math.h"
class Camera
{
public:
cy::Vec3f pos;
cy::Vec3f lookat;
cy::Vec3f up;
private:
float yaw; // Vertical angle in rad
float pitch; // Horizontal angle in rad
float sense = 0.02;
float r;
cy::Vec3f dir;
public:
Camera(cy::Vec3f pos, cy::Vec3f lookat, cy::Vec3f up)
{
this->pos = pos;
this->lookat = lookat;
this->up = up;
this->yaw = atan2(pos.y, sqrt(pow(pos.x, 2)+pow(pos.z, 2)));
this->pitch = atan2(pos.z, pos.x);
this->dir = (lookat-pos).GetNormalized();
this->r = (pos-lookat).Length();
}
void UpdatePosition(cy::Vec3f &p)
{
this->pos = p;
this->dir = (lookat-pos).GetNormalized();
this->r = (pos-lookat).Length();
}
void RotateHorizontal(float mag)
{
mag *= sense;
pitch += mag;
cy::Vec3f p(cos(yaw) * cos(pitch) * r,
sin(yaw) * r,
cos(yaw) * sin(pitch) * r);
UpdatePosition(p);
}
void RotateVertical(float mag)
{
mag *= sense;
if (abs(yaw+mag) < M_PI_2)
{
yaw += mag;
cy::Vec3f p(cos(yaw) * cos(pitch) * r,
sin(yaw) * r,
cos(yaw) * sin(pitch) * r);
UpdatePosition(p);
}
}
void Zoom(float mag)
{
mag *= sense;
cy::Vec3f p = pos + dir * mag;
UpdatePosition(p);
}
};
#endif