]> git.tdb.fi Git - netvis.git/commitdiff
Refactor the rendering code
authorMikko Rasa <tdb@tdb.fi>
Sat, 24 Oct 2009 14:26:12 +0000 (14:26 +0000)
committerMikko Rasa <tdb@tdb.fi>
Sat, 24 Oct 2009 14:26:12 +0000 (14:26 +0000)
Move color generation to the Port class

source/host.cpp
source/host.h
source/netvis.cpp
source/netvis.h
source/port.cpp
source/port.h

index bd93de190a1e6217bf1f1d7d3b23e7ca7ad80632..4314d75f63714c01c5a7e3f5f1ac42f9df06c9ae 100644 (file)
@@ -137,10 +137,10 @@ void Host::render() const
        if(!active)
                return;
 
-       GL::push_matrix();
+       GL::PushMatrix push_;
        GL::translate(static_cast<int>(pos.x), static_cast<int>(pos.y), 0);
 
-       GL::Immediate imm((GL::COLOR4_UBYTE, GL::VERTEX2));
+       GL::Immediate imm((GL::COLOR4_UBYTE, GL::TEXCOORD2, GL::VERTEX2));
        imm.begin(GL::QUADS);
        imm.color(1.0f, 1.0f, 1.0f, max(min(static_cast<float>(activity/10000), 1.0f), 0.2f));
        imm.vertex(-5, -5);
@@ -149,23 +149,13 @@ void Host::render() const
        imm.vertex(-5, 5);
        imm.end();
 
-       GL::pop_matrix();
-}
-
-void Host::render_label() const
-{
-       if(!active)
-               return;
-
        const GL::Font &font = netvis.get_font();
-
-       GL::push_matrix();
-       GL::translate(static_cast<int>(pos.x)-static_cast<int>(font.get_string_width(short_name)*5), static_cast<int>(pos.y)+6, 0);
+       GL::translate(-static_cast<int>(font.get_string_width(short_name)*5), 6, 0);
        GL::scale_uniform(10);
 
-       GL::Immediate imm((GL::COLOR4_UBYTE, GL::TEXCOORD2, GL::VERTEX2));
        imm.color(1.0f, 1.0f, 1.0f);
        font.draw_string(short_name, imm);
+       GL::Texture::unbind();
 
        GL::pop_matrix();
 }
index 2910ad694577f2412d459d95205cb54c2f415d08..b600dea04e3d9d315c865fbaabb3df7d69fc5e5b 100644 (file)
@@ -46,7 +46,6 @@ public:
 
        void tick(const Msp::Time::TimeDelta &);
        void render() const;
-       void render_label() const;
 };
 
 #endif
index cece749e9be6a2ab6003e5d02447673a5ad2d983..bcdba3f28c6314ff1ebf72a9891181e797dba08d 100644 (file)
@@ -91,6 +91,8 @@ NetVis::~NetVis()
                delete i->second;
        for(map<unsigned, Host *>::iterator i=disabled_hosts.begin(); i!=disabled_hosts.end(); ++i)
                delete i->second;
+       for(map<unsigned, Port *>::iterator i=ports.begin(); i!=ports.end(); ++i)
+               delete i->second;
        for(list<Packet *>::iterator i=packets.begin(); i!=packets.end(); ++i)
                delete *i;
 }
@@ -112,158 +114,116 @@ void NetVis::tick()
 
        wnd->get_display().tick();
 
+       while(pcap_dispatch(pcap, -1, &capture_handler, reinterpret_cast<unsigned char *>(this))>0) ;
+
+       resolver->tick();
+
+       float min_activity = numeric_limits<float>::max();
+       for(map<unsigned, Host *>::iterator i=hosts.begin(); i!=hosts.end(); ++i)
        {
-               Debug::ProfilingScope s(profiler, "capture");
-               while(pcap_dispatch(pcap, -1, &capture_handler, reinterpret_cast<unsigned char *>(this))>0) ;
+               i->second->tick(dt);
+               min_activity = min(min_activity, i->second->get_activity());
        }
