]> git.tdb.fi Git - r2c2.git/blob - source/3d/layout.cpp
b95d4e59c835798d2df81530d060e6e7303bff7d
[r2c2.git] / source / 3d / layout.cpp
1 /* $Id$
2
3 This file is part of the MSP Märklin suite
4 Copyright © 2006-2010 Mikkosoft Productions, Mikko Rasa
5 Distributed under the GPL
6 */
7
8 #include <algorithm>
9 #include <limits>
10 #include <msp/gl/clip.h>
11 #include <msp/gl/matrix.h>
12 #include <msp/gl/rendermode.h>
13 #include <msp/gl/select.h>
14 #include <msp/gl/texture.h>
15 #include <msp/datafile/parser.h>
16 #include "layout.h"
17
18 using namespace std;
19 using namespace Msp;
20
21 namespace Marklin {
22
23 Layout3D::Layout3D(Layout &l):
24         layout(l),
25         catalogue(layout.get_catalogue()),
26         quality(4)
27 {
28         layout.signal_track_added.connect(sigc::mem_fun(this, &Layout3D::track_added));
29         layout.signal_track_removed.connect(sigc::mem_fun(this, &Layout3D::track_removed));
30
31         const set<Track *> &ltracks = layout.get_tracks();
32         for(set<Track *>::iterator i=ltracks.begin(); i!=ltracks.end(); ++i)
33                 track_added(**i);
34 }
35
36 Layout3D::~Layout3D()
37 {
38         while(!tracks.empty())
39                 delete tracks.front();
40 }
41
42 void Layout3D::set_quality(unsigned q)
43 {
44         quality = q;
45 }
46
47 void Layout3D::add_track(Track3D &t)
48 {
49         tracks.push_back(&t);
50 }
51
52 void Layout3D::remove_track(Track3D &t)
53 {
54         list<Track3D *>::iterator i = find(tracks.begin(), tracks.end(), &t);
55         if(i!=tracks.end())
56                 tracks.erase(i);
57 }
58
59 Track3D &Layout3D::get_track(const Track &t) const
60 {
61         for(list<Track3D *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
62                 if(&(*i)->get_track()==&t)
63                         return **i;
64
65         throw KeyError("Unknown track");
66 }
67
68 Track3D *Layout3D::pick_track(float x, float y, float size) const
69 {
70         vector<GL::SelectRecord> select_buf;
71         GL::select_buffer(select_buf);
72         GL::render_mode(GL::SELECT);
73
74         {
75                 GL::PushMatrix push_mat;
76                 GL::load_identity();
77
78                 GL::ClipPlane(1, 0, x-size, 0).apply_to(0);
79                 GL::ClipPlane(-1, 0, -x-size, 0).apply_to(1);
80                 GL::ClipPlane(0, 1, y-size, 0).apply_to(2);
81                 GL::ClipPlane(0, -1, -y-size, 0).apply_to(3);
82         }
83
84         scene.render(0);
85
86         GL::ClipPlane::disable(0);
87         GL::ClipPlane::disable(1);
88         GL::ClipPlane::disable(2);
89         GL::ClipPlane::disable(3);
90
91         GL::render_mode(GL::RENDER);
92         Track3D *track = 0;
93         unsigned track_depth = numeric_limits<unsigned>::max();
94         for(vector<GL::SelectRecord>::iterator i=select_buf.begin(); i!=select_buf.end(); ++i)
95                 if(i->min_depth<track_depth)
96                 {
97                         track = reinterpret_cast<Track3D *>(i->names.back());
98                         track_depth = i->min_depth;
99                 }
100
101         return track;
102 }
103
104 void Layout3D::track_added(Track &t)
105 {
106         new Track3D(*this, t);
107 }
108
109 void Layout3D::track_removed(Track &t)
110 {
111         for(list<Track3D *>::iterator i=tracks.begin(); i!=tracks.end(); ++i)
112                 if(&(*i)->get_track()==&t)
113                 {
114                         delete *i;
115                         return;
116                 }
117 }
118
119 } // namespace Marklin