pulsaranimationwidget.cpp 29.1 KB
Newer Older
Oliver Bock's avatar
Oliver Bock committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
/******************************************************************************
 *   Copyright (C) 2008 by Oliver Bock                                        *
 *   oliver.bock[AT]aei.mpg.de                                                *
 *                                                                            *
 *   This file is part of PulsatingScience.                                   *
 *                                                                            *
 *   PulsatingScience 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, version 3 of the License.               *
 *                                                                            *
 *   PulsatingScience is distributed in the hope that it will be useful,      *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of           *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the             *
 *   GNU General Public License for more details.                             *
 *                                                                            *
 *   You should have received a copy of the GNU General Public License        *
 *   along with PulsatingScience. If not, see <http://www.gnu.org/licenses/>. *
 *                                                                            *
 ******************************************************************************/

#include "pulsaranimationwidget.h"

Oliver Bock's avatar
Oliver Bock committed
23
const double PulsarAnimationWidget::deg2rad = PI/180.0;
24

Oliver Bock's avatar
Oliver Bock committed
25
PulsarAnimationWidget::PulsarAnimationWidget(QWidget *parent) :
26
27
28
    QGLWidget(QGLFormat(QGL::AlphaChannel | QGL::SampleBuffers), parent),
    m_frameTimer(),
    m_pulseProfile(360, 0.0)
Oliver Bock's avatar
Oliver Bock committed
29
{
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
    QString msgThis = tr("3D animation");
    if(!format().directRendering()) {
        QString msg = tr("Sorry, no direct rendering support for %1...");
        qWarning() << msg.arg(msgThis);
    }
    if(!format().doubleBuffer()) {
        QString msg = tr("Sorry, no double buffering support for %1...");
        qWarning() << msg.arg(msgThis);
    }
    if(!format().rgba()) {
        QString msg = tr("Sorry, no RGBA support for %1...");
        qWarning() << msg.arg(msgThis);
    }
    if(!format().alpha()) {
        QString msg = tr("Sorry, no alpha channel support for %1...");
        qWarning() << msg.arg(msgThis);
    }
    if(!format().sampleBuffers()) {
        QString msg = tr("Sorry, no multisampling support for %1...");
        qWarning() << msg.arg(msgThis);
    }

    // connect primary rendering timer to local callback
Oliver Bock's avatar
Oliver Bock committed
53
    connect(&m_frameTimer, SIGNAL(timeout()), this, SLOT(updateFrame()));
Oliver Bock's avatar
Oliver Bock committed
54

Oliver Bock's avatar
Oliver Bock committed
55
    // initialize quadric pointers
Oliver Bock's avatar
Oliver Bock committed
56
57
    m_quadricCompanionOrbitPlane = NULL;
    m_quadricCompanion = NULL;
58
    m_quadricPulsarOrbitPlane = NULL;
Oliver Bock's avatar
Oliver Bock committed
59
    m_quadricPulsar = NULL;
60
61
    m_quadricPulsarCone1Shell = NULL;
    m_quadricPulsarCone2Shell = NULL;
62
    m_quadricPulsarSpinAxis = NULL;
63
64
    m_quadricPulsarSpinAxisTop1 = NULL;
    m_quadricPulsarSpinAxisTop2 = NULL;
65
66
67
68
    m_quadricPulsarMagneticAxis = NULL;

    // initialize texture pointers
    m_backgroundTexture = 0;
69
    m_beamTexture = 0;
70
71
72
73
74
75
76
77
78
79

    // initial render timing settings
    m_framesPerSecond = 25;
    m_pulsarRotationDelta = 0.0;
    m_orbitRotationDelta = 0.0;
    m_pulsarRotationAngle = 0.0;
    m_orbitRotationAngle = 0.0;

    // initial binary system parameters (have to match GUI!)
    m_pulsarMass = 1.4;
Oliver Bock's avatar
Oliver Bock committed
80
    m_pulsarRadius = 1.0;
81
82
83
84
85
    m_pulsarSpinAxisInclination = 0.0;
    m_pulsarMagneticAxisInclination = 60.0;
    m_pulsarSemiMajorAxis = 5.0;
    // initial spin frequency of 0.5 Hz
    m_pulsarRotationDelta = (360.0 * 0.5) / m_framesPerSecond;
86
87
88
    // beam properties (keep this order!)
    m_pulsarBeamLength = 3.0f;
    setPulsarBeamAngle(30);
Oliver Bock's avatar
Oliver Bock committed
89
90
91
    // initial companion is "Neutron Star"
    m_companionMass = 1.4;
    m_companionSemiMajorAxis = (m_pulsarMass/m_companionMass) * m_pulsarSemiMajorAxis;
92

93
94
95
96
97
98
    // update orbital period (based on settings above)
    updateOrbitPeriod();

    // initial view features
    m_showOrbits = false;
    m_showRotationAxes = false;
99
    m_showPulseFlashes = true;
100
101
102
103
104
105
106
107
108
109
110
111
112
    m_cameraInteraction = false;

    // initial view settings
    m_mouseAngleH = 90.0;
    m_mouseAngleV = 30.0;
    m_cameraZoom = 150.0;
    m_cameraZoomLBound = 10.0;
    m_cameraZoomUBound = 4500.0;
    m_mouseLastX = 0;
    m_mouseLastY = 0;

    // update camera based on settings above
    updateCameraPosition(m_mouseAngleH, m_mouseAngleV, m_cameraZoom);
Oliver Bock's avatar
Oliver Bock committed
113
114
115
116
}

