-
Notifications
You must be signed in to change notification settings - Fork 0
/
Orbit.cpp
162 lines (132 loc) · 3.45 KB
/
Orbit.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
155
156
157
158
159
160
161
162
// Copyright 2020 Jefferson Amstutz
// SPDX-License-Identifier: Unlicense
#include "Orbit.h"
// Helper functions ///////////////////////////////////////////////////////////
static glm::vec3 azelToDirection(float az, float el, OrbitAxis axis)
{
const float x = std::sin(az) * std::cos(el);
const float y = std::cos(az) * std::cos(el);
const float z = std::sin(el);
switch (axis) {
case OrbitAxis::POS_X:
return -normalize(glm::vec3(z, y, x));
case OrbitAxis::POS_Y:
return -normalize(glm::vec3(x, z, y));
case OrbitAxis::POS_Z:
return -normalize(glm::vec3(x, y, z));
case OrbitAxis::NEG_X:
return normalize(glm::vec3(z, y, x));
case OrbitAxis::NEG_Y:
return normalize(glm::vec3(x, z, y));
case OrbitAxis::NEG_Z:
return normalize(glm::vec3(x, y, z));
}
return {};
}
static OrbitAxis negateAxis(OrbitAxis current)
{
switch (current) {
case OrbitAxis::POS_X:
return OrbitAxis::NEG_X;
case OrbitAxis::POS_Y:
return OrbitAxis::NEG_Y;
case OrbitAxis::POS_Z:
return OrbitAxis::NEG_Z;
case OrbitAxis::NEG_X:
return OrbitAxis::POS_X;
case OrbitAxis::NEG_Y:
return OrbitAxis::POS_Y;
case OrbitAxis::NEG_Z:
return OrbitAxis::POS_Z;
}
return {};
}
static float maintainUnitCircle(float inDegrees)
{
while (inDegrees > 360.f)
inDegrees -= 360.f;
while (inDegrees < 0.f)
inDegrees += 360.f;
return inDegrees;
}
// Orbit definitions //////////////////////////////////////////////////////////
Orbit::Orbit(glm::vec3 at, float dist, glm::vec2 azel)
: m_at(at), m_distance(dist), m_azel(azel)
{
m_speed = m_distance;
update();
}
void Orbit::startNewRotation()
{
m_invertRotation = m_azel.y > 90.f && m_azel.y < 270.f;
}
void Orbit::rotate(glm::vec2 delta)
{
delta *= 100;
delta.x = m_invertRotation ? -delta.x : delta.x;
delta.y = m_distance < 0.f ? delta.y : -delta.y;
m_azel += delta;
m_azel.x = maintainUnitCircle(m_azel.x);
m_azel.y = maintainUnitCircle(m_azel.y);
update();
}
void Orbit::zoom(float delta)
{
m_distance += m_speed * delta;
update();
}
void Orbit::pan(glm::vec2 delta)
{
delta *= m_speed;
const glm::vec3 amount = delta.x * m_right + delta.y * m_up;
m_eye += amount;
m_at += amount;
update();
}
void Orbit::setAxis(OrbitAxis axis)
{
m_axis = axis;
update();
}
glm::vec2 Orbit::azel() const
{
return m_azel;
}
glm::vec3 Orbit::eye() const
{
return m_eye;
}
glm::vec3 Orbit::dir() const
{
return glm::normalize(m_at - m_eye);
}
glm::vec3 Orbit::up() const
{
return m_up;
}
float Orbit::distance() const
{
return m_distance;
}
glm::vec3 Orbit::eye_FixedDistance() const
{
return m_eyeFixedDistance;
}
void Orbit::update()
{
const float distance = std::abs(m_distance);
const OrbitAxis axis = m_distance < 0.f ? negateAxis(m_axis) : m_axis;
const float azimuth = glm::radians(m_azel.x);
const float elevation = glm::radians(m_azel.y);
const glm::vec3 toLocalOrbit = azelToDirection(azimuth, elevation, axis);
const glm::vec3 localOrbitPos = toLocalOrbit * distance;
const glm::vec3 fromLocalOrbit = -localOrbitPos;
const glm::vec3 alteredElevation =
azelToDirection(azimuth, elevation + 3, m_axis);
const glm::vec3 cameraRight = glm::cross(toLocalOrbit, alteredElevation);
const glm::vec3 cameraUp = glm::cross(cameraRight, fromLocalOrbit);
m_eye = localOrbitPos + m_at;
m_up = glm::normalize(cameraUp);
m_right = glm::normalize(cameraRight);
m_eyeFixedDistance = (toLocalOrbit * m_originalDistance) + m_at;
}