pulsaranimationwidget.cpp 19.8 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) :
Oliver Bock's avatar
Oliver Bock committed
26
	QGLWidget(QGLFormat(QGL::AlphaChannel | QGL::SampleBuffers), parent),
27
	m_frameTimer(),
Oliver Bock's avatar
Oliver Bock committed
28
	m_pulseProfile(360, 0.0)
Oliver Bock's avatar
Oliver Bock committed
29
{
Oliver Bock's avatar
Oliver Bock committed
30 31 32 33 34 35
	if(!format().directRendering()) {
		qWarning("Sorry, no direct rendering support for animation...");
	}
	if(!format().doubleBuffer()) {
		qWarning("Sorry, no double buffering support for animation...");
	}
36
	if(!format().rgba()) {
Oliver Bock's avatar
Oliver Bock committed
37
		qWarning("Sorry, no RGBA support for animation...");
38 39
	}
	if(!format().alpha()) {
Oliver Bock's avatar
Oliver Bock committed
40
		qWarning("Sorry, no alpha channel support for animation...");
41
	}
Oliver Bock's avatar
Oliver Bock committed
42
	if(!format().sampleBuffers()) {
Oliver Bock's avatar
Oliver Bock committed
43
		qWarning("Sorry, no multisampling support for animation...");
Oliver Bock's avatar
Oliver Bock committed
44
	}
45

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

Oliver Bock's avatar
Oliver Bock committed
49
    // initialize quadric pointers
Oliver Bock's avatar
Oliver Bock committed
50 51
    m_quadricCompanionOrbitPlane = NULL;
    m_quadricCompanion = NULL;
52
    m_quadricPulsarOrbitPlane = NULL;
Oliver Bock's avatar
Oliver Bock committed
53
    m_quadricPulsar = NULL;
54 55
	m_quadricPulsarCone1 = NULL;
	m_quadricPulsarCone2 = NULL;
56
	m_quadricPulsarSpinAxis = NULL;
57
	m_quadricPulsarMagneticAxis = NULL;
Oliver Bock's avatar
Oliver Bock committed
58

Oliver Bock's avatar
Oliver Bock committed
59
	// initialize texture pointers
60 61
	m_pulsarTexture = 0;
	m_backgroundTexture = 0;
62

Oliver Bock's avatar
Oliver Bock committed
63
	// initial render timing settings
Oliver Bock's avatar
Oliver Bock committed
64
	m_framesPerSecond = 25;
Oliver Bock's avatar
Oliver Bock committed
65 66
	m_pulsarRotationDelta = 0.0;
	m_orbitRotationDelta = 0.0;
67 68
	m_pulsarRotationAngle = 0.0;
	m_orbitRotationAngle = 0.0;
69

Oliver Bock's avatar
Oliver Bock committed
70
	// initial binary system parameters (have to match GUI!)
Oliver Bock's avatar
Oliver Bock committed
71
	m_pulsarMass = 1.4;
Oliver Bock's avatar
Oliver Bock committed
72
	// initial companion is "Neutron Star"
Oliver Bock's avatar
Oliver Bock committed
73 74 75 76
	m_companionMass = 1.4;
	m_pulsarSpinAxisInclination = 0.0;
	m_pulsarMagneticAxisInclination = 45.0;
	m_pulsarSemiMajorAxis = 5.0;
Oliver Bock's avatar
Oliver Bock committed
77 78
	m_companionSemiMajorAxis = (m_pulsarMass/m_companionMass) * m_pulsarSemiMajorAxis;
	// initial spin frequency of 0.5 Hz
Oliver Bock's avatar
Oliver Bock committed
79
	m_pulsarRotationDelta = (360.0 * 0.5) / m_framesPerSecond;
Oliver Bock's avatar
Oliver Bock committed
80 81
	// update orbital period (based on settings above)
	updateOrbitPeriod();
Oliver Bock's avatar
Oliver Bock committed
82

Oliver Bock's avatar
Oliver Bock committed
83
	// initial view features
84
	m_showOrbits = false;
Oliver Bock's avatar
Oliver Bock committed
85
	m_showRotationAxes = false;
86
	m_cameraInteraction = false;
87

Oliver Bock's avatar
Oliver Bock committed
88
	// initial view settings
Oliver Bock's avatar
Oliver Bock committed
89 90 91 92
	m_mouseAngleH = 90.0;
	m_mouseAngleV = 30.0;
	m_cameraZoom = 15.0;
	m_cameraZoomLBound = 2.0;
Oliver Bock's avatar
Oliver Bock committed
93 94
	m_mouseLastX = 0;
	m_mouseLastY = 0;
95

Oliver Bock's avatar
Oliver Bock committed
96
	// update camera based on settings above
97
	updateCameraPosition(m_mouseAngleH, m_mouseAngleV, m_cameraZoom);
98

Oliver Bock's avatar
Oliver Bock committed
99
	// finally, create initial pulse profile based on settings above
100
	updatePulseProfile();
Oliver Bock's avatar
Oliver Bock committed
101 102 103 104
}

