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

Generated by: LCOV version 2.0-1