]> git.tdb.fi Git - r2c2.git/commitdiff
Make use of GL::Renderer in Overlay3D
authorMikko Rasa <tdb@tdb.fi>
Sun, 3 Apr 2011 14:08:47 +0000 (14:08 +0000)
committerMikko Rasa <tdb@tdb.fi>
Sun, 3 Apr 2011 14:08:47 +0000 (14:08 +0000)
source/3d/overlay.cpp
source/3d/overlay.h
source/designer/designer.cpp
source/engineer/engineer.cpp

index ed413cd4f943cb59e7dbaed00481889d8363b230..6065dbf7ccbdac06e80503a9c6ae357ed2715ff4 100644 (file)
@@ -8,9 +8,11 @@ Distributed under the GPL
 #include <algorithm>
 #include <cmath>
 #include <msp/fs/path.h>
+#include <msp/gl/framebuffer.h>
 #include <msp/gl/immediate.h>
 #include <msp/gl/matrix.h>
-#include <msp/gl/texture.h>
+#include <msp/gl/renderer.h>
+#include <msp/gl/texture2d.h>
 #include <msp/io/print.h>
 #include "overlay.h"
 #include "track.h"
@@ -21,9 +23,7 @@ using namespace Msp;
 
 namespace R2C2 {
 
-Overlay3D::Overlay3D(const Graphics::Window &w, const GL::Camera &c, const GL::Font &f):
-       window(w),
-       camera(c),
+Overlay3D::Overlay3D(const GL::Font &f):
        font(f)
 { }
 
@@ -79,63 +79,53 @@ void Overlay3D::clear(const Object3D &track)
        }
 }
 
-void Overlay3D::render(const GL::Tag &tag) const
+void Overlay3D::render(GL::Renderer &renderer, const GL::Tag &) const
 {
-       if(tag==0)
+       unsigned fb_width = GL::Framebuffer::current()->get_width();
+       unsigned fb_height = GL::Framebuffer::current()->get_height();
+
+       GL::Renderer::Push push(renderer);
+       GL::MatrixStack::Push push_proj(GL::MatrixStack::projection());
+
+       GL::MatrixStack::projection() = GL::Matrix::scaling(2.0/fb_width, 2.0/fb_height, 1.0);
+
+       glLineWidth(1);
+
+       int size = int(font.get_default_size()+0.5);
+       float spacing = round(size*1.1)/size;
+       float baseline = round((0.5-font.get_ascent()*0.5-font.get_descent()*0.5)*size)/size;
+
+       for(map<const Object3D *, Icon *>::const_iterator i=icons.begin(); i!=icons.end(); ++i)
        {
-               GL::matrix_mode(GL::PROJECTION);
-               GL::push_matrix();
-               GL::load_identity();
-               GL::scale(2.0/window.get_width(), 2.0/window.get_height(), 1.0);
-               GL::matrix_mode(GL::MODELVIEW);
-               GL::push_matrix();
-               GL::load_identity();
+               if(!i->first->is_visible())
+                       continue;
+
+               const Icon &icon = *i->second;
 
-               glLineWidth(1);
+               Vector node = i->first->get_node();
+               GL::Vector3 p = renderer.get_camera()->project(GL::Vector3(node.x, node.y, node.z));
+               p.x = int(p.x*0.5*fb_width-icon.width*size/2);
+               p.y = int(p.y*0.5*fb_height);
 
-               int size = int(font.get_default_size()+0.5);
-               float spacing = round(size*1.1)/size;
-               float baseline = round((0.5-font.get_ascent()*0.5-font.get_descent()*0.5)*size)/size;
+               GL::Renderer::Push push2(renderer);
+               renderer.matrix_stack() = GL::Matrix::translation(p);
+               renderer.matrix_stack() *= GL::Matrix::scaling(size);
 
-               for(map<const Object3D *, Icon *>::const_iterator i=icons.begin(); i!=icons.end(); ++i)
+               icon.background.draw(renderer);
+
+               glColor3f(0.0, 1.0, 0.0);
+               for(vector<const GL::Mesh *>::const_iterator j=icon.graphics.begin(); j!=icon.graphics.end(); ++j)
                {
-                       if(!i->first->is_visible())
-                               continue;
-
-                       const Icon &icon = *i->second;
-
-                       Vector node = i->first->get_node();
-                       GL::Vector3 p = camera.project(GL::Vector3(node.x, node.y, node.z));
-
-                       GL::PushMatrix push_mat;
-                       p.x = int(p.x*0.5*window.get_width()-icon.width*size/2);
-                       p.y = int(p.y*0.5*window.get_height());
-                       GL::translate(p.x, p.y, p.z);
-                       GL::scale_uniform(size);
-
-                       icon.background.draw();
-
-                       glColor3f(0.0, 1.0, 0.0);
-                       for(vector<const GL::Mesh *>::const_iterator j=icon.graphics.begin(); j!=icon.graphics.end(); ++j)
-                       {
-                               (*j)->draw();
-                               GL::translate(spacing, 0, 0);
-                       }
-
-                       GL::translate(0, baseline, 0);
-                       GL::Immediate imm((GL::TEXCOORD2, GL::COLOR4_UBYTE, GL::VERTEX2));
-                       imm.color(0.0f, 1.0f, 0.0f);
-                       font.draw_string(icon.label, imm);
-                       GL::Texture::unbind();
+                       (*j)->draw(renderer);
+                       renderer.matrix_stack() *= GL::Matrix::translation(spacing, 0, 0);
                }
 
-               GL::matrix_mode(GL::PROJECTION);
-               GL::pop_matrix();
-               GL::matrix_mode(GL::MODELVIEW);
-               GL::pop_matrix();
-
-               glColor3f(1.0, 1.0, 1.0);
+               renderer.matrix_stack() *= GL::Matrix::translation(0, baseline, 0);
+               renderer.set_texture(&font.get_texture());
+               icon.text.draw(renderer);
        }
+
+       glColor3f(1.0, 1.0, 1.0);
 }
 
 Overlay3D::Icon &Overlay3D::get_icon(const Object3D &track)
