]> git.tdb.fi Git - r2c2.git/blob - source/3d/overlay.cpp
Rename the project to R²C²
[r2c2.git] / source / 3d / overlay.cpp
1 /* $Id$
2
3 This file is part of R²C²
4 Copyright © 2010 Mikkosoft Productions, Mikko Rasa
5 Distributed under the GPL
6 */
7
8 #include <algorithm>
9 #include <cmath>
10 #include <msp/fs/path.h>
11 #include <msp/gl/matrix.h>
12 #include <msp/gl/texture.h>
13 #include <msp/io/print.h>
14 #include "overlay.h"
15 #include "track.h"
16 #include "tracktype.h"
17
18 using namespace std;
19 using namespace Msp;
20
21 namespace R2C2 {
22
23 Overlay3D::Overlay3D(const Graphics::Window &w, const GL::Camera &c, const GL::Font &f):
24         window(w),
25         camera(c),
26         font(f)
27 { }
28
29 Overlay3D::~Overlay3D()
30 {
31         for(map<string, GL::Mesh *>::iterator i=graphics.begin(); i!=graphics.end(); ++i)
32                 delete i->second;
33         for(map<const Object3D *, Icon *>::iterator i=icons.begin(); i!=icons.end(); ++i)
34                 delete i->second;
35 }
36
37 void Overlay3D::set_label(const Object3D &track, const string &label)
38 {
39         get_icon(track).label = label;
40         update_icon(get_icon(track));
41 }
42
43 void Overlay3D::add_graphic(const Object3D &track, const string &grf_name)
44 {
45         const GL::Mesh *grf = get_graphic(grf_name);
46         if(!grf)
47                 return;
48
49         Icon &icon = get_icon(track);
50         if(find(icon.graphics.begin(), icon.graphics.end(), grf)==icon.graphics.end())
51                 icon.graphics.push_back(grf);
52
53         update_icon(icon);
54 }
55
56 void Overlay3D::remove_graphic(const Object3D &track, const string &grf_name)
57 {
58         const GL::Mesh *grf = get_graphic(grf_name);
59         Icon &icon = get_icon(track);
60         icon.graphics.erase(remove(icon.graphics.begin(), icon.graphics.end(), grf), icon.graphics.end());
61
62         update_icon(icon);
63 }
64
65 void Overlay3D::clear_graphics(const Object3D &track)
66 {
67         get_icon(track).graphics.clear();
68         update_icon(get_icon(track));
69 }
70
71 void Overlay3D::clear(const Object3D &track)
72 {
73         map<const Object3D *, Icon *>::iterator i = icons.find(&track);
74         if(i!=icons.end())
75         {
76                 delete i->second;
77                 icons.erase(i);
78         }
79 }
80
81 void Overlay3D::render(const GL::Tag &tag) const
82 {
83         if(tag==0)
84         {
85                 GL::matrix_mode(GL::PROJECTION);
86                 GL::push_matrix();
87                 GL::load_identity();
88                 GL::scale(2.0/window.get_width(), 2.0/window.get_height(), 1.0);
89                 GL::matrix_mode(GL::MODELVIEW);
90                 GL::push_matrix();
91                 GL::load_identity();
92
93                 glLineWidth(1);
94
95                 int size = int(font.get_default_size()+0.5);
96                 float spacing = round(size*1.1)/size;
97                 float baseline = round((0.5-font.get_ascent()*0.5-font.get_descent()*0.5)*size)/size;
98
99                 for(map<const Object3D *, Icon *>::const_iterator i=icons.begin(); i!=icons.end(); ++i)
100                 {
101                         if(!i->first->is_visible())
102                                 continue;
103
104                         const Icon &icon = *i->second;
105
106                         Point node = i->first->get_node();
107                         GL::Vector3 p = camera.project(GL::Vector3(node.x, node.y, node.z));
108
109                         GL::PushMatrix push_mat;
110                         p.x = int(p.x*0.5*window.get_width()-icon.width*size/2);
111                         p.y = int(p.y*0.5*window.get_height());
112                         GL::translate(p.x, p.y, p.z);
113                         GL::scale_uniform(size);
114
115                         icon.background.draw();
116
117                         glColor3f(0.0, 1.0, 0.0);
118                         for(vector<const GL::Mesh *>::const_iterator j=icon.graphics.begin(); j!=icon.graphics.end(); ++j)
119                         {
120                                 (*j)->draw();
121                                 GL::translate(spacing, 0, 0);
122                         }
123
124                         GL::translate(0, baseline, 0);
125                         font.draw_string(icon.label);
126                         GL::Texture::unbind();
127                 }
128
129                 GL::matrix_mode(GL::PROJECTION);
130                 GL::pop_matrix();
131                 GL::matrix_mode(GL::MODELVIEW);
132                 GL::pop_matrix();
133         }
134 }
135
136 Overlay3D::Icon &Overlay3D::get_icon(const Object3D &track)
137 {
138         Icon *&icon = icons[&track];
139         if(!icon)
140                 icon = new Icon;
141
142         return *icon;
143 }
144
145 const GL::Mesh *Overlay3D::get_graphic(const string &name)
146 {
147         GL::Mesh *&grf = graphics[name];
148         if(!grf)
149         {
150                 grf = new GL::Mesh;
151                 try
152                 {
153                         DataFile::load(*grf, (FS::Path("icons")/(name+".mesh")).str());
154                 }
155                 catch(const Exception &e)
156                 {
157                         IO::print("Error loading overlay graphic '%s': %s\n", name, e.what());
158                         delete grf;
159                         grf = 0;
160                 }
161         }
162
163         return grf;
164 }
165
166 void Overlay3D::update_icon(Icon &icon)
167 {
168         icon.background.clear();
169
170         icon.width = max(icon.graphics.size()*1.1+font.get_string_width(icon.label), 1.0);
171
172         GL::MeshBuilder bld(icon.background);
173         bld.color(0.2f, 0.2f, 0.2f, 0.7f);
174
175         bld.begin(GL::TRIANGLE_FAN);
176         bld.vertex(0.4, 0.5);
177         bld.vertex(icon.width-0.4, 0.5);
178         bld.vertex(icon.width-0.4, 1.2);
179         for(int i=4; i<=12; ++i)
180                 bld.vertex(0.4+cos(i*M_PI/8)*0.7, 0.5+sin(i*M_PI/8)*0.7);
181         bld.end();
182
183         bld.begin(GL::TRIANGLE_FAN);
184         bld.vertex(icon.width-0.4, 0.5);
185         bld.vertex(0.4, 0.5);
186         bld.vertex(0.4, -0.2);
187         for(int i=-4; i<=4; ++i)
188                 bld.vertex(icon.width-0.4+cos(i*M_PI/8)*0.7, 0.5+sin(i*M_PI/8)*0.7);
189         bld.end();
190 }
191
192
193 Overlay3D::Icon::Icon():
194         width(1.0),
195         background((GL::COLOR4_UBYTE, GL::VERTEX2))
196 { }
197
198 } // namespace R2C2