]> git.tdb.fi Git - netvis.git/blobdiff - source/port.cpp
Refactor the rendering code
[netvis.git] / source / port.cpp
index 11ccfe0331674ebe05a1fc37a332c7216e7e02aa..d4be82ce182b0c7d195557741d114cafbc8896f9 100644 (file)
@@ -5,14 +5,24 @@ Copyright @ 2008 Mikko Rasa, Mikkosoft Productions
 Distributed unter the GPL
 */
 
+#include <cstdlib>
 #include <netinet/in.h>
 #include <netdb.h>
+#include <msp/gl/immediate.h>
+#include <msp/gl/matrix.h>
+#include <msp/gl/meshbuilder.h>
+#include <msp/gl/texture.h>
 #include <msp/strings/lexicalcast.h>
 #include "port.h"
+#include "netvis.h"
 
-Port::Port(unsigned n, const Msp::GL::Color &c):
+using namespace std;
+using namespace Msp;
+
+Port::Port(NetVis &v, unsigned n):
+       netvis(v),
        number(n),
-       color(c)
+       mesh((GL::COLOR4_UBYTE, GL::VERTEX2))
 {
        char buf[128];
        sockaddr_in addr;
@@ -24,4 +34,62 @@ Port::Port(unsigned n, const Msp::GL::Color &c):
                name = buf;
        else
                name = Msp::lexical_cast(number);
+
+       float best_score = 0;
+       for(unsigned i=0; (i<100 && best_score<1); ++i)
+       {
+               GL::Color c;
+               c.r = rand()*1.0/RAND_MAX;
+               c.g = rand()*1.0/RAND_MAX;
+               c.b = rand()*1.0/RAND_MAX;
+               float high = max(max(c.r, c.g), c.b);
+               c = c*(1.0/high);
+               if(number<1024)
+               {
+                       float low = min(min(c.r, c.g), c.b);
+                       c = (c+-low)*(1/(1-low));
+               }
+               else
+                       c = c*0.6+0.4;
+
+               float score = 2;
+               const map<unsigned, Port *> &ports = netvis.get_ports();
+               for(map<unsigned, Port *>::const_iterator j=ports.begin(); j!=ports.end(); ++j)
+               {
+                       const GL::Color &other = j->second->get_color();
+                       float dr = c.r-other.r;
+                       float dg = c.g-other.g;
+                       float db = c.b-other.b;
+                       score = min(score, dr*dr+dg*dg+db*db);
+               }
+               if(score>best_score)
+               {
+                       best_score = score;
+                       color = c;
+               }
+       }
+       color.a = 0.4f;
+
+       GL::MeshBuilder bld(mesh);
+       bld.begin(GL::QUADS);
+       bld.color(color.r, color.g, color.b, color.a);
+       for(unsigned x=0; x<=4; x+=2)
+       {
+               bld.vertex(x+0, 0);
+               bld.vertex(x+10, 0);
+               bld.vertex(x+10, 10);
+               bld.vertex(x+0, 10);
+       }
+       bld.end();
+}
+
+void Port::render() const
+{
+       GL::PushMatrix push_;
+       mesh.draw();
+       GL::translate(16, 1, 0);
+       GL::scale_uniform(10);
+       GL::Immediate imm((GL::COLOR4_UBYTE, GL::TEXCOORD2, GL::VERTEX2));
+       netvis.get_font().draw_string(name, imm);
+       GL::Texture::unbind();
 }