]> git.tdb.fi Git - r2c2.git/blobdiff - source/3d/overlay.cpp
Make some internal colors darker to match linear color space
[r2c2.git] / source / 3d / overlay.cpp
index 39428a166d57ff4f622521b21471d9b9092e9eab..2a26915a6e5fce5288254ac8b9d3af907bac7f5b 100644 (file)
@@ -1,15 +1,11 @@
-/* $Id$
-
-This file is part of the MSP Märklin suite
-Copyright © 2010 Mikkosoft Productions, Mikko Rasa
-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"
@@ -18,11 +14,9 @@ Distributed under the GPL
 using namespace std;
 using namespace Msp;
 
-namespace Marklin {
+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)
 { }
 
@@ -78,59 +72,52 @@ 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)
-       {
-               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();
+       unsigned fb_width = GL::Framebuffer::current()->get_width();
+       unsigned fb_height = GL::Framebuffer::current()->get_height();
 
-               glLineWidth(1);
+       GL::Renderer::Push push(renderer);
+       GL::MatrixStack::Push push_proj(GL::MatrixStack::projection());
 
-               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::MatrixStack::projection() = GL::Matrix::scaling(2.0/fb_width, 2.0/fb_height, 1.0);
 
-               for(map<const Object3D *, Icon *>::const_iterator i=icons.begin(); i!=icons.end(); ++i)
-               {
-                       if(!i->first->is_visible())
-                               continue;
+       glLineWidth(1);
 
-                       const Icon &icon = *i->second;
+       int size = int(font.get_native_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;
 
-                       Point node = i->first->get_node();
-                       GL::Vector3 p = camera.project(GL::Vector3(node.x, node.y, node.z));
+       for(map<const Object3D *, Icon *>::const_iterator i=icons.begin(); i!=icons.end(); ++i)
+       {
+               if(!i->first->is_visible())
+                       continue;
+
+               const Icon &icon = *i->second;
 
-                       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);
+               GL::Vector3 p = renderer.get_camera()->project(i->first->get_node());
+               p.x = int(p.x*0.5*fb_width-icon.width*size/2);
+               p.y = int(p.y*0.5*fb_height);
 
-                       icon.background.draw();
+               GL::Renderer::Push push2(renderer);
+               renderer.matrix_stack() = GL::Matrix::translation(p);
+               renderer.matrix_stack() *= GL::Matrix::scaling(size);
 
-                       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);
-                       }
+               icon.background.draw(renderer);
 
-                       GL::translate(0, baseline, 0);
-                       font.draw_string(icon.label);
-                       GL::Texture::unbind();
+               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(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();
+               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)
@@ -152,7 +139,7 @@ const GL::Mesh *Overlay3D::get_graphic(const string &name)
                {
                        DataFile::load(*grf, (FS::Path("icons")/(name+".mesh")).str());
                }
-               catch(const Exception &e)
+               catch(const exception &e)
                {
                        IO::print("Error loading overlay graphic '%s': %s\n", name, e.what());
                        delete grf;
@@ -169,30 +156,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.05f, 0.05f, 0.05f, 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.build_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 Marklin
+} // namespace R2C2