GUITexturesHelper.cpp

Go to the documentation of this file.
00001 /****************************************************************************/
00007 // Global storage for textures; manages and draws them
00008 /****************************************************************************/
00009 // SUMO, Simulation of Urban MObility; see http://sumo.sourceforge.net/
00010 // Copyright 2001-2010 DLR (http://www.dlr.de/) and contributors
00011 /****************************************************************************/
00012 //
00013 //   This program is free software; you can redistribute it and/or modify
00014 //   it under the terms of the GNU General Public License as published by
00015 //   the Free Software Foundation; either version 2 of the License, or
00016 //   (at your option) any later version.
00017 //
00018 /****************************************************************************/
00019 
00020 
00021 // ===========================================================================
00022 // included modules
00023 // ===========================================================================
00024 #ifdef _MSC_VER
00025 #include <windows_config.h>
00026 #else
00027 #include <config.h>
00028 #endif
00029 
00030 #include <cassert>
00031 #include <iostream>
00032 #include <fx.h>
00033 #include <fx3d.h>
00034 #include "GUITexturesHelper.h"
00035 
00036 #include "p.xpm"
00037 #include "pl_1.xpm"
00038 #include "pl_2.xpm"
00039 #include "pl_3.xpm"
00040 #include "pr_1.xpm"
00041 #include "pr_2.xpm"
00042 #include "GUIImageGlobals.h"
00043 
00044 #ifdef _WIN32
00045 #include <windows.h>
00046 #endif
00047 
00048 #include <GL/gl.h>
00049 
00050 #ifdef CHECK_MEMORY_LEAKS
00051 #include <foreign/nvwa/debug_new.h>
00052 #endif // CHECK_MEMORY_LEAKS
00053 
00054 
00055 // ===========================================================================
00056 // static member variable definitions
00057 // ===========================================================================
00058 bool GUITexturesHelper::myWasInitialised = false;
00059 FXApp* GUITexturesHelper::myApp = 0;
00060 GLuint GUITexturesHelper::myTextureIDs[TEXTURE_MAX];
00061 FXImage *GUITexturesHelper::myTextures[TEXTURE_MAX];
00062 
00063 
00064 // ===========================================================================
00065 // method definitions
00066 // ===========================================================================
00067 void
00068 GUITexturesHelper::init(FXApp *a) {
00069     myApp = a;
00070     myWasInitialised = false;
00071 }
00072 
00073 
00074 void
00075 GUITexturesHelper::assignTextures() {
00076     if (myWasInitialised) {
00077         return;
00078     }
00079     // check whether other textures shall be used
00080     myWasInitialised = true;
00081     if (!gAllowTextures) {
00082         return;
00083     }
00084     // build texture images
00085     glGenTextures(6, myTextureIDs);
00086     myTextures[MSLink::LINKDIR_STRAIGHT] = new FXXPMImage(myApp, p_xpm, IMAGE_KEEP);
00087     myTextures[MSLink::LINKDIR_TURN] = new FXXPMImage(myApp, pl_3_xpm, IMAGE_KEEP);
00088     myTextures[MSLink::LINKDIR_LEFT] = new FXXPMImage(myApp, pl_2_xpm, IMAGE_KEEP);
00089     myTextures[MSLink::LINKDIR_RIGHT] = new FXXPMImage(myApp, pr_2_xpm, IMAGE_KEEP);
00090     myTextures[MSLink::LINKDIR_PARTLEFT] = new FXXPMImage(myApp, pl_1_xpm, IMAGE_KEEP);
00091     myTextures[MSLink::LINKDIR_PARTRIGHT] = new FXXPMImage(myApp, pr_1_xpm, IMAGE_KEEP);
00092     // allocate in gl (bind)
00093     for (size_t i=0; i<TEXTURE_MAX; i++) {
00094         glBindTexture(GL_TEXTURE_2D,myTextureIDs[i]);
00095         glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA,
00096                      myTextures[i]->getWidth(), myTextures[i]->getHeight(), 0,
00097                      GL_RGBA, GL_UNSIGNED_BYTE, myTextures[i]->getData());
00098         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
00099         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
00100         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
00101         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
00102         glBindTexture(GL_TEXTURE_2D, 0);
00103     }
00104 }
00105 
00106 unsigned int
00107 GUITexturesHelper::add(FXImage *i) {
00108     GLuint id;
00109     glGenTextures(1, &id);
00110     glBindTexture(GL_TEXTURE_2D, id);
00111     glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA,
00112                  i->getWidth(), i->getHeight(), 0,
00113                  GL_RGBA, GL_UNSIGNED_BYTE, i->getData());
00114     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
00115     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
00116     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
00117     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
00118     glBindTexture(GL_TEXTURE_2D, 0);
00119     return id;
00120 }
00121 
00122 
00123 void
00124 GUITexturesHelper::drawDirectionArrow(GUITexture which, SUMOReal size) {
00125     drawTexturedBox(myTextureIDs[which], size, size, -size, -size);
00126 }
00127 
00128 
00129 void
00130 GUITexturesHelper::drawDirectionArrow(unsigned int which,
00131                                       SUMOReal sizeX1, SUMOReal sizeY1,
00132                                       SUMOReal sizeX2, SUMOReal sizeY2) {
00133     drawTexturedBox(myTextureIDs[which], sizeX1, sizeY1, sizeX2, sizeY2);
00134 }
00135 
00136 void
00137 GUITexturesHelper::drawTexturedBox(unsigned int which, SUMOReal size) {
00138     drawTexturedBox(which, size, size, -size, -size);
00139 }
00140 
00141 
00142 void
00143 GUITexturesHelper::drawTexturedBox(unsigned int which,
00144                                    SUMOReal sizeX1, SUMOReal sizeY1,
00145                                    SUMOReal sizeX2, SUMOReal sizeY2) {
00146     if (!gAllowTextures) {
00147         return;
00148     }
00149     if (!myWasInitialised) {
00150         assignTextures();
00151     }
00152     glEnable(GL_TEXTURE_2D);
00153     glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
00154     glDisable(GL_CULL_FACE);
00155     glDisable(GL_DEPTH_TEST);
00156     glDisable(GL_LIGHTING);
00157     glDisable(GL_COLOR_MATERIAL);
00158     glDisable(GL_TEXTURE_GEN_S);
00159     glDisable(GL_TEXTURE_GEN_T);
00160     glDisable(GL_ALPHA_TEST);
00161     glEnable(GL_BLEND);
00162     glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
00163     glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
00164     glBindTexture(GL_TEXTURE_2D, which);
00165     glBegin(GL_TRIANGLE_STRIP);
00166     glTexCoord2f(0, 1);
00167     glVertex2d(sizeX1, sizeY1);
00168     glTexCoord2f(0, 0);
00169     glVertex2d(sizeX1, sizeY2);
00170     glTexCoord2f(1, 1);
00171     glVertex2d(sizeX2, sizeY1);
00172     glTexCoord2f(1, 0);
00173     glVertex2d(sizeX2, sizeY2);
00174     glEnd();
00175     glBindTexture(GL_TEXTURE_2D, 0);
00176     glEnable(GL_DEPTH_TEST);
00177 }
00178 
00179 
00180 void
00181 GUITexturesHelper::close() {
00182     if (!myWasInitialised) {
00183         // nothing to do
00184         return;
00185     }
00186     for (size_t i=0; i<TEXTURE_MAX; i++) {
00187         delete myTextures[i];
00188     }
00189 
00190 }
00191 
00192 
00193 
00194 /****************************************************************************/
00195 

Generated on Wed May 5 00:06:30 2010 for Sumo - Simulation of Urban MObility by  doxygen 1.5.6