SUMO - Simulation of Urban MObility
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
GUITexturesHelper.cpp
Go to the documentation of this file.
1 /****************************************************************************/
9 // Global storage for textures; manages and draws them
10 /****************************************************************************/
11 // SUMO, Simulation of Urban MObility; see http://sumo-sim.org/
12 // Copyright (C) 2004-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 <iostream>
34 #include <fx.h>
35 #include <fx3d.h>
41 #include "GUITexturesHelper.h"
42 
43 #ifdef CHECK_MEMORY_LEAKS
44 #include <foreign/nvwa/debug_new.h>
45 #endif // CHECK_MEMORY_LEAKS
46 
47 
48 // ===========================================================================
49 // definition of static variables
50 // ===========================================================================
51 std::map<std::string, int> GUITexturesHelper::myTextures;
53 
54 
55 // ===========================================================================
56 // method definitions
57 // ===========================================================================
58 int
60  int max;
61  glGetIntegerv(GL_MAX_TEXTURE_SIZE, &max);
62  return max;
63 }
64 
65 
66 GUIGlID
68  GUIGlID id;
69  glGenTextures(1, &id);
70  glBindTexture(GL_TEXTURE_2D, id);
71  glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA,
72  i->getWidth(), i->getHeight(), 0,
73  GL_RGBA, GL_UNSIGNED_BYTE, i->getData());
74  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
75  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
76  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
77  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
78  glBindTexture(GL_TEXTURE_2D, 0);
79  return id;
80 }
81 
82 
83 void
84 GUITexturesHelper::drawTexturedBox(unsigned int which, SUMOReal size) {
85  drawTexturedBox(which, size, size, -size, -size);
86 }
87 
88 
89 void
91  SUMOReal sizeX1, SUMOReal sizeY1,
92  SUMOReal sizeX2, SUMOReal sizeY2) {
93  if (!myAllowTextures) {
94  return;
95  }
96  glEnable(GL_TEXTURE_2D);
97  glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
98  glDisable(GL_CULL_FACE);
99  //glDisable(GL_DEPTH_TEST); // without DEPTH_TEST vehicles may be drawn below roads
100  glDisable(GL_LIGHTING);
101  glDisable(GL_COLOR_MATERIAL);
102  glDisable(GL_TEXTURE_GEN_S);
103  glDisable(GL_TEXTURE_GEN_T);
104  glDisable(GL_ALPHA_TEST);
105  glEnable(GL_BLEND);
106  glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
107  glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
108  glBindTexture(GL_TEXTURE_2D, which);
109  glBegin(GL_TRIANGLE_STRIP);
110  glTexCoord2f(0, 1);
111  glVertex2d(sizeX1, sizeY1);
112  glTexCoord2f(0, 0);
113  glVertex2d(sizeX1, sizeY2);
114  glTexCoord2f(1, 1);
115  glVertex2d(sizeX2, sizeY1);
116  glTexCoord2f(1, 0);
117  glVertex2d(sizeX2, sizeY2);
118  glEnd();
119  glBindTexture(GL_TEXTURE_2D, 0);
120  glEnable(GL_DEPTH_TEST);
121 }
122 
123 
124 int
125 GUITexturesHelper::getTextureID(const std::string& filename) {
126  if (myTextures.count(filename) == 0) {
127  try {
128  FXImage* i = MFXImageHelper::loadImage(GUIMainWindow::getInstance()->getApp(), filename);
130  WRITE_WARNING("Scaling '" + filename + "'.");
131  }
132  GUIGlID id = add(i);
133  delete i;
134  myTextures[filename] = (int)id;
135  } catch (InvalidArgument& e) {
136  WRITE_ERROR("Could not load '" + filename + "'.\n" + e.what());
137  myTextures[filename] = -1;
138  }
139  }
140  return myTextures[filename];
141 }
142 
143 
144 void
146  myTextures.clear();
147 }
148 
149 /****************************************************************************/