1 /****************************************************************************
2 
3  Copyright (C) 2002-2014 Gilles Debunne. All rights reserved.
4 
5  This file is part of the QGLViewer library version 2.7.2.
6 
7  http://www.libqglviewer.com - contact@libqglviewer.com
8 
9  This file may be used under the terms of the GNU General Public License
10  versions 2.0 or 3.0 as published by the Free Software Foundation and
11  appearing in the LICENSE file included in the packaging of this file.
12  In addition, as a special exception, Gilles Debunne gives you certain
13  additional rights, described in the file GPL_EXCEPTION in this package.
14 
15  libQGLViewer uses dual licensing. Commercial/proprietary software must
16  purchase a libQGLViewer Commercial License.
17 
18  This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
19  WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
20 
21 *****************************************************************************/
22 
23 #include "animation.h"
24 #include <math.h>
25 #include <stdlib.h> // RAND_MAX
26 
27 using namespace qglviewer;
28 using namespace std;
29 
30 ///////////////////////   V i e w e r  ///////////////////////
init()31 void Viewer::init() {
32   restoreStateFromFile();
33   glDisable(GL_LIGHTING);
34   nbPart_ = 2000;
35   particle_ = new Particle[nbPart_];
36   glPointSize(3.0);
37   setGridIsDrawn();
38   help();
39   startAnimation();
40 }
41 
draw()42 void Viewer::draw() {
43   glBegin(GL_POINTS);
44   for (int i = 0; i < nbPart_; i++)
45     particle_[i].draw();
46   glEnd();
47 }
48 
animate()49 void Viewer::animate() {
50   for (int i = 0; i < nbPart_; i++)
51     particle_[i].animate();
52 }
53 
helpString() const54 QString Viewer::helpString() const {
55   QString text("<h2>A n i m a t i o n</h2>");
56   text += "Use the <i>animate()</i> function to implement the animation part "
57           "of your ";
58   text += "application. Once the animation is started, <i>animate()</i> and "
59           "<i>draw()</i> ";
60   text += "are called in an infinite loop, at a frequency that can be "
61           "fixed.<br><br>";
62   text += "Press <b>Return</b> to start/stop the animation.";
63   return text;
64 }
65 
66 ///////////////////////   P a r t i c l e   ///////////////////////////////
67 
Particle()68 Particle::Particle() { init(); }
69 
animate()70 void Particle::animate() {
71   speed_.z -= 0.05f;
72   pos_ += 0.1f * speed_;
73 
74   if (pos_.z < 0.0) {
75     speed_.z = -0.8 * speed_.z;
76     pos_.z = 0.0;
77   }
78 
79   if (++age_ == ageMax_)
80     init();
81 }
82 
draw()83 void Particle::draw() {
84   glColor3f(age_ / (float)ageMax_, age_ / (float)ageMax_, 1.0);
85   glVertex3fv(pos_);
86 }
87 
init()88 void Particle::init() {
89   pos_ = Vec(0.0, 0.0, 0.0);
90   float angle = 2.0 * M_PI * rand() / RAND_MAX;
91   float norm = 0.04 * rand() / RAND_MAX;
92   speed_ = Vec(norm * cos(angle), norm * sin(angle),
93                rand() / static_cast<float>(RAND_MAX));
94   age_ = 0;
95   ageMax_ = 50 + static_cast<int>(100.0 * rand() / RAND_MAX);
96 }
97