Commit 7db83d74 authored by Oliver Bock's avatar Oliver Bock

Added basic animation

* One pulsar with "lighthouse" cones (not to scale!)
* Independent timers for rendering and animation
parent fe37e887
......@@ -20,26 +20,164 @@
#include "pulsaranimationwidget.h"
PulsarAnimationWidget::PulsarAnimationWidget(QWidget *parent) : QGLWidget(parent)
PulsarAnimationWidget::PulsarAnimationWidget(QWidget *parent) :
QGLWidget(parent),
m_frameTimer(),
m_pulsarSpinTimer()
{
connect(&m_frameTimer, SIGNAL(timeout()), this, SLOT(updateFrame()));
connect(&m_pulsarSpinTimer, SIGNAL(timeout()), this, SLOT(updatePulsarSpin()));
GLUquadricObj *m_quadricPulsar;
GLUquadricObj *m_quadricPulsarCone1;
GLUquadricObj *m_quadricPulsarCone2;
m_framesPerSecond = 25;
setPulsarSpinFrequency(0.5f);
m_pulsarRotationAngle = 0.0;
}
PulsarAnimationWidget::~PulsarAnimationWidget()
{
if(m_quadricPulsar) gluDeleteQuadric(m_quadricPulsar);
if(m_quadricPulsarCone1) gluDeleteQuadric(m_quadricPulsarCone1);
if(m_quadricPulsarCone2) gluDeleteQuadric(m_quadricPulsarCone2);
}
void PulsarAnimationWidget::initializeGL()
{
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);
m_quadricPulsar = gluNewQuadric();
m_quadricPulsarCone1 = gluNewQuadric();
m_quadricPulsarCone2 = gluNewQuadric();
gluQuadricNormals(m_quadricPulsar, GLU_SMOOTH);
gluQuadricNormals(m_quadricPulsarCone1, GLU_SMOOTH);
gluQuadricNormals(m_quadricPulsarCone2, GLU_SMOOTH);
}
void PulsarAnimationWidget::resizeGL(int w, int h)
{
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);
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();
}
void PulsarAnimationWidget::paintGL() {
void PulsarAnimationWidget::runAnimation()
{
m_frameTimer.start(1000.0 / m_framesPerSecond);
m_pulsarSpinTimer.start(m_pulsarSpinFrequency);
}
void PulsarAnimationWidget::pauseAnimation()
{
m_frameTimer.stop();
m_pulsarSpinTimer.stop();
}
void PulsarAnimationWidget::stopAnimation()
{
m_frameTimer.stop();
m_pulsarSpinTimer.stop();
m_pulsarRotationAngle = 0.0;
updateGL();
}
void PulsarAnimationWidget::updateFrame()
{
updateGL();
}
void PulsarAnimationWidget::updatePulsarSpin()
{
if(m_pulsarRotationAngle <= 360.0) {
m_pulsarRotationAngle++;
}
else {
m_pulsarRotationAngle = 0.0;
}
}
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);
}
......@@ -22,6 +22,7 @@
#define PULSARANIMATIONWIDGET_H_
#include <QGLWidget>
#include <QTimer>
class PulsarAnimationWidget : public QGLWidget
{
......@@ -31,6 +32,17 @@ public:
PulsarAnimationWidget(QWidget *parent);
virtual ~PulsarAnimationWidget();
void setFramePerSecond(const unsigned int fps);
void setPulsarSpinFrequency(const float frequency);
public slots:
void runAnimation();
void pauseAnimation();
void stopAnimation();
void updateFrame();
void updatePulsarSpin();
protected:
void initializeGL();
......@@ -38,6 +50,18 @@ protected:
void resizeGL(int w, int h);
void paintGL();
private:
QTimer m_frameTimer;
QTimer m_pulsarSpinTimer;
GLUquadricObj *m_quadricPulsar;
GLUquadricObj *m_quadricPulsarCone1;
GLUquadricObj *m_quadricPulsarCone2;
int m_framesPerSecond;
float m_pulsarSpinFrequency;
float m_pulsarRotationAngle;
};
#endif /* PULSARANIMATIONWIDGET_H_ */
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