Developer Documentation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
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 * $Revision$ *
45 * $LastChangedBy$ *
46 * $Date$ *
47 * *
48 \*===========================================================================*/
49 
50 #include "TextureData.hh"
51 #include <iostream>
52 
53 //-----------------------------------------------------------------------------------
54 
55 Texture::Texture() :
56  name_("No Texture"),
57  textureImageId_(0),
58  visibleName_(""),
59  filename_("Invalid"),
60  id_(-1),
61  glName_(0),
62  dimension_(0),
63  enabled_(false),
64  hidden_(false),
65  dirty_(true),
66  type_(UNSET),
67  indexMappingProperty_("f:textureindex")
68 {
69 
70 }
71 
73  nextInternalID_(1)
74 {
75  // map 0 to no texture
76  textureMap_[0] = 0;
77  propertyMap_[0] = "No Texture";
78 }
79 
80 //-----------------------------------------------------------------------------------
81 
83 
84 }
85 
86 //-----------------------------------------------------------------------------------
87 
93 bool TextureData::textureExists(QString _textureName)
94 {
95  return ( getTextureIndex(_textureName) != -1);
96 }
97 
98 //-----------------------------------------------------------------------------------
99 
105 bool TextureData::isEnabled(QString _textureName)
106 {
107  int id = getTextureIndex(_textureName);
108 
109  if ( id != -1)
110  return textures_[id].enabled();
111  else
112  return false;
113 }
114 
115 //-----------------------------------------------------------------------------------
116 
122 bool TextureData::enableTexture(QString _textureName, bool _exclusive)
123 {
124  int id = getTextureIndex(_textureName);
125 
126  if ( id != -1){
127 
128  textures_[id].enable();
129 
130  //disable other textures
131  if (_exclusive)
132  for ( int i = 0 ; i < (int)textures_.size() ; ++i )
133  if (i != id)
134  textures_[i].disable();
135  return true;
136  }
137  return false;
138 }
139 
140 //-----------------------------------------------------------------------------------
141 
146 void TextureData::disableTexture(QString _textureName)
147 {
148  int id = getTextureIndex(_textureName);
149 
150  if ( id != -1)
151  textures_[id].disable();
152 }
153 
154 //-----------------------------------------------------------------------------------
155 
164 int TextureData::addTexture(QString _textureName, QString _filename, uint _dimension, GLuint _glName)
165 {
166  //generate texture object
167  Texture tex;
168  tex.id( nextInternalID_++ );
169  tex.name( _textureName );
170  tex.glName( _glName );
171  tex.filename( _filename );
172  tex.dimension(_dimension);
173  tex.enable();
174  tex.setDirty();
175  tex.type( VERTEXBASED );
176  tex.hidden(false);
177 // tex.parameters = TexParameters;
178 
179  textures_.push_back( tex );
180 
181  textureMap_[ tex.id() ] = tex.glName();
182  propertyMap_[ tex.id() ] = tex.name().toStdString();
183 
184  return tex.id();
185 }
186 
187 int TextureData::addTexture ( Texture _texture, GLuint _glName ) {
188  _texture.id( nextInternalID_++ );
189  _texture.glName( _glName );
190  textures_.push_back(_texture);
191 
192  textureMap_[ _texture.id() ] = _texture.glName();
193  propertyMap_[ _texture.id() ] = _texture.name().toStdString();
194 
195  return _texture.id();
196 }
197 
198 bool TextureData::addMultiTexture( QString _textureName ) {
199  int textureid = getTextureIndex(_textureName);
200 
201  if ( textureid != -1) {
202  std::cerr << "Texture exists!" << std::endl;
203  return false;
204  }
205 
206  //generate texture object
207  Texture tex;
208  tex.id( nextInternalID_++ );
209  tex.name( _textureName );
210  tex.filename("MultiTexture");
211  tex.setDirty();
212  tex.type(MULTITEXTURE);
213  tex.hidden(false);
214 
215  textures_.push_back( tex );
216 
217  return true;
218 }
219 
221 bool TextureData::setImage( QString _textureName , int _id ) {
222  int textureid = getTextureIndex(_textureName);
223 
224  if ( textureid == -1) {
225  std::cerr << "setImage: Unknown Texture!" << std::endl;
226  return false;
227  }
228 
229  textures_[textureid].textureImageId(_id);
230  return true;
231 }
232 
233 //-----------------------------------------------------------------------------------
234 
235 // void TextureData::deleteTexture(QString _textureName)
236 // {
237 // int index = getTextureIndex(_textureName);
238 //
239 // if ( index != -1){
240 //
241 //
242 // textureMap_.erase( texture(_textureName).id );
243 // propertyMap_.erase( texture(_textureName).id );
244 // textures_.erase(textures_.begin()+index);
245 // }
246 // }
247 
248 //-----------------------------------------------------------------------------------
249 
250 // TexParameters TextureData::textureParameters(QString _textureName)
251 // {
252 // int id = getTextureIndex(_textureName);
253 //
254 // if ( id != -1)
255 // return textures_[id].parameters;
256 // else
257 // return TexParameters();
258 // }
259 
260 //-----------------------------------------------------------------------------------
261 
262 // void TextureData::setTextureParameters(QString _textureName, TexParameters _params)
263 // {
264 // int id = getTextureIndex(_textureName);
265 //
266 // if ( id != -1)
267 // textures_[id].parameters = _params;
268 // }
269 
270 //-----------------------------------------------------------------------------------
271 
277 Texture& TextureData::texture(QString _textureName)
278 {
279  int id = getTextureIndex(_textureName);
280 
281  if ( id != -1)
282  return textures_[id];
283  else {
284  std::cerr << "Invalid Texture" << _textureName.toStdString() << std::endl;
285  return noTexture;
286  }
287 }
288 
289 //-----------------------------------------------------------------------------------
290 
296 int TextureData::getTextureIndex(QString _textureName)
297 {
298  // Search the list of textures if we have the texture
299  int textureid = -1;
300  for ( int i = 0 ; i < (int)textures_.size() ; ++i ) {
301  if ( (textures_[i].name() == _textureName) || (textures_[i].visibleName() == _textureName) ) {
302  textureid = i;
303  break;
304  }
305  }
306 
307  return textureid;
308 }
309 
310 //-----------------------------------------------------------------------------------
311 
316 std::vector< Texture >& TextureData::textures(){
317  return textures_;
318 }
319 
320 //-----------------------------------------------------------------------------------
321 
326 std::map< int, GLuint>* TextureData::textureMap(){
327  return &textureMap_;
328 }
329 
330 //-----------------------------------------------------------------------------------
331 
336 std::map< int, std::string>* TextureData::propertyMap(){
337  return &propertyMap_;
338 }
339 
bool isEnabled(QString _textureName)
Check if a texture is enabled.
Definition: TextureData.cc:105
TextureData()
Constructor.
Definition: TextureData.cc:72
bool addMultiTexture(QString _textureName)
Adds a new multiTexture ( This texture will only contain a list of enabled textures for multitexturin...
Definition: TextureData.cc:198
std::vector< Texture > & textures()
Get reference to the texture vector.
Definition: TextureData.cc:316
~TextureData()
Destructor.
Definition: TextureData.cc:82
std::map< int, GLuint > * textureMap()
Get pointer to the textureMap.
Definition: TextureData.cc:326
int addTexture(QString _textureName, QString _filename, uint _dimension, GLuint _glName)
Add a Texture.
Definition: TextureData.cc:164
Texture & texture(QString _textureName)
Get the texture object.
Definition: TextureData.cc:277
int nextInternalID_
internal id for the next texture
Definition: TextureData.hh:277
bool enableTexture(QString _textureName, bool _exclusive=false)
Enable a given texture.
Definition: TextureData.cc:122
std::map< int, std::string > * propertyMap()
Get pointer to the propertyMap.
Definition: TextureData.cc:336
int getTextureIndex(QString _textureName)
Get the index of a given texture.
Definition: TextureData.cc:296
bool setImage(QString _textureName, int _id)
Stores the given image in the texture information.
Definition: TextureData.cc:221
void disableTexture(QString _textureName)
Disable a given texture.
Definition: TextureData.cc:146
bool textureExists(QString _textureName)
Check if a texture exists.
Definition: TextureData.cc:93
std::vector< Texture > textures_
vector containing all textures of an object
Definition: TextureData.hh:281