![]() |
TAPs 0.7.7.3
|
00001 /****************************************************************************** 00002 TAPsOpenGLTexturePool.cpp 00003 00004 TexturePool class (cpp file). 00005 00006 SUKITTI PUNAK (03/27/2009) 00007 ******************************************************************************/ 00008 #include "TAPsOpenGLTexturePool.hpp" 00009 // Using Inclusion Model (i.e. definitions are included in declarations) 00010 // (this name.cpp is included in name.hpp) 00011 // Each friend is defined directly inside its declaration. 00012 00013 BEGIN_NAMESPACE_TAPs__OpenGL 00014 //============================================================================= 00015 // Constructors 00016 //----------------------------------------------------------------------------- 00017 TexturePool::TexturePool () 00018 {} 00019 //----------------------------------------------------------------------------- 00020 //TexturePool::TexturePool ( TexturePool const &v ) 00021 //{} 00022 //----------------------------------------------------------------------------- 00023 TexturePool::~TexturePool () 00024 { 00025 Clear(); 00026 } 00027 //----------------------------------------------------------------------------- 00028 bool TexturePool::Clear () 00029 { 00030 // MORE CODING HERE!!! 00031 // Need to detach and delete each texture 00032 m_v1DTextureList.clear(); 00033 m_v2DTextureList.clear(); 00034 m_v3DTextureList.clear(); 00035 return true; 00036 } 00037 //----------------------------------------------------------------------------- 00038 std::string TexturePool::StrInfo () const 00039 { 00040 std::ostringstream ss; 00041 ss << "TexturePool: "; 00042 return ss.str(); 00043 } 00044 //----------------------------------------------------------------------------- 00045 //============================================================================= 00046 // Assignment Operator 00047 //----------------------------------------------------------------------------- 00048 //inline TexturePool & TexturePool::operator= ( TexturePool const &v ) 00049 //{ 00050 // return *this; 00051 //} 00052 00053 //----------------------------------------------------------------------------- 00054 GLuint TexturePool::Create1DTextureFromFile 00055 ( std::string const & fileName, std::string const & texName ) 00056 { 00057 //* 00058 #ifdef TAPs_USE_WXWIDGETS 00059 wxLogWarning( wxT( "THIS FUNCTION \"Create1DTextureFromFile\" IS NOT TESTED YET!!!" ) ); 00060 #else 00061 std::cout << "THIS FUNCTION \"Create1DTextureFromFile\" IS NOT TESTED YET!!!" << std::endl; 00062 #endif 00063 //*/ 00064 00065 if ( fileName.empty() ) return 0; 00066 00067 #ifdef TAPs_DEBUG_MODE 00068 #ifdef TAPs_USE_WXWIDGETS 00069 wxLogWarning( wxT( "Create 1D Texture \"%s\"" ), fileName.c_str() ); 00070 #else 00071 std::cout << "Create 1D Texture \"" << fileName << "\""; 00072 #endif 00073 #endif 00074 00075 //------------------------------------------------- 00076 // Load the image 00077 int width, height; 00078 // 00079 wxImage imageObj; 00080 if ( !imageObj.LoadFile( fileName.c_str() ) ) { 00081 #ifdef TAPs_USE_WXWIDGETS 00082 wxLogError( wxT( "Can't load the image for 1D texture" ) ); 00083 #else 00084 std::cout << "Can't load the image for 1D texture"; 00085 #endif 00086 return 0; 00087 } 00088 00089 TextureObj * new1DTexture = new TextureObj(); 00090 00091 #ifdef TAPs_DEBUG_MODE 00092 std::cout << "LOAD TEXTURE: "; 00093 std::cout << fileName << std::endl; 00094 #endif // #ifdef TAPs_DEBUG_MODE 00095 00096 //----------------------------- 00097 width = imageObj.GetWidth(); 00098 height = imageObj.GetHeight(); 00099 00100 // 00101 int numOfComponents = imageObj.HasAlpha() ? 4 : 3; 00102 // 00103 00104 #ifdef TAPs_DEBUG_MODE 00105 std::cout << "W,H --> " << width << "," << height << std::endl; 00106 #endif // #ifdef TAPs_DEBUG_MODE 00107 00108 GLubyte * texels = new GLubyte[width*height*numOfComponents]; 00109 00110 //--------------------------------------------- 00111 // Copy Data to texels 00112 long count = 0; 00113 unsigned char * imageData = imageObj.GetData(); 00114 unsigned char * imageAlpha = imageObj.GetAlpha(); 00115 int alphaIdx = 0; 00116 for ( int n = 0; n < width*height*3; n+=3 ) { 00117 texels[count++] = /*255 -*/ imageData[n+0]; 00118 texels[count++] = /*255 -*/ imageData[n+1]; 00119 texels[count++] = /*255 -*/ imageData[n+2]; 00120 if ( imageAlpha ) { 00121 texels[count++] = /*255 -*/ imageAlpha[alphaIdx++]; 00122 } 00123 } 00124 //------------------------------------------------- 00125 glewInit(); 00126 glGenTextures( 1, &(new1DTexture->TextureID) ); 00127 glBindTexture( GL_TEXTURE_1D, new1DTexture->TextureID ); 00128 glTexParameterf( GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, GL_LINEAR ); 00129 glTexParameterf( GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_LINEAR ); 00130 glTexParameterf( GL_TEXTURE_1D, GL_TEXTURE_WRAP_S, GL_MIRRORED_REPEAT ); 00131 TAPs::OpenGL::Fn::CHECK_GL_ERROR(); 00132 //------------------------------------------------- 00133 if ( numOfComponents == 4 ) { 00134 glTexImage1D( GL_TEXTURE_1D, 0, GL_RGBA, width, 0, 00135 GL_RGBA, GL_UNSIGNED_BYTE, texels ); 00136 } 00137 else if ( numOfComponents == 3 ) { 00138 glTexImage1D( GL_TEXTURE_1D, 0, GL_RGB, width, 0, 00139 GL_RGB, GL_UNSIGNED_BYTE, texels ); 00140 } 00141 //------------------------------------------------- 00142 delete [] texels; 00143 //------------------------------------------------- 00145 //--------------------------------------------------------------- 00146 if ( new1DTexture->TextureID > 0 ) { 00147 #ifdef TAPs_DEBUG_MODE 00148 #ifdef TAPs_USE_WXWIDGETS 00149 wxLogWarning( wxT( " is successful!" ) ); 00150 #else 00151 std::cout << " is successful!\n"; 00152 #endif 00153 #endif 00154 // Add the 1D texture to the list 00155 m_v1DTextureList.push_back( new1DTexture ); 00156 return new1DTexture->TextureID; 00157 } 00158 else { 00159 #ifdef TAPs_DEBUG_MODE 00160 #ifdef TAPs_USE_WXWIDGETS 00161 wxLogWarning( wxT( " is unsuccessful!" ) ); 00162 #else 00163 std::cout << " is unsuccessful!\n"; 00164 #endif 00165 #endif 00166 delete new1DTexture; 00167 return 0; 00168 } 00169 } // END: Texture * TexturePool::Create1DTextureFromFile ( ... ) 00170 //----------------------------------------------------------------------------- 00171 00172 //----------------------------------------------------------------------------- 00173 GLuint TexturePool::Create2DTextureFromFile 00174 ( std::string const & fileName, std::string const & texName ) 00175 { 00176 if ( fileName.empty() ) return 0; 00177 00178 #ifdef TAPs_DEBUG_MODE 00179 #ifdef TAPs_USE_WXWIDGETS 00180 wxLogWarning( wxT( "Create 2D Texture \"%s\"" ), fileName.c_str() ); 00181 #else 00182 std::cout << "Create 2D Texture \"" << fileName << "\""; 00183 #endif 00184 #endif 00185 00186 //------------------------------------------------- 00187 // Load the image 00188 int width, height; 00189 // 00190 wxImage imageObj; 00191 if ( !imageObj.LoadFile( fileName.c_str() ) ) { 00192 #ifdef TAPs_USE_WXWIDGETS 00193 wxLogError( wxT( "Can't load the image for 2D texture" ) ); 00194 #else 00195 std::cout << "Can't load the image for 2D texture"; 00196 #endif 00197 return 0; 00198 } 00199 00200 TextureObj * new2DTexture = new TextureObj(); 00201 00202 //#ifdef TAPs_DEBUG_MODE 00203 std::cout << "LOAD TEXTURE: "; 00204 std::cout << fileName << std::endl; 00205 //#endif // #ifdef TAPs_DEBUG_MODE 00206 00207 //----------------------------- 00208 width = imageObj.GetWidth(); 00209 height = imageObj.GetHeight(); 00210 00211 // 00212 int numOfComponents = imageObj.HasAlpha() ? 4 : 3; 00213 // 00214 00215 //#ifdef TAPs_DEBUG_MODE 00216 std::cout << "W,H --> " << width << "," << height << std::endl; 00217 //#endif // #ifdef TAPs_DEBUG_MODE 00218 00219 GLubyte * texels = new GLubyte[width*height*numOfComponents]; 00220 00221 //--------------------------------------------- 00222 // Copy Data to texels 00223 long count = 0; 00224 unsigned char * imageData = imageObj.GetData(); 00225 unsigned char * imageAlpha = imageObj.GetAlpha(); 00226 int alphaIdx = 0; 00227 for ( int n = 0; n < width*height*3; n+=3 ) { 00228 texels[count++] = /*255 -*/ imageData[n+0]; 00229 texels[count++] = /*255 -*/ imageData[n+1]; 00230 texels[count++] = /*255 -*/ imageData[n+2]; 00231 if ( imageAlpha ) { 00232 texels[count++] = /*255 -*/ imageAlpha[alphaIdx++]; 00233 } 00234 } 00235 //------------------------------------------------- 00236 glewInit(); 00237 glGenTextures( 1, &(new2DTexture->TextureID) ); 00238 glBindTexture( GL_TEXTURE_2D, new2DTexture->TextureID ); 00239 glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR ); 00240 glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR ); 00241 glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_MIRRORED_REPEAT ); 00242 glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_MIRRORED_REPEAT ); 00243 TAPs::OpenGL::Fn::CHECK_GL_ERROR(); 00244 //------------------------------------------------- 00245 if ( numOfComponents == 4 ) { 00246 glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, 00247 GL_RGBA, GL_UNSIGNED_BYTE, texels ); 00248 } 00249 else if ( numOfComponents == 3 ) { 00250 glTexImage2D( GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, 00251 GL_RGB, GL_UNSIGNED_BYTE, texels ); 00252 } 00253 //------------------------------------------------- 00254 delete [] texels; 00255 //------------------------------------------------- 00257 //--------------------------------------------------------------- 00258 if ( new2DTexture->TextureID > 0 ) { 00259 #ifdef TAPs_DEBUG_MODE 00260 #ifdef TAPs_USE_WXWIDGETS 00261 wxLogWarning( wxT( " is successful!" ) ); 00262 #else 00263 std::cout << " is successful!\n"; 00264 #endif 00265 #endif 00266 // Add the 2D texture to the list 00267 m_v2DTextureList.push_back( new2DTexture ); 00268 return new2DTexture->TextureID; 00269 } 00270 else { 00271 //#ifdef TAPs_DEBUG_MODE 00272 #ifdef TAPs_USE_WXWIDGETS 00273 wxLogWarning( wxT( " is unsuccessful!" ) ); 00274 #else 00275 std::cout << " is unsuccessful!\n"; 00276 #endif 00277 //#endif 00278 delete new2DTexture; 00279 return 0; 00280 } 00281 } // END: Texture * TexturePool::Create2DTextureFromFile ( ... ) 00282 //----------------------------------------------------------------------------- 00283 00284 //----------------------------------------------------------------------------- 00285 GLuint TexturePool::Create3DTextureFromFile 00286 ( std::string const & fileName, std::string const & fileType, const int depth, std::string const & texName ) 00287 { 00288 //* 00289 #ifdef TAPs_USE_WXWIDGETS 00290 wxLogWarning( wxT( "THIS FUNCTION \"Create3DTextureFromFile\" IS NOT TESTED YET!!!" ) ); 00291 #else 00292 std::cout << "THIS FUNCTION \"Create3DTextureFromFile\" IS NOT TESTED YET!!!" << std::endl; 00293 #endif 00294 //*/ 00295 00296 if ( fileName.empty() ) return 0; 00297 00298 #ifdef TAPs_DEBUG_MODE 00299 #ifdef TAPs_USE_WXWIDGETS 00300 wxLogWarning( wxT( "Create 3D Texture \"%s\"" ), fileName.c_str() ); 00301 #else 00302 std::cout << "Create 3D Texture \"" << fileName << "\""; 00303 #endif 00304 #endif 00305 00306 //------------------------------------------------- 00307 // Load images 00308 int width, height; 00309 std::string number = "0001"; 00310 std::string texFileName; 00311 long count = 0; 00312 // 00313 texFileName = fileName + number + fileType; 00314 wxImage imageObj; 00315 if ( !imageObj.LoadFile( texFileName.c_str() ) ) { 00316 #ifdef TAPs_USE_WXWIDGETS 00317 wxLogError( wxT( "Can't load the image(s)" ) ); 00318 #else 00319 std::cout << "Can't load the image(s)"; 00320 #endif 00321 return 0; 00322 } 00323 00324 TextureObj * new3DTexture = new TextureObj(); 00325 00326 #ifdef TAPs_DEBUG_MODE 00327 std::cout << "LOAD TEXTURE: "; 00328 std::cout << texFileName << std::endl; 00329 #endif // #ifdef TAPs_DEBUG_MODE 00330 00331 //----------------------------- 00332 width = imageObj.GetWidth(); 00333 height = imageObj.GetHeight(); 00334 00335 // 00336 int numOfComponents = imageObj.HasAlpha() ? 4 : 3; 00337 // 00338 00339 #ifdef TAPs_DEBUG_MODE 00340 std::cout << "W,H,D --> " << width << "," << height << "," << depth << std::endl; 00341 #endif // #ifdef TAPs_DEBUG_MODE 00342 00343 GLubyte * texels = new GLubyte[width*height*depth*numOfComponents]; 00344 00345 // one-based, not zero based 00346 for ( int i = 1; i <= depth; ++i ) { 00347 if ( i >= 100 ) { 00348 number = "0"; 00349 } 00350 else if ( i >= 10 ) { 00351 number = "00"; 00352 } 00353 else { 00354 number = "000"; 00355 } 00356 char val[8]; 00357 number += _itoa( i, val, 10 ); 00358 texFileName = fileName + number + fileType; 00359 if ( !imageObj.LoadFile( texFileName.c_str() ) ) { 00360 wxLogError( wxT( "Can't load the image(s) for 3D texture" ) ); 00361 } 00362 else { 00363 #ifdef TAPs_DEBUG_MODE 00364 std::cout << "LOAD TEXTURE: "; 00365 std::cout << texFileName << std::endl; 00366 #endif // #ifdef TAPs_DEBUG_MODE 00367 } 00368 //--------------------------------------------- 00369 // Copy Data to texels 00370 { 00371 #ifdef TAPs_DEBUG_MODE 00372 std::cout << "count: [" << count << ", "; 00373 #endif // #ifdef TAPs_DEBUG_MODE 00374 00375 unsigned char * imageData = imageObj.GetData(); 00376 unsigned char * imageAlpha = imageObj.GetAlpha(); 00377 int alphaIdx = 0; 00378 for ( int n = 0; n < width*height*3; n+=3 ) { 00379 texels[count++] = /*255 -*/ imageData[n+0]; 00380 texels[count++] = /*255 -*/ imageData[n+1]; 00381 texels[count++] = /*255 -*/ imageData[n+2]; 00382 if ( imageAlpha ) { 00383 texels[count++] = /*255 -*/ imageAlpha[alphaIdx++]; 00384 } 00385 } 00386 #ifdef TAPs_DEBUG_MODE 00387 std::cout << count << ")\n"; 00388 #endif // #ifdef TAPs_DEBUG_MODE 00389 } 00390 } // End for loop 00391 //------------------------------------------------- 00392 glewInit(); 00393 glGenTextures( 1, &(new3DTexture->TextureID) ); 00394 glBindTexture( GL_TEXTURE_3D, new3DTexture->TextureID ); 00395 glTexParameterf( GL_TEXTURE_3D, GL_TEXTURE_MAG_FILTER, GL_LINEAR ); 00396 glTexParameterf( GL_TEXTURE_3D, GL_TEXTURE_MIN_FILTER, GL_LINEAR ); 00397 glTexParameterf( GL_TEXTURE_3D, GL_TEXTURE_WRAP_S, GL_MIRRORED_REPEAT ); 00398 glTexParameterf( GL_TEXTURE_3D, GL_TEXTURE_WRAP_T, GL_MIRRORED_REPEAT ); 00399 glTexParameterf( GL_TEXTURE_3D, GL_TEXTURE_WRAP_R, GL_MIRRORED_REPEAT ); 00400 TAPs::OpenGL::Fn::CHECK_GL_ERROR(); 00401 //------------------------------------------------- 00402 if ( numOfComponents == 4 ) { 00403 glTexImage3D( GL_TEXTURE_3D, 0, GL_RGBA, width, height, depth, 0, 00404 GL_RGBA, GL_UNSIGNED_BYTE, texels ); 00405 } 00406 else if ( numOfComponents == 3 ) { 00407 glTexImage3D( GL_TEXTURE_3D, 0, GL_RGB, width, height, depth, 0, 00408 GL_RGB, GL_UNSIGNED_BYTE, texels ); 00409 } 00410 //------------------------------------------------- 00411 delete [] texels; 00412 //------------------------------------------------- 00414 //--------------------------------------------------------------- 00415 if ( new3DTexture->TextureID > 0 ) { 00416 #ifdef TAPs_DEBUG_MODE 00417 #ifdef TAPs_USE_WXWIDGETS 00418 wxLogWarning( wxT( " is successful!" ) ); 00419 #else 00420 std::cout << " is successful!\n"; 00421 #endif 00422 #endif 00423 // Add the 3D texture to the list 00424 m_v3DTextureList.push_back( new3DTexture ); 00425 return new3DTexture->TextureID; 00426 } 00427 else { 00428 #ifdef TAPs_DEBUG_MODE 00429 #ifdef TAPs_USE_WXWIDGETS 00430 wxLogWarning( wxT( " is unsuccessful!" ) ); 00431 #else 00432 std::cout << " is unsuccessful!\n"; 00433 #endif 00434 #endif 00435 delete new3DTexture; 00436 return 0; 00437 } 00438 } // END: Texture * TexturePool::Create3DTextureFromFile ( ... ) 00439 //----------------------------------------------------------------------------- 00440 00441 //----------------------------------------------------------------------------- 00442 //============================================================================= 00443 END_NAMESPACE_TAPs__OpenGL 00444 //34567890123456789012345678901234567890123456789012345678901234567890123456789 00445 //--+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----