SUMO - Simulation of Urban MObility
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.dlr.de/
12 // Copyright (C) 2004-2017 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>
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 // ===========================================================================
50 std::map<std::string, int> GUITexturesHelper::myTextures;
52 
53 
54 // ===========================================================================
55 // method definitions
56 // ===========================================================================
57 int
59  int max;
60  glGetIntegerv(GL_MAX_TEXTURE_SIZE, &max);
61  return max;
62 }
63 
64 
65 GUIGlID
67  GUIGlID id;
68  glGenTextures(1, &id);
69  glBindTexture(GL_TEXTURE_2D, id);
70  glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA,
71  i->getWidth(), i->getHeight(), 0,
72  GL_RGBA, GL_UNSIGNED_BYTE, i->getData());
73  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
74  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
75  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
76  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
77  glBindTexture(GL_TEXTURE_2D, 0);
78  return id;
79 }
80 
81 
82 void
84  drawTexturedBox(which, size, size, -size, -size);
85 }
86 
87 
88 void
90  SUMOReal sizeX1, SUMOReal sizeY1,
91  SUMOReal sizeX2, SUMOReal sizeY2) {
92  if (!myAllowTextures) {
93  return;
94  }
95  glEnable(GL_TEXTURE_2D);
96  glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
97  glDisable(GL_CULL_FACE);
98  //glDisable(GL_DEPTH_TEST); // without DEPTH_TEST vehicles may be drawn below roads
99  glDisable(GL_LIGHTING);
100  glDisable(GL_COLOR_MATERIAL);
101  glDisable(GL_TEXTURE_GEN_S);
102  glDisable(GL_TEXTURE_GEN_T);
103  glDisable(GL_ALPHA_TEST);
104  glEnable(GL_BLEND);
105  glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
106  glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
107  glBindTexture(GL_TEXTURE_2D, which);
108  glBegin(GL_TRIANGLE_STRIP);
109  glTexCoord2f(0, 1);
110  glVertex2d(sizeX1, sizeY1);
111  glTexCoord2f(0, 0);
112  glVertex2d(sizeX1, sizeY2);
113  glTexCoord2f(1, 1);
114  glVertex2d(sizeX2, sizeY1);
115  glTexCoord2f(1, 0);
116  glVertex2d(sizeX2, sizeY2);
117  glEnd();
118  glBindTexture(GL_TEXTURE_2D, 0);
119  glEnable(GL_DEPTH_TEST);
120 }
121 
122 
123 int
124 GUITexturesHelper::getTextureID(const std::string& filename, const bool mirrorX) {
125  if (myTextures.count(filename) == 0) {
126  try {
127  FXImage* i = MFXImageHelper::loadImage(GUIMainWindow::getInstance()->getApp(), filename);
128  if (mirrorX) {
129  i->mirror(false, true);
130  }
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 /****************************************************************************/
static GUIGlID add(FXImage *i)
Adds a texture to use.
static std::map< std::string, int > myTextures
mapping from image paths to decals (initialization on first use)
static void clearTextures()
clears loaded textures
static FXbool scalePower2(FXImage *image, int maxSize=(2<< 29))
#define max(a, b)
Definition: polyfonts.c:65
static GUIMainWindow * getInstance()
static int getTextureID(const std::string &filename, const bool mirrorX=false)
return texture id for the given filename (initialize on first use)
static bool myAllowTextures
whether textures are drawn
#define WRITE_ERROR(msg)
Definition: MsgHandler.h:206
unsigned int GUIGlID
Definition: GUIGlObject.h:50
static FXImage * loadImage(FXApp *a, const std::string &file)
static int getMaxTextureSize()
return maximum number of pixels in x and y direction
#define SUMOReal
Definition: config.h:208
static void drawTexturedBox(int which, SUMOReal size)
Draws a named texture as a box with the given size.