Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
eclipse
GitHub Repository: eclipse/sumo
Path: blob/main/src/utils/gui/images/GUITexturesHelper.cpp
169685 views
1
/****************************************************************************/
2
// Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3
// Copyright (C) 2004-2025 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
/****************************************************************************/
14
/// @file GUITexturesHelper.cpp
15
/// @author Daniel Krajzewicz
16
/// @author Michael Behrisch
17
/// @author Jakob Erdmann
18
/// @date Mon, 08.03.2004
19
///
20
// Global storage for textures; manages and draws them
21
/****************************************************************************/
22
#include <config.h>
23
24
#include <iostream>
25
#include <utils/foxtools/fxheader.h>
26
#include <utils/foxtools/MFXImageHelper.h>
27
#include <utils/gui/globjects/GUIGlObject.h>
28
#include <utils/gui/globjects/GLIncludes.h>
29
#include <utils/gui/windows/GUIMainWindow.h>
30
#include <utils/options/OptionsCont.h>
31
#include <utils/common/MsgHandler.h>
32
33
#include "GUITexturesHelper.h"
34
35
// ===========================================================================
36
// definition of static variables
37
// ===========================================================================
38
std::map<std::string, int> GUITexturesHelper::myTextures;
39
bool GUITexturesHelper::myAllowTextures = true;
40
41
42
// ===========================================================================
43
// method definitions
44
// ===========================================================================
45
int
46
GUITexturesHelper::getMaxTextureSize() {
47
int max;
48
glGetIntegerv(GL_MAX_TEXTURE_SIZE, &max);
49
return max;
50
}
51
52
53
GUIGlID
54
GUITexturesHelper::add(FXImage* i) {
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
70
void
71
GUITexturesHelper::drawTexturedBox(int which, double size) {
72
drawTexturedBox(which, size, size, -size, -size);
73
}
74
75
76
void
77
GUITexturesHelper::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
glDisable(GL_ALPHA_TEST);
91
glEnable(GL_BLEND);
92
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
93
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
94
glBindTexture(GL_TEXTURE_2D, which);
95
glBegin(GL_TRIANGLE_STRIP);
96
glTexCoord2f(0, 1);
97
glVertex2d(sizeX1, sizeY1);
98
glTexCoord2f(0, 0);
99
glVertex2d(sizeX1, sizeY2);
100
glTexCoord2f(1, 1);
101
glVertex2d(sizeX2, sizeY1);
102
glTexCoord2f(1, 0);
103
glVertex2d(sizeX2, sizeY2);
104
glEnd();
105
glBindTexture(GL_TEXTURE_2D, 0);
106
glEnable(GL_DEPTH_TEST);
107
}
108
109
110
int
111
GUITexturesHelper::getTextureID(const std::string& filename, const bool mirrorX) {
112
if (myTextures.count(filename) == 0) {
113
try {
114
// load image
115
FXImage* i = MFXImageHelper::loadImage(GUIMainWindow::getInstance()->getApp(), filename);
116
if (mirrorX) {
117
i->mirror(false, true);
118
}
119
MFXImageHelper::scalePower2(i, getMaxTextureSize());
120
// Create GL structure using texture
121
GUIGlID id = add(i);
122
// delete texture after creating GL Structure
123
delete i;
124
myTextures[filename] = (int)id;
125
} catch (InvalidArgument& e) {
126
WRITE_ERROR("Could not load '" + filename + "'.\n" + e.what());
127
myTextures[filename] = -1;
128
}
129
}
130
return myTextures[filename];
131
}
132
133
134
void
135
GUITexturesHelper::clearTextures() {
136
myTextures.clear();
137
}
138
139
140
/****************************************************************************/
141
142