@@ -174,30 +164,41 @@ void Overlay3D::update_icon(Icon &icon)
 
        icon.width = max(icon.graphics.size()*1.1+font.get_string_width(icon.label), 1.0);
 
-       GL::MeshBuilder bld(icon.background);
-       bld.color(0.2f, 0.2f, 0.2f, 0.7f);
-
-       bld.begin(GL::TRIANGLE_FAN);
-       bld.vertex(0.4, 0.5);
-       bld.vertex(icon.width-0.4, 0.5);
-       bld.vertex(icon.width-0.4, 1.2);
-       for(int i=4; i<=12; ++i)
-               bld.vertex(0.4+cos(i*M_PI/8)*0.7, 0.5+sin(i*M_PI/8)*0.7);
-       bld.end();
-
-       bld.begin(GL::TRIANGLE_FAN);
-       bld.vertex(icon.width-0.4, 0.5);
-       bld.vertex(0.4, 0.5);
-       bld.vertex(0.4, -0.2);
-       for(int i=-4; i<=4; ++i)
-               bld.vertex(icon.width-0.4+cos(i*M_PI/8)*0.7, 0.5+sin(i*M_PI/8)*0.7);
-       bld.end();
+       {
+               GL::MeshBuilder bld(icon.background);
+               bld.color(0.2f, 0.2f, 0.2f, 0.7f);
+
+               bld.begin(GL::TRIANGLE_FAN);
+               bld.vertex(0.4, 0.5);
+               bld.vertex(icon.width-0.4, 0.5);
+               bld.vertex(icon.width-0.4, 1.2);
+               for(int i=4; i<=12; ++i)
+                       bld.vertex(0.4+cos(i*M_PI/8)*0.7, 0.5+sin(i*M_PI/8)*0.7);
+               bld.end();
+
+               bld.begin(GL::TRIANGLE_FAN);
+               bld.vertex(icon.width-0.4, 0.5);
+               bld.vertex(0.4, 0.5);
+               bld.vertex(0.4, -0.2);
+               for(int i=-4; i<=4; ++i)
+                       bld.vertex(icon.width-0.4+cos(i*M_PI/8)*0.7, 0.5+sin(i*M_PI/8)*0.7);
+               bld.end();
+       }
+
+       icon.text.clear();
+
+       {
+               GL::MeshBuilder bld(icon.text);
+               bld.color(0.0f, 1.0f, 0.0f);
+               font.draw_string(icon.label, bld);
+       }
 }
 
 
 Overlay3D::Icon::Icon():
        width(1.0),
