SUMO - Simulation of Urban MObility
GUITexturesHelper.cpp
Go to the documentation of this file.
1 /****************************************************************************/
2 // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.org/sumo
3 // Copyright (C) 2004-2017 German Aerospace Center (DLR) and others.
4 /****************************************************************************/
5 //
6 // This program and the accompanying materials
7 // are made available under the terms of the Eclipse Public License v2.0
8 // which accompanies this distribution, and is available at
9 // http://www.eclipse.org/legal/epl-v20.html
10 //
11 /****************************************************************************/
19 // Global storage for textures; manages and draws them
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>
39 #include "GUITexturesHelper.h"
40 
41 
42 // ===========================================================================
43 // definition of static variables
44 // ===========================================================================
45 std::map<std::string, int> GUITexturesHelper::myTextures;
47 
48 
49 // ===========================================================================
50 // method definitions
51 // ===========================================================================
52 int
54  int max;
55  glGetIntegerv(GL_MAX_TEXTURE_SIZE, &max);
56  return max;
57 }
58 
59 
60 GUIGlID
62  GUIGlID id;
63  glGenTextures(1, &id);
64  glBindTexture(GL_TEXTURE_2D, id);
65  glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA,
66  i->getWidth(), i->getHeight(), 0,
67  GL_RGBA, GL_UNSIGNED_BYTE, i->getData());
68  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
69  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
70  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
71  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
72  glBindTexture(GL_TEXTURE_2D, 0);
73  return id;
74 }
75 
76 
77 void
78 GUITexturesHelper::drawTexturedBox(int which, double size) {
79  drawTexturedBox(which, size, size, -size, -size);
80 }
81 
82 
83 void
85  double sizeX1, double sizeY1,
86  double sizeX2, double sizeY2) {
87  if (!myAllowTextures) {
88  return;
89  }
90  glEnable(GL_TEXTURE_2D);
91  glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
92  glDisable(GL_CULL_FACE);
93  //glDisable(GL_DEPTH_TEST); // without DEPTH_TEST vehicles may be drawn below roads
94  glDisable(GL_LIGHTING);
95  glDisable(GL_COLOR_MATERIAL);
96  glDisable(GL_TEXTURE_GEN_S);
97  glDisable(GL_TEXTURE_GEN_T);
98  glDisable(GL_ALPHA_TEST);
99  glEnable(GL_BLEND);
100  glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
101  glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
102  glBindTexture(GL_TEXTURE_2D, which);
103  glBegin(GL_TRIANGLE_STRIP);
104  glTexCoord2f(0, 1);
105  glVertex2d(sizeX1, sizeY1);
106  glTexCoord2f(0, 0);
107  glVertex2d(sizeX1, sizeY2);
108  glTexCoord2f(1, 1);
109  glVertex2d(sizeX2, sizeY1);
110  glTexCoord2f(1, 0);
111  glVertex2d(sizeX2, sizeY2);
112  glEnd();
113  glBindTexture(GL_TEXTURE_2D, 0);
114  glEnable(GL_DEPTH_TEST);
115 }
116 
117 
118 int
119 GUITexturesHelper::getTextureID(const std::string& filename, const bool mirrorX) {
120  if (myTextures.count(filename) == 0) {
121  try {
122  FXImage* i = MFXImageHelper::loadImage(GUIMainWindow::getInstance()->getApp(), filename);
123  if (mirrorX) {
124  i->mirror(false, true);
125  }
127  GUIGlID id = add(i);
128  delete i;
129  myTextures[filename] = (int)id;
130  } catch (InvalidArgument& e) {
131  WRITE_ERROR("Could not load '" + filename + "'.\n" + e.what());
132  myTextures[filename] = -1;
133  }
134  }
135  return myTextures[filename];
136 }
137 
138 
139 void
141  myTextures.clear();
142 }
143 
144 /****************************************************************************/
static void drawTexturedBox(int which, double size)
Draws a named texture as a box with the given size.
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))
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
unsigned int GUIGlID
Definition: GUIGlObject.h:49
#define WRITE_ERROR(msg)
Definition: MsgHandler.h:205
static FXImage * loadImage(FXApp *a, const std::string &file)
static int getMaxTextureSize()
return maximum number of pixels in x and y direction