TextureData.cc 7.87 KB
Newer Older
Jan Möbius's avatar
Jan Möbius committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
//=============================================================================
//
//                               OpenFlipper
//        Copyright (C) 2008 by Computer Graphics Group, RWTH Aachen
//                           www.openflipper.org
//
//-----------------------------------------------------------------------------
//
//                                License
//
//  OpenFlipper is free software: you can redistribute it and/or modify
//  it under the terms of the GNU Lesser General Public License as published by
//  the Free Software Foundation, either version 3 of the License, or
//  (at your option) any later version.
//
//  OpenFlipper is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU Lesser General Public License for more details.
//
//  You should have received a copy of the GNU Lesser General Public License
//  along with OpenFlipper.  If not, see <http://www.gnu.org/licenses/>.
//
//-----------------------------------------------------------------------------
//
//   $Revision: 5183 $
//   $Author: wilden $
//   $Date: 2009-03-03 12:51:24 +0100 (Tue, 03 Mar 2009) $
//
//=============================================================================

#include "TextureData.hh"

//-----------------------------------------------------------------------------------

TextureData::TextureData(){
  nextInternalID_ = 0;
}

//-----------------------------------------------------------------------------------

TextureData::~TextureData() {

}
45

Jan Möbius's avatar
Jan Möbius committed
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
//-----------------------------------------------------------------------------------

/** \brief Check if a texture exists
 *
 * @param _textureName name of the texture
 * @return returns wether the texture exists
 */
bool TextureData::textureExists(QString _textureName)
{
  return ( getTextureIndex(_textureName) != -1);
}

//-----------------------------------------------------------------------------------

/** \brief Check if a texture is enabled
 *
 * @param _textureName name of the texture
 * @return returns wether the texture is enabled
 */
bool TextureData::isEnabled(QString _textureName)
{
  int id = getTextureIndex(_textureName);

  if ( id != -1)
    return textures_[id].enabled;
  else
    return false;
}

//-----------------------------------------------------------------------------------

/** \brief Enable a given texture
 *
 * @param _textureName name of the texture
 * @param _exclusive disable other textures?
 */
82
bool TextureData::enableTexture(QString _textureName, bool _exclusive)
Jan Möbius's avatar
Jan Möbius committed
83 84 85 86 87 88 89 90 91 92 93 94
{
  int id = getTextureIndex(_textureName);

  if ( id != -1){

    textures_[id].enabled = true;

    //disable other textures
    if (_exclusive)
      for ( int i = 0 ; i < (int)textures_.size() ; ++i )
        if (i != id)
          textures_[i].enabled = false;
95
    return true;
Jan Möbius's avatar
Jan Möbius committed
96
  }
97
  return false;
Jan Möbius's avatar
Jan Möbius committed
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
}

//-----------------------------------------------------------------------------------

/** \brief Disable a given texture
 *
 * @param _textureName name of the texture
 */
void TextureData::disableTexture(QString _textureName)
{
  int id = getTextureIndex(_textureName);

  if ( id != -1)
    textures_[id].enabled = false;
}

//-----------------------------------------------------------------------------------

/** \brief Add a Texture
 *
 * @param _textureName name of the texture
 * @param _filename filename of the texture
 * @param _dimension dimension of the texture
 * @param _glName glName for the texture
 * @return persistent id of the texture
 */
int TextureData::addTexture(QString _textureName, QString _filename, uint _dimension, GLuint _glName)
{
  //generate texture object
  Texture tex;
  tex.id         = nextInternalID_++;
  tex.name       = _textureName;
  tex.glName     = _glName;
  tex.filename   = _filename;
  tex.dimension  = _dimension;
  tex.enabled    = true;
  tex.dirty      = false;
  tex.type       = VERTEXBASED;
136
//   tex.parameters = TexParameters;
Jan Möbius's avatar
Jan Möbius committed
137 138 139 140 141 142 143 144 145

  textures_.push_back( tex );

  textureMap_[ tex.id ]  = tex.glName;
  propertyMap_[ tex.id ] = tex.name.toStdString();

  return tex.id;
}

146 147 148 149 150 151 152 153 154 155 156 157
int TextureData::addTexture ( Texture _texture, GLuint _glName ) {
  _texture.id     = nextInternalID_++;
  _texture.glName = _glName;
  textures_.push_back(_texture);

  textureMap_[ _texture.id ]  = _texture.glName;
  propertyMap_[ _texture.id ] = _texture.name.toStdString();

  std::cerr << "Added texture " << _texture.name.toStdString() << std::endl;
  return _texture.id;
}

