arbeit
Main Page | Namespace List | Class Hierarchy | Alphabetical List | Compound List | File List | Namespace Members | Compound Members | File Members

texData.cpp

Go to the documentation of this file.
00001 /////////////////////////////////////////////////////////////////////
00002 // 9/6/02       Aaron Lefohn    Scientific Computing and Imaging Institute
00003 // School of Computing          University of Utah
00004 //
00005 //  This library is free software; you can redistribute it and/or
00006 //  modify it under the terms of the GNU Lesser General Public
00007 //  License as published by the Free Software Foundation; either
00008 //  version 2.1 of the License, or (at your option) any later version.
00009 //
00010 //  This library is distributed in the hope that it will be useful,
00011 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013 //  Lesser General Public License for more details.
00014 //
00015 //  You should have received a copy of the GNU Lesser General Public
00016 //  License along with this library; if not, write to the Free Software
00017 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00018 ////////////////////////////////////////////////////////////////////////
00019 
00020 #include <texture/texData.h>
00021 #include <GL/glUtil.h>
00022 #include <iostream>
00023 
00024 using namespace std;
00025 using namespace gutz;
00026 
00027 using namespace glift;
00028 
00029 /// Full Non-Nrrd Constructor
00030 TexData::TexData( GLenum texDimenName, const vec3i& origin, const vec3i& dimen, int borderW,                            
00031                  GLenum glFormat, GLenum dataFormat, GLenum dataType, void* data,
00032                  bool isCompressed, int numDataBytes )
00033                  : m_data(                      data ),
00034                  m_texDimenName(        texDimenName ),
00035                  m_glFormat(            glFormat ),
00036                  m_dataFormat(  dataFormat ),
00037                  m_dataType(            dataType ),
00038                  m_texOrigin(           origin ),
00039                  m_dimen(                       dimen ),
00040                  m_borderW(             borderW ),
00041                  m_isCompressed(        isCompressed ),
00042                  m_numBytes(            numDataBytes )
00043 {
00044    checkInput();
00045 }
00046 
00047 void TexData::checkInput()
00048 {
00049    /// Check the input GLenum's
00050    checkDimenName(              m_texDimenName );
00051    checkFormatGL(       m_glFormat     );
00052    checkFormatData(     m_dataFormat   );
00053 }
00054 
00055 /// Ensure 1D, 2D, or 3D.
00056 void TexData::checkDimenName( GLenum texDimenName )
00057 {
00058    if( texDimenName != GL_TEXTURE_1D &&
00059       texDimenName != GL_TEXTURE_2D &&
00060       texDimenName != GL_TEXTURE_3D ) {
00061          err() << "checkDimenName(...) Error:\n\t'texDimenName' ( " << texDimenName 
00062             << " ) is not supported. Use 6 2D texData's for a cube map.\n";
00063          exit(1);
00064       }
00065 }
00066 
00067 /// Check that the interal format is valid
00068 ///     TODO: Make this complete for the current architecture
00069 void TexData::checkFormatGL( GLenum format )
00070 {
00071    if( format != GL_RGB &&
00072       format != GL_RGB8 &&
00073       format != GL_RGB16 &&
00074       format != GL_RGBA &&
00075       format != GL_RGBA8 &&
00076       format != GL_RGBA16 &&
00077       format != GL_RED &&
00078       format != GL_GREEN &&
00079       format != GL_BLUE &&
00080       format != GL_ALPHA &&
00081       format != GL_ALPHA8 &&
00082       format != GL_ALPHA16 &&
00083       format != GL_LUMINANCE &&
00084       format != GL_LUMINANCE8 &&
00085       format != GL_LUMINANCE16 &&
00086       format != GL_LUMINANCE_ALPHA &&
00087       format != GL_LUMINANCE8_ALPHA8 &&
00088       format != GL_LUMINANCE16_ALPHA16 &&
00089 
00090       format != GL_RGBA_FLOAT32_ATI &&
00091       format != GL_RGB_FLOAT32_ATI &&       
00092       format != GL_ALPHA_FLOAT32_ATI &&
00093       format != GL_INTENSITY_FLOAT32_ATI &&      
00094       format != GL_LUMINANCE_FLOAT32_ATI &&     
00095       format != GL_LUMINANCE_ALPHA_FLOAT32_ATI && 
00096       format != GL_RGBA_FLOAT16_ATI &&
00097       format != GL_RGB_FLOAT16_ATI &&  
00098       format != GL_ALPHA_FLOAT16_ATI &&   
00099       format != GL_INTENSITY_FLOAT16_ATI &&
00100       format != GL_LUMINANCE_FLOAT16_ATI &&
00101       format != GL_LUMINANCE_ALPHA_FLOAT16_ATI &&
00102 
00103       format != GL_COMPRESSED_ALPHA_ARB                 &&
00104       format != GL_COMPRESSED_LUMINANCE_ARB             &&      
00105       format != GL_COMPRESSED_LUMINANCE_ALPHA_ARB &&
00106       format != GL_COMPRESSED_INTENSITY_ARB             &&
00107       format != GL_COMPRESSED_RGB_ARB                           &&
00108       format != GL_COMPRESSED_RGBA_ARB ) 
00109    {
00110       err() << "checkInFormat(...) Error:\n\t'format' ( " << format 
00111          << " ) is not supported.\n";
00112       exit(1);
00113    }
00114 }
00115 
00116 /// Check that the data format is valid
00117 void TexData::checkFormatData( GLenum format )
00118 {
00119    if( format != GL_COLOR_INDEX &&
00120       format != GL_RGB &&
00121       format != GL_RGBA &&
00122       format != GL_RED &&
00123       format != GL_GREEN &&
00124       format != GL_BLUE &&
00125       format != GL_ALPHA &&
00126       format != GL_LUMINANCE &&
00127       format != GL_LUMINANCE_ALPHA &&
00128 
00129       format != GL_RGBA_FLOAT32_ATI &&
00130       format != GL_RGB_FLOAT32_ATI &&       
00131       format != GL_ALPHA_FLOAT32_ATI &&
00132       format != GL_INTENSITY_FLOAT32_ATI &&      
00133       format != GL_LUMINANCE_FLOAT32_ATI &&     
00134       format != GL_LUMINANCE_ALPHA_FLOAT32_ATI && 
00135       format != GL_RGBA_FLOAT16_ATI &&
00136       format != GL_RGB_FLOAT16_ATI &&  
00137       format != GL_ALPHA_FLOAT16_ATI &&   
00138       format != GL_INTENSITY_FLOAT16_ATI &&
00139       format != GL_LUMINANCE_FLOAT16_ATI &&
00140       format != GL_LUMINANCE_ALPHA_FLOAT16_ATI )
00141    {
00142       err() << "checkExFormat(...) Error:\n\t'format' ( " << format 
00143          << " ) is not supported.\n";
00144       exit(1);
00145    }
00146 
00147 }
00148 
00149 /// Write the data to texture memory with 'glTexImage*D(...)'
00150 void TexData::texImageND( GLenum texType, int mipLevel )
00151 {
00152    if( m_isCompressed ) {
00153       switch( m_texDimenName )
00154       {
00155       case GL_TEXTURE_1D:
00156          glCompressedTexImage1DARB(     texType, mipLevel, m_dataFormat, m_dimen.x, 
00157             m_borderW, m_numBytes, m_data );
00158          break;
00159       case GL_TEXTURE_2D:
00160          glCompressedTexImage2DARB(     texType, mipLevel, m_dataFormat, m_dimen.x, m_dimen.y,
00161             m_borderW, m_numBytes, m_data );
00162          break;
00163       case GL_TEXTURE_3D :
00164          glCompressedTexImage3DARB(     texType, mipLevel, m_dataFormat, m_dimen.x, m_dimen.y, m_dimen.z, 
00165             m_borderW, m_numBytes, m_data );
00166          break;
00167       default:
00168          err() << "texImageND Error:\n\tUnsupported texDimenName (" 
00169             << texType << endl;
00170          exit(1);
00171       }
00172 
00173    }
00174    else {
00175       switch( m_texDimenName )
00176       {
00177       case GL_TEXTURE_1D:
00178          glTexImage1D(  texType, mipLevel, m_glFormat, m_dimen.x, 
00179             m_borderW, m_dataFormat, m_dataType, m_data );
00180          break;
00181       case GL_TEXTURE_2D:
00182          glTexImage2D(  texType, mipLevel, m_glFormat, m_dimen.x, m_dimen.y,
00183             m_borderW, m_dataFormat, m_dataType, m_data );
00184          break;
00185       case GL_TEXTURE_3D :
00186          glTexImage3D(  texType, mipLevel, m_glFormat, m_dimen.x, m_dimen.y, m_dimen.z, 
00187             m_borderW, m_dataFormat, m_dataType, m_data );
00188          break;
00189       default:
00190          err() << "texImageND Error:\n\tUnsupported texDimenName (" 
00191             << texType << endl;
00192          exit(1);
00193       }
00194    }
00195 }
00196 
00197 /// Replace part of all of the texture with 'glTexSubImage*D(...)'
00198 void TexData::texSubImageND( GLenum texType, int mipLevel )
00199 {
00200    if( m_isCompressed ) {
00201       switch( m_texDimenName )
00202       {
00203       case GL_TEXTURE_1D:
00204          glCompressedTexSubImage1DARB( texType, mipLevel, m_texOrigin.x, 
00205             m_dimen.x, 
00206             m_dataFormat, m_numBytes, m_data );
00207          break;
00208       case GL_TEXTURE_2D:
00209          glCompressedTexSubImage2DARB( texType, mipLevel, m_texOrigin.x, m_texOrigin.y, 
00210             m_dimen.x, m_dimen.y,
00211             m_dataFormat, m_numBytes, m_data );
00212          break;
00213       case GL_TEXTURE_3D :
00214          glCompressedTexSubImage3DARB( texType, mipLevel, m_texOrigin.x, m_texOrigin.y, m_texOrigin.z,
00215             m_dimen.x, m_dimen.y, m_dimen.z, 
00216             m_dataFormat, m_numBytes, m_data );
00217          break;
00218       default:
00219          err() << "texImageND Error:\n\tUnsupported texDimenName (" 
00220             << texType << endl;
00221          exit(1);
00222       }
00223 
00224    }
00225    else {
00226       switch( m_texDimenName )
00227       {
00228       case GL_TEXTURE_1D:
00229          glTexSubImage1D( texType, mipLevel, m_texOrigin.x, 
00230             m_dimen.x, 
00231             m_dataFormat, m_dataType, m_data);
00232          break;
00233       case GL_TEXTURE_2D:
00234          glTexSubImage2D( texType, mipLevel, m_texOrigin.x, m_texOrigin.y, 
00235             m_dimen.x, m_dimen.y,
00236             m_dataFormat, m_dataType, m_data);
00237          break;
00238       case GL_TEXTURE_3D :
00239          glTexSubImage3D( texType, mipLevel, m_texOrigin.x, m_texOrigin.y, m_texOrigin.z,
00240             m_dimen.x, m_dimen.y, m_dimen.z, 
00241             m_dataFormat, m_dataType, m_data);
00242          break;
00243       default:
00244          err() << "texImageND Error:\n\tUnsupported texDimenName (" 
00245             << texType << endl;
00246          exit(1);
00247       }
00248    }
00249 }
00250 
00251 /// Make the OpenGL call to 'glTexImage*D(...)' or 'glTexSubImage*D(...)'
00252 void TexData::setTexData( GLenum texType, int mipLevel )
00253 {
00254    if( m_texOrigin == 0 ) { 
00255       texImageND( texType, mipLevel ); 
00256    }
00257    else {                                        
00258       texSubImageND( texType, mipLevel ); 
00259    }
00260 }
00261 
00262 
00263 /*// How many bytes are in 'data'
00264 // DEPRECATED: Works only for non-compressed texture images
00265 int TexData::numBytes( const vec3i& size, GLenum dataFormat, GLenum dataType )
00266 {
00267 int typeSize = 0;
00268 
00269 switch(dataType) {
00270 case GL_BYTE:
00271 case GL_UNSIGNED_BYTE: typeSize = 1; break;
00272 
00273 case GL_SHORT:
00274 case GL_UNSIGNED_SHORT:typeSize = 2; break;
00275 
00276 case GL_INT:
00277 case GL_UNSIGNED_INT:
00278 case GL_FLOAT:             typeSize = 4; break;
00279 default:
00280 cerr << "TexData::numBytes(..): ERROR:\n\tUnknown dataType.\n\n\a";
00281 exit(1);
00282 }
00283 
00284 vec3i sizeMul( (size.x == 0) ? 1 : size.x, 
00285 (size.y == 0) ? 1 : size.y, 
00286 (size.z == 0) ? 1 : size.z );
00287 
00288 int numBytes = sizeMul.x * sizeMul.y * sizeMul.z * typeSize * getNumChannels(dataFormat);
00289 cerr << "TexData::numBytes(...): numBytes = " << numBytes << endl;
00290 return numBytes;
00291 }
00292 */
00293 

Send questions, comments, and bug reports to:
jmk