PulsarAnimationWidget::~PulsarAnimationWidget()
{
117
118
119
120
    if(m_quadricCompanionOrbitPlane) gluDeleteQuadric(m_quadricCompanionOrbitPlane);
    if(m_quadricCompanion) gluDeleteQuadric(m_quadricCompanion);
    if(m_quadricPulsarOrbitPlane) gluDeleteQuadric(m_quadricPulsarOrbitPlane);
    if(m_quadricPulsar) gluDeleteQuadric(m_quadricPulsar);
121
122
    if(m_quadricPulsarCone1Shell) gluDeleteQuadric(m_quadricPulsarCone1Shell);
    if(m_quadricPulsarCone2Shell) gluDeleteQuadric(m_quadricPulsarCone2Shell);
123
    if(m_quadricPulsarSpinAxis) gluDeleteQuadric(m_quadricPulsarSpinAxis);
124
125
    if(m_quadricPulsarSpinAxisTop1) gluDeleteQuadric(m_quadricPulsarSpinAxisTop1);
    if(m_quadricPulsarSpinAxisTop2) gluDeleteQuadric(m_quadricPulsarSpinAxisTop2);
126
127
128
    if(m_quadricPulsarMagneticAxis) gluDeleteQuadric(m_quadricPulsarMagneticAxis);

    if(m_backgroundTexture) deleteTexture(m_backgroundTexture);
129
    if(m_beamTexture) deleteTexture(m_beamTexture);
Oliver Bock's avatar
Oliver Bock committed
130
131
132
133
}

void PulsarAnimationWidget::initializeGL()
{
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
    glClearColor(0.0, 0.0, 0.0, 0.0);
    glClearDepth(1.0);
    glDepthFunc(GL_LEQUAL);
    glEnable(GL_DEPTH_TEST);

    glShadeModel(GL_SMOOTH);
    glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);

    GLfloat LightAmbient[] = {0.3, 0.3, 0.3, 1.0};
    GLfloat LightDiffuse[] = {1.0, 1.0, 1.0, 1.0};
    GLfloat LightSpecular[] = {1.0, 1.0, 1.0, 1.0};
    GLfloat LightPosition[] = {0.0, 0.0, 3.0, 1.0};
    GLfloat spot_direction[] = {0.0, 0.0, -1.0};

    glLightfv(GL_LIGHT0, GL_AMBIENT, LightAmbient);
    glLightfv(GL_LIGHT0, GL_DIFFUSE, LightDiffuse);
    glLightfv(GL_LIGHT0, GL_SPECULAR, LightSpecular);
    glLightfv(GL_LIGHT0, GL_POSITION, LightPosition);
    glLightf(GL_LIGHT0, GL_SPOT_CUTOFF, 50.0);
    glLightfv(GL_LIGHT0, GL_SPOT_DIRECTION, spot_direction);
    glLightf(GL_LIGHT0, GL_SPOT_EXPONENT, 10.0);

    glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, GL_TRUE);
    glEnable(GL_LIGHT0);

    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
    glEnable(GL_BLEND);

    m_quadricCompanionOrbitPlane = gluNewQuadric();
    m_quadricCompanion = gluNewQuadric();
    m_quadricPulsarOrbitPlane = gluNewQuadric();
    m_quadricPulsar = gluNewQuadric();
166
167
    m_quadricPulsarCone1Shell = gluNewQuadric();
    m_quadricPulsarCone2Shell = gluNewQuadric();
168
    m_quadricPulsarSpinAxis = gluNewQuadric();
169
170
    m_quadricPulsarSpinAxisTop1 = gluNewQuadric();
    m_quadricPulsarSpinAxisTop2 = gluNewQuadric();
171
    m_quadricPulsarMagneticAxis = gluNewQuadric();
172

Oliver Bock's avatar
Oliver Bock committed
173
174
    gluQuadricNormals(m_quadricCompanionOrbitPlane, GLU_SMOOTH);
    gluQuadricNormals(m_quadricCompanion, GLU_SMOOTH);
175
    gluQuadricNormals(m_quadricPulsarOrbitPlane, GLU_SMOOTH);
Oliver Bock's avatar
Oliver Bock committed
176
    gluQuadricNormals(m_quadricPulsar, GLU_SMOOTH);
177
178
    gluQuadricNormals(m_quadricPulsarCone1Shell, GLU_SMOOTH);
    gluQuadricNormals(m_quadricPulsarCone2Shell, GLU_SMOOTH);
179
    gluQuadricNormals(m_quadricPulsarSpinAxis, GLU_SMOOTH);
180
181
    gluQuadricNormals(m_quadricPulsarSpinAxisTop1, GLU_SMOOTH);
    gluQuadricNormals(m_quadricPulsarSpinAxisTop2, GLU_SMOOTH);
