LCOV - code coverage report
Current view: top level - src/utils/gui/images - GUITexturesHelper.cpp (source / functions) Coverage Total Hit
Test: lcov.info Lines: 94.7 % 57 54
Test Date: 2024-10-17 15:44:51 Functions: 83.3 % 6 5

            Line data    Source code
       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              : /****************************************************************************/
      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           26 : GUITexturesHelper::getMaxTextureSize() {
      47              :     int max;
      48           26 :     glGetIntegerv(GL_MAX_TEXTURE_SIZE, &max);
      49           26 :     return max;
      50              : }
      51              : 
      52              : 
      53              : GUIGlID
      54           26 : GUITexturesHelper::add(FXImage* i) {
      55              :     GUIGlID id;
      56           26 :     glGenTextures(1, &id);
      57           26 :     glBindTexture(GL_TEXTURE_2D, id);
      58           26 :     glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA,
      59              :                  i->getWidth(), i->getHeight(), 0,
      60              :                  GL_RGBA, GL_UNSIGNED_BYTE, i->getData());
      61           26 :     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
      62           26 :     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
      63           26 :     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
      64           26 :     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
      65           26 :     glBindTexture(GL_TEXTURE_2D, 0);
      66           26 :     return id;
      67              : }
      68              : 
      69              : 
      70              : void
      71            0 : GUITexturesHelper::drawTexturedBox(int which, double size) {
      72            0 :     drawTexturedBox(which, size, size, -size, -size);
      73            0 : }
      74              : 
      75              : 
      76              : void
      77        55110 : GUITexturesHelper::drawTexturedBox(int which, double sizeX1, double sizeY1, double sizeX2, double sizeY2) {
      78              :     // first check that textures are allowed
      79        55110 :     if (!myAllowTextures) {
      80              :         return;
      81              :     }
      82        55110 :     glEnable(GL_TEXTURE_2D);
      83        55110 :     glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
      84        55110 :     glDisable(GL_CULL_FACE);
      85              :     //glDisable(GL_DEPTH_TEST); // without DEPTH_TEST vehicles may be drawn below roads
      86        55110 :     glDisable(GL_LIGHTING);
      87        55110 :     glDisable(GL_COLOR_MATERIAL);
      88        55110 :     glDisable(GL_TEXTURE_GEN_S);
      89        55110 :     glDisable(GL_TEXTURE_GEN_T);
      90        55110 :     glDisable(GL_ALPHA_TEST);
      91        55110 :     glEnable(GL_BLEND);
      92        55110 :     glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
      93        55110 :     glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
      94        55110 :     glBindTexture(GL_TEXTURE_2D, which);
      95        55110 :     glBegin(GL_TRIANGLE_STRIP);
      96        55110 :     glTexCoord2f(0, 1);
      97        55110 :     glVertex2d(sizeX1, sizeY1);
      98        55110 :     glTexCoord2f(0, 0);
      99        55110 :     glVertex2d(sizeX1, sizeY2);
     100        55110 :     glTexCoord2f(1, 1);
     101        55110 :     glVertex2d(sizeX2, sizeY1);
     102        55110 :     glTexCoord2f(1, 0);
     103        55110 :     glVertex2d(sizeX2, sizeY2);
     104        55110 :     glEnd();
     105        55110 :     glBindTexture(GL_TEXTURE_2D, 0);
     106        55110 :     glEnable(GL_DEPTH_TEST);
     107              : }
     108              : 
     109              : 
     110              : int
     111        52799 : GUITexturesHelper::getTextureID(const std::string& filename, const bool mirrorX) {
     112              :     if (myTextures.count(filename) == 0) {
     113              :         try {
     114              :             // load image
     115           20 :             FXImage* i = MFXImageHelper::loadImage(GUIMainWindow::getInstance()->getApp(), filename);
     116           19 :             if (mirrorX) {
     117            1 :                 i->mirror(false, true);
     118              :             }
     119           19 :             MFXImageHelper::scalePower2(i, getMaxTextureSize());
     120              :             // Create GL structure using texture
     121           19 :             GUIGlID id = add(i);
     122              :             // delete texture after creating GL Structure
     123           19 :             delete i;
     124           19 :             myTextures[filename] = (int)id;
     125            1 :         } catch (InvalidArgument& e) {
     126            2 :             WRITE_ERROR("Could not load '" + filename + "'.\n" + e.what());
     127            1 :             myTextures[filename] = -1;
     128            1 :         }
     129              :     }
     130        52799 :     return myTextures[filename];
     131              : }
     132              : 
     133              : 
     134              : void
     135        21981 : GUITexturesHelper::clearTextures() {
     136              :     myTextures.clear();
     137        21981 : }
     138              : 
     139              : 
     140              : /****************************************************************************/
        

Generated by: LCOV version 2.0-1