Commit 1263c133 authored by Oliver Bock's avatar Oliver Bock

Improved animation code

* Better efficiency (of course one timer is sufficient!)
* Less complexity, higher accuracy (just compute the actual rotations!)
parent 4ba0eaf2
...@@ -22,13 +22,9 @@ ...@@ -22,13 +22,9 @@
PulsarAnimationWidget::PulsarAnimationWidget(QWidget *parent) : PulsarAnimationWidget::PulsarAnimationWidget(QWidget *parent) :
QGLWidget(parent), QGLWidget(parent),
m_frameTimer(), m_frameTimer()
m_pulsarSpinTimer(),
m_orbitTimer()
{ {
connect(&m_frameTimer, SIGNAL(timeout()), this, SLOT(updateFrame())); connect(&m_frameTimer, SIGNAL(timeout()), this, SLOT(updateFrame()));
connect(&m_pulsarSpinTimer, SIGNAL(timeout()), this, SLOT(updatePulsarSpin()));
connect(&m_orbitTimer, SIGNAL(timeout()), this, SLOT(updateOrbit()));
m_quadricPulsar = NULL; m_quadricPulsar = NULL;
m_quadricPulsarCone1 = NULL; m_quadricPulsarCone1 = NULL;
...@@ -36,17 +32,17 @@ PulsarAnimationWidget::PulsarAnimationWidget(QWidget *parent) : ...@@ -36,17 +32,17 @@ PulsarAnimationWidget::PulsarAnimationWidget(QWidget *parent) :
m_framesPerSecond = 25; m_framesPerSecond = 25;
setPulsarSpinFrequency(0.5f); setPulsarRotationDelta(0.5f);
m_pulsarRotationAngle = 0.0f; setOrbitRotationDelta(0.2f);
setOrbitFrequency(0.2f);
setOrbitRadius(4.0f); setOrbitRadius(4.0f);
m_pulsarRotationAngle = 0.0f;
m_orbitRotationAngle = 0.0f; m_orbitRotationAngle = 0.0f;
m_mouseLastX = 0; m_mouseLastX = 0;
m_mouseLastY = 0; m_mouseLastY = 0;
m_mouseAngleH = 90; m_mouseAngleH = 90.0f;
m_mouseAngleV = 30; m_mouseAngleV = 30.f;
m_cameraZoom = 10.0f; m_cameraZoom = 10.0f;
m_cameraZoomLBound = 2.0f; m_cameraZoomLBound = 2.0f;
...@@ -164,52 +160,35 @@ void PulsarAnimationWidget::paintGL() ...@@ -164,52 +160,35 @@ void PulsarAnimationWidget::paintGL()
void PulsarAnimationWidget::runAnimation() void PulsarAnimationWidget::runAnimation()
{ {
m_frameTimer.start(1000.0 / m_framesPerSecond); m_frameTimer.start(1000.0f / m_framesPerSecond);
m_pulsarSpinTimer.start(m_pulsarSpinFrequency);
m_orbitTimer.start(m_orbitFrequency);
} }
void PulsarAnimationWidget::pauseAnimation() void PulsarAnimationWidget::pauseAnimation()
{ {
m_frameTimer.stop(); m_frameTimer.stop();
m_pulsarSpinTimer.stop();
m_orbitTimer.stop();
} }
void PulsarAnimationWidget::stopAnimation() void PulsarAnimationWidget::stopAnimation()
{ {
m_frameTimer.stop(); m_frameTimer.stop();
m_pulsarSpinTimer.stop();
m_orbitTimer.stop();
m_pulsarRotationAngle = 0.0; m_pulsarRotationAngle = 0.0;
m_orbitRotationAngle = 0.0; m_orbitRotationAngle = 0.0;
updateGL();
}
void PulsarAnimationWidget::updateFrame()
{
updateGL(); updateGL();
} }
void PulsarAnimationWidget::updatePulsarSpin() void PulsarAnimationWidget::updateFrame()
{ {
if(m_pulsarRotationAngle <= 360.0) { m_pulsarRotationAngle += m_pulsarRotationDelta;
m_pulsarRotationAngle++; if(m_pulsarRotationAngle > 360) {
m_pulsarRotationAngle = 0.0f;
} }
else { m_orbitRotationAngle += m_orbitRotationDelta;
m_pulsarRotationAngle = 0.0; if(m_orbitRotationAngle > 360) {
m_orbitRotationAngle = 0.0f;
} }
}
void PulsarAnimationWidget::updateOrbit() updateGL();
{
if(m_orbitRotationAngle <= 360.0) {
m_orbitRotationAngle++;
}
else {
m_orbitRotationAngle = 0.0;
}
} }
void PulsarAnimationWidget::mouseMoveEvent(QMouseEvent *event) void PulsarAnimationWidget::mouseMoveEvent(QMouseEvent *event)
...@@ -265,20 +244,18 @@ void PulsarAnimationWidget::setFramePerSecond(const unsigned int fps) ...@@ -265,20 +244,18 @@ void PulsarAnimationWidget::setFramePerSecond(const unsigned int fps)
m_framesPerSecond = fps; m_framesPerSecond = fps;
} }
void PulsarAnimationWidget::setPulsarSpinFrequency(const float frequency) void PulsarAnimationWidget::setPulsarRotationDelta(const float frequency)
{ {
m_pulsarSpinFrequency = (unsigned int) (1000 / (frequency * 360.0f) + 0.5f); m_pulsarRotationDelta = (360.0f * frequency) / m_framesPerSecond;
m_pulsarSpinTimer.setInterval(m_pulsarSpinFrequency);
} }
void PulsarAnimationWidget::setOrbitRadius(const float radius) void PulsarAnimationWidget::setOrbitRotationDelta(const float frequency)
{ {
m_orbitRadius = radius; m_orbitRotationDelta = (360.0f * frequency) / m_framesPerSecond;
m_orbitRadiusNeg = -1.0 * radius;
} }
void PulsarAnimationWidget::setOrbitFrequency(const float frequency) void PulsarAnimationWidget::setOrbitRadius(const float radius)
{ {
m_orbitFrequency = (unsigned int) (1000 / (frequency * 360.0f) + 0.5f); m_orbitRadius = radius;
m_orbitTimer.setInterval(m_orbitFrequency); m_orbitRadiusNeg = -1.0 * radius;
} }
...@@ -38,9 +38,10 @@ public: ...@@ -38,9 +38,10 @@ public:
virtual ~PulsarAnimationWidget(); virtual ~PulsarAnimationWidget();
void setFramePerSecond(const unsigned int fps); void setFramePerSecond(const unsigned int fps);
void setPulsarSpinFrequency(const float frequency); void setPulsarRotationDelta(const float frequency);
void setOrbitRotationDelta(const float frequency);
void setOrbitRadius(const float radius); void setOrbitRadius(const float radius);
void setOrbitFrequency(const float frequency);
public slots: public slots:
void runAnimation(); void runAnimation();
...@@ -48,8 +49,6 @@ public slots: ...@@ -48,8 +49,6 @@ public slots:
void stopAnimation(); void stopAnimation();
void updateFrame(); void updateFrame();
void updatePulsarSpin();
void updateOrbit();
protected: protected:
void initializeGL(); void initializeGL();
...@@ -62,8 +61,6 @@ protected: ...@@ -62,8 +61,6 @@ protected:
void updateCameraPosition(const int angleH, const int angleV, const float zoom); void updateCameraPosition(const int angleH, const int angleV, const float zoom);
QTimer m_frameTimer; QTimer m_frameTimer;
QTimer m_pulsarSpinTimer;
QTimer m_orbitTimer;
GLUquadricObj *m_quadricPulsar; GLUquadricObj *m_quadricPulsar;
GLUquadricObj *m_quadricPulsarCone1; GLUquadricObj *m_quadricPulsarCone1;
...@@ -71,13 +68,12 @@ protected: ...@@ -71,13 +68,12 @@ protected:
int m_framesPerSecond; int m_framesPerSecond;
float m_pulsarSpinFrequency;
float m_pulsarRotationAngle; float m_pulsarRotationAngle;
float m_pulsarRotationDelta;
float m_orbitRotationAngle;
float m_orbitRotationDelta;
float m_orbitRadius; float m_orbitRadius;
float m_orbitRadiusNeg; float m_orbitRadiusNeg;
float m_orbitFrequency;
float m_orbitRotationAngle;
int m_mouseLastX; int m_mouseLastX;
int m_mouseLastY; int m_mouseLastY;
......
...@@ -50,7 +50,7 @@ PulsatingScience::~PulsatingScience() ...@@ -50,7 +50,7 @@ PulsatingScience::~PulsatingScience()
void PulsatingScience::on_sliderPulsarSpinFrequency_valueChanged(int value) void PulsatingScience::on_sliderPulsarSpinFrequency_valueChanged(int value)
{ {
ui.pulsarGlWidget->setPulsarSpinFrequency(value / 10.0); ui.pulsarGlWidget->setPulsarRotationDelta(value / 10.0);
ui.lcdPulsarSpinFrequency->display(QString::number(value / 10.0, 'f', 1)); ui.lcdPulsarSpinFrequency->display(QString::number(value / 10.0, 'f', 1));
} }
...@@ -62,7 +62,7 @@ void PulsatingScience::on_sliderOrbitRadius_valueChanged(int value) ...@@ -62,7 +62,7 @@ void PulsatingScience::on_sliderOrbitRadius_valueChanged(int value)
void PulsatingScience::on_sliderOrbitFrequency_valueChanged(int value) void PulsatingScience::on_sliderOrbitFrequency_valueChanged(int value)
{ {
ui.pulsarGlWidget->setOrbitFrequency(value / 10.0); ui.pulsarGlWidget->setOrbitRotationDelta(value / 10.0);
ui.lcdOrbitFrequency->display(QString::number(value / 10.0, 'f', 1)); ui.lcdOrbitFrequency->display(QString::number(value / 10.0, 'f', 1));
} }
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment