mElite  1.0
An Elite clone based on TextElite by Jan-Philipp Kappmeier and Melanie Schmidt.
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
Textures.cpp
Go to the documentation of this file.
1 
6 #include "Textures.h"
7 #include "TextureLoaderBMP.h"
8 #include "TextureLoaderTGA.h"
9 #include "LittleHelper.h"
10 
11 using namespace std;
12 
18 
19 
23 Textures *Textures::m_textures = 0;
24 
26 
32  if( m_textures == 0 )
33  m_textures = new Textures();
34  return m_textures;
35 }
36 
38 
42 Textures::Textures( void ) {
43  reserve( 10 );
44  switchRGB2BGR = false;
45 }
46 
48 
53  for( unsigned int i=0; i < textures.size(); i++ )
54  freeTexture( i );
55  textures.clear();
56 }
58 
59 
66 
67 
72 void Textures::changeColors( bool value ) {
73  switchRGB2BGR = value;
74 }
75 
77 
82 void Textures::freeTexture ( unsigned int texture ) {
83  if( texture >= textures.size() )// try to access texture that is not in vector
84  return;
85  if( textures[texture] == -1 )// try to free unassigned texture
86  return;
87 
88  // delete texture and information
89  GLuint id = (GLuint) textures[texture];
90  glDeleteTextures( 1, &id );
91  textures[texture] = -1;
92 }
93 
95 
104 void Textures::loadTexture( unsigned int texture, std::string filename ) {
105  if( texture >= textures.size() )
106  reserve( texture + 1 );
107 
108  if( textures[texture] != -1 ) {
109  freeTexture( texture );
110  }
111 
112  GLuint id;
113  glGenTextures ( 1, &id );
114  textures[texture] = (int) id;
115 
116  // load new texture. currently, only bmp is usable. all files are identified by their ending
117  if( LittleHelper::stringCmpi( filename.substr(filename.length() - 3, 3), "bmp" ) == 0) {
118  //loadTextureBMP( texture, filename );
119  texLoader = new TextureLoaderBMP();
120  texLoader->load( filename );
121  }
122  if( LittleHelper::stringCmpi( filename.substr(filename.length() - 3, 3), "tga" ) == 0) {
123  //loadTextureTGA( texture, filename );
124  texLoader = new TextureLoaderTGA();
125  texLoader->load( filename );
126  // try to write on harddisk
127  texLoader->write( "testoutput.tga");
128  }
129 
130  // now the texture should be loaded
131  bindTexture( texture );
132 
133  // possible qualities
134  // GL_NEAREST
135  // GL_LINEAR
136  // GL_LINEAR_MIPMAP_NEAREST
137  // GL_LINEAR_MIPMAP_LINEAR
138  glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR );
139  glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR_MIPMAP_LINEAR );
140 
141  // enable texture repeating
142  glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT );
143  glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT );
144 
145  GLenum usedFormat;
146 #ifdef GL_BGR_EXT
147  if( switchRGB2BGR )
148  if( texLoader->isBGR() )
149  usedFormat = GL_RGB;
150  else
151  usedFormat = GL_BGR_EXT;
152  else
153  usedFormat = texLoader->getFormat();
154 #else
155  usedFormat = GL_RGB;
156 #endif
157 
158  gluBuild2DMipmaps(GL_TEXTURE_2D, 3, texLoader->getWidth(), texLoader->getHeight(), usedFormat, GL_UNSIGNED_BYTE, texLoader->getImage() );
159  // glTexImage2D(GL_TEXTURE_2D, 0, 3, TextureImage[0]->sizeX, TextureImage[0]->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, TextureImage[0]->data);
160  delete texLoader;
161 }
162 
164 
170 void Textures::loadTexture( unsigned int texture, TextureLoader* loader ) {
171  if( texture >= textures.size() )
172  reserve( texture + 1 );
173 
174  if( textures[texture] != -1 ) {
175  freeTexture( texture );
176  }
177 
178  GLuint id;
179  glGenTextures ( 1, &id );
180  textures[texture] = (int) id;
181 
182  // now the texture should be loaded
183  bindTexture( texture );
184 
185  // possible qualities
186  // GL_NEAREST
187  // GL_LINEAR
188  // GL_LINEAR_MIPMAP_NEAREST
189  // GL_LINEAR_MIPMAP_LINEAR
190  glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST); // Linear Filtering
191  glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST); // Linear Filtering
192  // enable texture repeating
193  //glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT );
194  //glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT );
195 
196  GLenum usedFormat;
197 #ifdef GL_BGR_EXT
198  if( switchRGB2BGR )
199  if( loader->isBGR() )
200  usedFormat = GL_RGB;
201  else
202  usedFormat = GL_BGR_EXT;
203  else
204  usedFormat = loader->getFormat();
205 #else
206  usedFormat = GL_RGB;
207 #endif
208 
209  gluBuild2DMipmaps(GL_TEXTURE_2D, 3, loader->getWidth(), loader->getHeight(), usedFormat, GL_UNSIGNED_BYTE, loader->getImage() );
210  //glTexImage2D(GL_TEXTURE_2D, 0, 3, loader->getWidth(), loader->getHeight(), 0, usedFormat, GL_UNSIGNED_BYTE, loader->getImage() );
211 }
212 
214 
221 void Textures::reserve( unsigned int textureCount ) {
222  if( textureCount <= textures.size() )// new size is not bigger than the old one
223  return;
224 
225  size_t oldSize = textures.size();
226 
227  textures.reserve( textureCount );
228  textures.resize ( textureCount );
229 
230  // initialize new texture space
231  for( size_t i=oldSize; i < textures.size(); i++ )
232  textures[i] = -1;
233 }
235 
241 
242 
246 void Textures::bindTexture( unsigned int texture ) {
247  if( texture >= textures.size() )// texture to high
248  return;
249  if( textures[texture] == -1 )// texture not loaded
250  return;
251 
252  glBindTexture( GL_TEXTURE_2D, textures[texture] );
253 }