SUMO - Simulation of Urban MObility
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
GLHelper.cpp
Go to the documentation of this file.
1 /****************************************************************************/
9 // Some methods which help to draw certain geometrical objects in openGL
10 /****************************************************************************/
11 // SUMO, Simulation of Urban MObility; see http://sumo-sim.org/
12 // Copyright (C) 2001-2014 DLR (http://www.dlr.de/) and contributors
13 /****************************************************************************/
14 //
15 // This file is part of SUMO.
16 // SUMO is free software: you can redistribute it and/or modify
17 // it under the terms of the GNU General Public License as published by
18 // the Free Software Foundation, either version 3 of the License, or
19 // (at your option) any later version.
20 //
21 /****************************************************************************/
22 
23 
24 // ===========================================================================
25 // included modules
26 // ===========================================================================
27 #ifdef _MSC_VER
28 #include <windows_config.h>
29 #else
30 #include <config.h>
31 #endif
32 
33 #include "GLHelper.h"
34 #include <utils/geom/GeomHelper.h>
35 #include <utils/common/StdDefs.h>
38 
39 #ifdef CHECK_MEMORY_LEAKS
40 #include <foreign/nvwa/debug_new.h>
41 #endif // CHECK_MEMORY_LEAKS
42 
43 
44 // ===========================================================================
45 // static member definitions
46 // ===========================================================================
47 std::vector<std::pair<SUMOReal, SUMOReal> > GLHelper::myCircleCoords;
48 
49 
50 void APIENTRY combCallback(GLdouble coords[3],
51  GLdouble* vertex_data[4],
52  GLfloat weight[4], GLdouble** dataOut) {
53  UNUSED_PARAMETER(weight);
54  UNUSED_PARAMETER(*vertex_data);
55  GLdouble* vertex;
56 
57  vertex = (GLdouble*)malloc(7 * sizeof(GLdouble));
58 
59  vertex[0] = coords[0];
60  vertex[1] = coords[1];
61  vertex[2] = coords[2];
62  *dataOut = vertex;
63 }
64 
65 // ===========================================================================
66 // method definitions
67 // ===========================================================================
68 
69 
70 void
72  if (v.size() == 0) {
73  return;
74  }
75  glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
76  glBegin(GL_POLYGON);
77  for (PositionVector::const_iterator i = v.begin(); i != v.end(); i++) {
78  const Position& p = *i;
79  glVertex2d(p.x(), p.y());
80  }
81  if (close) {
82  const Position& p = *(v.begin());
83  glVertex2d(p.x(), p.y());
84  }
85  glEnd();
86 }
87 
88 
89 void
91  if (v.size() == 0) {
92  return;
93  }
94  GLUtesselator* tobj = gluNewTess();
95  gluTessCallback(tobj, GLU_TESS_VERTEX, (GLvoid(APIENTRY*)()) &glVertex3dv);
96  gluTessCallback(tobj, GLU_TESS_BEGIN, (GLvoid(APIENTRY*)()) &glBegin);
97  gluTessCallback(tobj, GLU_TESS_END, (GLvoid(APIENTRY*)()) &glEnd);
98  gluTessCallback(tobj, GLU_TESS_COMBINE, (GLvoid(APIENTRY*)()) &combCallback);
99  gluTessProperty(tobj, GLU_TESS_WINDING_RULE, GLU_TESS_WINDING_ODD);
100  gluTessBeginPolygon(tobj, NULL);
101  gluTessBeginContour(tobj);
102  double* points = new double[(v.size() + int(close)) * 3];
103 
104  for (size_t i = 0; i != v.size(); ++i) {
105  points[3 * i] = v[(int)i].x();
106  points[3 * i + 1] = v[(int)i].y();
107  points[3 * i + 2] = 0;
108  gluTessVertex(tobj, points + 3 * i, points + 3 * i);
109  }
110  if (close) {
111  const size_t i = v.size();
112  points[3 * i] = v[0].x();
113  points[3 * i + 1] = v[0].y();
114  points[3 * i + 2] = 0;
115  gluTessVertex(tobj, points + 3 * i, points + 3 * i);
116  }
117  gluTessEndContour(tobj);
118  gluTessEndPolygon(tobj);
119  gluDeleteTess(tobj);
120 }
121 
122 
123 void
124 GLHelper::drawBoxLine(const Position& beg, SUMOReal rot, SUMOReal visLength,
125  SUMOReal width) {
126  glPushMatrix();
127  glTranslated(beg.x(), beg.y(), 0);
128  glRotated(rot, 0, 0, 1);
129  glBegin(GL_QUADS);
130  glVertex2d(-width, 0);
131  glVertex2d(-width, -visLength);
132  glVertex2d(width, -visLength);
133  glVertex2d(width, 0);
134  glEnd();
135  glPopMatrix();
136 }
137 
138 
139 void
140 GLHelper::drawBoxLine(const Position& beg1, const Position& beg2,
141  SUMOReal rot, SUMOReal visLength,
142  SUMOReal width) {
143  glPushMatrix();
144  glTranslated((beg2.x() + beg1.x())*.5, (beg2.y() + beg1.y())*.5, 0);
145  glRotated(rot, 0, 0, 1);
146  glBegin(GL_QUADS);
147  glVertex2d(-width, 0);
148  glVertex2d(-width, -visLength);
149  glVertex2d(width, -visLength);
150  glVertex2d(width, 0);
151  glEnd();
152  glPopMatrix();
153 }
154 
155 
156 void
158  const std::vector<SUMOReal>& rots,
159  const std::vector<SUMOReal>& lengths,
160  SUMOReal width, int cornerDetail) {
161  int e = (int) geom.size() - 1;
162  for (int i = 0; i < e; i++) {
163  drawBoxLine(geom[i], rots[i], lengths[i], width);
164  }
165  if (cornerDetail > 0) {
166  for (int i = 1; i < e; i++) {
167  glPushMatrix();
168  glTranslated(geom[i].x(), geom[i].y(), 0);
169  drawFilledCircle(width, cornerDetail);
170  glEnd();
171  glPopMatrix();
172  }
173  }
174 }
175 
176 
177 void
179  const PositionVector& geom2,
180  const std::vector<SUMOReal>& rots,
181  const std::vector<SUMOReal>& lengths,
182  SUMOReal width) {
183  int minS = (int) MIN4(rots.size(), lengths.size(), geom1.size(), geom2.size());
184  for (int i = 0; i < minS; i++) {
185  GLHelper::drawBoxLine(geom1[i], geom2[i], rots[i], lengths[i], width);
186  }
187 }
188 
189 
190 void
192  int e = (int) geom.size() - 1;
193  for (int i = 0; i < e; i++) {
194  const Position& f = geom[i];
195  const Position& s = geom[i + 1];
196  drawBoxLine(f,
197  RAD2DEG(atan2((s.x() - f.x()), (f.y() - s.y()))),
198  f.distanceTo(s),
199  width);
200  }
201 }
202 
203 
204 void
205 GLHelper::drawLine(const Position& beg, SUMOReal rot, SUMOReal visLength) {
206  glPushMatrix();
207  glTranslated(beg.x(), beg.y(), 0);
208  glRotated(rot, 0, 0, 1);
209  glBegin(GL_LINES);
210  glVertex2d(0, 0);
211  glVertex2d(0, -visLength);
212  glEnd();
213  glPopMatrix();
214 }
215 
216 
217 void
218 GLHelper::drawLine(const Position& beg1, const Position& beg2,
219  SUMOReal rot, SUMOReal visLength) {
220  glPushMatrix();
221  glTranslated((beg2.x() + beg1.x())*.5, (beg2.y() + beg1.y())*.5, 0);
222  glRotated(rot, 0, 0, 1);
223  glBegin(GL_LINES);
224  glVertex2d(0, 0);
225  glVertex2d(0, -visLength);
226  glEnd();
227  glPopMatrix();
228 }
229 
230 
231 
232 void
234  glBegin(GL_LINES);
235  int e = (int) v.size() - 1;
236  for (int i = 0; i < e; ++i) {
237  glVertex2d(v[i].x(), v[i].y());
238  glVertex2d(v[i + 1].x(), v[i + 1].y());
239  }
240  glEnd();
241 }
242 
243 
244 
245 void
246 GLHelper::drawLine(const Position& beg, const Position& end) {
247  glBegin(GL_LINES);
248  glVertex2d(beg.x(), beg.y());
249  glVertex2d(end.x(), end.y());
250  glEnd();
251 }
252 
253 
254 
255 void
257  drawFilledCircle(width, steps, 0, 360);
258 }
259 
260 
261 void
263  if (myCircleCoords.size() == 0) {
264  for (int i = 0; i < 360; i += 10) {
265  const SUMOReal x = (SUMOReal) sin(DEG2RAD(i));
266  const SUMOReal y = (SUMOReal) cos(DEG2RAD(i));
267  myCircleCoords.push_back(std::pair<SUMOReal, SUMOReal>(x, y));
268  }
269  }
270  glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
271  std::pair<SUMOReal, SUMOReal> p1 =
272  beg == 0 ? myCircleCoords[0] : myCircleCoords[((int) beg / 10) % 36];
273  for (int i = (int)(beg / 10); i < steps && (36.0 / (SUMOReal) steps * (SUMOReal) i) * 10 < end; i++) {
274  const std::pair<SUMOReal, SUMOReal>& p2 =
275  myCircleCoords[(size_t)(36.0 / (SUMOReal) steps * (SUMOReal) i)];
276  glBegin(GL_TRIANGLES);
277  glVertex2d(p1.first * width, p1.second * width);
278  glVertex2d(p2.first * width, p2.second * width);
279  glVertex2d(0, 0);
280  glEnd();
281  p1 = p2;
282  }
283  const std::pair<SUMOReal, SUMOReal>& p2 =
284  end == 360 ? myCircleCoords[0] : myCircleCoords[((int) end / 10) % 36];
285  glBegin(GL_TRIANGLES);
286  glVertex2d(p1.first * width, p1.second * width);
287  glVertex2d(p2.first * width, p2.second * width);
288  glVertex2d(0, 0);
289  glEnd();
290 }
291 
292 
293 void
294 GLHelper::drawOutlineCircle(SUMOReal width, SUMOReal iwidth, int steps) {
295  drawOutlineCircle(width, iwidth, steps, 0, 360);
296 }
297 
298 
299 void
301  SUMOReal beg, SUMOReal end) {
302  if (myCircleCoords.size() == 0) {
303  for (int i = 0; i < 360; i += 10) {
304  SUMOReal x = (SUMOReal) sin(DEG2RAD(i));
305  SUMOReal y = (SUMOReal) cos(DEG2RAD(i));
306  myCircleCoords.push_back(std::pair<SUMOReal, SUMOReal>(x, y));
307  }
308  }
309  glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
310  std::pair<SUMOReal, SUMOReal> p1 =
311  beg == 0 ? myCircleCoords[0] : myCircleCoords[((int) beg / 10) % 36];
312  for (int i = (int)(beg / 10); i < steps && (36.0 / (SUMOReal) steps * (SUMOReal) i) * 10 < end; i++) {
313  const std::pair<SUMOReal, SUMOReal>& p2 =
314  myCircleCoords[(size_t)(36.0 / (SUMOReal) steps * (SUMOReal) i)];
315  glBegin(GL_TRIANGLES);
316  glVertex2d(p1.first * width, p1.second * width);
317  glVertex2d(p2.first * width, p2.second * width);
318  glVertex2d(p2.first * iwidth, p2.second * iwidth);
319 
320  glVertex2d(p2.first * iwidth, p2.second * iwidth);
321  glVertex2d(p1.first * iwidth, p1.second * iwidth);
322  glVertex2d(p1.first * width, p1.second * width);
323  glEnd();
324  p1 = p2;
325  }
326  const std::pair<SUMOReal, SUMOReal>& p2 =
327  end == 360 ? myCircleCoords[0] : myCircleCoords[((int) end / 10) % 36];
328  glBegin(GL_TRIANGLES);
329  glVertex2d(p1.first * width, p1.second * width);
330  glVertex2d(p2.first * width, p2.second * width);
331  glVertex2d(p2.first * iwidth, p2.second * iwidth);
332 
333  glVertex2d(p2.first * iwidth, p2.second * iwidth);
334  glVertex2d(p1.first * iwidth, p1.second * iwidth);
335  glVertex2d(p1.first * width, p1.second * width);
336  glEnd();
337 }
338 
339 
340 void
342  SUMOReal tWidth) {
343  if (l.length() < tLength) {
344  tWidth = tWidth * l.length() / tLength;
345  tLength = l.length();
346  }
347  Line rl(l.getPositionAtDistance(l.length() - tLength), l.p2());
348  glPushMatrix();
349  glTranslated(rl.p1().x(), rl.p1().y(), 0);
350  glRotated(-l.atan2DegreeAngle(), 0, 0, 1);
351  glBegin(GL_TRIANGLES);
352  glVertex2d(0, -tLength);
353  glVertex2d(-tWidth, 0);
354  glVertex2d(+tWidth, 0);
355  glEnd();
356  glPopMatrix();
357 }
358 
359 
360 void
362  glColor4ub(c.red(), c.green(), c.blue(), c.alpha());
363 }
364 
365 
366 RGBColor
368  GLdouble current[4];
369  glGetDoublev(GL_CURRENT_COLOR, current);
370  return RGBColor(static_cast<unsigned char>(current[0] * 255. + 0.5),
371  static_cast<unsigned char>(current[1] * 255. + 0.5),
372  static_cast<unsigned char>(current[2] * 255. + 0.5),
373  static_cast<unsigned char>(current[3] * 255. + 0.5));
374 }
375 
376 
377 void
378 GLHelper::drawText(const std::string& text, const Position& pos,
379  const SUMOReal layer, const SUMOReal size,
380  const RGBColor& col, const SUMOReal angle) {
381  glPushMatrix();
382  glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
383  setColor(col);
384  glTranslated(pos.x(), pos.y(), layer);
385  pfSetPosition(0, 0);
386  pfSetScale(size);
387  SUMOReal w = pfdkGetStringWidth(text.c_str());
388  glRotated(180, 1, 0, 0);
389  glRotated(angle, 0, 0, 1);
390  glTranslated(-w / 2., 0.4, 0);
391  pfDrawString(text.c_str());
392  glPopMatrix();
393 }
394 
395 void
396 GLHelper::drawTextBox(const std::string& text, const Position& pos,
397  const SUMOReal layer, const SUMOReal size,
398  const RGBColor& txtColor, const RGBColor& bgColor, const RGBColor& borderColor,
399  const SUMOReal angle) {
400  SUMOReal boxAngle = angle + 90;
401  if (boxAngle > 360) {
402  boxAngle -= 360;
403  }
404  pfSetScale(size);
405  const SUMOReal stringWidth = pfdkGetStringWidth(text.c_str());
406  const SUMOReal borderWidth = size / 20;
407  const SUMOReal boxHeight = size * 0.8;
408  const SUMOReal boxWidth = stringWidth + size / 2;
409  glPushMatrix();
410  glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
411  glTranslated(0, 0, layer);
412  setColor(borderColor);
413  Position left = pos;
414  left.sub(boxWidth / 2, -boxHeight / 2.7);
415  drawBoxLine(left, boxAngle, boxWidth, boxHeight);
416  left.add(borderWidth * 1.5, 0);
417  setColor(bgColor);
418  glTranslated(0, 0, 0.01);
419  drawBoxLine(left, boxAngle, boxWidth - 3 * borderWidth, boxHeight - 2 * borderWidth);
420  // actually we should be able to use drawText here. however, there's
421  // something about the constant 0.4 offset which causes trouble
422  //drawText(text, pos, layer+0.02, size, txtColor, angle);
423  setColor(txtColor);
424  glTranslated(pos.x(), pos.y(), 0.01);
425  pfSetPosition(0, 0);
426  pfSetScale(size);
427  glRotated(180, 1, 0, 0);
428  glRotated(angle, 0, 0, 1);
429  glTranslated(-stringWidth / 2., 0, 0);
430  pfDrawString(text.c_str());
431  glPopMatrix();
432 }
433 
434 /****************************************************************************/
435