Eclipse 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-2019 German Aerospace Center (DLR) and others.
4 // This program and the accompanying materials
5 // are made available under the terms of the Eclipse Public License v2.0
6 // which accompanies this distribution, and is available at
7 // http://www.eclipse.org/legal/epl-v20.html
8 // SPDX-License-Identifier: EPL-2.0
9 /****************************************************************************/
16 // Global storage for textures; manages and draws them
17 /****************************************************************************/
18 
19 
20 // ===========================================================================
21 // included modules
22 // ===========================================================================
23 #include <config.h>
24 
25 #include <iostream>
26 #include <fx.h>
33 #include "GUITexturesHelper.h"
34 
35 
36 // ===========================================================================
37 // definition of static variables
38 // ===========================================================================
39 std::map<std::string, int> GUITexturesHelper::myTextures;
41 
42 
43 // ===========================================================================
44 // method definitions
45 // ===========================================================================
46 int
48  int max;
49  glGetIntegerv(GL_MAX_TEXTURE_SIZE, &max);
50  return max;
51 }
52 
53 
54 GUIGlID
56  GUIGlID id;
57  glGenTextures(1, &id);
58  glBindTexture(GL_TEXTURE_2D, id);
59  glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA,
60  i->getWidth(), i->getHeight(), 0,
61  GL_RGBA, GL_UNSIGNED_BYTE, i->getData());
62  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
63  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
64  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
65  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
66  glBindTexture(GL_TEXTURE_2D, 0);
67  return id;
68 }
69 
70 
71 void
72 GUITexturesHelper::drawTexturedBox(int which, double size) {
73  drawTexturedBox(which, size, size, -size, -size);
74 }
75 
76 
77 void
78 GUITexturesHelper::drawTexturedBox(int which, double sizeX1, double sizeY1, double sizeX2, double sizeY2) {
79  // first check that textures are allowed
80  if (!myAllowTextures) {
81  return;
82  }
83  glEnable(GL_TEXTURE_2D);
84  glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
85  glDisable(GL_CULL_FACE);
86  //glDisable(GL_DEPTH_TEST); // without DEPTH_TEST vehicles may be drawn below roads
87  glDisable(GL_LIGHTING);
88  glDisable(GL_COLOR_MATERIAL);
89  glDisable(GL_TEXTURE_GEN_S);
90  glDisable(GL_TEXTURE_GEN_T);
91  glDisable(GL_ALPHA_TEST);
92  glEnable(GL_BLEND);
93  glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
94  glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
95  glBindTexture(GL_TEXTURE_2D, which);
96  glBegin(GL_TRIANGLE_STRIP);
97  glTexCoord2f(0, 1);
98  glVertex2d(sizeX1, sizeY1);
99  glTexCoord2f(0, 0);
100  glVertex2d(sizeX1, sizeY2);
101  glTexCoord2f(1, 1);
102  glVertex2d(sizeX2, sizeY1);
103  glTexCoord2f(1, 0);
104  glVertex2d(sizeX2, sizeY2);
105  glEnd();
106  glBindTexture(GL_TEXTURE_2D, 0);
107  glEnable(GL_DEPTH_TEST);
108 }
109 
110 
111 int
112 GUITexturesHelper::getTextureID(const std::string& filename, const bool mirrorX) {
113  if (myTextures.count(filename) == 0) {
114  try {
115  // load image
116  FXImage* i = MFXImageHelper::loadImage(GUIMainWindow::getInstance()->getApp(), filename);
117  if (mirrorX) {
118  i->mirror(false, true);
119  }
121  // Create GL structure using texture
122  GUIGlID id = add(i);
123  // delete texture after creating GL Structure
124  delete i;
125  myTextures[filename] = (int)id;
126  } catch (InvalidArgument& e) {
127  WRITE_ERROR("Could not load '" + filename + "'.\n" + e.what());
128  myTextures[filename] = -1;
129  }
130  }
131  return myTextures[filename];
132 }
133 
134 
135 void
137  myTextures.clear();
138 }
139 
140 /****************************************************************************/
MFXImageHelper::loadImage
static FXImage * loadImage(FXApp *a, const std::string &file)
Definition: MFXImageHelper.cpp:59
GUIGlObject.h
MFXImageHelper.h
GUITexturesHelper::myAllowTextures
static bool myAllowTextures
whether textures are drawn
Definition: GUITexturesHelper.h:75
OptionsCont.h
MsgHandler.h
GUITexturesHelper::drawTexturedBox
static void drawTexturedBox(int which, double size)
Draws a named texture as a box with the given size.
Definition: GUITexturesHelper.cpp:72
GUIMainWindow.h
MFXImageHelper::scalePower2
static FXbool scalePower2(FXImage *image, int maxSize=(2<< 29))
Definition: MFXImageHelper.cpp:106
GUIMainWindow::getInstance
static GUIMainWindow * getInstance()
Definition: GUIMainWindow.cpp:182
GUITexturesHelper::getMaxTextureSize
static int getMaxTextureSize()
return maximum number of pixels in x and y direction
Definition: GUITexturesHelper.cpp:47
GUITexturesHelper.h
GUITexturesHelper::add
static GUIGlID add(FXImage *i)
Adds a texture to use.
Definition: GUITexturesHelper.cpp:55
GLIncludes.h
GUIGlID
unsigned int GUIGlID
Definition: GUIGlObject.h:42
GUITexturesHelper::myTextures
static std::map< std::string, int > myTextures
mapping from image paths to decals (initialization on first use)
Definition: GUITexturesHelper.h:72
InvalidArgument
Definition: UtilExceptions.h:56
config.h
GUITexturesHelper::clearTextures
static void clearTextures()
clears loaded textures
Definition: GUITexturesHelper.cpp:136
GUITexturesHelper::getTextureID
static int getTextureID(const std::string &filename, const bool mirrorX=false)
return texture id for the given filename (initialize on first use)
Definition: GUITexturesHelper.cpp:112
WRITE_ERROR
#define WRITE_ERROR(msg)
Definition: MsgHandler.h:283