-
+       float del_limit = pow(10, 6-0.1*(max_hosts-hosts.size()-disabled_hosts.size()));
+       for(map<unsigned, Host *>::iterator i=disabled_hosts.begin(); i!=disabled_hosts.end();)
        {
-               Debug::ProfilingScope s(profiler, "tick");
+               i->second->tick(dt);
 
-               resolver->tick();
-
-               float min_activity = numeric_limits<float>::max();
-               for(map<unsigned, Host *>::iterator i=hosts.begin(); i!=hosts.end(); ++i)
+               if(i->second->get_activity()>min_activity)
                {
-                       i->second->tick(dt);
-                       min_activity = min(min_activity, i->second->get_activity());
+                       i->second->set_active(true);
+                       hosts.insert(*i);
+                       disabled_hosts.erase(i++);
                }
-               float del_limit = pow(10, 6-0.1*(max_hosts-hosts.size()-disabled_hosts.size()));
-               for(map<unsigned, Host *>::iterator i=disabled_hosts.begin(); i!=disabled_hosts.end();)
+               else if(i->second->get_activity()<del_limit)
                {
-                       i->second->tick(dt);
-
-                       if(i->second->get_activity()>min_activity)
-                       {
-                               i->second->set_active(true);
-                               hosts.insert(*i);
-                               disabled_hosts.erase(i++);
-                       }
-                       else if(i->second->get_activity()<del_limit)
-                       {
-                               delete i->second;
-                               disabled_hosts.erase(i++);
-                       }
-                       else
-                               ++i;
+                       delete i->second;
+                       disabled_hosts.erase(i++);
                }
+               else
+                       ++i;
+       }
 
-               if(hosts.size()>max_visible_hosts)
-               {
-                       list<float> activity;
-                       for(map<unsigned, Host *>::iterator i=hosts.begin(); i!=hosts.end(); ++i)
-                               activity.push_back(i->second->get_activity());
-                       activity.sort();
-
-                       list<float>::iterator j = activity.begin();
-                       advance(j, activity.size()-max_visible_hosts);
-                       float limit = *j;
+       if(hosts.size()>max_visible_hosts)
+       {
+               list<float> activity;
+               for(map<unsigned, Host *>::iterator i=hosts.begin(); i!=hosts.end(); ++i)
+                       activity.push_back(i->second->get_activity());
+               activity.sort();
 
-                       for(map<unsigned, Host *>::iterator i=hosts.begin(); i!=hosts.end();)
-                       {
-                               if(i->second->get_activity()<limit)
-                               {
-                                       i->second->set_active(false);
-                                       disabled_hosts.insert(*i);
-                                       hosts.erase(i++);
-                               }
-                               else
-                                       ++i;
-                       }
-               }
+               list<float>::iterator j = activity.begin();
+               advance(j, activity.size()-max_visible_hosts);
+               float limit = *j;
 
-               for(list<Packet *>::iterator i=packets.begin(); i!=packets.end();)
+               for(map<unsigned, Host *>::iterator i=hosts.begin(); i!=hosts.end();)
                {
-                       (*i)->tick(dt);
-                       if((*i)->get_stale())
+                       if(i->second->get_activity()<limit)
                        {
-                               delete *i;
-                               i = packets.erase(i);
+                               i->second->set_active(false);
+                               disabled_hosts.insert(*i);
+                               hosts.erase(i++);
                        }
                        else
                                ++i;
                }
        }
 
+       for(list<Packet *>::iterator i=packets.begin(); i!=packets.end();)
        {
-               Debug::ProfilingScope s(profiler, "render");
-               GL::clear(GL::COLOR_BUFFER_BIT);
-
-               GL::matrix_mode(GL::PROJECTION);
-               GL::load_identity();
-               GL::ortho_centered(1024, 768);
-               GL::matrix_mode(GL::MODELVIEW);
-               GL::load_identity();
-
-               for(map<unsigned, Host *>::iterator i=hosts.begin(); i!=hosts.end(); ++i)
-                       i->second->render();
-               if(draw_labels)
+               (*i)->tick(dt);
+               if((*i)->get_stale())
                {
-                       for(map<unsigned, Host *>::iterator i=hosts.begin(); i!=hosts.end(); ++i)
-                               i->second->render_label();
-                       GL::Texture::unbind();
+                       delete *i;
+                       i = packets.erase(i);
                }
-               GL::Immediate imm((GL::COLOR4_UBYTE, GL::VERTEX2));
-               imm.begin(GL::QUADS);
-               for(list<Packet *>::iterator i=packets.begin(); i!=packets.end(); ++i)
-                       (*i)->render(imm);
-               imm.end();
+               else
+                       ++i;
+       }
 
