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-2024 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 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
110int
111GUITexturesHelper::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 }
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
134void
138
139
140/****************************************************************************/
unsigned int GUIGlID
Definition GUIGlObject.h:43
#define WRITE_ERROR(msg)
Definition MsgHandler.h:304
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))