![]() |
TAPs 0.7.7.3
|
00001 /****************************************************************************** 00002 TAPsOpenGLTexture.cpp 00003 00004 OpenGL Material Properties 00005 00006 SUKITTI PUNAK (05/12/2007) 00007 MAJOR UPDATE (08/14/2009) 00008 ******************************************************************************/ 00009 #include "TAPsOpenGLTexture.hpp" 00010 // Using Inclusion Model (i.e. definitions are included in declarations) 00011 // (this name.cpp is included in name.hpp) 00012 // Each friend is defined directly inside its declaration. 00013 00014 BEGIN_NAMESPACE_TAPs__OpenGL 00015 //============================================================================= 00016 //----------------------------------------------------------------------------- 00017 // Default Constructor 00018 Texture::Texture () : m_TextureNames( NULL ) 00019 { 00020 m_ParamTex_Wrap_S = GL_CLAMP; 00021 m_ParamTex_Wrap_T = GL_CLAMP; 00022 m_ParamTex_Wrap_R = GL_CLAMP; 00023 m_ParamTex_MagFilter = GL_NEAREST; 00024 m_ParamTex_MinFilter = GL_NEAREST; 00025 // 00026 ChangeTexture( 00027 GL_TEXTURE_2D, 0, GL_RGBA, 00028 1, 1, 1, 00029 0, GL_RGBA, GL_UNSIGNED_BYTE, 00030 NULL, 00031 1 00032 ); 00033 } 00034 //----------------------------------------------------------------------------- 00035 // Constructor for 1D Texture 00036 Texture::Texture ( 00037 GLenum target, 00038 GLint level, 00039 GLint internalFormat, 00040 GLsizei width, 00041 GLint border, 00042 GLenum pixelFormat, 00043 GLenum dataType, 00044 //----------------------------- 00045 GLvoid * texels, // texel data 00046 //----------------------------- 00047 unsigned short numOfTextures, // number of 2D textures 00048 //----------------------------- 00049 GLint magFilter, 00050 GLint minFilter, 00051 GLint wrapS, 00052 GLint wrapT, 00053 GLint wrapR 00054 //----------------------------- 00055 ) : m_TextureNames( NULL ) 00056 { 00057 std::cout << "START new Texture()" << std::endl; 00058 00059 m_ParamTex_Wrap_S = wrapS; 00060 m_ParamTex_Wrap_T = wrapT; 00061 m_ParamTex_Wrap_R = wrapR; 00062 m_ParamTex_MagFilter = magFilter; 00063 m_ParamTex_MinFilter = minFilter; 00064 // 00065 ChangeTexture( 00066 target, level, internalFormat, 00067 width, 1, 1, 00068 border, pixelFormat, dataType, 00069 texels, 00070 numOfTextures 00071 ); 00072 } 00073 //----------------------------------------------------------------------------- 00074 // Constructor for 2D Texture 00075 Texture::Texture ( 00076 GLenum target, 00077 GLint level, 00078 GLint internalFormat, 00079 GLsizei width, 00080 GLsizei height, 00081 GLint border, 00082 GLenum pixelFormat, 00083 GLenum dataType, 00084 //----------------------------- 00085 GLvoid * texels, // texel data 00086 //----------------------------- 00087 unsigned short numOfTextures, // number of 2D textures 00088 //----------------------------- 00089 GLint magFilter, 00090 GLint minFilter, 00091 GLint wrapS, 00092 GLint wrapT, 00093 GLint wrapR 00094 //----------------------------- 00095 ) : m_TextureNames( NULL ) 00096 { 00097 m_ParamTex_Wrap_S = wrapS; 00098 m_ParamTex_Wrap_T = wrapT; 00099 m_ParamTex_Wrap_R = wrapR; 00100 m_ParamTex_MagFilter = magFilter; 00101 m_ParamTex_MinFilter = minFilter; 00102 // 00103 ChangeTexture( 00104 target, level, internalFormat, 00105 width, height, 1, 00106 border, pixelFormat, dataType, 00107 texels, 00108 numOfTextures 00109 ); 00110 } 00111 //----------------------------------------------------------------------------- 00112 // Constructor for 3D Texture 00113 Texture::Texture ( 00114 GLenum target, 00115 GLint level, 00116 GLint internalFormat, 00117 GLsizei width, 00118 GLsizei height, 00119 GLsizei depth, 00120 GLint border, 00121 GLenum pixelFormat, 00122 GLenum dataType, 00123 //----------------------------- 00124 GLvoid * texels, // texel data 00125 //----------------------------- 00126 unsigned short numOfTextures, // number of 2D textures 00127 //----------------------------- 00128 GLint magFilter, 00129 GLint minFilter, 00130 GLint wrapS, 00131 GLint wrapT, 00132 GLint wrapR 00133 //----------------------------- 00134 ) : m_TextureNames( NULL ) 00135 { 00136 m_ParamTex_MagFilter = magFilter; 00137 m_ParamTex_MinFilter = minFilter; 00138 m_ParamTex_Wrap_S = wrapS; 00139 m_ParamTex_Wrap_T = wrapT; 00140 m_ParamTex_Wrap_R = wrapR; 00141 // 00142 ChangeTexture( 00143 target, level, internalFormat, 00144 width, height, depth, 00145 border, pixelFormat, dataType, 00146 texels, 00147 numOfTextures 00148 ); 00149 } 00150 //----------------------------------------------------------------------------- 00151 // Destructor 00152 Texture::~Texture () 00153 { 00154 DeleteTexture(); 00155 } 00156 //----------------------------------------------------------------------------- 00157 // Delete 00158 void Texture::DeleteTexture () 00159 { 00160 if ( m_TextureNames ) { 00161 glDeleteTextures( m_NumOfTextures, m_TextureNames ); 00162 delete [] m_TextureNames; 00163 m_TextureNames = NULL; 00164 } 00165 } 00166 //----------------------------------------------------------------------------- 00167 // Change (1D) Texture -- Delete and Recrete with the input data 00168 void Texture::ChangeTexture ( 00169 GLenum target, 00170 GLint level, 00171 GLint internalFormat, 00172 GLsizei width, 00173 GLint border, 00174 GLenum pixelFormat, 00175 GLenum dataType, 00176 //----------------------------- 00177 GLvoid * texels, // texel data 00178 //----------------------------- 00179 unsigned short numOfTextures // number of 2D textures 00180 //----------------------------- 00181 ) 00182 { 00183 //----------------------------------------------------- 00184 m_Target = target; 00185 m_Level = level; 00186 m_InternalFormat = internalFormat; 00187 m_Width = width; 00188 m_Height = 1; 00189 m_Depth = 1; 00190 m_Border = border; 00191 m_PixelFormat = pixelFormat; 00192 m_DataType = dataType; 00193 //----------------------- 00194 m_NumOfTextures = numOfTextures; 00195 //----------------------------------------------------- 00196 CreateTexture( texels ); 00197 } 00198 //----------------------------------------------------------------------------- 00199 // Change (2D) Texture -- Delete and Recrete with the input data 00200 void Texture::ChangeTexture ( 00201 GLenum target, 00202 GLint level, 00203 GLint internalFormat, 00204 GLsizei width, 00205 GLsizei height, 00206 GLint border, 00207 GLenum pixelFormat, 00208 GLenum dataType, 00209 //----------------------------- 00210 GLvoid * texels, // texel data 00211 //----------------------------- 00212 unsigned short numOfTextures // number of 2D textures 00213 //----------------------------- 00214 ) 00215 { 00216 //----------------------------------------------------- 00217 m_Target = target; 00218 m_Level = level; 00219 m_InternalFormat = internalFormat; 00220 m_Width = width; 00221 m_Height = height; 00222 m_Depth = 1; 00223 m_Border = border; 00224 m_PixelFormat = pixelFormat; 00225 m_DataType = dataType; 00226 //----------------------- 00227 m_NumOfTextures = numOfTextures; 00228 //----------------------------------------------------- 00229 CreateTexture( texels ); 00230 } 00231 //----------------------------------------------------------------------------- 00232 // Change (3D) Texture -- Delete and Recreate with the input data 00233 void Texture::ChangeTexture ( 00234 GLenum target, 00235 GLint level, 00236 GLint internalFormat, 00237 GLsizei width, 00238 GLsizei height, 00239 GLsizei depth, 00240 GLint border, 00241 GLenum pixelFormat, 00242 GLenum dataType, 00243 //----------------------------- 00244 GLvoid * texels, // texel data 00245 //----------------------------- 00246 unsigned short numOfTextures // number of 2D textures 00247 //----------------------------- 00248 ) 00249 { 00250 //----------------------------------------------------- 00251 m_Target = target; 00252 m_Level = level; 00253 m_InternalFormat = internalFormat; 00254 m_Width = width; 00255 m_Height = height; 00256 m_Depth = depth; 00257 m_Border = border; 00258 m_PixelFormat = pixelFormat; 00259 m_DataType = dataType; 00260 //----------------------- 00261 m_NumOfTextures = numOfTextures; 00262 //----------------------------------------------------- 00263 CreateTexture( texels ); 00264 } 00265 //----------------------------------------------------------------------------- 00266 // Change Texture -- Delete and Recrete with the input data 00267 void Texture::CreateTexture ( GLvoid const * texels ) 00268 { 00269 DeleteTexture (); 00270 //----------------------------------------------------- 00271 // Set Number of Components for Internal Format 00272 //----------------------------------------------------- 00273 // FOUR COMPONENTS 00274 if ( m_InternalFormat == 4 00275 || m_InternalFormat == GL_COMPRESSED_RGBA 00276 || m_InternalFormat == GL_RGBA 00277 || m_InternalFormat == GL_RGBA2 00278 || m_InternalFormat == GL_RGBA4 00279 || m_InternalFormat == GL_RGB5_A1 00280 || m_InternalFormat == GL_RGBA8 00281 || m_InternalFormat == GL_RGB10_A2 00282 || m_InternalFormat == GL_RGBA12 00283 || m_InternalFormat == GL_RGBA16 00284 || m_InternalFormat == GL_RGBA16F_ARB 00285 || m_InternalFormat == GL_RGBA32F_ARB 00286 ) { 00287 m_InternalFormatNumOfComponents = 4; 00288 } 00289 // THREE COMPONENTS 00290 else if ( m_InternalFormat == 3 00291 || m_InternalFormat == GL_COMPRESSED_RGB 00292 || m_InternalFormat == GL_RGB 00293 || m_InternalFormat == GL_R3_G3_B2 00294 || m_InternalFormat == GL_RGB4 00295 || m_InternalFormat == GL_RGB5 00296 || m_InternalFormat == GL_RGB8 00297 || m_InternalFormat == GL_RGB10 00298 || m_InternalFormat == GL_RGB12 00299 || m_InternalFormat == GL_RGB16 00300 || m_InternalFormat == GL_RGB16F_ARB 00301 || m_InternalFormat == GL_RGB32F_ARB 00302 ) { 00303 m_InternalFormatNumOfComponents = 3; 00304 } 00305 // ONE COMPONENT 00306 else if ( m_InternalFormat == 1 00307 || m_InternalFormat == GL_COMPRESSED_ALPHA 00308 || m_InternalFormat == GL_ALPHA 00309 || m_InternalFormat == GL_ALPHA4 00310 || m_InternalFormat == GL_ALPHA8 00311 || m_InternalFormat == GL_ALPHA12 00312 || m_InternalFormat == GL_ALPHA16 00313 || m_InternalFormat == GL_ALPHA16F_ARB 00314 || m_InternalFormat == GL_ALPHA32F_ARB 00315 || m_InternalFormat == GL_COMPRESSED_LUMINANCE 00316 || m_InternalFormat == GL_LUMINANCE 00317 || m_InternalFormat == GL_LUMINANCE4 00318 || m_InternalFormat == GL_LUMINANCE8 00319 || m_InternalFormat == GL_LUMINANCE12 00320 || m_InternalFormat == GL_LUMINANCE16 00321 || m_InternalFormat == GL_LUMINANCE16F_ARB 00322 || m_InternalFormat == GL_LUMINANCE32F_ARB 00323 || m_InternalFormat == GL_COMPRESSED_INTENSITY 00324 || m_InternalFormat == GL_INTENSITY 00325 || m_InternalFormat == GL_INTENSITY4 00326 || m_InternalFormat == GL_INTENSITY8 00327 || m_InternalFormat == GL_INTENSITY12 00328 || m_InternalFormat == GL_INTENSITY16 00329 || m_InternalFormat == GL_INTENSITY16F_ARB 00330 || m_InternalFormat == GL_INTENSITY32F_ARB 00331 || m_InternalFormat == GL_DEPTH_COMPONENT 00332 || m_InternalFormat == GL_DEPTH_COMPONENT16 00333 || m_InternalFormat == GL_DEPTH_COMPONENT24 00334 || m_InternalFormat == GL_DEPTH_COMPONENT32 00335 ) { 00336 m_InternalFormatNumOfComponents = 1; 00337 } 00338 // TWO COMPONENTS 00339 else if ( m_InternalFormat == 2 00340 || m_InternalFormat == GL_COMPRESSED_LUMINANCE_ALPHA 00341 || m_InternalFormat == GL_LUMINANCE_ALPHA 00342 || m_InternalFormat == GL_LUMINANCE4_ALPHA4 00343 || m_InternalFormat == GL_LUMINANCE6_ALPHA2 00344 || m_InternalFormat == GL_LUMINANCE8_ALPHA8 00345 || m_InternalFormat == GL_LUMINANCE12_ALPHA4 00346 || m_InternalFormat == GL_LUMINANCE12_ALPHA12 00347 || m_InternalFormat == GL_LUMINANCE16_ALPHA16 00348 || m_InternalFormat == GL_LUMINANCE_ALPHA16F_ARB 00349 || m_InternalFormat == GL_LUMINANCE_ALPHA32F_ARB 00350 ) { 00351 m_InternalFormatNumOfComponents = 2; 00352 } 00353 else { 00354 std::cerr << "Internal Format is unsupported!" << std::endl; 00355 assert( false ); 00356 } 00357 //----------------------------------------------------- 00358 //----------------------------------------------------- 00359 // Set Number of Components for Pixel Format 00360 //----------------------------------------------------- 00361 // FOUR COMPONENTS 00362 if ( m_PixelFormat == GL_RGBA 00363 || m_PixelFormat == GL_BGRA 00364 ) { 00365 m_PixelFormatNumOfComponents = 4; 00366 } 00367 // THREE COMPONENTS 00368 else if ( m_PixelFormat == GL_RGB 00369 || m_PixelFormat == GL_BGR 00370 ) { 00371 m_PixelFormatNumOfComponents = 3; 00372 } 00373 // ONE COMPONENT 00374 else if ( m_PixelFormat == GL_COLOR_INDEX // a single color index 00375 || m_PixelFormat == GL_RED 00376 || m_PixelFormat == GL_GREEN 00377 || m_PixelFormat == GL_BLUE 00378 || m_PixelFormat == GL_ALPHA 00379 || m_PixelFormat == GL_LUMINANCE 00380 || m_PixelFormat == GL_DEPTH_COMPONENT 00381 ) { 00382 m_PixelFormatNumOfComponents = 1; 00383 } 00384 // TWO COMPONENTS 00385 else if ( m_PixelFormat == GL_LUMINANCE_ALPHA 00386 ) { 00387 m_PixelFormatNumOfComponents = 2; 00388 } 00389 else { 00390 std::cerr << "Pixel Format is unsupported!" << std::endl; 00391 assert( false ); 00392 } 00393 //----------------------------------------------------- 00394 // Set Max & Min Texture Coordinates 00395 if ( m_Target == GL_TEXTURE_RECTANGLE_ARB ) { 00396 m_MaxCoordS = m_Width; 00397 m_MaxCoordT = m_Height; 00398 m_MaxCoordR = m_Depth; 00399 } 00400 else { 00401 m_MaxCoordS = 1; 00402 m_MaxCoordT = 1; 00403 m_MaxCoordR = 1; 00404 } 00405 m_MinCoordS = 0; 00406 m_MinCoordT = 0; 00407 m_MinCoordR = 0; 00408 //----------------------------------------------------- 00409 assert( m_NumOfTextures > 0 ); 00410 m_TextureNames = new GLuint[ m_NumOfTextures ]; 00411 glGenTextures( m_NumOfTextures, m_TextureNames ); 00412 if ( m_Target == GL_TEXTURE_2D || m_Target == GL_TEXTURE_RECTANGLE_ARB ) { 00413 for ( int i = 0; i < m_NumOfTextures; ++i ) { 00414 glBindTexture( m_Target, m_TextureNames[i] ); 00415 glTexParameterf( m_Target, GL_TEXTURE_MAG_FILTER, m_ParamTex_MagFilter ); 00416 glTexParameterf( m_Target, GL_TEXTURE_MIN_FILTER, m_ParamTex_MinFilter ); 00417 glTexParameterf( m_Target, GL_TEXTURE_WRAP_S, m_ParamTex_Wrap_S ); 00418 glTexParameterf( m_Target, GL_TEXTURE_WRAP_T, m_ParamTex_Wrap_T ); 00419 } 00420 } 00421 else if ( m_Target == GL_TEXTURE_3D ) { 00422 for ( int i = 0; i < m_NumOfTextures; ++i ) { 00423 glBindTexture( m_Target, m_TextureNames[i] ); 00424 glTexParameterf( m_Target, GL_TEXTURE_MAG_FILTER, m_ParamTex_MagFilter ); 00425 glTexParameterf( m_Target, GL_TEXTURE_MIN_FILTER, m_ParamTex_MinFilter ); 00426 glTexParameterf( m_Target, GL_TEXTURE_WRAP_S, m_ParamTex_Wrap_S ); 00427 glTexParameterf( m_Target, GL_TEXTURE_WRAP_T, m_ParamTex_Wrap_T ); 00428 glTexParameterf( m_Target, GL_TEXTURE_WRAP_R, m_ParamTex_Wrap_R ); 00429 } 00430 } 00431 else if ( m_Target == GL_TEXTURE_1D ) { 00432 for ( int i = 0; i < m_NumOfTextures; ++i ) { 00433 glBindTexture( m_Target, m_TextureNames[i] ); 00434 glTexParameterf( m_Target, GL_TEXTURE_MAG_FILTER, m_ParamTex_MagFilter ); 00435 glTexParameterf( m_Target, GL_TEXTURE_MIN_FILTER, m_ParamTex_MinFilter ); 00436 glTexParameterf( m_Target, GL_TEXTURE_WRAP_S, m_ParamTex_Wrap_S ); 00437 } 00438 } 00439 else { 00440 std::cerr << "Texture target is unsupported!" << std::endl; 00441 assert( false ); 00442 } 00443 00444 00445 00446 // DEBUG 00447 //std::cout << "m_NumOfTextures: " << m_NumOfTextures << std::endl; 00448 //for ( int i = 0; i < m_NumOfTextures; ++i ) { 00449 // std::cout << "m_TextureNames[" << i << "] is " << m_TextureNames[i] << std::endl; 00450 //} 00451 00452 //----------------------------------------------------- 00453 // Determine Data Type 00454 //----------------------------------------------------- 00455 GLubyte * texelsUByte = NULL; 00456 GLushort * texelsUShort = NULL; 00457 GLuint * texelsUInt = NULL; 00458 // 00459 GLfloat * texelsFloat = NULL; 00460 GLdouble * texelsDouble = NULL; 00461 // 00462 GLbyte * texelsByte = NULL; 00463 GLshort * texelsShort = NULL; 00464 GLint * texelsInt = NULL; 00465 //----------------------------------------------------- 00466 // GLubyte * 00467 if ( m_DataType == GL_UNSIGNED_BYTE 00468 //|| m_DataType == GL_BITMAP // single bits in unsigned 8-bit integers 00469 // using the same format is glBitmap() 00470 || m_DataType == GL_UNSIGNED_BYTE_3_3_2 00471 || m_DataType == GL_UNSIGNED_BYTE_2_3_3_REV 00472 ) { 00473 CreateTexImageFrom( (GLubyte *) texels ); 00474 } 00475 // GLushort * 00476 else if ( m_DataType == GL_UNSIGNED_SHORT 00477 || m_DataType == GL_UNSIGNED_SHORT_5_6_5 00478 || m_DataType == GL_UNSIGNED_SHORT_5_6_5_REV 00479 || m_DataType == GL_UNSIGNED_SHORT_4_4_4_4 00480 || m_DataType == GL_UNSIGNED_SHORT_4_4_4_4_REV 00481 || m_DataType == GL_UNSIGNED_SHORT_5_5_5_1 00482 || m_DataType == GL_UNSIGNED_SHORT_1_5_5_5_REV 00483 ) { 00484 CreateTexImageFrom( (GLushort *) texels ); 00485 } 00486 // GLuint * 00487 else if ( m_DataType == GL_UNSIGNED_INT 00488 || m_DataType == GL_UNSIGNED_INT_8_8_8_8 00489 || m_DataType == GL_UNSIGNED_INT_8_8_8_8_REV 00490 || m_DataType == GL_UNSIGNED_INT_10_10_10_2 00491 || m_DataType == GL_UNSIGNED_INT_2_10_10_10_REV 00492 ) { 00493 CreateTexImageFrom( (GLuint *) texels ); 00494 } 00495 // GLfloat * 00496 else if ( m_DataType == GL_FLOAT 00497 ) { 00498 CreateTexImageFrom( (GLfloat *) texels ); 00499 } 00500 // GLdouble * 00501 else if ( m_DataType == GL_DOUBLE 00502 ) { 00503 CreateTexImageFrom( (GLdouble *) texels ); 00504 } 00505 // GLbyte * 00506 else if ( m_DataType == GL_BYTE 00507 ) { 00508 CreateTexImageFrom( (GLbyte *) texels ); 00509 } 00510 // GLshort * 00511 else if ( m_DataType == GL_SHORT 00512 ) { 00513 CreateTexImageFrom( (GLshort *) texels ); 00514 } 00515 // GLint * 00516 else if ( m_DataType == GL_INT 00517 ) { 00518 CreateTexImageFrom( (GLint *) texels ); 00519 } 00520 else { 00521 std::cerr << "DataType is unsupported!" << std::endl; 00522 } 00523 //----------------------------------------------------- 00524 } 00525 //----------------------------------------------------------------------------- 00526 template <typename T> 00527 void Texture::CreateTexImageFrom ( T const * texels ) 00528 { 00529 //* 00530 if ( m_Target == GL_TEXTURE_2D || m_Target == GL_TEXTURE_RECTANGLE_ARB ) { 00531 if ( texels ) { 00532 int idx = 0; 00533 int offset = m_Width * m_Height * m_PixelFormatNumOfComponents; 00534 for ( int i = 0; i < m_NumOfTextures; ++i ) { 00535 glBindTexture( m_Target, m_TextureNames[i] ); 00536 UseParameters(); 00537 glTexImage2D( m_Target, m_Level, m_InternalFormat, 00538 m_Width, m_Height, m_Border, 00539 m_PixelFormat, m_DataType, 00540 &texels[ idx ] 00541 ); 00542 idx += offset; 00543 } 00544 } 00545 else { 00546 for ( int i = 0; i < m_NumOfTextures; ++i ) { 00547 glBindTexture( m_Target, m_TextureNames[i] ); 00548 UseParameters(); 00549 glTexImage2D( m_Target, m_Level, m_InternalFormat, 00550 m_Width, m_Height, m_Border, 00551 m_PixelFormat, m_DataType, 00552 NULL 00553 ); 00554 } 00555 } 00556 } 00557 #ifdef TAPs_USE_GLSL 00558 else if ( m_Target == GL_TEXTURE_3D ) { 00559 if ( texels ) { 00560 int idx = 0; 00561 int offset = m_Width * m_Height * m_Depth * m_PixelFormatNumOfComponents; 00562 for ( int i = 0; i < m_NumOfTextures; ++i ) { 00563 glBindTexture( m_Target, m_TextureNames[i] ); 00564 UseParameters(); 00565 glTexImage3D( m_Target, m_Level, m_InternalFormat, 00566 m_Width, m_Height, m_Depth, m_Border, 00567 m_PixelFormat, m_DataType, 00568 &texels[ idx ] 00569 ); 00570 idx += offset; 00571 } 00572 } 00573 else { 00574 for ( int i = 0; i < m_NumOfTextures; ++i ) { 00575 glBindTexture( m_Target, m_TextureNames[i] ); 00576 UseParameters(); 00577 glTexImage3D( m_Target, m_Level, m_InternalFormat, 00578 m_Width, m_Height, m_Depth, m_Border, 00579 m_PixelFormat, m_DataType, 00580 NULL 00581 ); 00582 } 00583 } 00584 } 00585 #endif//TAPs_USE_GLSL 00586 else if ( m_Target == GL_TEXTURE_1D ) { 00587 if ( texels ) { 00588 int idx = 0; 00589 int offset = m_Width * m_PixelFormatNumOfComponents; 00590 for ( int i = 0; i < m_NumOfTextures; ++i ) { 00591 glBindTexture( m_Target, m_TextureNames[i] ); 00592 UseParameters(); 00593 glTexImage1D( m_Target, m_Level, m_InternalFormat, 00594 m_Width, m_Border, 00595 m_PixelFormat, m_DataType, 00596 &texels[ idx ] 00597 ); 00598 idx += offset; 00599 } 00600 } 00601 else { 00602 for ( int i = 0; i < m_NumOfTextures; ++i ) { 00603 glBindTexture( m_Target, m_TextureNames[i] ); 00604 UseParameters(); 00605 glTexImage1D( m_Target, m_Level, m_InternalFormat, 00606 m_Width, m_Border, 00607 m_PixelFormat, m_DataType, 00608 NULL 00609 ); 00610 } 00611 } 00612 } 00613 else { 00614 std::cerr << "Texture Type is unsupported!" << std::endl; 00615 assert( false ); 00616 } 00617 //*/ 00618 } 00619 //----------------------------------------------------------------------------- 00620 void Texture::PrintTextureData ( unsigned short texNum ) 00621 { 00622 assert( texNum < m_NumOfTextures ); 00623 //----------------------------------------------------- 00624 // Determine Data Type 00625 //----------------------------------------------------- 00626 unsigned int size = m_Width * m_Height * m_Depth * 00627 m_InternalFormatNumOfComponents; 00628 //----------------------------------------------------- 00629 // GLubyte * 00630 if ( m_DataType == GL_UNSIGNED_BYTE 00631 //|| m_DataType == GL_BITMAP // single bits in unsigned 8-bit integers 00632 // using the same format is glBitmap() 00633 || m_DataType == GL_UNSIGNED_BYTE_3_3_2 00634 || m_DataType == GL_UNSIGNED_BYTE_2_3_3_REV 00635 ) { 00636 GLubyte * dataType = new GLubyte[ size ]; 00637 PrintTextureData( texNum, dataType, size ); 00638 } 00639 // GLushort * 00640 else if ( m_DataType == GL_UNSIGNED_SHORT 00641 || m_DataType == GL_UNSIGNED_SHORT_5_6_5 00642 || m_DataType == GL_UNSIGNED_SHORT_5_6_5_REV 00643 || m_DataType == GL_UNSIGNED_SHORT_4_4_4_4 00644 || m_DataType == GL_UNSIGNED_SHORT_4_4_4_4_REV 00645 || m_DataType == GL_UNSIGNED_SHORT_5_5_5_1 00646 || m_DataType == GL_UNSIGNED_SHORT_1_5_5_5_REV 00647 ) { 00648 GLushort * dataType = new GLushort[ size ]; 00649 PrintTextureData( texNum, dataType, size ); 00650 } 00651 // GLuint * 00652 else if ( m_DataType == GL_UNSIGNED_INT 00653 || m_DataType == GL_UNSIGNED_INT_8_8_8_8 00654 || m_DataType == GL_UNSIGNED_INT_8_8_8_8_REV 00655 || m_DataType == GL_UNSIGNED_INT_10_10_10_2 00656 || m_DataType == GL_UNSIGNED_INT_2_10_10_10_REV 00657 ) { 00658 GLuint * dataType = new GLuint[ size ]; 00659 PrintTextureData( texNum, dataType, size ); 00660 } 00661 // GLfloat * 00662 else if ( m_DataType == GL_FLOAT 00663 ) { 00664 GLfloat * dataType= new GLfloat[ size ]; 00665 PrintTextureData( texNum, dataType, size ); 00666 } 00667 // GLdouble * 00668 else if ( m_DataType == GL_DOUBLE 00669 ) { 00670 GLdouble * dataType = new GLdouble[ size ]; 00671 PrintTextureData( texNum, dataType, size ); 00672 } 00673 // GLbyte * 00674 else if ( m_DataType == GL_BYTE 00675 ) { 00676 GLbyte * dataType = new GLbyte[ size ]; 00677 PrintTextureData( texNum, dataType, size ); 00678 } 00679 // GLshort * 00680 else if ( m_DataType == GL_SHORT 00681 ) { 00682 GLshort * dataType = new GLshort[ size ]; 00683 PrintTextureData( texNum, dataType, size ); 00684 } 00685 // GLint * 00686 else if ( m_DataType == GL_INT 00687 ) { 00688 GLint * dataType = new GLint[ size ]; 00689 PrintTextureData( texNum, dataType, size ); 00690 } 00691 else { 00692 std::cerr << "DataType is unsupported!" << std::endl; 00693 } 00694 //----------------------------------------------------- 00695 } 00696 //----------------------------------------------------------------------------- 00697 template <typename T> 00698 void Texture::PrintTextureData ( unsigned short texNum, T * outData, unsigned int size ) 00699 { 00700 //--------------------------------------------------------------- 00701 if ( m_Target == GL_TEXTURE_2D || m_Target == GL_TEXTURE_RECTANGLE_ARB ) { 00702 //std::cout << "PrintTextureData for GL_TEXTURE_2D and GL_TEXTURE_RECTANGLE_ARB is NOT Implemented Yet!" << std::endl; 00703 glBindTexture( m_Target, m_TextureNames[texNum] ); 00704 //----------------------------- 00705 std::cout << "2D (or RECTANGLE) TEXTURE TOTAL SIZE: " << size << std::endl; 00706 glGetTexImage( m_Target, m_Level, m_PixelFormat, m_DataType, outData ); 00707 //----------------------------- 00708 int count = 0; 00709 int line = 0; 00710 int sizeWH = m_Width * m_Height * m_InternalFormatNumOfComponents; 00711 int lineWidth = m_Width * m_InternalFormatNumOfComponents; 00712 int index = 0; 00713 { 00714 std::cout << "2D Texture:\n"; 00715 std::cout << "SIZE: " << sizeWH << "\n"; 00716 for ( int i = 0; i < sizeWH; ++i ) { 00717 if ( line == 0 ) { 00718 std::cout << "("; 00719 } 00720 std::cout << outData[index++]; 00721 if ( ++line == lineWidth ) { 00722 std::cout << ")\n"; 00723 line = 0; 00724 count = 0; 00725 continue; 00726 } 00727 if ( ++count % m_InternalFormatNumOfComponents != 0 ) 00728 std::cout << ", "; 00729 else 00730 std::cout << ")\t("; 00731 } 00732 } 00733 glBindTexture( m_Target, 0 ); 00734 } 00735 //--------------------------------------------------------------- 00736 else if ( m_Target == GL_TEXTURE_3D ) { 00737 glBindTexture( m_Target, m_TextureNames[texNum] ); 00738 //----------------------------- 00739 std::cout << "3D TEXTURE TOTAL SIZE: " << size << std::endl; 00740 glGetTexImage( m_Target, m_Level, m_PixelFormat, m_DataType, outData ); 00741 //----------------------------- 00742 int count = 0; 00743 int line = 0; 00744 int sizeWH = m_Width * m_Height * m_InternalFormatNumOfComponents; 00745 int lineWidth = m_Width * m_InternalFormatNumOfComponents; 00746 int index = 0; 00747 for ( int d = 0; d < m_Depth; ++d ) { 00748 std::cout << "3D Texture Slice #" << d << ":\n"; 00749 std::cout << "SIZE: " << sizeWH << "\n"; 00750 for ( int i = 0; i < sizeWH; ++i ) { 00751 if ( line == 0 ) { 00752 std::cout << "("; 00753 } 00754 std::cout << outData[index++]; 00755 if ( ++line == lineWidth ) { 00756 std::cout << ")\n"; 00757 line = 0; 00758 count = 0; 00759 continue; 00760 } 00761 if ( ++count % m_InternalFormatNumOfComponents != 0 ) 00762 std::cout << ", "; 00763 else 00764 std::cout << ")\t("; 00765 } 00766 } 00767 glBindTexture( m_Target, 0 ); 00768 } 00769 //--------------------------------------------------------------- 00770 else if ( m_Target == GL_TEXTURE_1D ) { 00771 //std::cout << "PrintTextureData for GL_TEXTURE_1D is NOT Implemented Yet!" << std::endl; 00772 glBindTexture( m_Target, m_TextureNames[texNum] ); 00773 //----------------------------- 00774 std::cout << "1D TEXTURE TOTAL SIZE: " << size << std::endl; 00775 glGetTexImage( m_Target, m_Level, m_PixelFormat, m_DataType, outData ); 00776 //----------------------------- 00777 int count = 0; 00778 int line = 0; 00779 int texelNum = 0; 00780 int sizeWH = m_Width * m_InternalFormatNumOfComponents; 00781 int lineWidth = m_InternalFormatNumOfComponents; 00782 int index = 0; 00783 { 00784 std::cout << "1D Texture:\n"; 00785 std::cout << "SIZE: " << sizeWH << "\n"; 00786 for ( int i = 0; i < sizeWH; ++i, ++texelNum ) { 00787 if ( line == 0 ) { 00788 std::cout << "#" << texelNum << "\t("; 00789 } 00790 std::cout << outData[index++]; 00791 if ( ++line == lineWidth ) { 00792 std::cout << ")\n"; 00793 line = 0; 00794 count = 0; 00795 continue; 00796 } 00797 if ( ++count % m_InternalFormatNumOfComponents != 0 ) 00798 std::cout << ", "; 00799 else 00800 std::cout << ")\t("; 00801 } 00802 } 00803 glBindTexture( m_Target, 0 ); 00804 } 00805 //--------------------------------------------------------------- 00806 delete [] outData; 00807 } 00808 //----------------------------------------------------------------------------- 00809 /* 00810 template <typename T> 00811 T* Texture::GetTextureData () const 00812 { 00813 //----------------------------------------------------- 00814 // Determine Data Type 00815 //----------------------------------------------------- 00816 unsigned int size = m_Width * m_Height * m_Depth * 00817 m_InternalFormatNumOfComponents; 00818 //----------------------------------------------------- 00819 // GLubyte * 00820 if ( m_DataType == GL_UNSIGNED_BYTE 00821 //|| m_DataType == GL_BITMAP // single bits in unsigned 8-bit integers 00822 // using the same format is glBitmap() 00823 || m_DataType == GL_UNSIGNED_BYTE_3_3_2 00824 || m_DataType == GL_UNSIGNED_BYTE_2_3_3_REV 00825 ) { 00826 GLubyte * outData = new GLubyte[ size ]; 00827 glGetTexImage( m_Target, m_Level, m_PixelFormat, m_DataType, outData ); 00828 return outData; 00829 } 00830 // GLushort * 00831 else if ( m_DataType == GL_UNSIGNED_SHORT 00832 || m_DataType == GL_UNSIGNED_SHORT_5_6_5 00833 || m_DataType == GL_UNSIGNED_SHORT_5_6_5_REV 00834 || m_DataType == GL_UNSIGNED_SHORT_4_4_4_4 00835 || m_DataType == GL_UNSIGNED_SHORT_4_4_4_4_REV 00836 || m_DataType == GL_UNSIGNED_SHORT_5_5_5_1 00837 || m_DataType == GL_UNSIGNED_SHORT_1_5_5_5_REV 00838 ) { 00839 GLushort * outData = new GLushort[ size ]; 00840 glGetTexImage( m_Target, m_Level, m_PixelFormat, m_DataType, outData ); 00841 return outData; 00842 } 00843 // GLuint * 00844 else if ( m_DataType == GL_UNSIGNED_INT 00845 || m_DataType == GL_UNSIGNED_INT_8_8_8_8 00846 || m_DataType == GL_UNSIGNED_INT_8_8_8_8_REV 00847 || m_DataType == GL_UNSIGNED_INT_10_10_10_2 00848 || m_DataType == GL_UNSIGNED_INT_2_10_10_10_REV 00849 ) { 00850 GLuint * outData = new GLuint[ size ]; 00851 glGetTexImage( m_Target, m_Level, m_PixelFormat, m_DataType, outData ); 00852 return outData; 00853 } 00854 // GLfloat * 00855 else if ( m_DataType == GL_FLOAT 00856 ) { 00857 GLfloat * outData= new GLfloat[ size ]; 00858 glGetTexImage( m_Target, m_Level, m_PixelFormat, m_DataType, outData ); 00859 return outData; 00860 } 00861 // GLdouble * 00862 else if ( m_DataType == GL_DOUBLE 00863 ) { 00864 GLdouble * outData = new GLdouble[ size ]; 00865 glGetTexImage( m_Target, m_Level, m_PixelFormat, m_DataType, outData ); 00866 return outData; 00867 } 00868 // GLbyte * 00869 else if ( m_DataType == GL_BYTE 00870 ) { 00871 GLbyte * outData = new GLbyte[ size ]; 00872 glGetTexImage( m_Target, m_Level, m_PixelFormat, m_DataType, outData ); 00873 return outData; 00874 } 00875 // GLshort * 00876 else if ( m_DataType == GL_SHORT 00877 ) { 00878 GLshort * outData = new GLshort[ size ]; 00879 glGetTexImage( m_Target, m_Level, m_PixelFormat, m_DataType, outData ); 00880 return outData; 00881 } 00882 // GLint * 00883 else if ( m_DataType == GL_INT 00884 ) { 00885 GLint * outData = new GLint[ size ]; 00886 glGetTexImage( m_Target, m_Level, m_PixelFormat, m_DataType, outData ); 00887 return outData; 00888 } 00889 else { 00890 std::cerr << "DataType is unsupported!" << std::endl; 00891 return NULL; 00892 } 00893 //----------------------------------------------------- 00894 } 00895 //*/ 00896 //----------------------------------------------------------------------------- 00897 //============================================================================= 00898 END_NAMESPACE_TAPs__OpenGL 00899 //34567890123456789012345678901234567890123456789012345678901234567890123456789 00900 //--+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----