]> git.tdb.fi Git - r2c2.git/blob - source/designer/trackwrap.cpp
Rename Point to Vector
[r2c2.git] / source / designer / trackwrap.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 <msp/gl/matrix.h>
9 #include <msp/gl/meshbuilder.h>
10 #include "3d/tracktype.h"
11 #include "designer.h"
12 #include "selection.h"
13 #include "trackwrap.h"
14
15 using namespace std;
16 using namespace Msp;
17 using namespace R2C2;
18
19 TrackWrap::TrackWrap(Designer &d, Selection &s):
20         designer(d),
21         selection(s)
22 {
23         selection.signal_changed.connect(sigc::mem_fun(this, &TrackWrap::selection_changed));
24 }
25
26 TrackWrap::~TrackWrap()
27 {
28         for(map<const TrackType *, GL::Mesh *>::iterator i=meshes.begin(); i!=meshes.end(); ++i)
29                 delete i->second;
30 }
31
32 void TrackWrap::render(const GL::Tag &) const
33 {
34         for(list<Wrap>::const_iterator i=wraps.begin(); i!=wraps.end(); ++i)
35         {
36                 GL::PushMatrix _pushm;
37                 const Vector &pos = i->track->get_position();
38                 GL::translate(pos.x, pos.y, pos.z);
39                 GL::rotate(i->track->get_rotation()*180/M_PI, 0, 0, 1);
40                 i->mesh->draw();
41         }
42 }
43
44 void TrackWrap::selection_changed()
45 {
46         wraps.clear();
47         const set<Track *> &tracks = selection.get_tracks();
48         for(set<Track *>::iterator i=tracks.begin(); i!=tracks.end(); ++i)
49         {
50                 Wrap wrap;
51                 wrap.track = *i;
52                 wrap.mesh = &get_mesh((*i)->get_type());
53                 wraps.push_back(wrap);
54         }
55 }
56
57 GL::Mesh &TrackWrap::get_mesh(const TrackType &type)
58 {
59         map<const TrackType *, GL::Mesh *>::iterator j = meshes.find(&type);
60         if(j!=meshes.end())
61                 return *j->second;
62
63         const TrackType3D &type3d = designer.get_layout_3d().get_catalogue().get_track(type);
64
65         float min_area = -1;
66         float angle = 0;
67         Vector center;
68         float width = 0;
69         float height = 0;
70         for(float a=0; a<M_PI; a+=0.01)
71         {
72                 Vector minp, maxp;
73                 type3d.get_bounds(a, minp, maxp);
74                 float area = (maxp.x-minp.x)*(maxp.y-minp.y);
75                 if(area<min_area || min_area<0)
76                 {
77                         float c = cos(a);
78                         float s = sin(a);
79                         float x = (minp.x+maxp.x)/2;
80                         float y = (minp.y+maxp.y)/2;
81                         center = Vector(c*x-s*y, s*x+c*y, minp.z);
82                         angle = a;
83                         width = maxp.x-minp.x+0.01;
84                         height = maxp.y-minp.y+0.01;
85
86                         min_area = area;
87                 }
88         }
89
90         GL::Mesh *mesh = new GL::Mesh((GL::COLOR4_UBYTE, GL::VERTEX2));
91         GL::MeshBuilder bld(*mesh);
92         bld.color(0.0f, 1.0f, 0.0f, 1.0f);
93
94         float c = cos(angle);
95         float s = sin(angle);
96
97         bld.begin(GL::LINE_LOOP);
98         bld.vertex(center.x-c*width/2+s*height/2, center.y-s*width/2-c*height/2);
99         bld.vertex(center.x+c*width/2+s*height/2, center.y+s*width/2-c*height/2);
100         bld.vertex(center.x+c*width/2-s*height/2, center.y+s*width/2+c*height/2);
101         bld.vertex(center.x-c*width/2-s*height/2, center.y-s*width/2+c*height/2);
102         bld.end();
103
104         meshes[&type] = mesh;
105
106         return *mesh;
107 }