-       background((GL::COLOR4_UBYTE, GL::VERTEX2))
+       background((GL::COLOR4_UBYTE, GL::VERTEX2)),
+       text((GL::COLOR4_UBYTE, GL::TEXCOORD2, GL::VERTEX2))
 { }
 
 } // namespace R2C2
index 8e818bf01e31b59a8fcf2cac1c01f7ecc54354de..e9ed52a93c35df148374511bd3ecf9db994e4fe6 100644 (file)
@@ -27,18 +27,17 @@ private:
                std::string label;
                float width;
                Msp::GL::Mesh background;
+               Msp::GL::Mesh text;
 
                Icon();
        };
 
-       const Msp::Graphics::Window &window;
-       const Msp::GL::Camera &camera;
        const Msp::GL::Font &font;
        std::map<std::string, Msp::GL::Mesh *> graphics;
        std::map<const Object3D *, Icon *> icons;
 
 public:
-       Overlay3D(const Msp::Graphics::Window &, const Msp::GL::Camera &, const Msp::GL::Font &);
+       Overlay3D(const Msp::GL::Font &);
        ~Overlay3D();
 
        void set_label(const Object3D &, const std::string &);
@@ -47,7 +46,7 @@ public:
        void clear_graphics(const Object3D &);
        void clear(const Object3D &);
 
-       virtual void render(const Msp::GL::Tag &) const;
+       virtual void render(Msp::GL::Renderer &, const Msp::GL::Tag &) const;
 
 private:
        Icon &get_icon(const Object3D &);
index e0da69f4110fc0e71674cc89cb9121a3ebc59fd4..fde251cfccd4c8f7afc7da6050b458a25439d159 100644 (file)
@@ -118,6 +118,9 @@ Designer::Designer(int argc, char **argv):
        pass->depth_test = &GL::DepthTest::lequal();
        pass->blend = &GL::Blend::alpha();
 
+       pass = &pipeline->add_pass("overlay");
+       pass->blend = &GL::Blend::alpha();
+
        camera.set_up_direction(GL::Vector3(0, 0, 1));
        camera.set_look_direction(GL::Vector3(0, 0.5, -0.866));
 
@@ -149,7 +152,8 @@ Designer::Designer(int argc, char **argv):
        statusbar->add(*lbl_status);
        lbl_status->set_geometry(GLtk::Geometry(20, 2, 300, 16));
 
-       overlay = new Overlay3D(window, camera, ui_res.get_default_font());
+       overlay = new Overlay3D(ui_res.get_default_font());
+       pipeline->add_renderable_for_pass(*overlay, "overlay");
 
        camera_ctl.view_all();
 
@@ -536,10 +540,6 @@ void Designer::render()
        else
        {
                pipeline->render_all();
-               {
-                       GL::Bind bind_blend(GL::Blend::alpha());
-                       overlay->render(0);
-               }
 
                GL::Bind bind_depth(GL::DepthTest::lequal());
                if(mode==MEASURE)
index 32b23ab900aa16ea6ab7009081d911ec8801a73a..8aea6f68ace07ad70360cd27bdb3fd874ccd913a 100644 (file)
@@ -73,7 +73,7 @@ Engineer::Engineer(int argc, char **argv):
        main_panel->set_position(0, window.get_height()-main_panel->get_geometry().h);
        main_panel->set_visible(true);
 
-       overlay = new Overlay3D(window, camera, ui_res.get_default_font());
+       overlay = new Overlay3D(ui_res.get_default_font());
 
        // Setup railroad control
        DataFile::load(catalogue, "tracks.dat");
@@ -100,6 +100,7 @@ Engineer::Engineer(int argc, char **argv):
        pipeline.set_camera(&camera);
        pipeline.add_renderable_for_pass(layout_3d.get_scene(), 0);
        pipeline.add_renderable_for_pass(layout_3d.get_path_scene(), "unlit");
+       pipeline.add_renderable_for_pass(*overlay, "unlit");
 
        light.set_position(GL::Vector4(0, -0.259, 0.966, 0));
        light.set_diffuse(GL::Color(0.9));
@@ -202,10 +203,6 @@ void Engineer::tick()
        GL::Framebuffer::system().clear(GL::COLOR_BUFFER_BIT|GL::DEPTH_BUFFER_BIT);
 
        pipeline.render_all();
-       {
-               GL::Bind blend(GL::Blend::alpha());
-               overlay->render(0);
-       }
 
        if(pointer_moved)
        {