]> git.tdb.fi Git - netvis.git/blob - source/packet.cpp
Resolve IP addresses to hostnames
[netvis.git] / source / packet.cpp
1 /* $Id$
2
3 This file is part of NetVis
4 Copyright @ 2008 Mikko Rasa, Mikkosoft Productions
5 Distributed unter the GPL
6 */
7
8 #include <cmath>
9 #include <msp/gl/immediate.h>
10 #include <msp/gl/matrix.h>
11 #include <msp/gl/transform.h>
12 #include <msp/time/units.h>
13 #include "host.h"
14 #include "packet.h"
15
16 using namespace Msp;
17
18 Packet::Packet(const Host &s, const Host *d, const GL::Color &c, unsigned i):
19         src(s),
20         dest(d),
21         color(c),
22         x(0),
23         size(cbrt(i)*1.5),
24         angle(rand()*M_PI*2/RAND_MAX),
25         rspeed(rand()*M_PI/RAND_MAX-M_PI/2)
26 { }
27
28 void Packet::tick(const Time::TimeDelta &dt)
29 {
30         double f=dt/Time::sec;
31         x+=f;
32         if(x>1)
33                 x=1;
34         angle+=rspeed*f;
35         if(angle<0)
36                 angle+=M_PI*2;
37         if(angle>=M_PI*2)
38                 angle-=M_PI;
39 }
40
41 void Packet::render(GL::PrimitiveBuilder &bld) const
42 {
43         if(x<0)
44                 return;
45         if(!src.get_active() || (dest && !dest->get_active()))
46                 return;
47
48         const Vector2 &spos=src.get_position();
49
50         if(dest)
51         {
52                 const Vector2 &dpos=dest->get_position();
53                 Vector2 pos(spos.x*(1-x)+dpos.x*x, spos.y*(1-x)+dpos.y*x);
54                 Vector2 corner(cos(angle)*size, sin(angle)*size);
55
56                 if(bld.get_type()!=GL::QUADS)
57                 {
58                         bld.end();
59                         bld.begin(GL::QUADS);
60                 }
61                 bld.color(color.r, color.g, color.b, color.a);
62                 bld.vertex(pos.x+corner.x, pos.y+corner.y);
63                 bld.vertex(pos.x-corner.y, pos.y+corner.x);
64                 bld.vertex(pos.x-corner.x, pos.y-corner.y);
65                 bld.vertex(pos.x+corner.y, pos.y-corner.x);
66         }
67         else
68         {
69                 bld.end();
70                 bld.begin(GL::TRIANGLE_FAN);
71                 bld.color(color.r, color.g, color.b, color.a*(1-x));
72                 bld.vertex(spos.x, spos.y);
73                 for(unsigned i=0; i<=24; ++i)
74                 {
75                         float a=i*M_PI/12;
76                         bld.vertex(spos.x+cos(a)*x*200, spos.y+sin(a)*x*200);
77                 }
78         }
79 }