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 
43 // ===========================================================================
44 // definition of static variables
45 // ===========================================================================
46 std::map<std::string, int> GUITexturesHelper::myTextures;
48 
49 
50 // ===========================================================================
51 // method definitions
52 // ===========================================================================
53 int
55  int max;
56  glGetIntegerv(GL_MAX_TEXTURE_SIZE, &max);
57  return max;
58 }
59 
60 
61 GUIGlID
63  GUIGlID id;
64  glGenTextures(1, &id);
65  glBindTexture(GL_TEXTURE_2D, id);
66  glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA,
67  i->getWidth(), i->getHeight(), 0,
68  GL_RGBA, GL_UNSIGNED_BYTE, i->getData());
69  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
70  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
71  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
72  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
73  glBindTexture(GL_TEXTURE_2D, 0);
74  return id;
75 }
76 
77 
78 void
79 GUITexturesHelper::drawTexturedBox(int which, double size) {
80  drawTexturedBox(which, size, size, -size, -size);
81 }
82 
83 
84 void
86  double sizeX1, double sizeY1,
87  double sizeX2, double sizeY2) {
88  if (!myAllowTextures) {
89  return;
90  }
91  glEnable(GL_TEXTURE_2D);
92  glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
93  glDisable(GL_CULL_FACE);
94  //glDisable(GL_DEPTH_TEST); // without DEPTH_TEST vehicles may be drawn below roads
95  glDisable(GL_LIGHTING);
96  glDisable(GL_COLOR_MATERIAL);
97  glDisable(GL_TEXTURE_GEN_S);
98  glDisable(GL_TEXTURE_GEN_T);
99  glDisable(GL_ALPHA_TEST);
100  glEnable(GL_BLEND);
101  glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
102  glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
103  glBindTexture(GL_TEXTURE_2D, which);
104  glBegin(GL_TRIANGLE_STRIP);
105  glTexCoord2f(0, 1);
106  glVertex2d(sizeX1, sizeY1);
107  glTexCoord2f(0, 0);
108  glVertex2d(sizeX1, sizeY2);
109  glTexCoord2f(1, 1);
110  glVertex2d(sizeX2, sizeY1);
111  glTexCoord2f(1, 0);
112  glVertex2d(sizeX2, sizeY2);
113  glEnd();
114  glBindTexture(GL_TEXTURE_2D, 0);
115  glEnable(GL_DEPTH_TEST);
116 }
117 
118 
119 int
120 GUITexturesHelper::getTextureID(const std::string& filename, const bool mirrorX) {
121  if (myTextures.count(filename) == 0) {
122  try {
123  FXImage* i = MFXImageHelper::loadImage(GUIMainWindow::getInstance()->getApp(), filename);
124  if (mirrorX) {
125  i->mirror(false, true);
126  }
128  GUIGlID id = add(i);
129  delete i;
130  myTextures[filename] = (int)id;
131  } catch (InvalidArgument& e) {
132  WRITE_ERROR("Could not load '" + filename + "'.\n" + e.what());
133  myTextures[filename] = -1;
134  }
135  }
136  return myTextures[filename];
137 }
138 
139 
140 void
142  myTextures.clear();
143 }
144 
145 /****************************************************************************/
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))
#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
unsigned int GUIGlID
Definition: GUIGlObject.h:50
#define WRITE_ERROR(msg)
Definition: MsgHandler.h:206
static FXImage * loadImage(FXApp *a, const std::string &file)
static int getMaxTextureSize()
return maximum number of pixels in x and y direction