PulsarAnimationWidget::~PulsarAnimationWidget()
{
Oliver Bock's avatar
Oliver Bock committed
105 106
	if(m_quadricCompanionOrbitPlane) gluDeleteQuadric(m_quadricCompanionOrbitPlane);
	if(m_quadricCompanion) gluDeleteQuadric(m_quadricCompanion);
107
	if(m_quadricPulsarOrbitPlane) gluDeleteQuadric(m_quadricPulsarOrbitPlane);
Oliver Bock's avatar
Oliver Bock committed
108 109 110
	if(m_quadricPulsar) gluDeleteQuadric(m_quadricPulsar);
	if(m_quadricPulsarCone1) gluDeleteQuadric(m_quadricPulsarCone1);
	if(m_quadricPulsarCone2) gluDeleteQuadric(m_quadricPulsarCone2);
111
	if(m_quadricPulsarSpinAxis) gluDeleteQuadric(m_quadricPulsarSpinAxis);
112
	if(m_quadricPulsarMagneticAxis) gluDeleteQuadric(m_quadricPulsarMagneticAxis);
113 114 115

	if(m_pulsarTexture) deleteTexture(m_pulsarTexture);
	if(m_backgroundTexture) deleteTexture(m_backgroundTexture);
Oliver Bock's avatar
Oliver Bock committed
116 117 118 119
}

void PulsarAnimationWidget::initializeGL()
{
Oliver Bock's avatar
Oliver Bock committed
120
	glClearColor(0.0, 0.0, 0.0, 0.0);
Oliver Bock's avatar
Oliver Bock committed
121
	glClearDepth(1.0);
Oliver Bock's avatar
Oliver Bock committed
122
	glDepthFunc(GL_LEQUAL);
123 124 125
	glEnable(GL_DEPTH_TEST);

	glShadeModel(GL_SMOOTH);
Oliver Bock's avatar
Oliver Bock committed
126 127
	glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);

Oliver Bock's avatar
Oliver Bock committed
128 129 130 131 132
	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};
Oliver Bock's avatar
Oliver Bock committed
133 134 135 136 137

	glLightfv(GL_LIGHT0, GL_AMBIENT, LightAmbient);
	glLightfv(GL_LIGHT0, GL_DIFFUSE, LightDiffuse);
	glLightfv(GL_LIGHT0, GL_SPECULAR, LightSpecular);
	glLightfv(GL_LIGHT0, GL_POSITION, LightPosition);
138
	glLightf(GL_LIGHT0, GL_SPOT_CUTOFF, 50.0);
Oliver Bock's avatar
Oliver Bock committed
139
	glLightfv(GL_LIGHT0, GL_SPOT_DIRECTION, spot_direction);
140
	glLightf(GL_LIGHT0, GL_SPOT_EXPONENT, 10.0);
Oliver Bock's avatar
Oliver Bock committed
141 142 143 144

	glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, GL_TRUE);
	glEnable(GL_LIGHT0);
	glEnable(GL_LIGHTING);
Oliver Bock's avatar
Oliver Bock committed
145

146
	glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
147
	glEnable(GL_BLEND);
148

Oliver Bock's avatar
Oliver Bock committed
149 150
	m_quadricCompanionOrbitPlane = gluNewQuadric();
	m_quadricCompanion = gluNewQuadric();
151
	m_quadricPulsarOrbitPlane = gluNewQuadric();