158 159 160 161 162 163 164 165 166 167 168 169 170 171
/// Stores the given image in the texture information
bool TextureData::setImage( QString _textureName , QImage& _image ) {
  int textureid = -1;
  textureid = getTextureIndex(_textureName);

  if ( textureid == -1) {
    std::cerr << "setImage: Unknown Texture!" << std::endl;
    return true;
  }

  textures_[textureid].textureImage = _image;
  return false;
}

Jan Möbius's avatar
Jan Möbius committed
172 173 174 175 176 177
//-----------------------------------------------------------------------------------

/** \brief Delete a given texture
 *
 * @param _textureName name of the texture
 */
178 179 180 181 182 183 184 185 186 187 188 189
// void TextureData::deleteTexture(QString _textureName)
// {
//   int index = getTextureIndex(_textureName);
//
//   if ( index != -1){
//
//
//     textureMap_.erase( texture(_textureName).id );
//     propertyMap_.erase( texture(_textureName).id );
//     textures_.erase(textures_.begin()+index);
//   }
// }
Jan Möbius's avatar
Jan Möbius committed
190 191 192 193 194 195 196 197

//-----------------------------------------------------------------------------------

/** \brief get parameters of a given texture
 *
 * @param _textureName name of the texture
 * @return corresponding textureParameters or invalid TexParameters
 */
198 199 200 201 202 203 204 205 206
// TexParameters TextureData::textureParameters(QString _textureName)
// {
//   int id = getTextureIndex(_textureName);
//
//   if ( id != -1)
//     return textures_[id].parameters;
//   else
//     return TexParameters();
// }
Jan Möbius's avatar
Jan Möbius committed
207 208 209 210 211 212 213 214

//-----------------------------------------------------------------------------------

/** \brief Set Parameters for a given texture
 *
 * @param _textureName name of the texture
 * @param _params new parameters for the texture
 */
215 216 217 218 219 220 221
// void TextureData::setTextureParameters(QString _textureName, TexParameters _params)
// {
//   int id = getTextureIndex(_textureName);
//
//   if ( id != -1)
//     textures_[id].parameters = _params;
// }
Jan Möbius's avatar
Jan Möbius committed
222 223 224 225 226 227 228 229

//-----------------------------------------------------------------------------------

/** \brief get texture object of a given texture
 *
 * @param _textureName name of the texture
 * @return corresponding texture object
 */
230
Texture& TextureData::texture(QString _textureName)
Jan Möbius's avatar
Jan Möbius committed
231 232 233 234 235
{
  int id = getTextureIndex(_textureName);

  if ( id != -1)
    return textures_[id];
236 237 238 239
  else {
    std::cerr << "Invalid Texture" << _textureName.toStdString() << std::endl;
    return noTexture;
  }
Jan Möbius's avatar
Jan Möbius committed
240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265
}

//-----------------------------------------------------------------------------------

/** \brief Get the index of a given texture
 *
 * @param _textureName name of the texture
 * @return index in texture vector or -1 if not found
 */
int TextureData::getTextureIndex(QString _textureName)
{
  // Search the list of textures if we have the texture
  int textureid = -1;
  for ( int i = 0 ; i < (int)textures_.size() ; ++i ) {
    if ( textures_[i].name == _textureName ) {
      textureid = i;
      break;
    }
  }

  return textureid;
}

//-----------------------------------------------------------------------------------

/** \brief Get reference to the texture vector
266
 *
Jan Möbius's avatar
Jan Möbius committed
267 268 269 270 271 272 273 274 275
 * @return texture vector
 */
std::vector< Texture >& TextureData::textures(){
  return textures_;
}

//-----------------------------------------------------------------------------------

/** \brief Get pointer to the textureMap
276
 *
Jan Möbius's avatar
Jan Möbius committed
277 278 279 280 281 282 283 284 285
 * @return textureMap
 */
std::map< int, GLuint>* TextureData::textureMap(){
  return &textureMap_;
}

//-----------------------------------------------------------------------------------

/** \brief Get pointer to the propertyMap
286
 *
Jan Möbius's avatar
Jan Möbius committed
287 288 289 290 291 292
 * @return propertyMap
 */
std::map< int, std::string>* TextureData::propertyMap(){
  return &propertyMap_;
}