182
    gluQuadricNormals(m_quadricPulsarMagneticAxis, GLU_SMOOTH);
183

Oliver Bock's avatar
Oliver Bock committed
184
185
186
187
    // query max texture size (estimate)
    GLint maxTextureSize;
    glGetIntegerv(GL_MAX_TEXTURE_SIZE, &maxTextureSize);

Oliver Bock's avatar
Oliver Bock committed
188
189
190
191
192
    // prepare local messages
    QString msgShape = tr("%1 texture shape not quadratic!");
    QString msgPower = tr("%1 texture dimensions not a power of 2!");
    QString msgSize = tr("Maximum texture size exceeded! Scaling down %1 texture to %2x%3...");

Oliver Bock's avatar
Oliver Bock committed
193
    // prepare and check background texture
Oliver Bock's avatar
Oliver Bock committed
194
    QImage backgroundTexture(":/textures/resources/texture_background_carina.png");
Oliver Bock's avatar
Oliver Bock committed
195
    if(backgroundTexture.width() != backgroundTexture.height()) {
196
        qWarning() << msgShape.arg(tr("Background"));
Oliver Bock's avatar
Oliver Bock committed
197
198
    }
    else {
199
200
201
202
203
204
        double integer = 0.0;
        double fraction = 0.0;
        fraction = modf(log(backgroundTexture.width()) / log(2.0), &integer);
        if(fraction > 0.0) {
            qWarning() << msgPower.arg(tr("Background"));
        }
Oliver Bock's avatar
Oliver Bock committed
205
206
    }
    if(backgroundTexture.width() > maxTextureSize) {
207
208
        qWarning() << msgSize.arg(tr("background").arg(maxTextureSize).arg(maxTextureSize));
        backgroundTexture = backgroundTexture.scaled(maxTextureSize, maxTextureSize, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
Oliver Bock's avatar
Oliver Bock committed
209
210
    }

211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
    // prepare and check beam texture
    QImage beamTexture(":/textures/resources/texture_beam.png");
    if(beamTexture.width() != beamTexture.height()) {
        qWarning() << msgShape.arg(tr("Beam"));
    }
    else {
        double integer = 0.0;
        double fraction = 0.0;
        fraction = modf(log(beamTexture.width()) / log(2.0), &integer);
        if(fraction > 0.0) {
            qWarning() << msgPower.arg(tr("Beam"));
        }
    }
    if(beamTexture.width() > maxTextureSize) {
        qWarning() << msgSize.arg(tr("beam").arg(maxTextureSize).arg(maxTextureSize));
        beamTexture = beamTexture.scaled(maxTextureSize, maxTextureSize, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
    }

Oliver Bock's avatar
Oliver Bock committed
229
    // bind textures
Oliver Bock's avatar
Oliver Bock committed
230
    m_backgroundTexture = bindTexture(backgroundTexture, GL_TEXTURE_2D, GL_RGBA);
231
    m_beamTexture = bindTexture(beamTexture, GL_TEXTURE_2D, GL_RGBA);
232

233
    // use mipmapped textures
234
235
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);
Oliver Bock's avatar
Oliver Bock committed
236
237
238
239
}

void PulsarAnimationWidget::resizeGL(int w, int h)
{
240
    glViewport(0, 0, w, h);
Oliver Bock's avatar
Oliver Bock committed
241

242
243
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
Oliver Bock's avatar
Oliver Bock committed
244

245
    gluPerspective(4.5, (GLfloat)w / (GLfloat)h, 0.1, 5000.0);
Oliver Bock's avatar
Oliver Bock committed
246

247
248
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
Oliver Bock's avatar
Oliver Bock committed
249
250
251
252
}

