GLHelper.cpp

Go to the documentation of this file.
00001 /****************************************************************************/
00007 // Some methods which help to draw certain geometrical objects in openGL
00008 /****************************************************************************/
00009 // SUMO, Simulation of Urban MObility; see http://sumo.sourceforge.net/
00010 // Copyright 2001-2010 DLR (http://www.dlr.de/) and contributors
00011 /****************************************************************************/
00012 //
00013 //   This program is free software; you can redistribute it and/or modify
00014 //   it under the terms of the GNU General Public License as published by
00015 //   the Free Software Foundation; either version 2 of the License, or
00016 //   (at your option) any later version.
00017 //
00018 /****************************************************************************/
00019 
00020 
00021 // ===========================================================================
00022 // included modules
00023 // ===========================================================================
00024 #ifdef _MSC_VER
00025 #include <windows_config.h>
00026 #else
00027 #include <config.h>
00028 #endif
00029 
00030 #ifdef _WIN32
00031 #include <windows.h>
00032 #endif
00033 
00034 #include <GL/gl.h>
00035 
00036 #include "GLHelper.h"
00037 #include <utils/geom/GeomHelper.h>
00038 #include <utils/common/StdDefs.h>
00039 
00040 #ifdef CHECK_MEMORY_LEAKS
00041 #include <foreign/nvwa/debug_new.h>
00042 #endif // CHECK_MEMORY_LEAKS
00043 
00044 
00045 // ===========================================================================
00046 // static member definitions
00047 // ===========================================================================
00048 std::vector<std::pair<SUMOReal, SUMOReal> > GLHelper::myCircleCoords;
00049 
00050 
00051 // ===========================================================================
00052 // method definitions
00053 // ===========================================================================
00054 void
00055 GLHelper::drawFilledPoly(const Position2DVector &v, bool close) throw() {
00056     if (v.size()==0) {
00057         return;
00058     }
00059     glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
00060     glBegin(GL_POLYGON);
00061     const Position2DVector::ContType &l = v.getCont();
00062     for (Position2DVector::ContType ::const_iterator i=l.begin(); i!=l.end(); i++) {
00063         const Position2D &p = *i;
00064         glVertex2d(p.x(), p.y());
00065     }
00066     if (close) {
00067         const Position2D &p = *(l.begin());
00068         glVertex2d(p.x(), p.y());
00069     }
00070     glEnd();
00071 }
00072 
00073 
00074 void
00075 GLHelper::drawBoxLine(const Position2D &beg, SUMOReal rot, SUMOReal visLength,
00076                       SUMOReal width) throw() {
00077     glPushMatrix();
00078     glTranslated(beg.x(), beg.y(), 0);
00079     glRotated(rot, 0, 0, 1);
00080     glBegin(GL_QUADS);
00081     glVertex2d(-width, 0);
00082     glVertex2d(-width, -visLength);
00083     glVertex2d(width, -visLength);
00084     glVertex2d(width, 0);
00085     glEnd();
00086     glPopMatrix();
00087 }
00088 
00089 
00090 void
00091 GLHelper::drawBoxLine(const Position2D &beg1, const Position2D &beg2,
00092                       SUMOReal rot, SUMOReal visLength,
00093                       SUMOReal width) throw() {
00094     glPushMatrix();
00095     glTranslated((beg2.x()+beg1.x())*.5, (beg2.y()+beg1.y())*.5, 0);
00096     glRotated(rot, 0, 0, 1);
00097     glBegin(GL_QUADS);
00098     glVertex2d(-width, 0);
00099     glVertex2d(-width, -visLength);
00100     glVertex2d(width, -visLength);
00101     glVertex2d(width, 0);
00102     glEnd();
00103     glPopMatrix();
00104 }
00105 
00106 
00107 void
00108 GLHelper::drawBoxLines(const Position2DVector &geom,
00109                        const std::vector<SUMOReal> &rots,
00110                        const std::vector<SUMOReal> &lengths,
00111                        SUMOReal width) throw() {
00112     int e = (int) geom.size() - 1;
00113     for (int i=0; i<e; i++) {
00114         drawBoxLine(geom[i], rots[i], lengths[i], width);
00115     }
00116 }
00117 
00118 
00119 void
00120 GLHelper::drawBoxLines(const Position2DVector &geom1,
00121                        const Position2DVector &geom2,
00122                        const std::vector<SUMOReal> &rots,
00123                        const std::vector<SUMOReal> &lengths,
00124                        SUMOReal width) throw() {
00125     int minS = (int) MIN4(rots.size(), lengths.size(), geom1.size(), geom2.size());
00126     for (int i=0; i<minS; i++) {
00127         GLHelper::drawBoxLine(geom1[i], geom2[i], rots[i], lengths[i], width);
00128     }
00129 }
00130 
00131 
00132 void
00133 GLHelper::drawBoxLines(const Position2DVector &geom, SUMOReal width) throw() {
00134     int e = (int) geom.size() - 1;
00135     for (int i=0; i<e; i++) {
00136         const Position2D &f = geom[i];
00137         const Position2D &s = geom[i+1];
00138         drawBoxLine(f,
00139                     (SUMOReal) atan2((s.x()-f.x()), (f.y()-s.y()))*(SUMOReal) 180.0/(SUMOReal) PI,
00140                     f.distanceTo(s),
00141                     width);
00142     }
00143 }
00144 
00145 
00146 void
00147 GLHelper::drawLine(const Position2D &beg, SUMOReal rot, SUMOReal visLength) throw() {
00148     glPushMatrix();
00149     glTranslated(beg.x(), beg.y(), 0);
00150     glRotated(rot, 0, 0, 1);
00151     glBegin(GL_LINES);
00152     glVertex2d(0, 0);
00153     glVertex2d(0, -visLength);
00154     glEnd();
00155     glPopMatrix();
00156 }
00157 
00158 
00159 void
00160 GLHelper::drawLine(const Position2D &beg1, const Position2D &beg2,
00161                    SUMOReal rot, SUMOReal visLength) throw() {
00162     glPushMatrix();
00163     glTranslated((beg2.x()+beg1.x())*.5, (beg2.y()+beg1.y())*.5, 0);
00164     glRotated(rot, 0, 0, 1);
00165     glBegin(GL_LINES);
00166     glVertex2d(0, 0);
00167     glVertex2d(0, -visLength);
00168     glEnd();
00169     glPopMatrix();
00170 }
00171 
00172 
00173 
00174 void
00175 GLHelper::drawLine(const Position2DVector &v) throw() {
00176     glBegin(GL_LINES);
00177     int e = (int) v.size() - 1;
00178     for (int i=0; i<e; ++i) {
00179         glVertex2d(v[i].x(), v[i].y());
00180         glVertex2d(v[i+1].x(), v[i+1].y());
00181     }
00182     glEnd();
00183 }
00184 
00185 
00186 
00187 void
00188 GLHelper::drawLine(const Position2D &beg, const Position2D &end) throw() {
00189     glBegin(GL_LINES);
00190     glVertex2d(beg.x(), end.y());
00191     glVertex2d(beg.x(), end.y());
00192     glEnd();
00193 }
00194 
00195 
00196 
00197 void
00198 GLHelper::drawFilledCircle(SUMOReal width, int steps) throw() {
00199     drawFilledCircle(width, steps, 0, 360);
00200 }
00201 
00202 
00203 void
00204 GLHelper::drawFilledCircle(SUMOReal width, int steps, SUMOReal beg, SUMOReal end) throw() {
00205     if (myCircleCoords.size()==0) {
00206         for (int i=0; i<360; i+=10) {
00207             SUMOReal x = (SUMOReal) sin((SUMOReal) i / 180.0 * PI);
00208             SUMOReal y = (SUMOReal) cos((SUMOReal) i / 180.0 * PI);
00209             myCircleCoords.push_back(std::pair<SUMOReal, SUMOReal>(x, y));
00210         }
00211     }
00212     glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
00213     std::pair<SUMOReal, SUMOReal> p1 =
00214         beg==0 ? myCircleCoords[0] : myCircleCoords[((int) beg/10)%36];
00215     for (int i=(int)(beg/10); i<steps&&(36.0/(SUMOReal) steps *(SUMOReal) i)*10<end; i++) {
00216         const std::pair<SUMOReal, SUMOReal> &p2 =
00217             myCircleCoords[(size_t)(36.0/(SUMOReal) steps * (SUMOReal) i)];
00218         glBegin(GL_TRIANGLES);
00219         glVertex2d(p1.first * width, p1.second * width);
00220         glVertex2d(p2.first * width, p2.second * width);
00221         glVertex2d(0, 0);
00222         glEnd();
00223         p1 = p2;
00224     }
00225     const std::pair<SUMOReal, SUMOReal> &p2 =
00226         end==360 ? myCircleCoords[0] : myCircleCoords[((int) end/10)%36];
00227     glBegin(GL_TRIANGLES);
00228     glVertex2d(p1.first * width, p1.second * width);
00229     glVertex2d(p2.first * width, p2.second * width);
00230     glVertex2d(0, 0);
00231     glEnd();
00232 }
00233 
00234 
00235 void
00236 GLHelper::drawOutlineCircle(SUMOReal width, SUMOReal iwidth, int steps) throw() {
00237     drawOutlineCircle(width, iwidth, steps, 0, 360);
00238 }
00239 
00240 
00241 void
00242 GLHelper::drawOutlineCircle(SUMOReal width, SUMOReal iwidth, int steps,
00243                             SUMOReal beg, SUMOReal end) throw() {
00244     if (myCircleCoords.size()==0) {
00245         for (int i=0; i<360; i+=10) {
00246             SUMOReal x = (SUMOReal) sin((SUMOReal) i / 180.0 * PI);
00247             SUMOReal y = (SUMOReal) cos((SUMOReal) i / 180.0 * PI);
00248             myCircleCoords.push_back(std::pair<SUMOReal, SUMOReal>(x, y));
00249         }
00250     }
00251     glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
00252     std::pair<SUMOReal, SUMOReal> p1 =
00253         beg==0 ? myCircleCoords[0] : myCircleCoords[((int) beg/10)%36];
00254     for (int i=(int)(beg/10); i<steps&&(36.0/(SUMOReal) steps *(SUMOReal) i)*10<end; i++) {
00255         const std::pair<SUMOReal, SUMOReal> &p2 =
00256             myCircleCoords[(size_t)(36.0/(SUMOReal) steps * (SUMOReal) i)];
00257         glBegin(GL_TRIANGLES);
00258         glVertex2d(p1.first * width, p1.second * width);
00259         glVertex2d(p2.first * width, p2.second * width);
00260         glVertex2d(p2.first * iwidth, p2.second * iwidth);
00261 
00262         glVertex2d(p2.first * iwidth, p2.second * iwidth);
00263         glVertex2d(p1.first * iwidth, p1.second * iwidth);
00264         glVertex2d(p1.first * width, p1.second * width);
00265         glEnd();
00266         p1 = p2;
00267     }
00268     const std::pair<SUMOReal, SUMOReal> &p2 =
00269         end==360 ? myCircleCoords[0] : myCircleCoords[((int) end/10)%36];
00270     glBegin(GL_TRIANGLES);
00271     glVertex2d(p1.first * width, p1.second * width);
00272     glVertex2d(p2.first * width, p2.second * width);
00273     glVertex2d(p2.first * iwidth, p2.second * iwidth);
00274 
00275     glVertex2d(p2.first * iwidth, p2.second * iwidth);
00276     glVertex2d(p1.first * iwidth, p1.second * iwidth);
00277     glVertex2d(p1.first * width, p1.second * width);
00278     glEnd();
00279 }
00280 
00281 
00282 void
00283 GLHelper::drawTriangleAtEnd(const Line2D &l, SUMOReal tLength,
00284                             SUMOReal tWidth) throw() {
00285     if (l.length()<tLength) {
00286         tWidth = tWidth * l.length() / tLength;
00287         tLength = l.length();
00288     }
00289     Line2D rl(l.getPositionAtDistance(l.length()-tLength), l.p2());
00290     glPushMatrix();
00291     glTranslated(rl.p1().x(), rl.p1().y(), 0);
00292     glRotated(-l.atan2DegreeAngle(), 0, 0, 1);
00293     glBegin(GL_TRIANGLES);
00294     glVertex2d(0, -tLength);
00295     glVertex2d(-tWidth, 0);
00296     glVertex2d(+tWidth, 0);
00297     glEnd();
00298     glPopMatrix();
00299 }
00300 
00301 
00302 
00303 /****************************************************************************/
00304 

Generated on Wed May 5 00:06:29 2010 for Sumo - Simulation of Urban MObility by  doxygen 1.5.6