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 <iostream>
46 
47 //-----------------------------------------------------------------------------------
48 
49 Texture::Texture() :
50  name_("No Texture"),
51  textureImageId_(0),
52  visibleName_(""),
53  filename_("Invalid"),
54  id_(-1),
55  glName_(0),
56  dimension_(0),
57  enabled_(false),
58  hidden_(false),
59  dirty_(true),
60  type_(UNSET),
61  indexMappingProperty_("f:textureindex")
62 {
63 
64 }
65 
67  nextInternalID_(1)
68 {
69  // map 0 to no texture
70  textureMap_[0] = 0;
71  propertyMap_[0] = "No Texture";
72 }
73 
74 //-----------------------------------------------------------------------------------
75 
77 
78 }
79 
80 //-----------------------------------------------------------------------------------
81 
87 bool TextureData::textureExists(QString _textureName)
88 {
89  return ( getTextureIndex(_textureName) != -1);
90 }
91 
92 //-----------------------------------------------------------------------------------
93 
99 bool TextureData::isEnabled(QString _textureName)
100 {
101  int id = getTextureIndex(_textureName);
102 
103  if ( id != -1)
104  return textures_[id].enabled();
105  else
106  return false;
107 }
108 
109 //-----------------------------------------------------------------------------------
110 
116 bool TextureData::enableTexture(QString _textureName, bool _exclusive)
117 {
118  int id = getTextureIndex(_textureName);
119 
120  if ( id != -1){
121 
122  textures_[id].enable();
123 
124  //disable other textures
125  if (_exclusive)
126  for ( int i = 0 ; i < (int)textures_.size() ; ++i )
127  if (i != id)
128  textures_[i].disable();
129  return true;
130  }
131  return false;
132 }
133 
134 //-----------------------------------------------------------------------------------
135 
140 void TextureData::disableTexture(QString _textureName)
141 {
142  int id = getTextureIndex(_textureName);
143 
144  if ( id != -1)
145  textures_[id].disable();
146 }
147 
148 //-----------------------------------------------------------------------------------
149 
158 int TextureData::addTexture(QString _textureName, QString _filename, uint _dimension, GLuint _glName)
159 {
160  //generate texture object
161  Texture tex;
162  tex.id( nextInternalID_++ );
163  tex.name( _textureName );
164  tex.glName( _glName );
165  tex.filename( _filename );
166  tex.dimension(_dimension);
167  tex.enable();
168  tex.setDirty();
169  tex.type( VERTEXBASED );
170  tex.hidden(false);
171 // tex.parameters = TexParameters;
172 
173  textures_.push_back( tex );
174 
175  textureMap_[ tex.id() ] = tex.glName();
176  propertyMap_[ tex.id() ] = tex.name().toStdString();
177 
178  return tex.id();
179 }
180 
181 int TextureData::addTexture ( Texture _texture, GLuint _glName ) {
182  _texture.id( nextInternalID_++ );
183  _texture.glName( _glName );
184  textures_.push_back(_texture);
185 
186  textureMap_[ _texture.id() ] = _texture.glName();
187  propertyMap_[ _texture.id() ] = _texture.name().toStdString();
188 
189  return _texture.id();
190 }
191 
192 bool TextureData::addMultiTexture( QString _textureName ) {
193  int textureid = getTextureIndex(_textureName);
194 
195  if ( textureid != -1) {
196  std::cerr << "Texture exists!" << std::endl;
197  return false;
198  }
199 
200  //generate texture object
201  Texture tex;
202  tex.id( nextInternalID_++ );
203  tex.name( _textureName );
204  tex.filename("MultiTexture");
205  tex.setDirty();
206  tex.type(MULTITEXTURE);
207  tex.hidden(false);
208 
209  textures_.push_back( tex );
210 
211  return true;
212 }
213 
215 bool TextureData::setImage( QString _textureName , int _id ) {
216  int textureid = getTextureIndex(_textureName);
217 
218  if ( textureid == -1) {
219  std::cerr << "setImage: Unknown Texture!" << std::endl;
220  return false;
221  }
222 
223  textures_[textureid].textureImageId(_id);
224  return true;
225 }
226 
227 //-----------------------------------------------------------------------------------
228 
229 // void TextureData::deleteTexture(QString _textureName)
230 // {
231 // int index = getTextureIndex(_textureName);
232 //
233 // if ( index != -1){
234 //
235 //
236 // textureMap_.erase( texture(_textureName).id );
237 // propertyMap_.erase( texture(_textureName).id );
238 // textures_.erase(textures_.begin()+index);
239 // }
240 // }
241 
242 //-----------------------------------------------------------------------------------
243 
244 // TexParameters TextureData::textureParameters(QString _textureName)
245 // {
246 // int id = getTextureIndex(_textureName);
247 //
248 // if ( id != -1)
249 // return textures_[id].parameters;
250 // else
251 // return TexParameters();
252 // }
253 
254 //-----------------------------------------------------------------------------------
255 
256 // void TextureData::setTextureParameters(QString _textureName, TexParameters _params)
257 // {
258 // int id = getTextureIndex(_textureName);
259 //
260 // if ( id != -1)
261 // textures_[id].parameters = _params;
262 // }
263 
264 //-----------------------------------------------------------------------------------
265 
271 Texture& TextureData::texture(QString _textureName)
272 {
273  int id = getTextureIndex(_textureName);
274 
275  if ( id != -1)
276  return textures_[id];
277  else {
278  std::cerr << "Invalid Texture" << _textureName.toStdString() << std::endl;
279  return noTexture;
280  }
281 }
282 
283 //-----------------------------------------------------------------------------------
284 
290 int TextureData::getTextureIndex(QString _textureName)
291 {
292  // Search the list of textures if we have the texture
293  int textureid = -1;
294  for ( int i = 0 ; i < (int)textures_.size() ; ++i ) {
295  if ( (textures_[i].name() == _textureName) || (textures_[i].visibleName() == _textureName) ) {
296  textureid = i;
297  break;
298  }
299  }
300 
301  return textureid;
302 }
303 
304 //-----------------------------------------------------------------------------------
305 
310 std::vector< Texture >& TextureData::textures(){
311  return textures_;
312 }
313 
314 //-----------------------------------------------------------------------------------
315 
320 std::map< int, GLuint>* TextureData::textureMap(){
321  return &textureMap_;
322 }
323 
324 //-----------------------------------------------------------------------------------
325 
330 std::map< int, std::string>* TextureData::propertyMap(){
331  return &propertyMap_;
332 }
333 
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:192
std::vector< Texture > & textures()
Get reference to the texture vector.
Definition: TextureData.cc:310
bool setImage(QString _textureName, int _id)
Stores the given image in the texture information.
Definition: TextureData.cc:215
std::map< int, std::string > * propertyMap()
Get pointer to the propertyMap.
Definition: TextureData.cc:330
int getTextureIndex(QString _textureName)
Get the index of a given texture.
Definition: TextureData.cc:290
bool enableTexture(QString _textureName, bool _exclusive=false)
Enable a given texture.
Definition: TextureData.cc:116
TextureData()
Constructor.
Definition: TextureData.cc:66
std::map< int, GLuint > * textureMap()
Get pointer to the textureMap.
Definition: TextureData.cc:320
bool isEnabled(QString _textureName)
Check if a texture is enabled.
Definition: TextureData.cc:99
~TextureData()
Destructor.
Definition: TextureData.cc:76
Texture & texture(QString _textureName)
Get the texture object.
Definition: TextureData.cc:271
std::vector< Texture > textures_
vector containing all textures of an object
Definition: TextureData.hh:278
bool textureExists(QString _textureName)
Check if a texture exists.
Definition: TextureData.cc:87
void disableTexture(QString _textureName)
Disable a given texture.
Definition: TextureData.cc:140
int nextInternalID_
internal id for the next texture
Definition: TextureData.hh:274