void PulsarAnimationWidget::paintGL()
{
253
254
    GLfloat x,y,angle;

255
256
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

257
258
259
260
261
262
263
264
265
266
267
268
269
    // TODO: should be located elsewhere
    static GLfloat no_mat[] = {0.0, 0.0, 0.0, 1.0};
    static GLfloat mat_diffuse[] = {0.5, 0.5, 0.5, 1.0};
    static GLfloat mat_specular[] = {1.0, 1.0, 1.0, 1.0};
    static GLfloat low_shininess[] = {2.5};
    static GLfloat translucent[] = {1.0, 1.0, 1.0, 0.33};

    glMaterialfv(GL_FRONT, GL_AMBIENT, no_mat);
    glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
    glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
    glMaterialfv(GL_FRONT, GL_SHININESS, low_shininess);
    glMaterialfv(GL_FRONT, GL_EMISSION, no_mat);

270
271
272
273
274
275
276
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();

    gluLookAt(m_cameraPosX, m_cameraPosY, m_cameraPosZ,
              0.0, 0.0, 0.0,
              0.0, 1.0, 0.0);

277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
    // save current state (the following is using parallel projection)
    glMatrixMode(GL_PROJECTION);
    glPushMatrix();
    {
        glLoadIdentity();
        glOrtho(0, width(), 0, height(), 0.1, 501.0);
        glMatrixMode(GL_MODELVIEW);
        glPushMatrix();
        {
            glLoadIdentity();

            // draw backdrop (independent parallel projection)
            glColor3f(0.5, 0.5, 0.5f);
            glTranslatef(0.0, 0.0, -501.0);
            drawTexture(QPointF(0.0, 0.0), m_backgroundTexture);

            // restore original state
            glMatrixMode(GL_PROJECTION);
        }
        glPopMatrix();
        glMatrixMode(GL_MODELVIEW);
    }
    glPopMatrix();

301
302
    // draw companion
    glPushMatrix();
303
304
305
306
    {
        glTranslatef(sin((m_orbitRotationAngle + 180.0) * deg2rad) * m_companionSemiMajorAxis,
                     0.0,
                     cos((m_orbitRotationAngle + 180.0) * deg2rad) * m_companionSemiMajorAxis);
307
308
309

        // draw, with proper lighting
        glEnable(GL_LIGHTING);
310
        gluSphere(m_quadricCompanion, 1.0, 32, 32);
311
        glDisable(GL_LIGHTING);
312
    }
313
314
    glPopMatrix();

315
316
317
    // enable wireframe mode
    glPolygonMode(GL_FRONT_AND_BACK,GL_LINE);

318
319
    // draw pulsar
    glPushMatrix();
320
321
322
323
    {
        glTranslatef(sin(m_orbitRotationAngle * deg2rad) * m_pulsarSemiMajorAxis,
                     0.0,
                     cos(m_orbitRotationAngle * deg2rad) * m_pulsarSemiMajorAxis);
324
325

        glPushMatrix();
326
327
328
        {
            glRotatef(m_pulsarSpinAxisInclination, 0.0, 0.0, 1.0);
            glRotatef(m_pulsarRotationAngle, 0.0, 1.0, 0.0);
Oliver Bock's avatar
Oliver Bock committed
329
            glRotatef(90, 1.0, 0.0, 0.0);
330
331
332

            // draw spin axis
            if(m_showRotationAxes) {
333
334
                glPolygonMode(GL_FRONT_AND_BACK,GL_FILL);
                glColor3f(1.0f, 1.0f, 1.0f);
335
336
                glPushMatrix();
                {
337
338
                    glTranslatef(0.0, 0.0, -4.5);
                    gluCylinder(m_quadricPulsarSpinAxis, 0.020, 0.020, 9.0, 32, 1);
339
340
                }
                glPopMatrix();
341
342
                glPushMatrix();
                {
343
                    glTranslatef(0.0, 0.0, -4.5);
344
345
346
347
348
                    gluDisk(m_quadricPulsarSpinAxisTop1, 0, 0.020, 32, 8);
                }
                glPopMatrix();
                glPushMatrix();
                {
349
                    glTranslatef(0.0, 0.0, 4.5);
350
351
352
353
                    gluDisk(m_quadricPulsarSpinAxisTop2, 0, 0.020, 32, 8);
                }
                glPopMatrix();
                glPolygonMode(GL_FRONT_AND_BACK,GL_LINE);
354
            }
355

356
357
358
359
            // draw sphere, with proper lighting
            glEnable(GL_LIGHTING);
            glMaterialfv(GL_FRONT, GL_AMBIENT, no_mat);
            glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
Oliver Bock's avatar
Oliver Bock committed
360
            gluSphere(m_quadricPulsar, m_pulsarRadius, 32, 32);
361
            glDisable(GL_LIGHTING);
362
363
        }
        glPopMatrix();
364

365
        // first cone
366
        glPushMatrix();
367
368
369
370
371
372
373
374
375
        {
            glRotatef(90.0, 1.0, 0.0, 0.0);
            glRotatef(m_pulsarSpinAxisInclination, 0.0, 1.0, 0.0);

            glRotatef(-m_pulsarRotationAngle - 90.0, 0.0, 0.0, 1.0);
            glRotatef(-m_pulsarMagneticAxisInclination, 1.0, 0.0, 0.0);

            // draw magnetic axis (for both cones)
            if(m_showRotationAxes) {
376
377
                glPolygonMode(GL_FRONT_AND_BACK,GL_FILL);
                glColor3f(1.0f, 1.0f, 0.0f);
378
379
                glPushMatrix();
                {
380
381
                    glTranslatef(0.0, 0.0, -4.5);
                    gluCylinder(m_quadricPulsarMagneticAxis, 0.020, 0.020, 9.0, 32, 1);
382
383
                }
                glPopMatrix();
384
385
                glPushMatrix();
                {
386
                    glTranslatef(0.0, 0.0, -4.5);
387
388
389
390
391
                    gluDisk(m_quadricPulsarSpinAxisTop1, 0, 0.020, 32, 8);
                }
                glPopMatrix();
                glPushMatrix();
                {
392
                    glTranslatef(0.0, 0.0, 4.5);
393
394
395
396
                    gluDisk(m_quadricPulsarSpinAxisTop2, 0, 0.020, 32, 8);
                }
                glPopMatrix();
                glPolygonMode(GL_FRONT_AND_BACK,GL_LINE);
397
            }
398

399
            glTranslatef(0.0, 0.0, -(m_pulsarBeamLength + m_pulsarRadius + 0.1));
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

            // draw first cone's outer layer
            glBegin(GL_TRIANGLE_FAN);
            {
                // Pinnacle of cone is shared vertex for fan, moved up z-axis
                // to produce a cone instead of a circle
                glColor4f(1.0f, 1.0f, 0.0f, 0.33f);
                glVertex3f(0.0f, 0.0f, m_pulsarBeamLength);

                // Loop around in a circle and specify even points along the circle
                // as the vertices of the triangle fan (32 sections)
                for(angle = 0.0f; angle < (2.0f*PI); angle += (PI/32.0f))
                {
                    // Calculate x and y position of the next vertex
                    x = m_pulsarBeamOuterRadius * sin(angle);
                    y = m_pulsarBeamOuterRadius * cos(angle);

                    // Specify the next vertex for the triangle fan
                    glVertex2f(x, y);
                }
            }
            glEnd();

            // draw first cone's "base" (textured bottom to visualize beam intensity)
            glPolygonMode(GL_FRONT_AND_BACK,GL_FILL);
            glColor4f(1.0f, 1.0f, 1.0f, 1.0f);

            // create texture coordinates and enable texturing
            gluQuadricTexture(m_quadricPulsarCone1Shell, GL_TRUE);
            glBindTexture(GL_TEXTURE_2D, m_beamTexture);
            glEnable(GL_TEXTURE_2D);

            gluDisk(m_quadricPulsarCone1Shell, 0, m_pulsarBeamOuterRadius, 32, 1);

            // disable texturing
            glDisable(GL_TEXTURE_2D);
436

437
            glPolygonMode(GL_FRONT_AND_BACK,GL_LINE);
438
439
        }
        glPopMatrix();
440

441
        // second cone (pointing towards from camera)
442
443
444
445
        glPushMatrix();
        {
            glRotatef(-90.0, 1.0, 0.0, 0.0);
            glRotatef(-m_pulsarSpinAxisInclination, 0.0, 1.0, 0.0);
446

447
448
            glRotatef(m_pulsarRotationAngle - 90.0, 0.0, 0.0, 1.0);
            glRotatef(m_pulsarMagneticAxisInclination, 1.0, 0.0, 0.0);
449

450
            glTranslatef(0.0, 0.0, -(m_pulsarBeamLength + m_pulsarRadius + 0.1));
451

452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
            // draw second cone's outer layer
            glBegin(GL_TRIANGLE_FAN);
            {
                // Pinnacle of cone is shared vertex for fan, moved up z-axis
                // to produce a cone instead of a circle
                glColor4f(1.0f, 1.0f, 0.0f, 0.33f);
                glVertex3f(0.0f, 0.0f, m_pulsarBeamLength);

                // Loop around in a circle and specify even points along the circle
                // as the vertices of the triangle fan (32 sections)
                for(angle = 0.0f; angle < (2.0f*PI); angle += (PI/32.0f))
                {
                    // Calculate x and y position of the next vertex
                    x = m_pulsarBeamOuterRadius * sin(angle);
                    y = m_pulsarBeamOuterRadius * cos(angle);
467

468
469
470
471
472
                    // Specify the next vertex for the triangle fan
                    glVertex2f(x, y);
                }
            }
            glEnd();
473

474
475
476
            // draw second cone's "base" (textured bottom to visualize beam intensity)
            glPolygonMode(GL_FRONT_AND_BACK,GL_FILL);
            glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
477

478
479
480
481
482
483
484
485
486
            // create texture coordinates and enable texturing
            gluQuadricTexture(m_quadricPulsarCone2Shell, GL_TRUE);
            glBindTexture(GL_TEXTURE_2D, m_beamTexture);
            glEnable(GL_TEXTURE_2D);

            gluDisk(m_quadricPulsarCone2Shell, 0, m_pulsarBeamOuterRadius, 32, 1);

            // disable texturing
            glDisable(GL_TEXTURE_2D);
487
488
489
        }
        glPopMatrix();
    }
490
491
    glPopMatrix();

492
493
494
    // back to solid rendering
    glPolygonMode(GL_FRONT_AND_BACK,GL_FILL);

495
496
497
    // draw orbital planes
    if(m_cameraInteraction || m_showOrbits) {
        glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, translucent);
498
        glEnable(GL_LIGHTING);
499
500
501
502
503
504

        static double sizeOffset = 0.25;

        // companion's plane (only if not identical with pulsar's)
        if(m_companionSemiMajorAxis != m_pulsarSemiMajorAxis) {
            glPushMatrix();
505
506
507
508
509
510
511
512
513
514
515
516
517
518
            {
                glRotatef(90.0, 1.0, 0.0, 0.0);

                // separate them slightly in case of overlap
                // based on ugly mass diff check (no collision detection)
                // single known problematic pair: (m_c=1.4, m_p=1.5)
                if((int)(10*(m_pulsarMass-m_companionMass)+0.5) == 1){
                    glTranslatef(0.0, 0.0, 0.01);
                }

                gluDisk(m_quadricCompanionOrbitPlane,
                        m_companionSemiMajorAxis - sizeOffset,
                        m_companionSemiMajorAxis + sizeOffset,
                        64, 1);
519
520
521
522
523
524
            }
            glPopMatrix();
        }

        // pulsar's plane
        glPushMatrix();
525
526
527
528
529
530
531
        {
            glRotatef(90.0, 1.0, 0.0, 0.0);
            gluDisk(m_quadricPulsarOrbitPlane,
                    m_pulsarSemiMajorAxis - sizeOffset,
                    m_pulsarSemiMajorAxis + sizeOffset,
                    64, 1);
        }
532
        glPopMatrix();
533
534

        glDisable(GL_LIGHTING);
535
    }
