-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathCamera.cpp
More file actions
280 lines (250 loc) · 6.94 KB
/
Camera.cpp
File metadata and controls
280 lines (250 loc) · 6.94 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
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
/*
hale: support for minimalist scientific visualization
Copyright (C) 2014, 2015 University of Chicago
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software. Permission is granted to anyone to
use this software for any purpose, including commercial applications, and
to alter it and redistribute it freely, subject to the following
restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software in a
product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/
#include "Hale.h"
#include "privateHale.h"
namespace Hale {
Camera::Camera(glm::vec3 from, glm::vec3 at, glm::vec3 up, double fov, double aspect,
double clipNear, double clipFar, bool orthographic) {
// static const char me[]="Hale::Camera::Camera";
_verbose = 0;
_from = from;
_at = at;
_up = up;
_fov = AIR_CLAMP(fovMin / fovPerc, fov, fovMax * fovPerc);
_aspect = aspect;
_clipNear = clipNear;
_clipFar = clipFar;
_orthographic = false;
updateView();
updateProject();
}
int
Camera::verbose() {
return _verbose;
}
void
Camera::verbose(int vv) {
_verbose = vv;
}
void
Camera::init(glm::vec3 from, glm::vec3 at, glm::vec3 up, double fov, double aspect,
double clipNear, double clipFar, bool orthographic) {
_from = from;
_at = at;
_up = up;
_fov = AIR_CLAMP(fovMin / fovPerc, fov, fovMax * fovPerc);
_aspect = aspect;
_clipNear = clipNear;
_clipFar = clipFar;
_orthographic = orthographic;
updateView();
updateProject();
}
glm::vec3
Camera::from() {
return _from;
}
glm::vec3
Camera::at() {
return _at;
}
glm::vec3
Camera::up() {
return _up;
}
void
Camera::from(glm::vec3 ff) {
_from = ff;
updateView();
updateProject();
}
void
Camera::at(glm::vec3 aa) {
_at = aa;
updateView();
updateProject();
}
void
Camera::up(glm::vec3 uu) {
_up = uu;
updateView();
}
void
Camera::reup() {
glm::vec3 edir = glm::normalize(_from - _at);
_up = glm::normalize(_up - glm::dot(_up, edir) * edir);
updateView();
}
double
Camera::fov() {
return _fov;
}
double
Camera::aspect() {
return _aspect;
}
double
Camera::clipNear() {
return _clipNear;
}
double
Camera::clipFar() {
return _clipFar;
}
bool
Camera::orthographic() {
return _orthographic;
}
void
Camera::fov(double vv) {
_fov = AIR_CLAMP(fovMin, vv, fovMax);
updateProject();
}
void
Camera::aspect(double aa) {
_aspect = aa;
updateProject();
}
void
Camera::clipNear(double nn) {
_clipNear = nn;
updateProject();
}
void
Camera::clipFar(double ff) {
_clipFar = ff;
updateProject();
}
void
Camera::orthographic(bool ortho) {
_orthographic = ortho;
updateProject();
}
glm::mat4
Camera::view() {
return _view;
}
glm::mat4
Camera::viewInv() {
return _viewInv;
}
glm::mat4
Camera::project() {
return _project;
}
const float *
Camera::viewPtr() {
return glm::value_ptr(_view);
}
const float *
Camera::projectPtr() {
return glm::value_ptr(_project);
}
glm::vec3
Camera::U() {
return _uu;
}
glm::vec3
Camera::V() {
return _vv;
}
glm::vec3
Camera::N() {
return _nn;
}
void
Camera::updateView() {
// static const char me[]="Camera::updateView";
_view = glm::lookAt(_from, _at, _up);
_viewInv = glm::inverse(_view);
/* not actually needed, but these lines may help for disambiguation
and documentation; writing out the 16 elements of the value
pointer shows that "glm is column major" (by default):
vp[ 0] vp[ 4] vp[ 8] vp[12]
vp[ 1] vp[ 5] vp[ 9] vp[13]
vp[ 2] vp[ 6] vp[10] vp[14]
vp[ 3] vp[ 7] vp[11] vp[15]
const float *vp = glm::value_ptr(_view); */
/*
printf("!%s: M_view = \n", me);
ell_4m_print_f(stdout, glm::value_ptr(glm::transpose(_view)));
*/
/* N points *towards* eye, from look-at; same as if by
_nn = glm::vec3(_viewInv * glm::vec4(0, 0, 1, 0)); (3rd column) or
_nn = glm::vec3(glm::vec4(0, 0, 1, 0) * _view); (3rd row of _view) or
_nn = glm::vec3(vp[2], vp[6], vp[10]); (3rd row) */
_nn = glm::normalize(_from - _at);
/* U points towards right; same as if by
_uu = glm::vec3(_viewInv * glm::vec4(1, 0, 0, 0)); (1st column) or
_uu = glm::vec3(glm::vec4(1, 0, 0, 0) * _view); (1st row of _view) or
_uu = glm::vec3(vp[0], vp[4], vp[8]); (1st row of _view) */
_uu = glm::normalize(glm::cross(_up, _nn));
/* V is screen-space up; same as if by
_vv = glm::vec3(_viewInv * glm::vec4(0, 1, 0, 0)); (2nd column) or
_vv = glm::vec3(glm::vec4(0, 1, 0, 0) * _view); (2nd row or _view) or
_vv = glm::vec3(vp[1], vp[5], vp[9]); (2nd row of _view)
*/
_vv = glm::cross(_nn, _uu);
return;
}
void
Camera::updateProject() {
// static const char me[]="Camera::updateProject";
glm::vec3 diff = _at - _from;
double dist = glm::length(diff);
double vspNear = dist + _clipNear;
double vspFar = dist + _clipFar;
double fangle = _fov * AIR_PI / 360;
if (_orthographic) {
double vMax = dist * tan(fangle);
double uMax = _aspect * vMax;
_project = glm::ortho(-uMax, uMax, -vMax, vMax, vspNear, vspFar);
} else {
_project = glm::perspective(2 * fangle, _aspect, vspNear, vspFar);
/*
double hght = 2*vspNear * tan(fangle);
double wdth = _aspect*hght;
printf("!%s: 2n/w = %g, 2n/h = %g\n", me,
2*vspNear/wdth, 2*vspNear/hght);
printf("!%s: (f+n)/(f-n) = %g\n", me,
(vspFar+vspNear)/(vspFar-vspNear));
printf("!%s: -2fn/(f-n) = %g\n", me,
-2*vspFar*vspNear/(vspFar-vspNear));
*/
}
// printf("!%s: %s projection = \n", me, _orthographic ? "ortho" : "persp");
// ell_4m_print_f(stdout, glm::value_ptr(glm::transpose(_project)));
return;
}
#define V2S(vv) \
(sprintf(buff[0], "%g", vv[0]), sprintf(buff[1], "%g", vv[1]), \
sprintf(buff[2], "%g", vv[2]), \
std::string(buff[0]) + " " + std::string(buff[1]) + " " + std::string(buff[2]))
#define F2S(vv) (sprintf(buff[0], "%g", vv), std::string(buff[0]))
std::string
Camera::hest(void) {
char buff[3][32];
std::string ret;
ret = ("-fr " + V2S(_from) + " -at " + V2S(_at) + " -up " + V2S(_up) + " -nc "
+ F2S(_clipNear) + " -fc " + F2S(_clipFar) + " -fov " + F2S(_fov));
if (_orthographic) {
ret += " -ortho";
}
return ret;
}
} // namespace Hale