Home | Documentation | Download | Screenshots | Developper |
The animate()
function used on a water particle simulation.
When animation is activated (the Return key toggles animation), the animate()
and
then the draw()
functions are called in an infinite loop.
You can tune the frequency of your animation (default is 25Hz) using
setAnimationInterval()
. The frame rate will then be fixed, provided that your
animation loop function is fast enough.
#include "qglviewer.h" class Particle { public : Particle(); void init(); void draw(); void animate(); private : qglviewer::Vec speed_, pos_; int age_, ageMax_; }; class Viewer : public QGLViewer { protected : void draw(); void init(); void help(); void animate(); private: int nbPart_; Particle* particle_; };
#include "animation.h" #include <math.h> using namespace qglviewer; using namespace std; /////////////////////// V i e w e r /////////////////////// void Viewer::init() { nbPart_ = 2000; particle_ = new Particle[nbPart_]; glDisable(GL_LIGHTING); glPointSize(3.0); setDrawGrid(); help(); startAnimation(); } void Viewer::draw() { glBegin(GL_POINTS); for (int i=0; i<nbPart_; i++) particle_[i].draw(); glEnd(); } void Viewer::animate() { for (int i=0; i<nbPart_; i++) particle_[i].animate(); } void Viewer::help() { cout << endl << "\t\t- - A n i m a t i o n - -" << endl << endl; cout << "Use the animate() function to implement the animation part of your" << endl; cout << "application. Once the animation is started, animate() and draw() are" << endl; cout << "called in an infinite loop, at a frequency that can be fixed." << endl << endl; cout << "Press return to start/stop the animation." << endl << endl; } /////////////////////// P a r t i c l e /////////////////////////////// Particle::Particle() { init(); } void Particle::animate() { speed_.z -= 0.05; pos_ += 0.1 * speed_; if (pos_.z < 0.0) { speed_.z = -0.8*speed_.z; pos_.z = 0.0; } if (++age_ == ageMax_) init(); } void Particle::draw() { glColor3f(age_/(float)ageMax_, age_/(float)ageMax_, 1.0); glVertex3fv(pos_.address()); } void Particle::init() { pos_ = Vec(0.0, 0.0, 0.0); float angle = 2.0 * M_PI * rand() / RAND_MAX; float norm = 0.04 * rand() / RAND_MAX; speed_ = Vec(norm*cos(angle), norm*sin(angle), rand() / static_cast<float>(RAND_MAX) ); age_ = 0; ageMax_ = 50 + static_cast<int>(100.0 * rand() / RAND_MAX); }
#include "animation.h" #include <qapplication.h> int main(int argc, char** argv) { // Read command lines arguments. QApplication application(argc,argv); // Instantiate the viewer. Viewer v; // Make the viewer window visible on screen. v.show(); // Set the viewer as the application main widget. application.setMainWidget(&v); // Run main loop. return application.exec(); }
Back to the main page