Oliver Bock's avatar
Oliver Bock committed
152 153 154
	m_quadricPulsar = gluNewQuadric();
    m_quadricPulsarCone1 = gluNewQuadric();
    m_quadricPulsarCone2 = gluNewQuadric();
155
    m_quadricPulsarSpinAxis = gluNewQuadric();
156
    m_quadricPulsarMagneticAxis = gluNewQuadric();
157

Oliver Bock's avatar
Oliver Bock committed
158 159
    gluQuadricNormals(m_quadricCompanionOrbitPlane, GLU_SMOOTH);
    gluQuadricNormals(m_quadricCompanion, GLU_SMOOTH);
160
    gluQuadricNormals(m_quadricPulsarOrbitPlane, GLU_SMOOTH);
Oliver Bock's avatar
Oliver Bock committed
161 162 163
    gluQuadricNormals(m_quadricPulsar, GLU_SMOOTH);
    gluQuadricNormals(m_quadricPulsarCone1, GLU_SMOOTH);
    gluQuadricNormals(m_quadricPulsarCone2, GLU_SMOOTH);
164
    gluQuadricNormals(m_quadricPulsarSpinAxis, GLU_SMOOTH);
165
    gluQuadricNormals(m_quadricPulsarMagneticAxis, GLU_SMOOTH);
166

Oliver Bock's avatar
Oliver Bock committed
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
    // query max texture size (estimate)
    GLint maxTextureSize;
    glGetIntegerv(GL_MAX_TEXTURE_SIZE, &maxTextureSize);

    // prepare and check pulsar texture
    QImage pulsarTexture(":/textures/resources/texture_pulsar.png");
    if(pulsarTexture.width() != pulsarTexture.height()) {
    	qWarning("Warning: pulsar texture shape not quadratic!");
    }
    else {
    	double integer = 0.0;
    	double fraction = 0.0;
    	fraction = modf(log(pulsarTexture.width()) / log(2.0), &integer);
    	if(fraction > 0.0) {
    		qWarning("Warning: pulsar texture dimensions not a power of 2!");
    	}
    }
    if(pulsarTexture.width() > maxTextureSize) {
Oliver Bock's avatar
Oliver Bock committed
185
    	qWarning("Warning: maximum texture size exceeded! Scaling down pulsar texture to %ix%i...", maxTextureSize, maxTextureSize);
Oliver Bock's avatar
Oliver Bock committed
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
    	pulsarTexture = pulsarTexture.scaled(maxTextureSize, maxTextureSize, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
    }

    // prepare and check background texture
    QImage backgroundTexture(":/textures/resources/texture_background_carina.jpg");
    if(backgroundTexture.width() != backgroundTexture.height()) {
    	qWarning("Warning: invalid background texture shape!");
    }
    else {
    	double integer = 0.0;
    	double fraction = 0.0;
    	fraction = modf(log(backgroundTexture.width()) / log(2.0), &integer);
    	if(fraction > 0.0) {
    		qWarning("Warning: background texture dimensions not a power of 2!");
    	}
    }
    if(backgroundTexture.width() > maxTextureSize) {
Oliver Bock's avatar
Oliver Bock committed
203
    	qWarning("Warning: maximum texture size exceeded! Scaling down background texture to %ix%i...", maxTextureSize, maxTextureSize);
Oliver Bock's avatar
Oliver Bock committed
204 205 206 207 208 209
    	backgroundTexture = backgroundTexture.scaled(maxTextureSize, maxTextureSize, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
    }

    // bind textures
    m_pulsarTexture = bindTexture(pulsarTexture, GL_TEXTURE_2D, GL_RGBA);
    m_backgroundTexture = bindTexture(backgroundTexture, GL_TEXTURE_2D, GL_RGB);
210

211
    // use mipmapped textures
212 213 214
    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
215 216 217
    // enable 2D mapping (s/t coordinates, sphere map won't rotate texture!)
    glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);
    glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);
Oliver Bock's avatar
Oliver Bock committed
218 219 220 221
}

