00001
00002
00003
00004
00005
00006
00007 #include <iostream>
00008 #include <QFile>
00009 #include <QTextStream>
00010 #include "skyviewer.h"
00011 #include "mainwindow.h"
00012 #include "heal.h"
00013 #include "debug.h"
00014
00015 using namespace qglviewer;
00016 using namespace std;
00017
00018
00019
00020 static const char stateFile[] = {".skyviewer.xml"};
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034 SkyViewer::SkyViewer (QWidget *parent, mainWindow *mw) :
00035 QGLViewer(parent), whitecolor(255,255,255,255), blackcolor(0,0,0,255)
00036 {
00037 pulseflg = true;
00038 mollview = false;
00039 mwin = mw;
00040 texture = NULL;
00041 rigging = NULL;
00042 whiterig = NULL;
00043 polar = NULL;
00044 hlite = 1.0;
00045 delhlite = -0.1;
00046 setStateFileName(stateFile);
00047 }
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058 SkyViewer::~SkyViewer()
00059 {
00060 saveStateToFile();
00061 texture = NULL;
00062 rigging = NULL;
00063 whiterig = NULL;
00064 polar = NULL;
00065 if (constraint != NULL) delete constraint;
00066 constraint = NULL;
00067 }
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080 void SkyViewer::setTexture(SkyTexture *t)
00081 {
00082 if( texture )
00083 disconnect( texture, SIGNAL(retextured()), this, SLOT( updateGL() ) );
00084
00085 texture = t;
00086 connect( texture, SIGNAL(retextured()), this, SLOT( updateGL() ) );
00087 }
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101 void SkyViewer::init(void)
00102 {
00103
00104
00105
00106 setBackgroundColor(QColor(0,0,0));
00107 glEnable(GL_BLEND);
00108 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
00109
00110
00111
00112
00113 GLfloat light_ambient[] = { 1.0, 1.0, 1.0, 1.0 };
00114 GLfloat light_zero[] = { 0.0, 0.0, 0.0, 1.0 };
00115 glEnable(GL_LIGHT0);
00116 glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);
00117 glLightfv(GL_LIGHT0, GL_DIFFUSE, light_zero);
00118 glLightfv(GL_LIGHT0, GL_SPECULAR, light_zero);
00119
00120
00121
00122 glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST );
00123 glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST );
00124 glEnable( GL_TEXTURE_2D );
00125
00126 glHint( GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST );
00127
00128
00129
00130
00131 camera()->setPosition( Vec(3.0,0,0) );
00132 camera()->lookAt( Vec(0,0,0) );
00133 camera()->setUpVector(Vec(0,0,1));
00134 constraint = new CameraConstraint(camera());
00135 constraint->setRotationConstraintType(AxisPlaneConstraint::FREE);
00136 camera()->frame()->setConstraint(constraint);
00137
00138
00139
00140 setAnimationPeriod(200);
00141
00142
00143
00144 restoreStateFromFile();
00145 }
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155
00156
00157
00158 void SkyViewer::draw(void)
00159 {
00160 if (showrigging) {
00161 glDisable( GL_TEXTURE_2D );
00162 rigging->draw();
00163 }
00164 if (showtex) {
00165 bool sr = showrigging;
00166 showrigging = false;
00167 if (whiterig != NULL)
00168 {
00169 glDisable(GL_TEXTURE_2D);
00170 if (pulseflg)
00171 {
00172 glColor4f(whitecolor.redF(), whitecolor.greenF(), whitecolor.blueF(), 1.);
00173 }
00174 else
00175 {
00176 glColor4f(blackcolor.redF(), blackcolor.greenF(), blackcolor.blueF(), 1.);
00177 }
00178 whiterig->draw();
00179 }
00180 if (rigging != NULL)
00181 {
00182 glColor4f(1., 1., 1., 1.);
00183 if (texture != NULL) glEnable( GL_TEXTURE_2D );
00184 rigging->draw();
00185 }
00186 showrigging = sr;
00187 }
00188 if ((polar != NULL) && (polar->isOn()) && showpolar) {
00189 glDisable( GL_TEXTURE_2D );
00190 polar->draw();
00191 }
00192 }
00193
00194
00195
00196
00197
00198
00199
00200
00201
00202
00203
00204
00205 void SkyViewer::animate (void)
00206 {
00207 const double llim = 0.3, ulim = 0.9;
00208 hlite += delhlite;
00209 if (hlite <= llim)
00210 {
00211 hlite = llim;
00212 delhlite = -delhlite;
00213 }
00214 if (hlite >= ulim)
00215 {
00216 hlite = ulim;
00217 delhlite = -delhlite;
00218 pulseflg = pulseflg ? false : true;
00219 }
00220 mwin->highlightPixels(hlite);
00221 }
00222
00223
00224
00225
00226
00227
00228
00229
00230
00231
00232
00233
00234
00235 void SkyViewer::postSelection (const QPoint &pt)
00236 {
00237 Vec orig, dir;
00238 double lambda, phi;
00239 camera()->convertClickToLine(pt, orig, dir);
00240 if (! rigging->projectSelection(orig, dir, phi, lambda)) return;
00241 mwin->selectPixel(phi, lambda);
00242 }
00243
00244
00245
00246
00247
00248
00249
00250
00251
00252
00253
00254
00255 void SkyViewer::constrainMollweide (bool b)
00256 {
00257
00258
00259
00260 mollview = b;
00261 if( mollview )
00262 {
00263 camera()->setPosition( Vec(-3.0,0,0) );
00264 camera()->lookAt( Vec(0,0,0) );
00265 camera()->setUpVector(Vec(0,0,1));
00266 constraint->setRotationConstraintType(AxisPlaneConstraint::FORBIDDEN);
00267 camera()->frame()->setConstraint(constraint);
00268 }
00269
00270
00271
00272 else
00273 {
00274 constraint->setRotationConstraintType(AxisPlaneConstraint::FREE);
00275 constraint->setTranslationConstraintType(AxisPlaneConstraint::FREE);
00276 camera()->frame()->setConstraint(constraint);
00277 }
00278 }
00279
00280
00281
00282
00283
00284
00285
00286
00287
00288
00289
00290
00291
00292 QString SkyViewer::helpString () const
00293 {
00294 QString line, contents;
00295
00296
00297
00298 QFile qf(":/general.txt");
00299 qf.open(QIODevice::ReadOnly | QIODevice::Text);
00300 QTextStream in(&qf);
00301 while (!in.atEnd())
00302 {
00303 line = in.readLine();
00304 contents += line;
00305 }
00306 qf.close();
00307
00308
00309
00310 return contents;
00311 }
00312
00313
00314
00315
00316
00317
00318
00319
00320
00321
00322
00323 void SkyViewer::recenterAt(const double x_, const double y_, const double z_)
00324 {
00325 cout << "SkyViewer::recenterAt" << endl;
00326
00327 cout << "pos: " << camera()->position();
00328 cout << "\tlookat: " << camera()->viewDirection();
00329 cout << "\tup: " << camera()->upVector() << endl;
00330
00331 Vec p(x_,y_,z_);
00332 Vec z(0,0,1);
00333 Vec d = z-(p*z)*p;
00334
00335
00336 cout << "new pos: " << p;
00337 p *= camera()->position().norm();
00338 cout << " -> " << p << endl;
00339
00340 camera()->setPosition( p );
00341 camera()->setViewDirection(-p);
00342 camera()->setUpVector( d );
00343
00344 cout << "pos: " << camera()->position();
00345 cout << "\tlookat: " << camera()->viewDirection();
00346 cout << "\tup: " << camera()->upVector() << endl;
00347
00348 cout << "d: " << d << ", d.p: "<< d*p << ", d.z: " << d*z << endl;
00349 cout << endl;
00350
00351 return;
00352 }
00353
00354
00355
00356
00357
00358
00359
00360
00361
00362
00363
00364 void SkyViewer::recenterAt(const double theta, const double phi)
00365 {
00366 Vec pos;
00367 Vec view;
00368 Vec up;
00369
00370 if( mollview ) {
00371 double phi = asin(cos(theta));
00372 double lambda = atan2(sin(phi)*sin(theta),cos(phi)*sin(theta));
00373 double x,y;
00374 ::toMollweide(phi, lambda, x, y);
00375
00376 ::toMollweide(phi,theta,x,y);
00377 x /= sqrt(2.0);
00378 y /= sqrt(2.0);
00379 cout << camera()->position() << "\t" << x << ", "<< y <<endl;
00380 pos = Vec(camera()->position()[0],x,y);
00381 view = Vec(-1,0,0);
00382 up = Vec(0,0,1);
00383 }
00384 else {
00385 Vec z(0,0,1);
00386 pos = Vec(cos(phi)*sin(theta),sin(phi)*sin(theta),cos(theta));
00387 up = z-(pos*z)*pos;
00388 view = -pos;
00389 pos *= camera()->position().norm();
00390 }
00391 camera()->setPosition( pos );
00392 camera()->setViewDirection( view );
00393 camera()->setUpVector( up );
00394
00395 return;
00396 }