pulsaranimationwidget.cpp 6.85 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 24 25
PulsarAnimationWidget::PulsarAnimationWidget(QWidget *parent) :
	QGLWidget(parent),
	m_frameTimer(),
26 27
	m_pulsarSpinTimer(),
	m_orbitTimer()
Oliver Bock's avatar
Oliver Bock committed
28
{
Oliver Bock's avatar
Oliver Bock committed
29 30
    connect(&m_frameTimer, SIGNAL(timeout()), this, SLOT(updateFrame()));
    connect(&m_pulsarSpinTimer, SIGNAL(timeout()), this, SLOT(updatePulsarSpin()));
31
    connect(&m_orbitTimer, SIGNAL(timeout()), this, SLOT(updateOrbit()));
Oliver Bock's avatar
Oliver Bock committed
32

33 34 35
	m_quadricPulsar = NULL;
	m_quadricPulsarCone1 = NULL;
	m_quadricPulsarCone2 = NULL;
Oliver Bock's avatar
Oliver Bock committed
36 37

	m_framesPerSecond = 25;
38

Oliver Bock's avatar
Oliver Bock committed
39
	setPulsarSpinFrequency(0.5f);
40 41
	m_pulsarRotationAngle = 0.0f;

Oliver Bock's avatar
Oliver Bock committed
42
	setOrbitFrequency(0.2f);
43 44
	setOrbitRadius(4.0f);
	m_orbitRotationAngle = 0.0f;
Oliver Bock's avatar
Oliver Bock committed
45 46 47 48
}

PulsarAnimationWidget::~PulsarAnimationWidget()
{
Oliver Bock's avatar
Oliver Bock committed
49 50 51
	if(m_quadricPulsar) gluDeleteQuadric(m_quadricPulsar);
	if(m_quadricPulsarCone1) gluDeleteQuadric(m_quadricPulsarCone1);
	if(m_quadricPulsarCone2) gluDeleteQuadric(m_quadricPulsarCone2);
Oliver Bock's avatar
Oliver Bock committed
52 53 54 55
}

void PulsarAnimationWidget::initializeGL()
{
Oliver Bock's avatar
Oliver Bock committed
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
	glShadeModel(GL_SMOOTH);
	glClearColor(0.0, 0.0, 0.0, 0.0);
	glClearDepth(1.0f);
	glEnable(GL_DEPTH_TEST);
	glDepthFunc(GL_LEQUAL);
	glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);

	GLfloat LightAmbient[] = { 0.0f, 0.0f, 0.0f, 1.0f };
	GLfloat LightDiffuse[] = { 1.0f, 1.0f, 1.0f, 1.0f };
	GLfloat LightSpecular[] = { 1.0f, 1.0f, 1.0f, 1.0f };
	GLfloat LightPosition[] = { 0.0f, 0.0f, 3.0f, 1.0f };
	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, 20.0);
	glLightfv(GL_LIGHT0, GL_SPOT_DIRECTION, spot_direction);
	glLightf(GL_LIGHT0, GL_SPOT_EXPONENT, 5.0);

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

Oliver Bock's avatar
Oliver Bock committed
81 82 83 84 85 86
	m_quadricPulsar = gluNewQuadric();
    m_quadricPulsarCone1 = gluNewQuadric();
    m_quadricPulsarCone2 = gluNewQuadric();
    gluQuadricNormals(m_quadricPulsar, GLU_SMOOTH);
    gluQuadricNormals(m_quadricPulsarCone1, GLU_SMOOTH);
    gluQuadricNormals(m_quadricPulsarCone2, GLU_SMOOTH);
Oliver Bock's avatar
Oliver Bock committed
87 88 89 90
}