Oliver Bock's avatar
Oliver Bock committed
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554

    // save current state (the following is using parallel projection)
    glMatrixMode(GL_PROJECTION);
    glPushMatrix();
    {
        glLoadIdentity();
        glOrtho(0, width(), 0, height(), 0.1, 501.0);
        glMatrixMode(GL_MODELVIEW);
        glPushMatrix();
        {
            glLoadIdentity();

            // draw copyright info last (appears in front of everything)
            glColor4f(1.0f, 1.0f, 1.0f, 0.5f);
            QFont font;
            font.setPointSize(11);
            font.setBold(true);
            font.setFamily("Arial");
            font.setStyleStrategy((QFont::StyleStrategy) (QFont::OpenGLCompatible | QFont::PreferQuality));
Oliver Bock's avatar
Oliver Bock committed
555
            renderText(10, 25, -100, QString("Copyright %1 2017").arg(QChar(0x00A9)), font);
556
            renderText(10, 10, -100, tr("Max Planck Institute for Gravitational Physics"), font);
Oliver Bock's avatar
Oliver Bock committed
557
558
559
560
561
562
563
564

            // restore original state
            glMatrixMode(GL_PROJECTION);
        }
        glPopMatrix();
        glMatrixMode(GL_MODELVIEW);
    }
    glPopMatrix();
