]> git.tdb.fi Git - netvis.git/blobdiff - source/packet.cpp
Initial revision
[netvis.git] / source / packet.cpp
diff --git a/source/packet.cpp b/source/packet.cpp
new file mode 100644 (file)
index 0000000..491e807
--- /dev/null
@@ -0,0 +1,72 @@
+#include <cmath>
+#include <msp/gl/immediate.h>
+#include <msp/gl/matrix.h>
+#include <msp/gl/transform.h>
+#include <msp/time/units.h>
+#include "host.h"
+#include "packet.h"
+
+using namespace Msp;
+
+Packet::Packet(const Host &s, const Host *d, const GL::Color &c, unsigned i):
+       src(s),
+       dest(d),
+       color(c),
+       x(0),
+       size(cbrt(i)),
+       angle(rand()*180.0/RAND_MAX),
+       rspeed(rand()*180.0/RAND_MAX-90.0)
+{
+}
+
+void Packet::tick(const Time::TimeDelta &dt)
+{
+       double f=dt/Time::sec;
+       x+=f;
+       if(x>1)
+               x=1;
+       angle+=rspeed*f;
+       if(angle<0)
+               angle+=360;
+       if(angle>=360)
+               angle-=360;
+}
+
+void Packet::render() const
+{
+       GL::push_matrix();
+
+       const Vector2 &spos=src.get_position();
+
+       if(dest)
+       {
+               const Vector2 &dpos=dest->get_position();
+               GL::translate(spos.x*(1-x)+dpos.x*x, spos.y*(1-x)+dpos.y*x, 0);
+               GL::rotate(angle, 0, 0, 1);
+
+               GL::Immediate imm((GL::COLOR4_UBYTE,GL::VERTEX2));
+               imm.begin(GL::QUADS);
+               imm.color(color.r, color.g, color.b, color.a);
+               imm.vertex(-size, -size);
+               imm.vertex(size, -size);
+               imm.vertex(size, size);
+               imm.vertex(-size, size);
+               imm.end();
+       }
+       else
+       {
+               GL::translate(spos.x, spos.y, 0);
+               GL::Immediate imm((GL::COLOR4_UBYTE,GL::VERTEX2));
+               imm.begin(GL::TRIANGLE_FAN);
+               imm.color(color.r, color.g, color.b, color.a*(1-x));
+               imm.vertex(0, 0);
+               for(unsigned i=0; i<=24; ++i)
+               {
+                       float a=i*M_PI/12;
+                       imm.vertex(cos(a)*x*200, sin(a)*x*200);
+               }
+               imm.end();
+       }
+
+       GL::pop_matrix();
+}