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 /****************************************************************************/
8 // Global storage for textures; manages and draws them
9 /****************************************************************************/
10 // SUMO, Simulation of Urban MObility; see http://sumo.sourceforge.net/
11 // Copyright (C) 2001-2013 DLR (http://www.dlr.de/) and contributors
12 /****************************************************************************/
13 //
14 // This file is part of SUMO.
15 // SUMO is free software: you can redistribute it and/or modify
16 // it under the terms of the GNU General Public License as published by
17 // the Free Software Foundation, either version 3 of the License, or
18 // (at your option) any later version.
19 //
20 /****************************************************************************/
21 
22 
23 // ===========================================================================
24 // included modules
25 // ===========================================================================
26 #ifdef _MSC_VER
27 #include <windows_config.h>
28 #else
29 #include <config.h>
30 #endif
31 
32 #include <iostream>
33 #include <fx.h>
34 #include <fx3d.h>
40 #include "GUITexturesHelper.h"
41 
42 #ifdef CHECK_MEMORY_LEAKS
43 #include <foreign/nvwa/debug_new.h>
44 #endif // CHECK_MEMORY_LEAKS
45 
46 
47 // ===========================================================================
48 // definition of static variables
49 // ===========================================================================
51 std::map<std::string, int> GUITexturesHelper::myTextures;
52 
53 
54 // ===========================================================================
55 // method definitions
56 // ===========================================================================
57 GUIGlID
59  GUIGlID id;
60  glGenTextures(1, &id);
61  glBindTexture(GL_TEXTURE_2D, id);
62  glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA,
63  i->getWidth(), i->getHeight(), 0,
64  GL_RGBA, GL_UNSIGNED_BYTE, i->getData());
65  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
66  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
67  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
68  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
69  glBindTexture(GL_TEXTURE_2D, 0);
70  return id;
71 }
72 
73 
74 void
75 GUITexturesHelper::drawTexturedBox(unsigned int which, SUMOReal size) {
76  drawTexturedBox(which, size, size, -size, -size);
77 }
78 
79 
80 void
82  SUMOReal sizeX1, SUMOReal sizeY1,
83  SUMOReal sizeX2, SUMOReal sizeY2) {
84  if (!gAllowTextures) {
85  return;
86  }
87  glEnable(GL_TEXTURE_2D);
88  glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
89  glDisable(GL_CULL_FACE);
90  //glDisable(GL_DEPTH_TEST); // without DEPTH_TEST vehicles may be drawn below roads
91  glDisable(GL_LIGHTING);
92  glDisable(GL_COLOR_MATERIAL);
93  glDisable(GL_TEXTURE_GEN_S);
94  glDisable(GL_TEXTURE_GEN_T);
95  glDisable(GL_ALPHA_TEST);
96  glEnable(GL_BLEND);
97  glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
98  glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
99  glBindTexture(GL_TEXTURE_2D, which);
100  glBegin(GL_TRIANGLE_STRIP);
101  glTexCoord2f(0, 1);
102  glVertex2d(sizeX1, sizeY1);
103  glTexCoord2f(0, 0);
104  glVertex2d(sizeX1, sizeY2);
105  glTexCoord2f(1, 1);
106  glVertex2d(sizeX2, sizeY1);
107  glTexCoord2f(1, 0);
108  glVertex2d(sizeX2, sizeY2);
109  glEnd();
110  glBindTexture(GL_TEXTURE_2D, 0);
111  glEnable(GL_DEPTH_TEST);
112 }
113 
114 
115 
116 void
118  myTextures.clear();
119 }
120 
121 
122 int
123 GUITexturesHelper::getTextureID(const std::string& filename) {
124  if (myTextures.count(filename) == 0) {
125  try {
126  FXImage* i = MFXImageHelper::loadImage(GUIMainWindow::getInstance()->getApp(), filename);
128  WRITE_WARNING("Scaling '" + filename + "'.");
129  }
130  GUIGlID id = add(i);
131  delete i;
132  myTextures[filename] = (int)id;
133  } catch (InvalidArgument& e) {
134  WRITE_ERROR("Could not load '" + filename + "'.\n" + e.what());
135  myTextures[filename] = -1;
136  }
137  }
138  return myTextures[filename];
139 }
140 
141 /****************************************************************************/