]> git.tdb.fi Git - netvis.git/blob - source/port.cpp
Refactor the rendering code
[netvis.git] / source / port.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 <cstdlib>
9 #include <netinet/in.h>
10 #include <netdb.h>
11 #include <msp/gl/immediate.h>
12 #include <msp/gl/matrix.h>
13 #include <msp/gl/meshbuilder.h>
14 #include <msp/gl/texture.h>
15 #include <msp/strings/lexicalcast.h>
16 #include "port.h"
17 #include "netvis.h"
18
19 using namespace std;
20 using namespace Msp;
21
22 Port::Port(NetVis &v, unsigned n):
23         netvis(v),
24         number(n),
25         mesh((GL::COLOR4_UBYTE, GL::VERTEX2))
26 {
27         char buf[128];
28         sockaddr_in addr;
29         addr.sin_family = AF_INET;
30         addr.sin_port = ntohs(number);
31         addr.sin_addr.s_addr = 0;
32         int err = getnameinfo(reinterpret_cast<sockaddr *>(&addr), sizeof(sockaddr_in), 0, 0, buf, sizeof(buf), 0);
33         if(err==0)
34                 name = buf;
35         else
36                 name = Msp::lexical_cast(number);
37
38         float best_score = 0;
39         for(unsigned i=0; (i<100 && best_score<1); ++i)
40         {
41                 GL::Color c;
42                 c.r = rand()*1.0/RAND_MAX;
43                 c.g = rand()*1.0/RAND_MAX;
44                 c.b = rand()*1.0/RAND_MAX;
45                 float high = max(max(c.r, c.g), c.b);
46                 c = c*(1.0/high);
47                 if(number<1024)
48                 {
49                         float low = min(min(c.r, c.g), c.b);
50                         c = (c+-low)*(1/(1-low));
51                 }
52                 else
53                         c = c*0.6+0.4;
54
55                 float score = 2;
56                 const map<unsigned, Port *> &ports = netvis.get_ports();
57                 for(map<unsigned, Port *>::const_iterator j=ports.begin(); j!=ports.end(); ++j)
58                 {
59                         const GL::Color &other = j->second->get_color();
60                         float dr = c.r-other.r;
61                         float dg = c.g-other.g;
62                         float db = c.b-other.b;
63                         score = min(score, dr*dr+dg*dg+db*db);
64                 }
65                 if(score>best_score)
66                 {
67                         best_score = score;
68                         color = c;
69                 }
70         }
71         color.a = 0.4f;
72
73         GL::MeshBuilder bld(mesh);
74         bld.begin(GL::QUADS);
75         bld.color(color.r, color.g, color.b, color.a);
76         for(unsigned x=0; x<=4; x+=2)
77         {
78                 bld.vertex(x+0, 0);
79                 bld.vertex(x+10, 0);
80                 bld.vertex(x+10, 10);
81                 bld.vertex(x+0, 10);
82         }
83         bld.end();
84 }
85
86 void Port::render() const
87 {
88         GL::PushMatrix push_;
89         mesh.draw();
90         GL::translate(16, 1, 0);
91         GL::scale_uniform(10);
92         GL::Immediate imm((GL::COLOR4_UBYTE, GL::TEXCOORD2, GL::VERTEX2));
93         netvis.get_font().draw_string(name, imm);
94         GL::Texture::unbind();
95 }