Eclipse SUMO - Simulation of Urban MObility
Loading...
Searching...
No Matches
GUITexturesHelper.cpp
Go to the documentation of this file.
1/****************************************************************************/
2// Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3// Copyright (C) 2004-2026 German Aerospace Center (DLR) and others.
4// This program and the accompanying materials are made available under the
5// terms of the Eclipse Public License 2.0 which is available at
6// https://www.eclipse.org/legal/epl-2.0/
7// This Source Code may also be made available under the following Secondary
8// Licenses when the conditions for such availability set forth in the Eclipse
9// Public License 2.0 are satisfied: GNU General Public License, version 2
10// or later which is available at
11// https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
12// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
13/****************************************************************************/
20// Global storage for textures; manages and draws them
21/****************************************************************************/
22#include <config.h>
23
24#include <iostream>
32
33#include "GUITexturesHelper.h"
34
35// ===========================================================================
36// definition of static variables
37// ===========================================================================
38std::map<std::string, int> GUITexturesHelper::myTextures;
40
41
42// ===========================================================================
43// method definitions
44// ===========================================================================
45int
47 int max;
48 glGetIntegerv(GL_MAX_TEXTURE_SIZE, &max);
49 return max;
50}
51
52
55 GUIGlID id;
56 glGenTextures(1, &id);
57 glBindTexture(GL_TEXTURE_2D, id);
58 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA,
59 i->getWidth(), i->getHeight(), 0,
60 GL_RGBA, GL_UNSIGNED_BYTE, i->getData());
61 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
62 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
63 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
64 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
65 glBindTexture(GL_TEXTURE_2D, 0);
66 return id;
67}
68
69
70void
71GUITexturesHelper::drawTexturedBox(int which, double size) {
72 drawTexturedBox(which, size, size, -size, -size);
73}
74
75
76void
77GUITexturesHelper::drawTexturedBox(int which, double sizeX1, double sizeY1, double sizeX2, double sizeY2) {
78 // first check that textures are allowed
79 if (!myAllowTextures) {
80 return;
81 }
82 glEnable(GL_TEXTURE_2D);
83 glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
84 glDisable(GL_CULL_FACE);
85 //glDisable(GL_DEPTH_TEST); // without DEPTH_TEST vehicles may be drawn below roads
86 glDisable(GL_LIGHTING);
87 glDisable(GL_COLOR_MATERIAL);
88 glDisable(GL_TEXTURE_GEN_S);
89 glDisable(GL_TEXTURE_GEN_T);
90 glAlphaFunc(GL_GREATER, 0);
91 glEnable(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 glDisable(GL_ALPHA_TEST);
108 glEnable(GL_DEPTH_TEST);
109}
110
111
112int
113GUITexturesHelper::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
136void
138 // delete textures
139 for (const auto& pair : myTextures) {
140 if (pair.second > 0) {
141 GLuint texId = static_cast<GLuint>(pair.second);
142 glDeleteTextures(1, &texId);
143 }
144 }
145 myTextures.clear();
146}
147
148
149/****************************************************************************/
unsigned int GUIGlID
Definition GUIGlObject.h:44
#define WRITE_ERROR(msg)
Definition MsgHandler.h:295
static GUIMainWindow * getInstance()
get instance
static void drawTexturedBox(int which, double size)
Draws a named texture as a box with the given size.
static bool myAllowTextures
whether textures are drawn
static std::map< std::string, int > myTextures
mapping from image paths to decals (initialization on first use)
static GUIGlID add(FXImage *i)
Adds a texture to use.
static int getTextureID(const std::string &filename, const bool mirrorX=false)
return texture id for the given filename (initialize on first use)
static int getMaxTextureSize()
return maximum number of pixels in x and y direction
static void clearTextures()
clears loaded textures
static FXImage * loadImage(FXApp *a, const std::string &file)
static FXbool scalePower2(FXImage *image, int maxSize=(2<< 29))