Oliver Bock's avatar
Oliver Bock committed
565
566
}

Oliver Bock's avatar
Oliver Bock committed
567
568
void PulsarAnimationWidget::runAnimation()
{
569
    m_frameTimer.start(qRound(1000.0 / m_framesPerSecond));
Oliver Bock's avatar
Oliver Bock committed
570
}
Oliver Bock's avatar
Oliver Bock committed
571

Oliver Bock's avatar
Oliver Bock committed
572
573
void PulsarAnimationWidget::pauseAnimation()
{
574
    m_frameTimer.stop();
Oliver Bock's avatar
Oliver Bock committed
575
576
577
578
}

void PulsarAnimationWidget::stopAnimation()
{
579
580
    m_frameTimer.stop();
    resetParameters();
Oliver Bock's avatar
Oliver Bock committed
581

582
    updateGL();
Oliver Bock's avatar
Oliver Bock committed
583
584
}

Oliver Bock's avatar
Oliver Bock committed
585
void PulsarAnimationWidget::updateFrame()
Oliver Bock's avatar
Oliver Bock committed
586
{
587
588
589
590
591
592
593
594
595
    m_pulsarRotationAngle += m_pulsarRotationDelta;
    if(m_pulsarRotationAngle >   360.0) {
        m_pulsarRotationAngle -= 360.0;
    }
    m_orbitRotationAngle += m_orbitRotationDelta;
    if(m_orbitRotationAngle >   360.0) {
        m_orbitRotationAngle -= 360.0;
    }

596
597
    updatePulseProfile();

598
599
600
    updateGL();

    emit pulsarAnimationStep(m_pulsarRotationAngle);
601
602
}

603
604
void PulsarAnimationWidget::showOrbits(bool enabled)
{
605
    m_showOrbits = enabled;
606

607
    updateGL();
608
609
}

Oliver Bock's avatar
Oliver Bock committed
610
611
void PulsarAnimationWidget::showRotationAxes(bool enabled)
{
612
    m_showRotationAxes = enabled;
Oliver Bock's avatar
Oliver Bock committed
613

614
    updateGL();
Oliver Bock's avatar
Oliver Bock committed
615
616
}

617
618
619
620
621
622
623
void PulsarAnimationWidget::showPulseFlashes(bool enabled)
{
    m_showPulseFlashes = enabled;

    updateGL();
}

624
625
void PulsarAnimationWidget::mousePressEvent(QMouseEvent *event)
{
626
    Q_UNUSED(event);
627

628
629
    m_cameraInteraction = true;
    updateGL();
630
631
}