void PulsarAnimationWidget::resizeGL(int w, int h)
{
Oliver Bock's avatar
Oliver Bock committed
222 223 224 225 226
	glViewport(0, 0, w, h);

	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();

Oliver Bock's avatar
Oliver Bock committed
227
	gluPerspective(45.0, (GLfloat)w / (GLfloat)h, 0.1, 500.0);
Oliver Bock's avatar
Oliver Bock committed
228 229 230 231 232 233 234 235 236 237 238 239

	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();
}

void PulsarAnimationWidget::paintGL()
{
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();

240 241
	gluLookAt(m_cameraPosX, m_cameraPosY, m_cameraPosZ,
			  0.0, 0.0, 0.0,
242
			  0.0, 1.0, 0.0);
Oliver Bock's avatar
Oliver Bock committed
243 244

	// TODO: should be located elsewhere
Oliver Bock's avatar
Oliver Bock committed
245 246 247 248 249
	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};
250

Oliver Bock's avatar
Oliver Bock committed
251 252 253 254 255
	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);
Oliver Bock's avatar
Oliver Bock committed
256

257
	// draw companion
Oliver Bock's avatar
Oliver Bock committed
258
	glPushMatrix();
Oliver Bock's avatar
Oliver Bock committed
259 260 261 262
		glTranslatef(sin((m_orbitRotationAngle + 180.0) * deg2rad) * m_companionSemiMajorAxis,
					 0.0,
					 cos((m_orbitRotationAngle + 180.0) * deg2rad) * m_companionSemiMajorAxis);
		gluSphere(m_quadricCompanion, 1.0, 32, 32);
Oliver Bock's avatar
Oliver Bock committed
263 264
	glPopMatrix();

265
	// draw pulsar
Oliver Bock's avatar
Oliver Bock committed
266
	glPushMatrix();
Oliver Bock's avatar
Oliver Bock committed
267
		glTranslatef(sin(m_orbitRotationAngle * deg2rad) * m_pulsarSemiMajorAxis,
Oliver Bock's avatar
Oliver Bock committed
268
					 0.0,
Oliver Bock's avatar
Oliver Bock committed
269
					 cos(m_orbitRotationAngle * deg2rad) * m_pulsarSemiMajorAxis);
270 271

		glPushMatrix();
Oliver Bock's avatar
Oliver Bock committed
272 273
			glRotatef(m_pulsarSpinAxisInclination, 0.0, 0.0, 1.0);
			glRotatef(m_pulsarRotationAngle, 0.0, 1.0, 0.0);
274

275
			// draw spin axis
Oliver Bock's avatar
Oliver Bock committed
276
			if(m_showRotationAxes) {
277 278
				glPushMatrix();
					glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, translucent);
Oliver Bock's avatar
Oliver Bock committed
279 280 281
					glRotatef(90.0, 1.0, 0.0, 0.0);
					glTranslatef(0.0, 0.0, -5.0);
					gluCylinder(m_quadricPulsarSpinAxis, 0.05, 0.05, 10.0, 32, 1);
282 283 284
				glPopMatrix();
			}

285 286 287 288 289
			// create texture coordinates and enable texturing
	        glEnable(GL_TEXTURE_GEN_S);
	        glEnable(GL_TEXTURE_GEN_T);
	        glBindTexture(GL_TEXTURE_2D, m_pulsarTexture);
	        glEnable(GL_TEXTURE_2D);
290 291 292

			glMaterialfv(GL_FRONT, GL_AMBIENT, no_mat);
			glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
Oliver Bock's avatar
Oliver Bock committed
293
			gluSphere(m_quadricPulsar, 1.0, 32, 32);
294

295
			// disable texturing
296 297 298
	        glDisable(GL_TEXTURE_GEN_S);
	        glDisable(GL_TEXTURE_GEN_T);
	        glDisable(GL_TEXTURE_2D);
299 300
		glPopMatrix();

301
		// TODO: should be located elsewhere
Oliver Bock's avatar
Oliver Bock committed
302 303 304
		static GLfloat coneAmbient[] = {1.0, 1.0, 0.0, 1.0};
		static GLfloat coneDiffuse[] = {1.0, 1.0, 0.0, 1.0};
		static GLfloat coneSpecular[] = {1.0, 1.0, 0.5, 1.0};
305 306 307

		glMaterialfv(GL_FRONT, GL_SPECULAR, coneSpecular);

