]> git.tdb.fi Git - netvis.git/blob - source/packet.cpp
491e8076245bee11bb81aa49825db1a4e45f5997
[netvis.git] / source / packet.cpp
1 #include <cmath>
2 #include <msp/gl/immediate.h>
3 #include <msp/gl/matrix.h>
4 #include <msp/gl/transform.h>
5 #include <msp/time/units.h>
6 #include "host.h"
7 #include "packet.h"
8
9 using namespace Msp;
10
11 Packet::Packet(const Host &s, const Host *d, const GL::Color &c, unsigned i):
12         src(s),
13         dest(d),
14         color(c),
15         x(0),
16         size(cbrt(i)),
17         angle(rand()*180.0/RAND_MAX),
18         rspeed(rand()*180.0/RAND_MAX-90.0)
19 {
20 }
21
22 void Packet::tick(const Time::TimeDelta &dt)
23 {
24         double f=dt/Time::sec;
25         x+=f;
26         if(x>1)
27                 x=1;
28         angle+=rspeed*f;
29         if(angle<0)
30                 angle+=360;
31         if(angle>=360)
32                 angle-=360;
33 }
34
35 void Packet::render() const
36 {
37         GL::push_matrix();
38
39         const Vector2 &spos=src.get_position();
40
41         if(dest)
42         {
43                 const Vector2 &dpos=dest->get_position();
44                 GL::translate(spos.x*(1-x)+dpos.x*x, spos.y*(1-x)+dpos.y*x, 0);
45                 GL::rotate(angle, 0, 0, 1);
46
47                 GL::Immediate imm((GL::COLOR4_UBYTE,GL::VERTEX2));
48                 imm.begin(GL::QUADS);
49                 imm.color(color.r, color.g, color.b, color.a);
50                 imm.vertex(-size, -size);
51                 imm.vertex(size, -size);
52                 imm.vertex(size, size);
53                 imm.vertex(-size, size);
54                 imm.end();
55         }
56         else
57         {
58                 GL::translate(spos.x, spos.y, 0);
59                 GL::Immediate imm((GL::COLOR4_UBYTE,GL::VERTEX2));
60                 imm.begin(GL::TRIANGLE_FAN);
61                 imm.color(color.r, color.g, color.b, color.a*(1-x));
62                 imm.vertex(0, 0);
63                 for(unsigned i=0; i<=24; ++i)
64                 {
65                         float a=i*M_PI/12;
66                         imm.vertex(cos(a)*x*200, sin(a)*x*200);
67                 }
68                 imm.end();
69         }
70
71         GL::pop_matrix();
72 }