Repository navigation
Expand file tree
/
Copy pathcamera.cpp
More file actions
182 lines (158 loc) · 5.04 KB
/
Copy pathcamera.cpp
File metadata and controls
182 lines (158 loc) · 5.04 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
175
176
177
178
179
180
181
182
#include <assert.h>
#include <stdlib.h>
#include "camera.h"
#include "iostream"
#include "math.h"
/*
* for orbital camera controls, see
* https://github.com/mrdoob/three.js/blob/master/examples/js/controls/OrbitControls.js
*/
static const float NEAR = 0.1f;
static const float FAR = 10000;
static const float FOVY = TO_RADIANS(60);
static const Vector3f UP = { 0, 1, 0 };
Camera::Camera(Vector3f position, Vector3f target, float aspect) {
assert((position - target).normal() > EPSILON && aspect > 0);
this->position = position;
this->target = target;
this->aspect = aspect;
}
/* camera updating */
void Camera::set_transform(Vector3f position, Vector3f target) {
assert((position - target).normal() > EPSILON);
this->position = position;
this->target = target;
}
void Camera::set_aspect(float aspect) {
this->aspect = aspect;
}
static Vector3f calculate_pan(Vector3f from_camera, Motion motion) {
Vector3f _from_camera = from_camera;
Vector3f forward = _from_camera.normalize();
Vector3f left = cross(UP, forward);
Vector3f up = cross(forward, left);
float distance = from_camera.normal();
float factor = distance * (float)tan(FOVY / 2) * 2;
Vector3f delta_x = left * motion.pan.x * factor;
Vector3f delta_y = up * motion.pan.y * factor;
return delta_x + delta_y;
}
static Vector3f calculate_offset(Vector3f from_target, Motion motion) {
float radius = from_target.normal();
float theta = (float)atan2(from_target.x, from_target.z); /* azimuth */
float phi = (float)acos(from_target.y / radius); /* polar */
float factor = PI * 2;
Vector3f offset;
radius *= (float)pow(0.95, motion.dolly);
theta -= motion.orbit.x * factor;
phi -= motion.orbit.y * factor;
phi = clamp(phi, EPSILON, PI - EPSILON);
offset.x = radius * (float)sin(phi) * (float)sin(theta);
offset.y = radius * (float)cos(phi);
offset.z = radius * (float)sin(phi) * (float)cos(theta);
return offset;
}
void Camera::update_transform(Motion motion) {
Vector3f from_target = position - target;
Vector3f from_camera = target - position;
Vector3f pan = calculate_pan(from_camera, motion);
Vector3f offset = calculate_offset(from_target, motion);
target = target + pan;
position = target + offset;
}
/* property retrieving */
Vector3f Camera::get_position() {
return position;
}
Vector3f Camera::get_forward() {
return (target - position).normalize();
}
Matrix4x4 Camera::get_view_matrix() {
Vector3f up = Vector3f(UP.x, UP.y, UP.z);
Matrix4x4 m = lookat(position, target, up);
Matrix4x4 ret = Matrix4x4::identity();
return m;
}
Matrix4x4 Camera::get_proj_matrix() {
Matrix4x4 m = perspective(FOVY, aspect, NEAR, FAR);
return m;
}
Vector3f Camera::get_target_position() {
//return target;
return Vector3f::Zero;
}
Vector2f get_pos_delta(Vector2f old_pos, Vector2f new_pos, float window_height) {
Vector2f delta = new_pos - old_pos;
return delta / window_height;
}
Vector2f get_cursor_pos(window_t* window) {
float xpos, ypos;
input_query_cursor(window, &xpos, &ypos);
return Vector2f(xpos, ypos);
}
void scroll_callback(window_t* window, float offset) {
Record* record = (Record*)window_get_userdata(window);
record->dolly_delta += offset;
}
void button_callback(window_t* window, button_t button, int pressed) {
Record* record = (Record*)window_get_userdata(window);
Vector2f cursor_pos = get_cursor_pos(window);
if (button == BUTTON_L) {
float curr_time = platform_get_time();
if (pressed) {
record->is_orbiting = 1;
record->orbit_pos = cursor_pos;
record->press_time = curr_time;
record->press_pos = cursor_pos;
}
else {
float prev_time = record->release_time;
Vector2f pos_delta = get_pos_delta(record->orbit_pos, cursor_pos, record->window_height);
record->is_orbiting = 0;
record->orbit_delta = record->orbit_delta + pos_delta;
if (prev_time && curr_time - prev_time < CLICK_DELAY) {
record->double_click = 1;
record->release_time = 0;
}
else {
record->release_time = curr_time;
record->release_pos = cursor_pos;
}
}
}
else if (button == BUTTON_R) {
if (pressed) {
record->is_panning = 1;
record->pan_pos = cursor_pos;
}
else {
Vector2f pos_delta = get_pos_delta(record->pan_pos, cursor_pos, record->window_height);
record->is_panning = 0;
record->pan_delta = record->pan_delta + pos_delta;
}
}
}
void update_camera(window_t* window, Camera* camera,
Record* record) {
Vector2f cursor_pos = get_cursor_pos(window);
if (record->is_orbiting) {
Vector2f pos_delta = get_pos_delta(record->orbit_pos, cursor_pos, record->window_height);
record->orbit_delta = record->orbit_delta + pos_delta;
record->orbit_pos = cursor_pos;
}
if (record->is_panning) {
Vector2f pos_delta = get_pos_delta(record->pan_pos, cursor_pos, record->window_height);
record->pan_delta = record->pan_delta + pos_delta;
record->pan_pos = cursor_pos;
}
if (input_key_pressed(window, KEY_SPACE)) {
camera->set_transform(CAMERA_POSITION, CAMERA_TARGET);
}
else {
Motion motion;
motion.orbit = record->orbit_delta;
motion.pan = record->pan_delta;
motion.dolly = record->dolly_delta;
camera->update_transform(motion);
}
}