308
		// first cone
309
		glPushMatrix();
Oliver Bock's avatar
Oliver Bock committed
310 311
			glRotatef(90.0, 1.0, 0.0, 0.0);
			glRotatef(m_pulsarSpinAxisInclination, 0.0, 1.0, 0.0);
312

Oliver Bock's avatar
Oliver Bock committed
313 314
			glRotatef(-m_pulsarRotationAngle - 90.0, 0.0, 0.0, 1.0);
			glRotatef(-m_pulsarMagneticAxisInclination, 1.0, 0.0, 0.0);
315

316
			// draw magnetic axis (for both cones)
Oliver Bock's avatar
Oliver Bock committed
317
			if(m_showRotationAxes) {
318 319
				glPushMatrix();
					glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, translucent);
Oliver Bock's avatar
Oliver Bock committed
320 321
					glTranslatef(0.0, 0.0, -5.0);
					gluCylinder(m_quadricPulsarMagneticAxis, 0.05, 0.05, 10.0, 32, 1);
322
				glPopMatrix();
323 324
			}

Oliver Bock's avatar
Oliver Bock committed
325
			glTranslatef(0.0, 0.0, -4.0);
326 327 328

			glMaterialfv(GL_FRONT, GL_AMBIENT, coneAmbient);
			glMaterialfv(GL_FRONT, GL_DIFFUSE, coneDiffuse);
Oliver Bock's avatar
Oliver Bock committed
329
			gluCylinder(m_quadricPulsarCone1, 0.475, 0.0, 3.0, 32, 32);
330 331
		glPopMatrix();

332
		// second cone
333
		glPushMatrix();
Oliver Bock's avatar
Oliver Bock committed
334 335
			glRotatef(-90.0, 1.0, 0.0, 0.0);
			glRotatef(-m_pulsarSpinAxisInclination, 0.0, 1.0, 0.0);
336

Oliver Bock's avatar
Oliver Bock committed
337 338
			glRotatef(m_pulsarRotationAngle - 90.0, 0.0, 0.0, 1.0);
			glRotatef(m_pulsarMagneticAxisInclination, 1.0, 0.0, 0.0);
339

Oliver Bock's avatar
Oliver Bock committed
340
			glTranslatef(0.0, 0.0, -4.0);
341 342 343

			glMaterialfv(GL_FRONT, GL_AMBIENT, coneAmbient);
			glMaterialfv(GL_FRONT, GL_DIFFUSE, coneDiffuse);
Oliver Bock's avatar
Oliver Bock committed
344
			gluCylinder(m_quadricPulsarCone2, 0.475, 0.0, 3.0, 32, 32);
345
		glPopMatrix();
Oliver Bock's avatar
Oliver Bock committed
346
	glPopMatrix();
347

348 349 350 351
	// save current state
	glMatrixMode(GL_PROJECTION);
	glPushMatrix();
	glLoadIdentity();
Oliver Bock's avatar
Oliver Bock committed
352
	glOrtho(0, width(), 0, height(), 0.1, 501.0);
353 354 355 356 357 358
	glMatrixMode(GL_MODELVIEW);
	glPushMatrix();
	glLoadIdentity();

	// draw backdrop (independent parallel projection)
	glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, mat_specular);
Oliver Bock's avatar
Oliver Bock committed
359 360
	glTranslatef(0.0, 0.0, -501.0);
	drawTexture(QPointF(0.0, 0.0), m_backgroundTexture);
361 362 363 364 365 366 367

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

Oliver Bock's avatar
Oliver Bock committed
368
	// draw orbital planes
369
	if(m_cameraInteraction || m_showOrbits) {
370
		glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, translucent);
371

Oliver Bock's avatar
Oliver Bock committed
372
		static double sizeOffset = 0.25;
