TextureData.cc 10.2 KB
Newer Older
1
/*===========================================================================*\
Jan Möbius's avatar
Jan Möbius committed
2
3
*                                                                            *
*                              OpenFlipper                                   *
Martin Schultz's avatar
Martin Schultz committed
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
 *           Copyright (c) 2001-2015, RWTH-Aachen University                 *
 *           Department of Computer Graphics and Multimedia                  *
 *                          All rights reserved.                             *
 *                            www.openflipper.org                            *
 *                                                                           *
 *---------------------------------------------------------------------------*
 * This file is part of OpenFlipper.                                         *
 *---------------------------------------------------------------------------*
 *                                                                           *
 * Redistribution and use in source and binary forms, with or without        *
 * modification, are permitted provided that the following conditions        *
 * are met:                                                                  *
 *                                                                           *
 * 1. Redistributions of source code must retain the above copyright notice, *
 *    this list of conditions and the following disclaimer.                  *
 *                                                                           *
 * 2. Redistributions in binary form must reproduce the above copyright      *
 *    notice, this list of conditions and the following disclaimer in the    *
 *    documentation and/or other materials provided with the distribution.   *
 *                                                                           *
 * 3. Neither the name of the copyright holder nor the names of its          *
 *    contributors may be used to endorse or promote products derived from   *
 *    this software without specific prior written permission.               *
 *                                                                           *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS       *
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED *
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A           *
 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER *
 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,  *
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,       *
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR        *
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF    *
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING      *
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS        *
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.              *
Jan Möbius's avatar
Jan Möbius committed
39
*                                                                            *
40
41
\*===========================================================================*/

Jan Möbius's avatar
Jan Möbius committed
42

Jan Möbius's avatar
Jan Möbius committed
43
44

#include "TextureData.hh"
45
#include "ImageStorage.hh"
Matthias Möller's avatar
Matthias Möller committed
46
#include <iostream>
Jan Möbius's avatar
Jan Möbius committed
47
48
49

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

50
51
Texture::Texture() :
  name_("No Texture"),
Jan Möbius's avatar
Jan Möbius committed
52
  textureImageId_(0),
53
  visibleName_(""),
54
  filename_("Invalid"),
55
56
  id_(-1),
  glName_(0),
57
58
  dimension_(0),
  enabled_(false),
Jan Möbius's avatar
Jan Möbius committed
59
  hidden_(false),
60
61
62
  dirty_(true),
  type_(UNSET),
  indexMappingProperty_("f:textureindex")
63
64
65
66
{

}

67
TextureData::TextureData() :
68
  nextInternalID_(1)
69
70
{
  // map 0 to no texture
71
72
  textureMap_[0]  = 0;
  propertyMap_[0] = "No Texture";
Jan Möbius's avatar
Jan Möbius committed
73
74
75
76
77
}

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

TextureData::~TextureData() {
78
79
80
81
  for(const int& id:managedImageId_)
  {
    imageStore().removeImage(id);
  }
Jan Möbius's avatar
Jan Möbius committed
82
}
83

Jan Möbius's avatar
Jan Möbius committed
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
//-----------------------------------------------------------------------------------

/** \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)
108
    return textures_[id].enabled();
Jan Möbius's avatar
Jan Möbius committed
109
110
111
112
113
114
115
116
117
118
119
  else
    return false;
}

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

/** \brief Enable a given texture
 *
 * @param _textureName name of the texture
 * @param _exclusive disable other textures?
 */
120
bool TextureData::enableTexture(QString _textureName, bool _exclusive)
Jan Möbius's avatar
Jan Möbius committed
121
122
123
124
125
{
  int id = getTextureIndex(_textureName);

  if ( id != -1){

126
    textures_[id].enable();
Jan Möbius's avatar
Jan Möbius committed
127
128
129
130
131

    //disable other textures
    if (_exclusive)
      for ( int i = 0 ; i < (int)textures_.size() ; ++i )
        if (i != id)
132
          textures_[i].disable();
133
    return true;
Jan Möbius's avatar
Jan Möbius committed
134
  }
135
  return false;
Jan Möbius's avatar
Jan Möbius committed
136
137
138
139
140
141
142
143
144
145
146
147
148
}

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

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

  if ( id != -1)
149
    textures_[id].disable();
Jan Möbius's avatar
Jan Möbius committed
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
}

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

