-
Notifications
You must be signed in to change notification settings - Fork 10
/
GLCamera.cpp
451 lines (370 loc) · 14.1 KB
/
GLCamera.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
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
//-*****************************************************************************
//
// Copyright (c) 2009-2010,
// Sony Pictures Imageworks, Inc. and
// Industrial Light & Magic, a division of Lucasfilm Entertainment Company Ltd.
//
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
// * Neither the name of Sony Pictures Imageworks, nor
// Industrial Light & Magic nor the names of their contributors may be used
// to endorse or promote products derived from this software without specific
// prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
//-*****************************************************************************
#include "GLCamera.h"
#include <cstdio>
#include <qgl.h>
#if defined(__APPLE__) && defined(__MACH__)
# include <OpenGL/glu.h>
#else
# include <GL/glu.h>
#endif
#include <assert.h>
#include <vector>
using namespace Imath;
//-*****************************************************************************
GLCamera::GLCamera()
: m_rotation( 0.0, 0.0, 0.0 ),
m_scale( 1.0, 1.0, 1.0 ),
m_translation( 0.0, 0.0, 0.0 ),
m_centerOfInterest( 15.0 ),
m_fovy( 45.0 ),
m_clip( 0.01, 1000 ),
m_size( 100, 100 ),
m_aspect( 1.0 )
{
// Nothing
}
//-*****************************************************************************
static inline void rotateVector( double rx, double ry, V3d &v )
{
rx = radians( rx );
const double sinX = sinf( rx );
const double cosX = cosf( rx );
const V3d t( v.x,
( v.y * cosX ) - ( v.z * sinX ),
( v.y * sinX ) + ( v.z * cosX ) );
ry = radians( ry );
const double sinY = sinf( ry );
const double cosY = cosf( ry );
v.x = ( t.x * cosY ) + ( t.z * sinY );
v.y = t.y;
v.z = ( t.x * -sinY ) + ( t.z * cosY );
}
//-*****************************************************************************
void GLCamera::frame( const Box3d &bounds )
{
// Center the camera properly
const V3d dt = m_translation - pointOfInterest();
const V3d look = bounds.center();
const V3d newTrans = look + dt;
lookAt(newTrans, look);
// Translate back to see the object
Imath::V3d checkPoints[8];
checkPoints[0] = Imath::V3d(bounds.min.x, bounds.min.y, bounds.min.z);
checkPoints[1] = Imath::V3d(bounds.max.x, bounds.min.y, bounds.min.z);
checkPoints[2] = Imath::V3d(bounds.min.x, bounds.max.y, bounds.min.z);
checkPoints[3] = Imath::V3d(bounds.min.x, bounds.min.y, bounds.max.z);
checkPoints[4] = Imath::V3d(bounds.min.x, bounds.max.y, bounds.max.z);
checkPoints[5] = Imath::V3d(bounds.max.x, bounds.min.y, bounds.max.z);
checkPoints[6] = Imath::V3d(bounds.max.x, bounds.max.y, bounds.min.z);
checkPoints[7] = Imath::V3d(bounds.max.x, bounds.max.y, bounds.max.z);
// Decide which fov is smaller and use that
const double fovx = m_fovy * m_aspect;
double usefulFOV = fovx;
if (m_fovy < fovx) usefulFOV = m_fovy;
// Figure out which of the 8 bbox extents is the furthest away and use that.
Imath::V3d xn(0,0,0);
for (int i = 0; i < 8; i++)
{
const Imath::Line3d lookVec(look, m_translation);
const Imath::V3d closestPoint = lookVec.closestPointTo(checkPoints[i]);
const double distance = (closestPoint - checkPoints[i]).length();
const double newDistance = distance / tan(radians(usefulFOV) / 2.0);
const double dDiff = (look - m_translation).length() - (closestPoint - m_translation).length();
const Imath::V3d finalVec = (lookVec.dir * dDiff) + (lookVec.dir * newDistance);
if (finalVec.length() > xn.length())
xn = finalVec;
}
lookAt(look+xn, look);
}
//-*****************************************************************************
Imath::V3d GLCamera::pointOfInterest() const
{
Imath::V3d v(0.0, 0.0, -m_centerOfInterest);
rotateVector(m_rotation.x, m_rotation.y, v);
return m_translation + v;
}
//-*****************************************************************************
void GLCamera::autoSetClippingPlanes( const Box3d &bounds )
{
const double rotX = m_rotation.x;
const double rotY = m_rotation.y;
const V3d &eye = m_translation;
double clipNear = FLT_MAX;
double clipFar = FLT_MIN;
V3d v( 0.0, 0.0, -m_centerOfInterest );
rotateVector( rotX, rotY, v );
v.normalize();
V3d points[8];
points[0] = V3d( bounds.min.x, bounds.min.y, bounds.min.z );
points[1] = V3d( bounds.min.x, bounds.min.y, bounds.max.z );
points[2] = V3d( bounds.min.x, bounds.max.y, bounds.min.z );
points[3] = V3d( bounds.min.x, bounds.max.y, bounds.max.z );
points[4] = V3d( bounds.max.x, bounds.min.y, bounds.min.z );
points[5] = V3d( bounds.max.x, bounds.min.y, bounds.max.z );
points[6] = V3d( bounds.max.x, bounds.max.y, bounds.min.z );
points[7] = V3d( bounds.max.x, bounds.max.y, bounds.max.z );
for( int p = 0; p < 8; ++p )
{
V3d dp = points[p] - eye;
double proj = dp.dot( v );
clipNear = std::min( proj, clipNear );
clipFar = std::max( proj, clipFar );
}
clipNear -= 0.5f;
clipFar += 0.5f;
clipNear = clamp( clipNear, 0.1, 100000.0 );
clipFar = clamp( clipFar, 0.1, 100000.0 );
assert( clipFar > clipNear );
m_clip[0] = clipNear;
m_clip[1] = clipFar;
}
//-*****************************************************************************
void GLCamera::lookAt( const V3d &eye, const V3d &at )
{
// TODO: Fix when the world is upside-down
m_translation = eye;
const V3d dt = at - eye;
const double xzLen = sqrt( ( dt.x * dt.x ) +
( dt.z * dt.z ) );
m_rotation.x = degrees( atan2( dt.y, xzLen ) );
m_rotation.y = -degrees( atan2( dt.x, -dt.z ) );
m_centerOfInterest = dt.length();
}
//-*****************************************************************************
void GLCamera::apply() const
{
glViewport( 0, 0, ( GLsizei )m_size[0], ( GLsizei )m_size[1] );
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
gluPerspective( m_fovy,
( ( GLdouble )m_size[0] ) /
( ( GLdouble )m_size[1] ),
m_clip[0],
m_clip[1] );
glMatrixMode( GL_MODELVIEW );
glLoadIdentity();
::glScaled( 1.0 / m_scale[0], 1.0 / m_scale[1], 1.0 / m_scale[2] );
::glRotated( -m_rotation[2], 0.0, 0.0, 1.0 );
::glRotated( -m_rotation[0], 1.0, 0.0, 0.0 );
::glRotated( -m_rotation[1], 0.0, 1.0, 0.0 );
::glTranslated( -m_translation[0], -m_translation[1], -m_translation[2] );
}
//-*****************************************************************************
M44d GLCamera::transform() const
{
M44d m;
M44d tmp;
m.makeIdentity();
tmp.setScale( V3d( 1.0 / m_scale[0],
1.0 / m_scale[1],
1.0 / m_scale[2] ) );
m = m * tmp;
tmp.setAxisAngle( V3d( 0.0, 0.0, 1.0 ), radians( m_rotation[2] ) );
m = m * tmp;
tmp.setAxisAngle( V3d( 1.0, 0.0, 0.0 ), radians( m_rotation[0] ) );
m = m * tmp;
tmp.setAxisAngle( V3d( 0.0, 1.0, 0.0 ), radians( m_rotation[1] ) );
m = m * tmp;
tmp.setTranslation( V3d( m_translation[0],
m_translation[1],
m_translation[2] ) );
m = m * tmp;
return m;
}
//-*****************************************************************************
M44d GLCamera::projection() const
{
const double zFar = m_clip[1];
const double zNear = m_clip[0];
double rads = radians(m_fovy) / 2.0;
const double deltaZ = zFar - zNear;
const double sine = sin(rads);
if ((deltaZ == 0) || (sine == 0) || (m_aspect == 0))
return M44d();
const double cotangent = cos(rads) / sine;
M44d m;
m[0][0] = cotangent / m_aspect;
m[1][1] = cotangent;
m[2][2] = -(zFar + zNear) / deltaZ;
m[2][3] = -1;
m[3][2] = -2 * zNear * zFar / deltaZ;
m[3][3] = 0;
return m;
}
//-*****************************************************************************
Imath::V2d GLCamera::project(const Imath::V3d worldPoint)
{
Imath::V3d pInCam;
transform().inverse().multVecMatrix(worldPoint, pInCam);
Imath::V3d proj;
projection().multVecMatrix(pInCam, proj);
proj = (proj * 0.5) + Imath::V3d(0.5, 0.5, 0.5);
proj.x = proj.x * (double)width() + 0; // TODO: Viewport offset
proj.y = proj.y * (double)height() + 0;
return Imath::V2d(proj.x, proj.y);
}
//-*****************************************************************************
Imath::Line3d GLCamera::unproject(const Imath::V2d cameraPoint)
{
Imath::V3d cPoint(cameraPoint.x, cameraPoint.y, -1.0);
cPoint.x = (cPoint.x - 0.0) / (double)width(); // TODO: Viewport offset
cPoint.y = (cPoint.y - 0.0) / (double)height();
cPoint.x = (cPoint.x * 2.0) - 1.0;
cPoint.y = (cPoint.y * 2.0) - 1.0;
Imath::V3d result1;
projection().inverse().multVecMatrix(cPoint, result1);
Imath::V3d result2;
transform().multVecMatrix(result1, result2);
return Imath::Line3d(transform().translation(), result2);
}
//-*****************************************************************************
void GLCamera::track( const V2d &point )
{
// INIT
const double rotX = m_rotation.x;
const double rotY = m_rotation.y;
V3d dS( 1.0, 0.0, 0.0 );
rotateVector( rotX, rotY, dS );
V3d dT( 0.0, 1.0, 0.0 );
rotateVector( rotX, rotY, dT );
double multS = 2.0 * m_centerOfInterest * tanf( radians( fovy() ) / 2.0 );
const double multT = multS / double( height() );
multS /= double( width() );
// TRACK
const double s = -multS * point.x;
const double t = multT * point.y;
// ALTER
setTranslation( ( m_translation +
( s * dS ) + ( t * dT ) ) );
}
//-*****************************************************************************
void GLCamera::dolly( const V2d &point,
double dollySpeed )
{
// INIT
const double rotX = m_rotation.x;
const double rotY = m_rotation.y;
const V3d &eye = m_translation;
V3d v( 0.0, 0.0, -m_centerOfInterest );
rotateVector( rotX, rotY, v );
const V3d view = eye + v;
v.normalize();
// DOLLY
const double t = point.x / double( width() );
// Magic dolly function
double dollyBy = 1.0 - expf( -dollySpeed * t );
//assert( fabsf( dollyBy ) < 1.0 );
dollyBy *= m_centerOfInterest;
const V3d newEye = eye + ( dollyBy * v );
// ALTER
setTranslation( newEye );
v = newEye - view;
m_centerOfInterest = v.length();
}
//-*****************************************************************************
void GLCamera::rotate( const V2d &point,
double rotateSpeed )
{
// INIT
double rotX = m_rotation.x;
double rotY = m_rotation.y;
const double rotZ = m_rotation.z;
V3d eye = m_translation;
V3d v( 0.0, 0.0, -m_centerOfInterest );
rotateVector( rotX, rotY, v );
const V3d view = eye + v;
// ROTATE
rotY += -rotateSpeed * ( point.x / double( width() ) );
rotX += -rotateSpeed * ( point.y / double( height() ) );
v[0] = 0.0;
v[1] = 0.0;
v[2] = m_centerOfInterest;
rotateVector( rotX, rotY, v );
// ALTER
setTranslation( view + v );
setRotation( V3d( rotX, rotY, rotZ ) );
}
//-*****************************************************************************
void GLCamera::rotateAngle( const V2d &angle )
{
// INIT
V3d eye = m_translation;
double rotX = m_rotation.x;
double rotY = m_rotation.y;
const double rotZ = m_rotation.z;
V3d v( 0.0, 0.0, -m_centerOfInterest );
rotateVector( rotX, rotY, v );
const V3d view = eye + v;
// ROTATE
rotY += angle[0];
rotX += angle[1];
v[0] = 0.0;
v[1] = 0.0;
v[2] = m_centerOfInterest;
rotateVector( rotX, rotY, v );
// ALTER
setTranslation( view + v );
setRotation( V3d( rotX, rotY, rotZ ) );
}
//-*****************************************************************************
std::string GLCamera::RIB() const
{
char str[1024];
sprintf (str, "Format %d %d 1\n"
"Clipping %f %f\n"
"Projection \"perspective\" \"fov\" [%f]\n"
"Scale 1 1 -1\n"
"Scale %f %f %f\n"
"Rotate %f 0 0 1\n"
"Rotate %f 1 0 0\n"
"Rotate %f 0 1 0\n"
"Translate %f %f %f\n",
( int )m_size[0], ( int )m_size[1],
( float )m_clip[0], ( float )m_clip[1],
( float )m_fovy,
( float )( 1.0/m_scale[0] ),
( float )( 1.0/m_scale[1] ),
( float )( 1.0/m_scale[2] ),
( float )( -m_rotation[2] ),
( float )( -m_rotation[0] ),
( float )( -m_rotation[1] ),
( float )( -m_translation[0] ),
( float )( -m_translation[1] ),
( float )( -m_translation[2] ));
// Then transpose and print.
return ( std::string( str ) );
}