373 374 375 376

		// companion's plane (only if not identical with pulsar's)
		if(m_companionSemiMajorAxis != m_pulsarSemiMajorAxis) {
			glPushMatrix();
Oliver Bock's avatar
Oliver Bock committed
377
				glRotatef(90.0, 1.0, 0.0, 0.0);
378 379 380 381

				// 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)
Oliver Bock's avatar
Oliver Bock committed
382 383
				if((int)(10*(m_pulsarMass-m_companionMass)+0.5) == 1){
					glTranslatef(0.0, 0.0, 0.01);
384 385 386 387 388 389 390 391 392 393
				}

				gluDisk(m_quadricCompanionOrbitPlane,
						m_companionSemiMajorAxis - sizeOffset,
						m_companionSemiMajorAxis + sizeOffset,
						64, 1);
			glPopMatrix();
		}

		// pulsar's plane
Oliver Bock's avatar
Oliver Bock committed
394
		glPushMatrix();
Oliver Bock's avatar
Oliver Bock committed
395
			glRotatef(90.0, 1.0, 0.0, 0.0);
396
			gluDisk(m_quadricPulsarOrbitPlane,
397 398
					m_pulsarSemiMajorAxis - sizeOffset,
					m_pulsarSemiMajorAxis + sizeOffset,
399 400
					64, 1);
		glPopMatrix();
401
	}
Oliver Bock's avatar
Oliver Bock committed
402 403
}

Oliver Bock's avatar
Oliver Bock committed
404 405
void PulsarAnimationWidget::runAnimation()
{
Oliver Bock's avatar
Oliver Bock committed
406
	m_frameTimer.start(1000.0 / m_framesPerSecond);
Oliver Bock's avatar
Oliver Bock committed
407
}
Oliver Bock's avatar
Oliver Bock committed
408

Oliver Bock's avatar
Oliver Bock committed
409 410 411 412 413 414 415 416
void PulsarAnimationWidget::pauseAnimation()
{
	m_frameTimer.stop();
}

void PulsarAnimationWidget::stopAnimation()
{
	m_frameTimer.stop();
417
	resetParameters();
Oliver Bock's avatar
Oliver Bock committed
418 419 420 421

	updateGL();
}

Oliver Bock's avatar
Oliver Bock committed
422
void PulsarAnimationWidget::updateFrame()
Oliver Bock's avatar
Oliver Bock committed
423
{
Oliver Bock's avatar
Oliver Bock committed
424
	m_pulsarRotationAngle += m_pulsarRotationDelta;
Oliver Bock's avatar
Oliver Bock committed
425 426
	if(m_pulsarRotationAngle > 360.0) {
		m_pulsarRotationAngle -= 360.0;
427
		updatePulseProfile();
Oliver Bock's avatar
Oliver Bock committed
428
	}
Oliver Bock's avatar
Oliver Bock committed
429
	m_orbitRotationAngle += m_orbitRotationDelta;
Oliver Bock's avatar
Oliver Bock committed
430 431
	if(m_orbitRotationAngle > 360.0) {
		m_orbitRotationAngle -= 360.0;
Oliver Bock's avatar
Oliver Bock committed
432 433
	}

Oliver Bock's avatar
Oliver Bock committed
434
	updateGL();
Oliver Bock's avatar
Oliver Bock committed
435

436
	emit pulsarAnimationStep(m_pulsarRotationAngle);
437 438
}

439 440 441 442 443 444 445
void PulsarAnimationWidget::showOrbits(bool enabled)
{
	m_showOrbits = enabled;

	updateGL();
}

Oliver Bock's avatar
Oliver Bock committed
446 447 448 449 450 451 452
void PulsarAnimationWidget::showRotationAxes(bool enabled)
{
	m_showRotationAxes = enabled;

	updateGL();
}

453 454
void PulsarAnimationWidget::mousePressEvent(QMouseEvent *event)
{
455 456
	Q_UNUSED(event);

457 458 459 460
	m_cameraInteraction = true;
	updateGL();
}

