TextureData.cc 8.59 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
//=============================================================================
//
//                               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"

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

36
37
38
Texture::Texture() :
  name_("No Texture"),
  filename_("Invalid"),
39
40
  id_(-1),
  glName_(0),
41
42
  dimension_(0),
  enabled_(false),
43
44
45
  dirty_(true),
  type_(UNSET),
  indexMappingProperty_("f:textureindex")
46
47
48
49
{

}

50
TextureData::TextureData() :
51
  nextInternalID_(1)
52
53
54
55
{
  // map 0 to no texture
  textureMap_[0]     = 0;
  propertyMap_[0] = "No Texture";;
Jan Möbius's avatar
Jan Möbius committed
56
57
58
59
60
61
62
}

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

TextureData::~TextureData() {

}
63

Jan Möbius's avatar
Jan Möbius committed
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
//-----------------------------------------------------------------------------------

/** \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)
88
    return textures_[id].enabled();
Jan Möbius's avatar
Jan Möbius committed
89
90
91
92
93
94
95
96
97
98
99
  else
    return false;
}

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

/** \brief Enable a given texture
 *
 * @param _textureName name of the texture
 * @param _exclusive disable other textures?
 */
100
bool TextureData::enableTexture(QString _textureName, bool _exclusive)
Jan Möbius's avatar
Jan Möbius committed
101
102
103
104
105
{
  int id = getTextureIndex(_textureName);

  if ( id != -1){

106
    textures_[id].enable();
Jan Möbius's avatar
Jan Möbius committed
107
108
109
110
111

    //disable other textures
    if (_exclusive)
      for ( int i = 0 ; i < (int)textures_.size() ; ++i )
        if (i != id)
112
          textures_[i].disable();
113
    return true;
Jan Möbius's avatar
Jan Möbius committed
114
  }
115
  return false;
Jan Möbius's avatar
Jan Möbius committed
116
117
118
119
120
121
122
123
124
125
126
127
128
}

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

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

  if ( id != -1)
129
    textures_[id].disable();
Jan Möbius's avatar
Jan Möbius committed
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
}

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

/** \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;
146
147
148
149
  tex.id( nextInternalID_++ );
  tex.name( _textureName );
  tex.glName( _glName );
  tex.filename( _filename );
150
151
  tex.dimension(_dimension);
  tex.enable();
152
153
  tex.setDirty();
  tex.type( VERTEXBASED );
154
  tex.hidden(false);
155
//   tex.parameters = TexParameters;
Jan Möbius's avatar
Jan Möbius committed
156
157
158

  textures_.push_back( tex );

159
160
  textureMap_[ tex.id() ]  = tex.glName();
  propertyMap_[ tex.id() ] = tex.name().toStdString();
Jan Möbius's avatar
Jan Möbius committed
161

162
  return tex.id();
Jan Möbius's avatar
Jan Möbius committed
163
164
}

165
int TextureData::addTexture ( Texture _texture, GLuint _glName ) {
166
167
  _texture.id( nextInternalID_++ );
  _texture.glName( _glName );
168
169
  textures_.push_back(_texture);

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

173
  return _texture.id();
174
175
}

176
177
178
179
180
181
182
183
184
185
186
187
188
bool TextureData::addMultiTexture( QString _textureName ) {
  int textureid = -1;
  textureid = getTextureIndex(_textureName);

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

  std::cerr << "TextureData::addMultiTexture" << std::endl;

  //generate texture object
  Texture tex;
189
190
191
  tex.id( nextInternalID_++ );
  tex.name( _textureName );
  tex.filename("MultiTexture");
192
193
  tex.setDirty();
  tex.type(MULTITEXTURE);
194
  tex.hidden(false);
195
196
197
198
199
200

  textures_.push_back( tex );

  return true;
}

201
202
203
204
205
206
207
208
209
210
211
212
213
214
/// 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
215
216
217
218
219
220
//-----------------------------------------------------------------------------------

/** \brief Delete a given texture
 *
 * @param _textureName name of the texture
 */
221
222
223
224
225
226
227
228
229
230
231
232
// 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
233
234
235
236
237
238
239
240

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

/** \brief get parameters of a given texture
 *
 * @param _textureName name of the texture
 * @return corresponding textureParameters or invalid TexParameters
 */
241
242
243
244
245
246
247
248
249
// 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
250
251
252
253
254
255
256
257

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

/** \brief Set Parameters for a given texture
 *
 * @param _textureName name of the texture
 * @param _params new parameters for the texture
 */
258
259
260
261
262
263
264
// 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
265
266
267
268
269
270
271
272

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

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

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

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

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

  return textureid;
}

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

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

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

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

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

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