1 #include "PonyGame.hh"
2 #include "LineList.hh"
3
4 using std::multiset;
5
add_point(int i,V2f point,Heightmap & heightmap)6 bool LineList::add_point(int i, V2f point, Heightmap& heightmap) {
7 bool return_value;
8
9 vector<V2f>* v = &line_strips[i];
10 vector<V3f>* v3 = &trails[i];
11
12 V2f old_point;
13
14 if (v->empty()) {
15 v->push_back(point);
16 v->push_back(point);
17
18 return_value = false;
19 } else {
20 if (((*v)[v->size()-2] - point).length() > 2.0) {
21 Line new_line((*v)[v->size()-2], point);
22
23 v->back() = point;
24 v->push_back(point);
25
26 V2f i1(0,0);
27
28 if (intersects(new_line, &i1)) {
29 lines.push_back(new_line);
30 return_value = true;
31 } else {
32 lines.push_back(new_line);
33 return_value = false;
34 }
35 } else {
36 old_point = v->back();
37 v->back() = point;
38 return_value = false;
39 }
40 }
41
42 v3->resize(v->size() * 2);
43
44 V3f p1 = heightmap.get_pos(v->at(v->size()-2), false);
45 V3f p2 = heightmap.get_pos(v->at(v->size()-1), false);
46
47 v3->at(v3->size()-4) = p1 + V3f(0, 1,0);
48 v3->at(v3->size()-3) = p1 + V3f(0,-1,0);
49 v3->at(v3->size()-2) = p2 + V3f(0, 1,0);
50 v3->at(v3->size()-1) = p2 + V3f(0,-1,0);
51
52 return return_value;
53 }
54
intersects(Line & line,V2f * intersection)55 bool LineList::intersects(Line& line, V2f* intersection) {
56
57 for (vector<Line>::iterator i = lines.begin();
58 i != lines.end();
59 i++) {
60 if (line.intersects(*i)) {
61 *intersection = line.intersection(*i);
62 return true;
63 }
64 }
65
66 return false;
67
68 }
69
intersects(Line & line,Line * intersection)70 bool LineList::intersects(Line& line, Line* intersection) {
71
72 bool found = false;
73 float distance = 10000000.0;
74
75 for (vector<Line>::iterator i = lines.begin();
76 i != lines.end();
77 i++) {
78 if (line.intersects(*i)) {
79 if (!found) {
80 *intersection = *i;
81 found = true;
82
83 distance = (line.intersection(*i)-line.a).length();
84 } else {
85 float new_distance =
86 (line.intersection(*i)-line.a).length();
87
88 if (new_distance < distance) {
89 distance = new_distance;
90 *intersection = *i;
91 }
92 }
93 }
94 }
95
96 return found;
97
98 }
99
draw_lines(Config * config)100 void LineList::draw_lines(Config* config)
101 {
102 for (int i = 0; i < 4; i++) {
103 if (line_strips[i].size() < 1)
104 continue;
105
106 int pony_no = i;
107
108 glLineWidth(2.0f);
109
110 glColor(config->pony_color[pony_no]);
111
112 glEnableClientState(GL_VERTEX_ARRAY);
113 glVertexPointer(2, GL_FLOAT, 0, &(line_strips[i][0]));
114
115 glDrawArrays(GL_LINE_STRIP, 0, line_strips[i].size());
116
117 glDisableClientState(GL_VERTEX_ARRAY);
118
119 glLineWidth(1.0f);
120 }
121 }
122
draw_trails(PonyGame * game)123 void LineList::draw_trails(PonyGame* game)
124 {
125 glDisable(GL_LIGHTING);
126 glDisable(GL_CULL_FACE);
127 glDepthMask(GL_TRUE);
128
129 glEnable(GL_BLEND);
130 glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
131
132 for (int i = 0; i < 4; i++) {
133 if (trails[i].size() < 1)
134 continue;
135
136 Color4f c = game->config()->pony_color[i];
137
138 glColor4f(c.r, c.g, c.b, 0.75);
139
140 glEnableClientState(GL_VERTEX_ARRAY);
141 glVertexPointer(3, GL_FLOAT, 0, &(trails[i][0]));
142
143 glDrawArrays(GL_QUAD_STRIP, 0, trails[i].size());
144
145 glDisableClientState(GL_VERTEX_ARRAY);
146 }
147
148 glDisable(GL_BLEND);
149
150 glEnable(GL_LIGHTING);
151 glEnable(GL_CULL_FACE);
152 glDepthMask(GL_TRUE);
153 }
154