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 /****************************************************************************/
17 // Global storage for textures; manages and draws them
18 /****************************************************************************/
19 
20 
21 // ===========================================================================
22 // included modules
23 // ===========================================================================
24 #include <config.h>
25 
26 #include <iostream>
27 #include <fx.h>
34 #include "GUITexturesHelper.h"
35 
36 
37 // ===========================================================================
38 // definition of static variables
39 // ===========================================================================
40 std::map<std::string, int> GUITexturesHelper::myTextures;
42 
43 
44 // ===========================================================================
45 // method definitions
46 // ===========================================================================
47 int
49  int max;
50  glGetIntegerv(GL_MAX_TEXTURE_SIZE, &max);
51  return max;
52 }
53 
54 
55 GUIGlID
57  GUIGlID id;
58  glGenTextures(1, &id);
59  glBindTexture(GL_TEXTURE_2D, id);
60  glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA,
61  i->getWidth(), i->getHeight(), 0,
62  GL_RGBA, GL_UNSIGNED_BYTE, i->getData());
63  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
64  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
65  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
66  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
67  glBindTexture(GL_TEXTURE_2D, 0);
68  return id;
69 }
70 
71 
72 void
73 GUITexturesHelper::drawTexturedBox(int which, double size) {
74  drawTexturedBox(which, size, size, -size, -size);
75 }
76 
77 
78 void
79 GUITexturesHelper::drawTexturedBox(int which, double sizeX1, double sizeY1, double sizeX2, double sizeY2) {
80  // first check that textures are allowed
81  if (!myAllowTextures) {
82  return;
83  }
84  glEnable(GL_TEXTURE_2D);
85  glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
86  glDisable(GL_CULL_FACE);
87  //glDisable(GL_DEPTH_TEST); // without DEPTH_TEST vehicles may be drawn below roads
88  glDisable(GL_LIGHTING);
89  glDisable(GL_COLOR_MATERIAL);
90  glDisable(GL_TEXTURE_GEN_S);
91  glDisable(GL_TEXTURE_GEN_T);
92  glDisable(GL_ALPHA_TEST);
93  glEnable(GL_BLEND);
94  glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
95  glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
96  glBindTexture(GL_TEXTURE_2D, which);
97  glBegin(GL_TRIANGLE_STRIP);
98  glTexCoord2f(0, 1);
99  glVertex2d(sizeX1, sizeY1);
100  glTexCoord2f(0, 0);
101  glVertex2d(sizeX1, sizeY2);
102  glTexCoord2f(1, 1);
103  glVertex2d(sizeX2, sizeY1);
104  glTexCoord2f(1, 0);
105  glVertex2d(sizeX2, sizeY2);
106  glEnd();
107  glBindTexture(GL_TEXTURE_2D, 0);
108  glEnable(GL_DEPTH_TEST);
109 }
110 
111 
112 int
113 GUITexturesHelper::getTextureID(const std::string& filename, const bool mirrorX) {
114  if (myTextures.count(filename) == 0) {
115  try {
116  // load image
117  FXImage* i = MFXImageHelper::loadImage(GUIMainWindow::getInstance()->getApp(), filename);
118  if (mirrorX) {
119  i->mirror(false, true);
120  }
122  // Create GL structure using texture
123  GUIGlID id = add(i);
124  // delete texture after creating GL Structure
125  delete i;
126  myTextures[filename] = (int)id;
127  } catch (InvalidArgument& e) {
128  WRITE_ERROR("Could not load '" + filename + "'.\n" + e.what());
129  myTextures[filename] = -1;
130  }
131  }
132  return myTextures[filename];
133 }
134 
135 
136 void
138  myTextures.clear();
139 }
140 
141 /****************************************************************************/
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:43
#define WRITE_ERROR(msg)
Definition: MsgHandler.h:245
static FXImage * loadImage(FXApp *a, const std::string &file)
static int getMaxTextureSize()
return maximum number of pixels in x and y direction