-               GL::push_matrix();
-               GL::translate(-500, 360, 0);
-               unsigned n = 0;
-               for(map<unsigned, Port>::iterator i=ports.begin(); (i!=ports.end() && n<50); ++i, ++n)
-               {
-                       const GL::Color &color = i->second.get_color();
+       render();
+       wnd->swap_buffers();
 
-                       imm.begin(GL::QUADS);
-                       imm.color(color.r, color.g, color.b, color.a);
-                       for(unsigned x=0; x<=4; x+=2)
-                       {
-                               imm.vertex(x+0, 0);
-                               imm.vertex(x+10, 0);
-                               imm.vertex(x+10, 10);
-                               imm.vertex(x+0, 10);
-                       }
-                       imm.end();
+       ++frames;
+}
 
-                       GL::translate(0, -12, 0);
-               }
-               GL::pop_matrix();
+void NetVis::render()
+{
+       GL::clear(GL::COLOR_BUFFER_BIT);
 
-               GL::push_matrix();
-               if(draw_labels)
-               {
-                       GL::push_matrix();
-                       GL::translate(-484, 361, 0);
-                       GL::scale_uniform(10);
-                       n = 0;
-                       for(map<unsigned, Port>::iterator i=ports.begin(); (i!=ports.end() && n<50); ++i, ++n)
-                       {
-                               GL::Immediate imm2((GL::COLOR4_UBYTE, GL::TEXCOORD2, GL::VERTEX2));
-                               imm.color(1.0f, 1.0f, 1.0f);
-                               font->draw_string(i->second.get_name(), imm2);
+       GL::matrix_mode(GL::PROJECTION);
+       GL::load_identity();
+       GL::ortho_centered(1024, 768);
+       GL::matrix_mode(GL::MODELVIEW);
+       GL::load_identity();
 
-                               GL::translate(0, -1.2, 0);
-                       }
-                       GL::pop_matrix();
-                       GL::Texture::unbind();
-               }
-               GL::pop_matrix();
-
-               GL::push_matrix();
-               GL::translate(-500, -360, 0);
-               GL::scale_uniform(10);
-               font->draw_string(format("%d hosts", hosts.size()+disabled_hosts.size()));
-               GL::translate(0, -1.2, 0);
-               font->draw_string(format("%.2f fps", fps));
-               GL::pop_matrix();
-               GL::Texture::unbind();
-
-               wnd->swap_buffers();
+       for(map<unsigned, Host *>::iterator i=hosts.begin(); i!=hosts.end(); ++i)
+               i->second->render();
+       {
+               GL::Immediate imm((GL::COLOR4_UBYTE, GL::VERTEX2));
+               imm.begin(GL::QUADS);
+               for(list<Packet *>::iterator i=packets.begin(); i!=packets.end(); ++i)
+                       (*i)->render(imm);
+               imm.end();
        }
 
-       ++frames;
+       GL::push_matrix();
+       GL::translate(-500, 360, 0);
+       unsigned n = 0;
+       for(map<unsigned, Port *>::iterator i=ports.begin(); (i!=ports.end() && n<50); ++i, ++n)
+       {
+               i->second->render();
+               GL::translate(0, -12, 0);
+       }
+       GL::pop_matrix();
+
+       GL::push_matrix();
+       GL::translate(-500, -360, 0);
+       GL::scale_uniform(10);
+       font->draw_string(format("%d hosts", hosts.size()+disabled_hosts.size()));
+       GL::translate(0, -1.2, 0);
+       font->draw_string(format("%.2f fps", fps));
+       GL::Texture::unbind();
+       GL::pop_matrix();
 }
 
 Host &NetVis::get_host(unsigned a)