461 462 463 464 465
void PulsarAnimationWidget::mouseMoveEvent(QMouseEvent *event)
{
	Qt::MouseButtons buttons = event->buttons();
	if((buttons & Qt::LeftButton) == Qt::LeftButton) {
		if(m_mouseLastX != 0) {
Oliver Bock's avatar
Oliver Bock committed
466
			m_mouseAngleH += (m_mouseLastX - event->x());
Oliver Bock's avatar
Oliver Bock committed
467
			m_mouseAngleH = m_mouseAngleH < 360 ? m_mouseAngleH : 0;
Oliver Bock's avatar
Oliver Bock committed
468
			m_mouseAngleH = m_mouseAngleH >= 0 ? m_mouseAngleH : 359;
469 470
		}
		if(m_mouseLastY != 0) {
Oliver Bock's avatar
Oliver Bock committed
471
			m_mouseAngleV -= (m_mouseLastY - event->y());
Oliver Bock's avatar
Oliver Bock committed
472 473
			m_mouseAngleV = m_mouseAngleV < 90 ? m_mouseAngleV : 90;
			m_mouseAngleV = m_mouseAngleV > -90 ? m_mouseAngleV : -90;
474 475 476 477 478 479 480
		}

		m_mouseLastX = event->x();
		m_mouseLastY = event->y();
	}
	else if((buttons & Qt::RightButton) == Qt::RightButton) {
		if(m_mouseLastY != 0) {
Oliver Bock's avatar
Oliver Bock committed
481
			m_cameraZoom -= (m_mouseLastY - event->y());
Oliver Bock's avatar
Oliver Bock committed
482
			m_cameraZoom = m_cameraZoom >= m_cameraZoomLBound ? m_cameraZoom : m_cameraZoomLBound;
483 484 485 486 487 488 489 490 491 492
		}

		m_mouseLastY = event->y();
	}

	updateCameraPosition(m_mouseAngleH, m_mouseAngleV, m_cameraZoom);
}

void PulsarAnimationWidget::mouseReleaseEvent(QMouseEvent *event)
{
493 494
	Q_UNUSED(event);

495 496
	m_mouseLastX = 0;
	m_mouseLastY = 0;
497
	m_cameraInteraction = false;
498

499
	updateGL();
500 501
}

Oliver Bock's avatar
Oliver Bock committed
502
void PulsarAnimationWidget::updateCameraPosition(const int angleH, const int angleV, const double zoom)
503
{
504
	m_cameraPosX = sin(angleH * deg2rad) * cos(angleV * deg2rad) * zoom;
505
	m_cameraPosY = sin(angleV * deg2rad) * zoom;
Oliver Bock's avatar
Oliver Bock committed
506
	m_cameraPosZ = cos(angleH * deg2rad) * cos(angleV * deg2rad) * zoom;
507

508 509
	updatePulseProfile();

510 511 512
	updateGL();
}

Oliver Bock's avatar
Oliver Bock committed
513 514 515 516 517
void PulsarAnimationWidget::setFramePerSecond(const unsigned int fps)
{
	m_framesPerSecond = fps;
}

Oliver Bock's avatar
Oliver Bock committed
518
void PulsarAnimationWidget::setPulsarSemiMajorAxis(const double length)
Oliver Bock's avatar
Oliver Bock committed
519
{
Oliver Bock's avatar
Oliver Bock committed
520 521 522
	m_pulsarSemiMajorAxis = length;
	m_companionSemiMajorAxis = (m_pulsarMass/m_companionMass) * m_pulsarSemiMajorAxis;
	updateOrbitPeriod();
523
	updatePulseProfile();
Oliver Bock's avatar
Oliver Bock committed
524 525

	updateGL();
Oliver Bock's avatar
Oliver Bock committed
526
}
527

Oliver Bock's avatar
Oliver Bock committed
528
void PulsarAnimationWidget::setCompanionMass(const double mass)
529
{
Oliver Bock's avatar
Oliver Bock committed
530 531
	m_companionMass = mass;
	updateOrbitRadii();
532
	updatePulseProfile();
Oliver Bock's avatar
Oliver Bock committed
533 534

	updateGL();
535 536
}

Oliver Bock's avatar
Oliver Bock committed
537
void PulsarAnimationWidget::setPulsarMass(const double mass)
538
{
Oliver Bock's avatar
Oliver Bock committed
539 540
	m_pulsarMass = mass;
	updateOrbitRadii();
541
	updatePulseProfile();
Oliver Bock's avatar
Oliver Bock committed
542 543 544 545

	updateGL();
}

Oliver Bock's avatar
Oliver Bock committed
546
void PulsarAnimationWidget::setPulsarSpinFrequency(const double frequency)
Oliver Bock's avatar
Oliver Bock committed
547
{
Oliver Bock's avatar
Oliver Bock committed
548
	m_pulsarRotationDelta = (360.0 * frequency) / m_framesPerSecond;
549
	updatePulseProfile();
550
}
551

