-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathscm-state.cpp
415 lines (310 loc) · 9.25 KB
/
scm-state.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
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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
// Copyright (C) 2011-2012 Robert Kooima
//
// LIBSCM is free software; you can redistribute it and/or modify it under the
// terms of the GNU General Public License as published by the Free Software
// Foundation; either version 2 of the License, or (at your option) any later
// version.
//
// This program is distributed in the hope that it will be useful, but WITH-
// OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
// more details.
#include <cmath>
#include <cassert>
#include <cstdlib>
#include "GL/glew.h"
#include "util3d/math3d.h"
#include "scm-state.hpp"
#include "scm-scene.hpp"
//------------------------------------------------------------------------------
/// Initialize a new SCM viewer state using default values.
scm_state::scm_state()
{
foreground0 = 0;
foreground1 = 0;
background0 = 0;
background1 = 0;
orientation[0] = 0.0;
orientation[1] = 0.0;
orientation[2] = 0.0;
orientation[3] = 1.0;
position[0] = 0.0;
position[1] = 0.0;
position[2] = 1.0;
light[0] = 0.0;
light[1] = 2.0;
light[2] = 1.0;
distance = 0.0;
zoom = 1.0;
fade = 0.0;
vnormalize(light, light);
}
/// Initialize a new SCM viewer state as a copy of the given state.
scm_state::scm_state(const scm_state& a)
{
name = a.name;
foreground0 = a.foreground0;
foreground1 = a.foreground1;
background0 = a.background0;
background1 = a.background1;
distance = a.distance;
zoom = a.zoom;
fade = a.fade;
qcpy(orientation, a.orientation);
vcpy(position, a.position);
vcpy(light, a.light);
}
/// Initialize a new SCM viewer state using linear interpolation of given states.
scm_state::scm_state(const scm_state& a, const scm_state& b, double t)
{
foreground0 = a.foreground0;
foreground1 = b.foreground0;
background0 = a.background0;
background1 = b.background0;
distance = lerp(a.distance, b.distance, t);
zoom = lerp(a.zoom, b.zoom, t);
fade = lerp(a.fade, b.fade, t);
qslerp(orientation, a.orientation, b.orientation, t);
vslerp(position, a.position, b.position, t);
vslerp(light, a.light, b.light, t);
qnormalize(orientation, orientation);
vnormalize(position, position);
vnormalize(light, light);
}
/// Initialize a new SCM viewer state using the given camera configuration. position, camera
/// orientation, and lightsource orientation.
///
/// @param t Camera position (3D vector)
/// @param r Camera orientation (Euler angles)
/// @param l Light direction (Euler angles)
scm_state::scm_state(const double *t, const double *r, const double *l)
{
assert(t);
assert(r);
assert(l);
double M[16];
foreground0 = 0;
foreground1 = 0;
background0 = 0;
background1 = 0;
qeuler(orientation, r);
meuler(M, l);
vnormalize(light, M + 8);
vnormalize(position, t);
distance = vlen(t);
zoom = 1.0;
fade = 0.0;
}
//------------------------------------------------------------------------------
/// Return the orientation quaternion.
void scm_state::get_orientation(double *q) const
{
qcpy(q, orientation);
}
/// Return the position vector.
void scm_state::get_position(double *v) const
{
vcpy(v, position);
}
/// Return the light direction vector.
void scm_state::get_light(double *v) const
{
vcpy(v, light);
}
//------------------------------------------------------------------------------
/// Set the name of the state.
void scm_state::set_name(const std::string& s)
{
name = s;
}
/// Set the starting foreground scene.
void scm_state::set_foreground0(scm_scene *s)
{
foreground0 = s;
}
/// Set the ending foreground scene.
void scm_state::set_foreground1(scm_scene *s)
{
foreground1 = s;
}
/// Set the starting background scene.
void scm_state::set_background0(scm_scene *s)
{
background0 = s;
}
/// Set the ending background scene.
void scm_state::set_background1(scm_scene *s)
{
background1 = s;
}
/// Set the orientation quaternion.
void scm_state::set_orientation(const double *q)
{
qnormalize(orientation, q);
}
/// Set the position vector.
void scm_state::set_position(const double *v)
{
vnormalize(position, v);
}
/// Set the light direction vector.
void scm_state::set_light(const double *v)
{
vnormalize(light, v);
}
/// Set the distance of the camera from the center of the sphere.
void scm_state::set_distance(double d)
{
distance = d;
}
/// Set the camera zoom.
void scm_state::set_zoom(double z)
{
zoom = z;
}
/// Set the transition progress.
void scm_state::set_fade(double f)
{
fade = f;
}
//------------------------------------------------------------------------------
/// Return the view transformation matrix.
void scm_state::get_matrix(double *M) const
{
vquaternionx(M + 0, orientation);
vquaterniony(M + 4, orientation);
vquaternionz(M + 8, orientation);
vcpy(M + 12, position);
M[13] *= distance;
M[14] *= distance;
M[12] *= distance;
M[ 3] = 0.0;
M[ 7] = 0.0;
M[11] = 0.0;
M[15] = 1.0;
}
/// Return the Y axis of the matrix form of the orientation quaternion, thus
/// giving the view up vector.
void scm_state::get_up(double *v) const
{
vquaterniony(v, orientation);
}
/// Return the X axis of the matrix form of the orientation quaternion, thus
/// giving the view right vector.
void scm_state::get_right(double *v) const
{
vquaternionx(v, orientation);
}
/// Return the negated Z axis of the matrix form of the orientation quaternion,
/// thus giving the view forward vector.
void scm_state::get_forward(double *v) const
{
vquaternionz(v, orientation);
vneg(v, v);
}
//------------------------------------------------------------------------------
/// Return the ground level of current scene at the given location. O(log n).
/// This may incur data access in the render thread.
float scm_state::get_current_ground() const
{
if (foreground0 && foreground1)
return std::max(foreground0->get_current_ground(position),
foreground1->get_current_ground(position));
if (foreground0)
return foreground0->get_current_ground(position);
if (foreground1)
return foreground1->get_current_ground(position);
return 1.f;
}
/// Return the minimum ground level of the current scene, e.g. the radius of
/// the planet at the bottom of the deepest valley. O(1).
float scm_state::get_minimum_ground() const
{
if (foreground0 && foreground1)
return std::min(foreground0->get_minimum_ground(),
foreground1->get_minimum_ground());
if (foreground0)
return foreground0->get_minimum_ground();
if (foreground1)
return foreground1->get_minimum_ground();
return 1.f;
}
//------------------------------------------------------------------------------
/// Reorient the view to the given pitch in radians
void scm_state::set_pitch(double a)
{
double r[3];
double p[3];
double u[3];
double b[3];
double R[16];
// Get the position and right vectors.
vnormalize (p, position);
vquaternionx(r, orientation);
// Make certain the right vector is perpendicular.
vcrs(b, r, p);
vnormalize(b, b);
vcrs(r, p, b);
// Pitch around the right vector and build a basis.
mrotate (R, r, a);
vtransform(u, R, p);
vnormalize(u, u);
vcrs (b, r, u);
vnormalize(b, b);
mbasis (R, r, u, b);
// Convert the matrix to a new quaternion.
qmatrix (orientation, R);
qnormalize(orientation, orientation);
}
/// Set the camera position and orientation using the given view matrix
void scm_state::set_matrix(const double *M)
{
const double *p = M + 12;
qmatrix(orientation, M);
vnormalize(position, p);
distance = vlen(p);
}
//------------------------------------------------------------------------------
/// Transform the current camera orientation
///
/// @param M Transformation matrix in OpenGL column-major order.
void scm_state::transform_orientation(const double *M)
{
double A[16];
double B[16];
mquaternion(A, orientation);
mmultiply(B, M, A);
qmatrix(orientation, B);
qnormalize(orientation, orientation);
}
/// Transform the current camera position
///
/// @param M Transformation matrix in OpenGL column-major order.
void scm_state::transform_position(const double *M)
{
double v[3];
vtransform(v, M, position);
vnormalize(position, v);
}
/// Transform the current light direction
///
/// @param M Transformation matrix in OpenGL column-major order.
void scm_state::transform_light(const double *M)
{
double v[3];
vtransform(v, M, light);
vnormalize(light, v);
}
//------------------------------------------------------------------------------
/// Return the linear distance between two states
double operator-(const scm_state& a, const scm_state& b)
{
double u[3];
double v[3];
double w[3];
vmul(u, a.position, a.distance);
vmul(v, b.position, b.distance);
vsub(w, u, v);
return vlen(w);
}
//------------------------------------------------------------------------------