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