SUMO - Simulation of Urban MObility
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
GUITexturesHelper.cpp
Go to the documentation of this file.
1 /****************************************************************************/
8 // Global storage for textures; manages and draws them
9 /****************************************************************************/
10 // SUMO, Simulation of Urban MObility; see http://sumo-sim.org/
11 // Copyright (C) 2001-2013 DLR (http://www.dlr.de/) and contributors
12 /****************************************************************************/
13 //
14 // This file is part of SUMO.
15 // SUMO is free software: you can redistribute it and/or modify
16 // it under the terms of the GNU General Public License as published by
17 // the Free Software Foundation, either version 3 of the License, or
18 // (at your option) any later version.
19 //
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>
34 #include <fx3d.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
83 GUITexturesHelper::drawTexturedBox(unsigned int which, SUMOReal size) {
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) {
125  if (myTextures.count(filename) == 0) {
126  try {
127  FXImage* i = MFXImageHelper::loadImage(GUIMainWindow::getInstance()->getApp(), filename);
129  WRITE_WARNING("Scaling '" + filename + "'.");
130  }
131  GUIGlID id = add(i);
132  delete i;
133  myTextures[filename] = (int)id;
134  } catch (InvalidArgument& e) {
135  WRITE_ERROR("Could not load '" + filename + "'.\n" + e.what());
136  myTextures[filename] = -1;
137  }
138  }
139  return myTextures[filename];
140 }
141 
142 
143 void
145  myTextures.clear();
146 }
147 
148 /****************************************************************************/
static GUIGlID add(FXImage *i)
Adds a texture to use.
#define WRITE_WARNING(msg)
Definition: MsgHandler.h:196
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:61
static void drawTexturedBox(unsigned int which, SUMOReal size)
Draws a named texture as a box with the given size.
static GUIMainWindow * getInstance()
static bool myAllowTextures
whether textures are drawn
unsigned int GUIGlID
Definition: GUIGlObject.h:48
#define WRITE_ERROR(msg)
Definition: MsgHandler.h:201
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:215
static int getTextureID(const std::string &filename)
return texture id for the given filename (initialize on first use)