632
633
void PulsarAnimationWidget::mouseMoveEvent(QMouseEvent *event)
{
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
    Qt::MouseButtons buttons = event->buttons();
    if((buttons & Qt::LeftButton) == Qt::LeftButton) {
        if(m_mouseLastX != 0) {
            m_mouseAngleH += (m_mouseLastX - event->x());
            m_mouseAngleH = m_mouseAngleH < 360 ? m_mouseAngleH : 0;
            m_mouseAngleH = m_mouseAngleH >=  0 ? m_mouseAngleH : 359;
        }
        if(m_mouseLastY != 0) {
            m_mouseAngleV -= (m_mouseLastY - event->y());
            m_mouseAngleV = m_mouseAngleV <  90 ? m_mouseAngleV : 90;
            m_mouseAngleV = m_mouseAngleV > -90 ? m_mouseAngleV : -90;
        }

        m_mouseLastX = event->x();
        m_mouseLastY = event->y();
    }
    else if((buttons & Qt::RightButton) == Qt::RightButton) {
        if(m_mouseLastY != 0) {
            m_cameraZoom -= (m_mouseLastY - event->y());
            m_cameraZoom = m_cameraZoom >= m_cameraZoomLBound ? m_cameraZoom : m_cameraZoomLBound;
            m_cameraZoom = m_cameraZoom >= m_cameraZoomUBound ? m_cameraZoomUBound : m_cameraZoom;
        }

        m_mouseLastY = event->y();
    }

    updateCameraPosition(m_mouseAngleH, m_mouseAngleV, m_cameraZoom);
661
662
663
664
}

void PulsarAnimationWidget::mouseReleaseEvent(QMouseEvent *event)
{
665
    Q_UNUSED(event);
666

667
668
669
    m_mouseLastX = 0;
    m_mouseLastY = 0;
    m_cameraInteraction = false;
670

671
    updateGL();
672
673
}

Oliver Bock's avatar
Oliver Bock committed
674
675
void PulsarAnimationWidget::showEvent(QShowEvent *event)
{
676
    Q_UNUSED(event);
Oliver Bock's avatar
Oliver Bock committed
677

678
679
    // update and propagate pulse profile
    updatePulseProfile();
Oliver Bock's avatar
Oliver Bock committed
680
681
}

Oliver Bock's avatar
Oliver Bock committed
682
void PulsarAnimationWidget::updateCameraPosition(const double angleH, const double angleV, const double zoom)
683
{
684
685
686
    m_cameraPosX = sin(angleH * deg2rad) * cos(angleV * deg2rad) * zoom;
    m_cameraPosY = sin(angleV * deg2rad) * zoom;
    m_cameraPosZ = cos(angleH * deg2rad) * cos(angleV * deg2rad) * zoom;
687

688
    updatePulseProfile();
689

690
    updateGL();
691
692
}

Oliver Bock's avatar
Oliver Bock committed
693
694
void PulsarAnimationWidget::setFramePerSecond(const unsigned int fps)
{
695
    m_framesPerSecond = fps;
Oliver Bock's avatar
Oliver Bock committed
696
697
}

Oliver Bock's avatar
Oliver Bock committed
698
void PulsarAnimationWidget::setPulsarSemiMajorAxis(const double length)
Oliver Bock's avatar
Oliver Bock committed
699
{
700
701
702
703
    m_pulsarSemiMajorAxis = length;
    m_companionSemiMajorAxis = (m_pulsarMass/m_companionMass) * m_pulsarSemiMajorAxis;
    updateOrbitPeriod();
    updatePulseProfile();
Oliver Bock's avatar
Oliver Bock committed
704

705
    updateGL();
Oliver Bock's avatar
Oliver Bock committed
706
}
707

Oliver Bock's avatar
Oliver Bock committed
708
void PulsarAnimationWidget::setCompanionMass(const double mass)
709
{
710
711
712
    m_companionMass = mass;
    updateOrbitRadii();
    updatePulseProfile();
Oliver Bock's avatar
Oliver Bock committed
713

714
    updateGL();
715
716
}

Oliver Bock's avatar
Oliver Bock committed
717
void PulsarAnimationWidget::setPulsarMass(const double mass)
718
{
719
720
721
    m_pulsarMass = mass;
    updateOrbitRadii();
    updatePulseProfile();
Oliver Bock's avatar
Oliver Bock committed
722

723
    updateGL();
Oliver Bock's avatar
Oliver Bock committed
724
725
}

Oliver Bock's avatar
Oliver Bock committed
726
void PulsarAnimationWidget::setPulsarSpinFrequency(const double frequency)
Oliver Bock's avatar
Oliver Bock committed
727
{
728
729
    m_pulsarRotationDelta = (360.0 * frequency) / m_framesPerSecond;
    updatePulseProfile();
730
}
731

732
733
void PulsarAnimationWidget::setPulsarSpinAxisInclination(const int degrees)
{
734
735
    m_pulsarSpinAxisInclination = degrees;
    updatePulseProfile();
736

737
    updateGL();
738
739
}

740
741
void PulsarAnimationWidget::setPulsarMagneticAxisInclination(const int degrees)
{
742
743
    m_pulsarMagneticAxisInclination = degrees;
    updatePulseProfile();
744

745
    updateGL();
746
}
747

748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
void PulsarAnimationWidget::setPulsarBeamAngle(const int degrees)
{
    double beamTexturePeakCorrectionFactor = 0.83;
    double correctedOuterRadius;

    // compute visual radius
    m_pulsarBeamOuterRadius = tan(deg2rad * degrees * 0.5f) * m_pulsarBeamLength;

    // compute corrected angle for pulse profile
    correctedOuterRadius = m_pulsarBeamOuterRadius * beamTexturePeakCorrectionFactor;
    m_pulsarBeamAngle = 2 * atan(correctedOuterRadius / m_pulsarBeamLength) * 180.0/PI;

    updatePulseProfile();

    updateGL();
}