@@ -285,54 +245,14 @@ Host &NetVis::get_host(unsigned a)
        return *host;
 }
 
-GL::Color NetVis::generate_color(bool privileged) const
-{
-       GL::Color color;
-       color.r = rand()*1.0/RAND_MAX;
-       color.g = rand()*1.0/RAND_MAX;
-       color.b = rand()*1.0/RAND_MAX;
-       float high = max(max(color.r, color.g), color.b);
-       color = color*(1.0/high);
-       if(privileged)
-       {
-               float low = min(min(color.r, color.g), color.b);
-               color = (color+-low)*(1/(1-low));
-       }
-       else
-               color = color*0.6+0.4;
-       return color;
-}
-
 const Port &NetVis::get_port(unsigned number)
 {
-       map<unsigned, Port>::iterator i = ports.find(number);
+       map<unsigned, Port *>::iterator i = ports.find(number);
        if(i!=ports.end())
-               return i->second;
-
-       GL::Color best_color;
-       float best_score = 0;
-       for(unsigned j=0; (j<100 && best_score<1); ++j)
-       {
-               GL::Color color = generate_color(number<1024);
-
-               float score = 2;
-               for(i=ports.begin(); i!=ports.end(); ++i)
-               {
-                       const GL::Color &other = i->second.get_color();
-                       float dr = color.r-other.r;
-                       float dg = color.g-other.g;
-                       float db = color.b-other.b;
-                       score = min(score, dr*dr+dg*dg+db*db);
-               }
-               if(score>best_score)
-               {
-                       best_score = score;
-                       best_color = color;
-               }
-       }
-       best_color.a = 0.4f;
-       i = ports.insert(map<unsigned, Port>::value_type(number, Port(number, best_color))).first;
-       return i->second;
+               return *i->second;
+       Port *port = new Port(*this, number);
+       ports[number] = port;
+       return *port;
 }
 
 void NetVis::key_press(unsigned key, unsigned, wchar_t)
index fd1b73cddc57440a38e7a3fc3ad48203fd1d9a5c..3739d330ec941de08c03c2698a576269bcfa4b7a 100644 (file)
@@ -42,7 +42,7 @@ private:
        std::map<unsigned, Host *> hosts;
        std::map<unsigned, Host *> disabled_hosts;
        std::list<Packet *> packets;
-       std::map<unsigned, Port> ports;
+       std::map<unsigned, Port *> ports;
        bool draw_labels;
        bool blend;
 
@@ -51,16 +51,16 @@ private:
        unsigned frames;
        float fps;
 
-       Msp::Debug::Profiler profiler;
-
 public:
        NetVis(int, char **);
        ~NetVis();
 
        const Msp::GL::Font &get_font() const { return *font; }
        const std::map<unsigned, Host *> &get_hosts() const { return hosts; }
+       const std::map<unsigned, Port *> &get_ports() const { return ports; }
 private:
        virtual void tick();
+       void render();
        Host &get_host(unsigned);
        Msp::GL::Color generate_color(bool) const;
        const Port &get_port(unsigned);
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();
 }
index ebc9c22d98afee9e50035933a6568c2bc4c8a50d..d0511e96751c5ed6ca095a6a09a3677bb4fc9510 100644 (file)
@@ -10,20 +10,27 @@ Distributed unter the GPL
 
 #include <string>
 #include <msp/gl/color.h>
+#include <msp/gl/mesh.h>
+
+class NetVis;
 
 class Port
 {
 private:
+       NetVis &netvis;
        unsigned number;
        std::string name;
        Msp::GL::Color color;
+       Msp::GL::Mesh mesh;
 
 public:
-       Port(unsigned, const Msp::GL::Color &);
+       Port(NetVis &, unsigned);
 
        unsigned get_number() const { return number; }
        const std::string &get_name() const { return name; }
        const Msp::GL::Color &get_color() const { return color; }
+
+       void render() const;
 };
 
 #endif