/** \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;
166
167
168
169
  tex.id( nextInternalID_++ );
  tex.name( _textureName );
  tex.glName( _glName );
  tex.filename( _filename );
170
171
  tex.dimension(_dimension);
  tex.enable();
172
173
  tex.setDirty();
  tex.type( VERTEXBASED );
174
  tex.hidden(false);
175
//   tex.parameters = TexParameters;
Jan Möbius's avatar
Jan Möbius committed
176
177
178

  textures_.push_back( tex );

179
180
  textureMap_[ tex.id() ]  = tex.glName();
  propertyMap_[ tex.id() ] = tex.name().toStdString();
Jan Möbius's avatar
Jan Möbius committed
181

182
  return tex.id();
Jan Möbius's avatar
Jan Möbius committed
183
184
}

185
int TextureData::addTexture ( Texture _texture, GLuint _glName ) {
186
187
  _texture.id( nextInternalID_++ );
  _texture.glName( _glName );
188
189
  textures_.push_back(_texture);

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

193
  return _texture.id();
194
195
}

196
bool TextureData::addMultiTexture( QString _textureName ) {
Jan Möbius's avatar
Jan Möbius committed
197
  int textureid = getTextureIndex(_textureName);
198
199
200
201
202
203
204
205

  if ( textureid != -1) {
    std::cerr << "Texture exists!" << std::endl;
    return false;
  }

  //generate texture object
  Texture tex;
206
207
208
  tex.id( nextInternalID_++ );
  tex.name( _textureName );
  tex.filename("MultiTexture");
209
210
  tex.setDirty();
  tex.type(MULTITEXTURE);
211
  tex.hidden(false);
212
213
214
215
216
217

  textures_.push_back( tex );

  return true;
}

218
/// Stores the given image in the texture information
219
bool TextureData::setImage( QString _textureName , int _id ) {
Jan Möbius's avatar
Jan Möbius committed
220
  int textureid = getTextureIndex(_textureName);
221
222
223

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

227
228
  textures_[textureid].textureImageId(_id);
  return true;
229
230
}

Jan Möbius's avatar
Jan Möbius committed
231
232
//-----------------------------------------------------------------------------------

233
234
235
236
237
238
239
240
241
242
243
244
// 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
245
246
247

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

248
249
250
251
252
253
254
255
256
// 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
257
258
259

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

260
261
262
263
264
265
266
// 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
267
268
269
270
271
272
273
274

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

/** \brief get texture object of a given texture
 *
 * @param _textureName name of the texture
 * @return corresponding texture object
 */
275
Texture& TextureData::texture(QString _textureName)
Jan Möbius's avatar
Jan Möbius committed
276
277
278
279
280
{
  int id = getTextureIndex(_textureName);

  if ( id != -1)
    return textures_[id];
281
282
283
284
  else {
    std::cerr << "Invalid Texture" << _textureName.toStdString() << std::endl;
    return noTexture;
  }
Jan Möbius's avatar
Jan Möbius committed
285
286
287
288
289
290
291
292
293
294
295
296
297
298
}

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

/** \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 ) {
299
    if ( (textures_[i].name() == _textureName) || (textures_[i].visibleName() == _textureName) ) {
Jan Möbius's avatar
Jan Möbius committed
300
301
302
303
304
305
306
307
308
309
310
      textureid = i;
      break;
    }
  }

  return textureid;
}

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

/** \brief Get reference to the texture vector
311
 *
Jan Möbius's avatar
Jan Möbius committed
312
313
314
315
316
317
318
319
320
 * @return texture vector
 */
std::vector< Texture >& TextureData::textures(){
  return textures_;
}

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

/** \brief Get pointer to the textureMap
321
 *
Jan Möbius's avatar
Jan Möbius committed
322
323
324
325
326
327
328
329
330
 * @return textureMap
 */
std::map< int, GLuint>* TextureData::textureMap(){
  return &textureMap_;
}

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

/** \brief Get pointer to the propertyMap
331
 *
Jan Möbius's avatar
Jan Möbius committed
332
333
334
335
336
337
 * @return propertyMap
 */
std::map< int, std::string>* TextureData::propertyMap(){
  return &propertyMap_;
}

338
339
340
void TextureData::addManagedImageId(int _imageId){
  managedImageId_.push_back(_imageId);
}