765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
void PulsarAnimationWidget::getCameraPosition(double& cameraAngleH, double& cameraAngleV, double& cameraZoom)
{
    cameraAngleH = m_mouseAngleH;
    cameraAngleV = m_mouseAngleV;
    cameraZoom = m_cameraZoom;
}

void PulsarAnimationWidget::resetCameraPosition(const double angleH, const double angleV, const double zoom)
{
    m_mouseAngleH = angleH;
    m_mouseAngleV = angleV;
    m_cameraZoom = zoom;

    updateCameraPosition(m_mouseAngleH, m_mouseAngleV, m_cameraZoom);
}

Oliver Bock's avatar
Oliver Bock committed
781
782
void PulsarAnimationWidget::updateOrbitPeriod()
{
783
784
785
    m_orbitalPeriod = 3.1553e7 * sqrt(
                                    (pow(m_pulsarSemiMajorAxis, 3.0) * pow(m_pulsarMass+m_companionMass, 2.0)) / pow(m_companionMass, 3.0)
                                 );
Oliver Bock's avatar
Oliver Bock committed
786

787
788
    // visual correction factor (increase orbital momentum)
    double visualCorrection = 1e-8;
Oliver Bock's avatar
Oliver Bock committed
789

790
    m_orbitRotationDelta  = (360.0 / (m_orbitalPeriod*visualCorrection)) / m_framesPerSecond;
Oliver Bock's avatar
Oliver Bock committed
791
792
793
794
}

void PulsarAnimationWidget::updateOrbitRadii()
{
795
796
797
798
    m_pulsarSemiMajorAxis = 1.0015e-5 * pow(
                                            (pow(m_orbitalPeriod, 2.0) * pow(m_companionMass, 3.0)) / pow(m_pulsarMass+m_companionMass, 2.0),
                                            1.0/3.0
                                        );
Oliver Bock's avatar
Oliver Bock committed
799

800
    m_companionSemiMajorAxis = (m_pulsarMass/m_companionMass) * m_pulsarSemiMajorAxis;
Oliver Bock's avatar
Oliver Bock committed
801

802
    emit pulsarSemiMajorAxisUpdated(m_pulsarSemiMajorAxis);
Oliver Bock's avatar
Oliver Bock committed
803
804
}

805
806
void PulsarAnimationWidget::resetParameters()
{
807
808
809
    m_pulsarRotationAngle = 0.0;
    m_orbitRotationAngle = 0.0;
    updatePulseProfile();
Oliver Bock's avatar
Oliver Bock committed
810

811
    emit pulsarAnimationStep(m_pulsarRotationAngle);
812
}
813
814
815

void PulsarAnimationWidget::updatePulseProfile()
{
816
817
818
    // avoid division by zero (keep profile visible if pulsar doesn't spin)
    double pulsarRotationDelta = m_pulsarRotationDelta == 0.0 ? 0.01 : m_pulsarRotationDelta;

819
820
821
822
823
    // prepare parameters (e.g. convert to radians where necessary)
    const double    i               = deg2rad * m_pulsarSpinAxisInclination;
    const double    y               = deg2rad * m_pulsarMagneticAxisInclination;
    double          phiOrb          = deg2rad * (m_orbitRotationAngle + 90.0);
    const double    deltaPhiRot     = deg2rad * 1.0;
824
    const double    deltaPhiOrb     = deg2rad * deltaPhiRot * m_orbitRotationDelta / pulsarRotationDelta;
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
    const double    rp              = m_pulsarSemiMajorAxis;
    const double    xk              = -m_cameraPosZ;
    const double    yk              = -m_cameraPosX;
    const double    zk              = m_cameraPosY;
    const double    cam             = pow(xk, 2.0) + pow(yk, 2.0) + pow(zk, 2.0);
    const double    alpha           = deg2rad * (90.0 - m_mouseAngleH);
    const double    delta           = deg2rad * m_mouseAngleV;
    const double    gaussProfile    = 0.012337;

    for(int x = 0; x < 360; ++x) {
        // determine angle between pulsar's magnetic axis and line of sight
        phiOrb += deltaPhiOrb;
        const double phiRot = x * deltaPhiRot;

        double a = -sin(y) * sin(phiRot) * (xk + rp * cos(phiOrb)) \
                   + (cos(i) * sin(y) * cos(phiRot) + sin(i) * cos(y)) * (yk + rp * sin(phiOrb)) \
                   - (sin(i) * sin(y) * cos(phiRot) - cos(i) * cos(y)) * zk;

        double b = sqrt(pow(rp,2.0) + cam - (2.0 * sqrt(cam) * rp * cos(delta) * sin(alpha + phiOrb)));

        // determine and store pulse amplitude
        m_pulseProfile[x] = exp(-2.0 * (1.0 - fabs(a/b)) / gaussProfile);
    }

    // propagate new profile
    emit pulseProfileUpdated(m_pulseProfile);
851
}