552 553 554
void PulsarAnimationWidget::setPulsarSpinAxisInclination(const int degrees)
{
	m_pulsarSpinAxisInclination = degrees;
555
	updatePulseProfile();
556 557 558 559

	updateGL();
}

560 561 562
void PulsarAnimationWidget::setPulsarMagneticAxisInclination(const int degrees)
{
	m_pulsarMagneticAxisInclination = degrees;
Oliver Bock's avatar
Oliver Bock committed
563
	updatePulseProfile();
564 565 566

	updateGL();
}
567

Oliver Bock's avatar
Oliver Bock committed
568 569 570
void PulsarAnimationWidget::updateOrbitPeriod()
{
	m_orbitalPeriod = 3.1553e7 * sqrt(
Oliver Bock's avatar
Oliver Bock committed
571 572
						(pow(m_pulsarSemiMajorAxis, 3.0) * pow(m_pulsarMass+m_companionMass, 2.0)) /
						 pow(m_companionMass, 3.0)
Oliver Bock's avatar
Oliver Bock committed
573 574 575 576 577 578 579 580 581 582 583
					  );

	// visual correction factor (increase orbital momentum)
	double visualCorrection = 1e-8;

	m_orbitRotationDelta  = (360.0 / (m_orbitalPeriod*visualCorrection)) / m_framesPerSecond;
}

void PulsarAnimationWidget::updateOrbitRadii()
{
	m_pulsarSemiMajorAxis = 1.0015e-5 * pow(
Oliver Bock's avatar
Oliver Bock committed
584 585 586
								(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
587 588 589 590 591 592 593
							);

	m_companionSemiMajorAxis = (m_pulsarMass/m_companionMass) * m_pulsarSemiMajorAxis;

	emit pulsarSemiMajorAxisUpdated(m_pulsarSemiMajorAxis);
}

594 595
void PulsarAnimationWidget::resetParameters()
{
Oliver Bock's avatar
Oliver Bock committed
596 597
	m_pulsarRotationAngle = 0.0;
	m_orbitRotationAngle = 0.0;
598
	updatePulseProfile();
Oliver Bock's avatar
Oliver Bock committed
599

600
	emit pulsarAnimationStep(m_pulsarRotationAngle);
601
}
602 603 604

void PulsarAnimationWidget::updatePulseProfile()
{
Oliver Bock's avatar
Oliver Bock committed
605 606 607 608 609 610 611 612 613 614
	// 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;
	const double	deltaPhiRot		= deg2rad * 1.0;
	const double	deltaPhiOrb		= deg2rad * deltaPhiRot * m_orbitRotationDelta / m_pulsarRotationDelta;
	const double	rp				= m_pulsarSemiMajorAxis;
	const double	xk				= -m_cameraPosZ;
	const double	yk				= -m_cameraPosX;
	const double	zk				= m_cameraPosY;
Oliver Bock's avatar
Oliver Bock committed
615 616
	const double	cam				= pow(xk, 2.0) + pow(yk, 2.0) + pow(zk, 2.0);
	const double	alpha			= deg2rad * (90.0 - m_mouseAngleH);
Oliver Bock's avatar
Oliver Bock committed
617 618
	const double	delta			= deg2rad * m_mouseAngleV;
	const double	gaussProfile	= 0.012337;
619 620 621

	for(int x = 0; x < 360; ++x) {
		// determine angle between pulsar's magnetic axis and line of sight
Oliver Bock's avatar
Oliver Bock committed
622
		phiOrb += deltaPhiOrb;
623 624 625 626 627 628
		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;

Oliver Bock's avatar
Oliver Bock committed
629
		double b = sqrt(pow(rp,2.0) + cam - (2.0 * sqrt(cam) * rp * cos(delta) * sin(alpha + phiOrb)));
630

Oliver Bock's avatar
Oliver Bock committed
631 632
		// determine and store pulse amplitude
		m_pulseProfile[x] = exp(-2.0 * (1.0 - fabs(a/b)) / gaussProfile);
633 634 635 636 637
	}

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