-
Notifications
You must be signed in to change notification settings - Fork 4
/
skyviewer.cpp
382 lines (345 loc) · 10 KB
/
skyviewer.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
/* ============================================================================
'skyviewer.cpp' extends the QGLViewer class.
============================================================================ */
/*
Fetch header files.
*/
#include <QFile>
#include <QTextStream>
#include "skyviewer.h"
#include "mainwindow.h"
#include "heal.h"
#include "debug.h"
using namespace qglviewer;
using namespace std;
/*
Constants.
*/
static const char stateFile[] = {".skyviewer.xml"};
/* ============================================================================
'SkyViewer' extends the QGLViewer class.
============================================================================ */
/* ----------------------------------------------------------------------------
'SkyViewer' is the class constructor.
Arguments:
parent - The parent widget.
name - The name of this widget.
Written by Nicholas Phillips.
QT4 implementation. Michael R. Greason, ADNET, 28 August 2007.
---------------------------------------------------------------------------- */
SkyViewer::SkyViewer (QWidget *parent, mainWindow *mw) :
QGLViewer(parent), whitecolor(255,255,255,255), blackcolor(0,0,0,255)
{
pulseflg = true;
mollview = false;
mwin = mw;
texture = NULL;
rigging = NULL;
whiterig = NULL;
polar = NULL;
hlite = 1.0;
delhlite = -0.1;
constraint = NULL;
setStateFileName(stateFile);
}
/* ----------------------------------------------------------------------------
'~SkyViewer' is the class destructor. It does NOT delete the rigging, texture,
or polar lines members.
Arguments:
None.
Written by Nicholas Phillips.
QT4 implementation. Michael R. Greason, ADNET, 28 August 2007.
---------------------------------------------------------------------------- */
SkyViewer::~SkyViewer()
{
saveStateToFile();
texture = NULL;
rigging = NULL;
whiterig = NULL;
polar = NULL;
if (constraint != NULL) delete constraint;
constraint = NULL;
}
/* ----------------------------------------------------------------------------
'setTexture' assigns the texture to be displayed.
Arguments:
t - The new texture.
Returned:
None.
Written by Nicholas Phillips.
QT4 implementation. Michael R. Greason, ADNET, 28 August 2007.
---------------------------------------------------------------------------- */
void SkyViewer::setTexture(SkyTexture *t)
{
if( texture )
disconnect( texture, static_cast<void(SkyTexture::*)()>(&SkyTexture::retextured),
this, static_cast<void(SkyViewer::*)()>(&SkyViewer::update));
texture = t;
connect( texture, static_cast<void(SkyTexture::*)()>(&SkyTexture::retextured),
this, static_cast<void(QGLViewer::*)()>(&SkyViewer::update));
}
/* ----------------------------------------------------------------------------
'init' is called the first time this widget is displayed, initializing the
widget.
Arguments:
None.
Returned:
None.
Written by Nicholas Phillips.
QT4 implementation. Michael R. Greason, ADNET, 28 August 2007.
---------------------------------------------------------------------------- */
void SkyViewer::init(void)
{
/*
General configuration.
*/
setBackgroundColor(QColor(0,0,0)); // Black background.
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
//setFPSIsDisplayed(true);
/*
Define the lighting.
*/
GLfloat light_ambient[] = { 1.0, 1.0, 1.0, 1.0 };
GLfloat light_zero[] = { 0.0, 0.0, 0.0, 1.0 };
glEnable(GL_LIGHT0);
glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);
glLightfv(GL_LIGHT0, GL_DIFFUSE, light_zero);
glLightfv(GL_LIGHT0, GL_SPECULAR, light_zero);
/*
Enable GL textures.
*/
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST );
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST );
glEnable( GL_TEXTURE_2D );
// Nice texture coordinate interpolation
glHint( GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST );
/*
Configure the camera.
*/
camera()->setPosition( Vec(3.0,0,0) );
camera()->lookAt( Vec(0,0,0) );
camera()->setUpVector(Vec(0,0,1));
if (constraint != NULL) delete constraint;
constraint = new CameraConstraint(camera());
constraint->setRotationConstraintType(AxisPlaneConstraint::FREE);
camera()->frame()->setConstraint(constraint);
/*
Configure animation.
*/
setAnimationPeriod(200);
/*
Recover the state from a prior run.
*/
restoreStateFromFile();
}
/* ----------------------------------------------------------------------------
'draw' is called the whenever the widget needs to be drawn on the screen.
Arguments:
None.
Returned:
None.
Written by Nicholas Phillips.
QT4 implementation. Michael R. Greason, ADNET, 28 August 2007.
---------------------------------------------------------------------------- */
void SkyViewer::draw(void)
{
if (showrigging) {
glDisable( GL_TEXTURE_2D );
rigging->draw();
}
if (showtex) {
bool sr = showrigging;
showrigging = false;
if (whiterig != NULL)
{
glDisable(GL_TEXTURE_2D);
if (pulseflg)
{
glColor4f(whitecolor.redF(), whitecolor.greenF(), whitecolor.blueF(), 1.);
}
else
{
glColor4f(blackcolor.redF(), blackcolor.greenF(), blackcolor.blueF(), 1.);
}
whiterig->draw();
}
if (rigging != NULL)
{
glColor4f(1., 1., 1., 1.);
if (texture != NULL) glEnable( GL_TEXTURE_2D );
rigging->draw();
}
showrigging = sr;
}
if ((polar != NULL) && (polar->isOn()) && showpolar) {
glDisable( GL_TEXTURE_2D );
polar->draw();
}
}
/* ----------------------------------------------------------------------------
'animate' is called to perform the next step in animating the viewer.
Arguments:
None.
Returned:
None.
Written by Nicholas Phillips.
QT4 implementation. Michael R. Greason, ADNET, 28 August 2007.
---------------------------------------------------------------------------- */
void SkyViewer::animate (void)
{
const double llim = 0.3, ulim = 0.9;
hlite += delhlite;
if (hlite <= llim)
{
hlite = llim;
delhlite = -delhlite;
}
if (hlite >= ulim)
{
hlite = ulim;
delhlite = -delhlite;
pulseflg = pulseflg ? false : true;
}
mwin->highlightPixels(hlite);
}
/* ----------------------------------------------------------------------------
'postSelection' is called at the end of the selection process by the QGLViewer
library.
Arguments:
pt - The point in the widget where the selection took place.
Returned:
None.
Written by Nicholas Phillips.
QT4 implementation. Michael R. Greason, ADNET, 30 August 2007.
---------------------------------------------------------------------------- */
void SkyViewer::postSelection (const QPoint &pt)
{
Vec orig, dir;
double lambda, phi;
camera()->convertClickToLine(pt, orig, dir);
if (! rigging->projectSelection(orig, dir, phi, lambda)) return;
mwin->selectPixel(phi, lambda);
}
/* ----------------------------------------------------------------------------
'constrainMollweide' constrains the camera if entering a Mollweide projection.
Arguments:
b - Entering Mollweide?.
Returned:
None.
Written by Nicholas Phillips.
QT4 implementation. Michael R. Greason, ADNET, 31 August 2007.
---------------------------------------------------------------------------- */
void SkyViewer::constrainMollweide (bool b)
{
/*
Mollweide.
*/
mollview = b;
if( mollview )
{
camera()->setPosition( Vec(-3.0,0,0) );
camera()->lookAt( Vec(0,0,0) );
camera()->setUpVector(Vec(0,0,1));
constraint->setRotationConstraintType(AxisPlaneConstraint::FORBIDDEN);
camera()->frame()->setConstraint(constraint);
}
/*
3D sphere.
*/
else
{
constraint->setRotationConstraintType(AxisPlaneConstraint::FREE);
constraint->setTranslationConstraintType(AxisPlaneConstraint::FREE);
camera()->frame()->setConstraint(constraint);
}
}
/* ----------------------------------------------------------------------------
'helpString' defines the general help string for the application. Used by
QGLViewer to generate the help window.
Arguments:
None.
Returned:
None.
Written by Nicholas Phillips.
QT4 implementation. Michael R. Greason, ADNET, 30 August 2007.
---------------------------------------------------------------------------- */
QString SkyViewer::helpString () const
{
QString line, contents;
/*
Read the file.
*/
QFile qf(":/general.txt");
qf.open(QIODevice::ReadOnly | QIODevice::Text);
QTextStream in(&qf);
while (!in.atEnd())
{
line = in.readLine();
contents += line;
}
qf.close();
/*
Write its contents.
*/
return contents;
}
/* ----------------------------------------------------------------------------
'recenterAt' recenters the current view to be centered on the passed
pixel coordinates.
Arguments:
x,y,z: coords of selected pixel
Returned:
None.
Written by Nicholas Phillips, 10/14/08
---------------------------------------------------------------------------- */
void SkyViewer::recenterAt(const double x_, const double y_, const double z_)
{
Vec p(x_,y_,z_);
Vec z(0,0,1);
Vec d = z-(p*z)*p;
p *= camera()->position().norm();
camera()->setPosition( p );
camera()->setViewDirection(-p);
camera()->setUpVector( d );
return;
}
/* ----------------------------------------------------------------------------
'recenterAt' recenters the current view to be centered on the passed
pixel coordinates.
Arguments:
x,y,z: coords of selected pixel
Returned:
None.
Written by Nicholas Phillips, 10/14/08
---------------------------------------------------------------------------- */
void SkyViewer::recenterAt(const double theta, const double phi)
{
Vec pos; // Camera position
Vec view; // View direction
Vec up; // Up direction for camera
if( mollview ) {
double phi = asin(cos(theta));
double lambda = atan2(sin(phi)*sin(theta),cos(phi)*sin(theta));
double x,y;
::toMollweide(phi, lambda, x, y);
//x *= -1;
::toMollweide(phi,theta,x,y);
x /= sqrt(2.0);
y /= sqrt(2.0);
pos = Vec(camera()->position()[0],x,y);
view = Vec(-1,0,0);
up = Vec(0,0,1);
}
else {
Vec z(0,0,1);
pos = Vec(cos(phi)*sin(theta),sin(phi)*sin(theta),cos(theta));
up = z-(pos*z)*pos; // Up direction
view = -pos;
pos *= camera()->position().norm(); // Same zoom as before
}
camera()->setPosition( pos );
camera()->setViewDirection( view );
camera()->setUpVector( up );
return;
}