Developer Documentation
TextureData.cc
1 /*===========================================================================*\
2 * *
3 * OpenFlipper *
4  * Copyright (c) 2001-2015, RWTH-Aachen University *
5  * Department of Computer Graphics and Multimedia *
6  * All rights reserved. *
7  * www.openflipper.org *
8  * *
9  *---------------------------------------------------------------------------*
10  * This file is part of OpenFlipper. *
11  *---------------------------------------------------------------------------*
12  * *
13  * Redistribution and use in source and binary forms, with or without *
14  * modification, are permitted provided that the following conditions *
15  * are met: *
16  * *
17  * 1. Redistributions of source code must retain the above copyright notice, *
18  * this list of conditions and the following disclaimer. *
19  * *
20  * 2. Redistributions in binary form must reproduce the above copyright *
21  * notice, this list of conditions and the following disclaimer in the *
22  * documentation and/or other materials provided with the distribution. *
23  * *
24  * 3. Neither the name of the copyright holder nor the names of its *
25  * contributors may be used to endorse or promote products derived from *
26  * this software without specific prior written permission. *
27  * *
28  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS *
29  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED *
30  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A *
31  * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER *
32  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, *
33  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, *
34  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR *
35  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF *
36  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING *
37  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS *
38  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *
39 * *
40 \*===========================================================================*/
41 
42 
43 
44 #include "TextureData.hh"
45 #include "ImageStorage.hh"
46 #include <iostream>
47 
48 //-----------------------------------------------------------------------------------
49 
50 Texture::Texture() :
51  name_("No Texture"),
52  textureImageId_(0),
53  visibleName_(""),
54  filename_("Invalid"),
55  id_(-1),
56  glName_(0),
57  dimension_(0),
58  enabled_(false),
59  hidden_(false),
60  dirty_(true),
61  type_(UNSET),
62  indexMappingProperty_("f:textureindex")
63 {
64 
65 }
66 
68  nextInternalID_(1)
69 {
70  // map 0 to no texture
71  textureMap_[0] = 0;
72  propertyMap_[0] = "No Texture";
73 }
74 
75 //-----------------------------------------------------------------------------------
76 
78  for(const int& id:managedImageId_)
79  {
80  imageStore().removeImage(id);
81  }
82 }
83 
84 //-----------------------------------------------------------------------------------
85 
91 bool TextureData::textureExists(QString _textureName)
92 {
93  return ( getTextureIndex(_textureName) != -1);
94 }
95 
96 //-----------------------------------------------------------------------------------
97 
103 bool TextureData::isEnabled(QString _textureName)
104 {
105  int id = getTextureIndex(_textureName);
106 
107  if ( id != -1)
108  return textures_[id].enabled();
109  else
110  return false;
111 }
112 
113 //-----------------------------------------------------------------------------------
114 
120 bool TextureData::enableTexture(QString _textureName, bool _exclusive)
121 {
122  int id = getTextureIndex(_textureName);
123 
124  if ( id != -1){
125 
126  textures_[id].enable();
127 
128  //disable other textures
129  if (_exclusive)
130  for ( int i = 0 ; i < (int)textures_.size() ; ++i )
131  if (i != id)
132  textures_[i].disable();
133  return true;
134  }
135  return false;
136 }
137 
138 //-----------------------------------------------------------------------------------
139 
144 void TextureData::disableTexture(QString _textureName)
145 {
146  int id = getTextureIndex(_textureName);
147 
148  if ( id != -1)
149  textures_[id].disable();
150 }
151 
152 //-----------------------------------------------------------------------------------
153 
162 int TextureData::addTexture(QString _textureName, QString _filename, uint _dimension, GLuint _glName)
163 {
164  //generate texture object
165  Texture tex;
166  tex.id( nextInternalID_++ );
167  tex.name( _textureName );
168  tex.glName( _glName );
169  tex.filename( _filename );
170  tex.dimension(_dimension);
171  tex.enable();
172  tex.setDirty();
173  tex.type( VERTEXBASED );
174  tex.hidden(false);
175 // tex.parameters = TexParameters;
176 
177  textures_.push_back( tex );
178 
179  textureMap_[ tex.id() ] = tex.glName();
180  propertyMap_[ tex.id() ] = tex.name().toStdString();
181 
182  return tex.id();
183 }
184 
185 int TextureData::addTexture ( Texture _texture, GLuint _glName ) {
186  _texture.id( nextInternalID_++ );
187  _texture.glName( _glName );
188  textures_.push_back(_texture);
189 
190  textureMap_[ _texture.id() ] = _texture.glName();
191  propertyMap_[ _texture.id() ] = _texture.name().toStdString();
192 
193  return _texture.id();
194 }
195 
196 bool TextureData::addMultiTexture( QString _textureName ) {
197  int textureid = getTextureIndex(_textureName);
198 
199  if ( textureid != -1) {
200  std::cerr << "Texture exists!" << std::endl;
201  return false;
202  }
203 
204  //generate texture object
205  Texture tex;
206  tex.id( nextInternalID_++ );
207  tex.name( _textureName );
208  tex.filename("MultiTexture");
209  tex.setDirty();
210  tex.type(MULTITEXTURE);
211  tex.hidden(false);
212 
213  textures_.push_back( tex );
214 
215  return true;
216 }
217 
219 bool TextureData::setImage( QString _textureName , int _id ) {
220  int textureid = getTextureIndex(_textureName);
221 
222  if ( textureid == -1) {
223  std::cerr << "setImage: Unknown Texture!" << std::endl;
224  return false;
225  }
226 
227  textures_[textureid].textureImageId(_id);
228  return true;
229 }
230 
231 //-----------------------------------------------------------------------------------
232 
233 // void TextureData::deleteTexture(QString _textureName)
234 // {
235 // int index = getTextureIndex(_textureName);
236 //
237 // if ( index != -1){
238 //
239 //
240 // textureMap_.erase( texture(_textureName).id );
241 // propertyMap_.erase( texture(_textureName).id );
242 // textures_.erase(textures_.begin()+index);
243 // }
244 // }
245 
246 //-----------------------------------------------------------------------------------
247 
248 // TexParameters TextureData::textureParameters(QString _textureName)
249 // {
250 // int id = getTextureIndex(_textureName);
251 //
252 // if ( id != -1)
253 // return textures_[id].parameters;
254 // else
255 // return TexParameters();
256 // }
257 
258 //-----------------------------------------------------------------------------------
259 
260 // void TextureData::setTextureParameters(QString _textureName, TexParameters _params)
261 // {
262 // int id = getTextureIndex(_textureName);
263 //
264 // if ( id != -1)
265 // textures_[id].parameters = _params;
266 // }
267 
268 //-----------------------------------------------------------------------------------
269 
275 Texture& TextureData::texture(QString _textureName)
276 {
277  int id = getTextureIndex(_textureName);
278 
279  if ( id != -1)
280  return textures_[id];
281  else {
282  std::cerr << "Invalid Texture" << _textureName.toStdString() << std::endl;
283  return noTexture;
284  }
285 }
286 
287 //-----------------------------------------------------------------------------------
288 
294 int TextureData::getTextureIndex(QString _textureName)
295 {
296  // Search the list of textures if we have the texture
297  int textureid = -1;
298  for ( int i = 0 ; i < (int)textures_.size() ; ++i ) {
299  if ( (textures_[i].name() == _textureName) || (textures_[i].visibleName() == _textureName) ) {
300  textureid = i;
301  break;
302  }
303  }
304 
305  return textureid;
306 }
307 
308 //-----------------------------------------------------------------------------------
309 
314 std::vector< Texture >& TextureData::textures(){
315  return textures_;
316 }
317 
318 //-----------------------------------------------------------------------------------
319 
324 std::map< int, GLuint>* TextureData::textureMap(){
325  return &textureMap_;
326 }
327 
328 //-----------------------------------------------------------------------------------
329 
334 std::map< int, std::string>* TextureData::propertyMap(){
335  return &propertyMap_;
336 }
337 
338 void TextureData::addManagedImageId(int _imageId){
339  managedImageId_.push_back(_imageId);
340 }
void disableTexture(QString _textureName)
Disable a given texture.
Definition: TextureData.cc:144
bool enableTexture(QString _textureName, bool _exclusive=false)
Enable a given texture.
Definition: TextureData.cc:120
int getTextureIndex(QString _textureName)
Get the index of a given texture.
Definition: TextureData.cc:294
std::vector< Texture > textures_
vector containing all textures of an object
Definition: TextureData.hh:279
TextureData()
Constructor.
Definition: TextureData.cc:67
bool textureExists(QString _textureName)
Check if a texture exists.
Definition: TextureData.cc:91
~TextureData()
Destructor.
Definition: TextureData.cc:77
int nextInternalID_
internal id for the next texture
Definition: TextureData.hh:275
std::map< int, GLuint > * textureMap()
Get pointer to the textureMap.
Definition: TextureData.cc:324
std::vector< Texture > & textures()
Get reference to the texture vector.
Definition: TextureData.cc:314
int addTexture(QString _textureName, uint _dimension, GLuint _glName)
Add a Texture without file backing.
Definition: TextureData.hh:224
bool addMultiTexture(QString _textureName)
Adds a new multiTexture ( This texture will only contain a list of enabled textures for multitexturin...
Definition: TextureData.cc:196
std::map< int, std::string > * propertyMap()
Get pointer to the propertyMap.
Definition: TextureData.cc:334
bool isEnabled(QString _textureName)
Check if a texture is enabled.
Definition: TextureData.cc:103
Texture & texture(QString _textureName)
Get the texture object.
Definition: TextureData.cc:275
bool setImage(QString _textureName, int _id)
Stores the given image in the texture information.
Definition: TextureData.cc:219