void PulsarAnimationWidget::resizeGL(int w, int h)
{
Oliver Bock's avatar
Oliver Bock committed
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
	glViewport(0, 0, w, h);

	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();

	gluPerspective(45.0f, (GLfloat)w / (GLfloat)h, 0.1f, 100.0f);

	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();
}

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

	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();

	gluLookAt(0.0, 6.0, 10.0, 0.0, 0.0, 0.0, 0.0, 2.0, -5.0);

	// TODO: should be located elsewhere
	static GLfloat no_mat[] = { 0.0, 0.0, 0.0, 1.0 };
	static GLfloat mat_ambient[] = { 0.7, 0.7, 0.7, 1.0 };
	static GLfloat mat_ambient_color[] = { 0.8, 0.8, 0.2, 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 no_shininess[] = { 0.0 };
	static GLfloat low_shininess[] = { 5.0 };
	static GLfloat high_shininess[] = { 100.0 };
	static GLfloat mat_emission[] = { 0.3, 0.2, 0.2, 0.0 };
	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
126

127 128 129 130 131 132 133
	glPushMatrix();
	gluSphere(m_quadricPulsar, 1.0f, 32, 32);
	glPopMatrix();

	glRotatef(m_orbitRotationAngle, 0.0f, 1.0f, 0.0f);
	glTranslatef(0.0f, 0.0f, m_orbitRadius);

Oliver Bock's avatar
Oliver Bock committed
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
	glPushMatrix();
	glRotatef(m_pulsarRotationAngle, 0.0f, 1.0f, 0.0f);
	gluSphere(m_quadricPulsar, 1.0f, 32, 32);
	glPopMatrix();

	glPushMatrix();
	glRotatef(m_pulsarRotationAngle-90, 0.0f, 1.0f, 0.0f);
	glTranslatef(0.0f, 0.0f, -4.0f);
	gluCylinder(m_quadricPulsarCone1, 0.5f, 0.0f, 3.0f, 32, 32);
	glPopMatrix();

	glPushMatrix();
	glRotatef(m_pulsarRotationAngle+90, 0.0f, 1.0f, 0.0f);
	glTranslatef(0.0f, 0.0f, -4.0f);
	gluCylinder(m_quadricPulsarCone2, 0.5f, 0.0f, 3.0f, 32, 32);
	glPopMatrix();
Oliver Bock's avatar
Oliver Bock committed
150 151
}

Oliver Bock's avatar
Oliver Bock committed
152 153 154 155
void PulsarAnimationWidget::runAnimation()
{
	m_frameTimer.start(1000.0 / m_framesPerSecond);
	m_pulsarSpinTimer.start(m_pulsarSpinFrequency);
156
	m_orbitTimer.start(m_orbitFrequency);
Oliver Bock's avatar
Oliver Bock committed
157
}
Oliver Bock's avatar
Oliver Bock committed
158

Oliver Bock's avatar
Oliver Bock committed
159 160 161 162
void PulsarAnimationWidget::pauseAnimation()
{
	m_frameTimer.stop();
	m_pulsarSpinTimer.stop();
163
	m_orbitTimer.stop();
Oliver Bock's avatar
Oliver Bock committed
164 165 166 167 168 169
}

void PulsarAnimationWidget::stopAnimation()
{
	m_frameTimer.stop();
	m_pulsarSpinTimer.stop();
170
	m_orbitTimer.stop();
Oliver Bock's avatar
Oliver Bock committed
171 172

	m_pulsarRotationAngle = 0.0;
173
	m_orbitRotationAngle = 0.0;
Oliver Bock's avatar
Oliver Bock committed
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
	updateGL();
}

void PulsarAnimationWidget::updateFrame()
{
	updateGL();
}

void PulsarAnimationWidget::updatePulsarSpin()
{
	if(m_pulsarRotationAngle <= 360.0) {
		m_pulsarRotationAngle++;
	}
	else {
		m_pulsarRotationAngle = 0.0;
	}
}

192 193 194 195 196 197 198 199 200 201
void PulsarAnimationWidget::updateOrbit()
{
	if(m_orbitRotationAngle <= 360.0) {
		m_orbitRotationAngle++;
	}
	else {
		m_orbitRotationAngle = 0.0;
	}
}

Oliver Bock's avatar
Oliver Bock committed
202 203 204 205 206 207 208 209 210
void PulsarAnimationWidget::setFramePerSecond(const unsigned int fps)
{
	m_framesPerSecond = fps;
}

void PulsarAnimationWidget::setPulsarSpinFrequency(const float frequency)
{
	m_pulsarSpinFrequency = (unsigned int) (1000 / (frequency * 360.0f));
	m_pulsarSpinTimer.setInterval(m_pulsarSpinFrequency);
Oliver Bock's avatar
Oliver Bock committed
211
}
212 213 214 215 216 217 218 219 220 221 222

void PulsarAnimationWidget::setOrbitRadius(const float radius)
{
	m_orbitRadius = -1.0 * radius;
}

void PulsarAnimationWidget::setOrbitFrequency(const float frequency)
{
	m_orbitFrequency = (unsigned int) (1000 / (frequency * 360.0f));
	m_orbitTimer.setInterval(m_orbitFrequency);
}