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

texNd.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/texNd.h>
00021 #include <iostream>
00022 
00023 using namespace std;
00024 using namespace gutz;
00025 
00026 using namespace glift;
00027 
00028 ////////////////////////////////////////
00029 /// Tex1D - 1D Textures
00030 ////////////////////////////////////////
00031 Tex1D::Tex1D( const MultiTexOState& texState, PBuffGlift* pbuff )
00032 : CoreTex( GL_TEXTURE_1D, texState, NULL, pbuff )
00033 {}
00034 
00035 Tex1D::Tex1D( const MultiTexOState& texState, TexData* texData, PBuffGlift* pbuff )
00036 : CoreTex( GL_TEXTURE_1D, texState, texData, pbuff )
00037 {}
00038 
00039 /// Copy data from framebuffer to this texture
00040 void Tex1D::copyToTex( int mipLevel, const vec3i& texOrig, 
00041                       const vec2i& screenOrig, const vec2i& copySize )
00042 {
00043    bindDef();
00044    glCopyTexSubImage1D( texType(), mipLevel, 
00045       texOrig.x,
00046       screenOrig.x, screenOrig.y,
00047       copySize.x  );
00048    releaseDef();
00049 }
00050 
00051 ////////////////////////////////////////
00052 /// Tex2D - 2D Textures
00053 ////////////////////////////////////////
00054 Tex2D::Tex2D( const MultiTexOState& texState, PBuffGlift* pbuff )
00055 : CoreTex( GL_TEXTURE_2D, texState, NULL, pbuff )
00056 {}
00057 
00058 Tex2D::Tex2D( const MultiTexOState& texState, TexData* texData, PBuffGlift* pbuff )
00059 : CoreTex( GL_TEXTURE_2D, texState, texData, pbuff )
00060 {}
00061 
00062 /// Fill entire 2D texture from framebuffer
00063 void Tex2D::copyToTex2D()               
00064 {
00065    int mipLevel = 0;
00066    vec2i texOrig(0);
00067    vec2i screenOrig(0);
00068    vec2i dimen( dimen().x, dimen().y );
00069 
00070    copyToTex( mipLevel, texOrig, screenOrig, dimen );
00071 }
00072 
00073 /// Copy data from framebuffer to this texture
00074 void Tex2D::copyToTex( int mipLevel, const vec3i& texOrig, 
00075                       const vec2i& screenOrig, const vec2i& copySize )
00076 {
00077    bindDef();
00078    glCopyTexSubImage2D( texType(), mipLevel, 
00079       texOrig.x, texOrig.y,
00080       screenOrig.x, screenOrig.y,
00081       copySize.x, copySize.y );
00082    releaseDef();
00083 }
00084 
00085 ////////////////////////////////////////
00086 /// TexRect - Rectangle Textures
00087 ////////////////////////////////////////
00088 TexRect::TexRect( const MultiTexOState& texState, PBuffGlift* pbuff )
00089 : CoreTex( getTarget(), texState, NULL, pbuff )
00090 {}
00091 
00092 TexRect::TexRect( const MultiTexOState& texState, TexData* texData, PBuffGlift* pbuff )
00093 : CoreTex( getTarget(), texState, texData, pbuff )
00094 {}
00095 
00096 /// Fill entire 2D texture from framebuffer
00097 void TexRect::copyToTex2D()             
00098 {
00099    int mipLevel = 0;
00100    vec2i texOrig(0);
00101    vec2i screenOrig(0);
00102    vec2i dimen( dimen().x, dimen().y );
00103 
00104    copyToTex( mipLevel, texOrig, screenOrig, dimen );
00105 }
00106 
00107 /// Copy data from framebuffer to this texture
00108 void TexRect::copyToTex( int mipLevel, const vec3i& texOrig, 
00109                         const vec2i& screenOrig, const vec2i& copySize )
00110 {
00111    bindDef();
00112    glCopyTexSubImage2D( texType(), mipLevel, 
00113       texOrig.x, texOrig.y,
00114       screenOrig.x, screenOrig.y,
00115       copySize.x, copySize.y );
00116    releaseDef();
00117 }
00118 
00119 GLenum TexRect::getTarget()
00120 {
00121    GLenum target;
00122    if( glew.NV_texture_rectangle ) {
00123       target = GL_TEXTURE_RECTANGLE_NV;
00124    }
00125    else if( glew.EXT_texture_rectangle ) {
00126       target = GL_TEXTURE_RECTANGLE_EXT;
00127    }
00128    else {
00129       err() << "getTarget() Error:\n\tTexture rectangles not supported on this GPU.\n";
00130       exit(1);
00131    }
00132    return target;
00133 }
00134 
00135 ////////////////////////////////////////
00136 /// Tex3D - 3D Textures
00137 ////////////////////////////////////////
00138 Tex3D::Tex3D( const MultiTexOState& texState, TexData* texData )
00139 : CoreTex( GL_TEXTURE_3D, texState, texData )
00140 {}
00141 
00142 /// Copy data from framebuffer to this texture
00143 void Tex3D::copyToTex( int mipLevel, const vec3i& texOrig, 
00144                       const vec2i& screenOrig, const vec2i& copySize )
00145 {
00146    bindDef();
00147    glCopyTexSubImage3D( texType(), mipLevel, 
00148       texOrig.x, texOrig.y, texOrig.z,
00149       screenOrig.x, screenOrig.y,
00150       copySize.x, copySize.y );
00151    releaseDef();
00152 }
00153 
00154 ////////////////////////////////////////
00155 /// TexCube - CubeMap Textures
00156 ////////////////////////////////////////
00157 TexCube::TexCube( const MultiTexOState& texState, const VecTexDataP& texData )
00158 : CoreTex( GL_TEXTURE_CUBE_MAP_ARB, texState, NULL ),
00159 m_pbuff( VecPbuffP(6) ),
00160 m_writeFace( GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB )
00161 {
00162    initPbuffVec( NULL, NULL, NULL, NULL, NULL, NULL );
00163 
00164    if( texData.size() == 6 ) {
00165       initData( texData );
00166    }
00167    else if( texData.size() > 0 ) {
00168       err() << "TexCube(...) Error:\n"
00169          << "\ttexData must contain 6 TexData pointers.\n";
00170       exit(1);
00171    }
00172 }
00173 
00174 TexCube::TexCube( const MultiTexOState& texState, PBuffGlift* pbPX, PBuffGlift* pbNX, 
00175                  PBuffGlift* pbPY, PBuffGlift* pbNY, PBuffGlift* pbPZ, PBuffGlift* pbNZ,
00176                  const VecTexDataP& texData )
00177                  : CoreTex( GL_TEXTURE_CUBE_MAP_ARB, texState, NULL ),
00178                  m_pbuff( VecPbuffP(6) ),
00179                  m_writeFace( GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB )
00180 {
00181    initPbuffVec( pbPX, pbNX, pbPY, pbNY, pbPZ, pbNZ );
00182 
00183    /// Ensure that no pbuffers are NULL
00184    for(uint i=0; i < m_pbuff.size(); i++) {
00185       if( m_pbuff[i] == NULL ) {
00186          err() << "TexCube(...) Error\n"
00187             << "\tPbuffer[" << i << "] == NULL. All pbuffers must be non-NULL.\n";
00188          exit(1);
00189       }
00190    }
00191 
00192    /// Initialize all 6 cube faces
00193    if( texData.size() == 6 ) {
00194       initData( texData );
00195    }
00196    else if( texData.size() > 0 ) {
00197       err() << "TexCube(...) Error:\n"
00198          << "\ttexData must contain 6 TexData pointers.\n";
00199       exit(1);
00200    }
00201 }
00202 
00203 void TexCube::initPbuffVec( PBuffGlift* pbPX, PBuffGlift* pbNX,  PBuffGlift* pbPY, PBuffGlift* pbNY, 
00204                            PBuffGlift* pbPZ, PBuffGlift* pbNZ )
00205 {
00206    m_pbuff[0] = pbPX;
00207    m_pbuff[1] = pbPX;
00208    m_pbuff[2] = pbPY;
00209    m_pbuff[3] = pbNY;
00210    m_pbuff[4] = pbPZ;
00211    m_pbuff[5] = pbNZ;
00212 }
00213 
00214 void TexCube::initData(  const VecTexDataP& texData )
00215 {
00216    setTexData( GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB, texData[0] );
00217    setTexData( GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB, texData[1] );
00218    setTexData( GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB, texData[2] );
00219    setTexData( GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB, texData[3] );
00220    setTexData( GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB, texData[4] );
00221    setTexData( GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB, texData[5] );
00222 }
00223 
00224 /// Set the target for a glCopyTexImage(...)
00225 void TexCube::setCopyCubeFace( GLenum cubeFace )                
00226 {
00227    checkCubeFace( cubeFace );
00228    m_writeFace = cubeFace;
00229 }
00230 
00231 /// Fill entire cubeFace with entire framebuffer contents
00232 void TexCube::copyToTex( GLenum cubeFace )
00233 {
00234    checkCubeFace( cubeFace ); /// Ensure that cubeFace is valid
00235    m_writeFace = cubeFace;
00236 
00237    vec2i texOrig(0);
00238    vec2i screenOrig(0);
00239    vec2i copyDimen( dimen().x, dimen().y );
00240 
00241    copyToTex( 0, texOrig, screenOrig, copyDimen );
00242 }
00243 
00244 /// Copy data from framebuffer to this texture
00245 void TexCube::copyToTex( int mipLevel, const vec3i& texOrig, 
00246                         const vec2i& screenOrig, const vec2i& copySize )
00247 {
00248    bindDef();
00249    glCopyTexSubImage2D( m_writeFace, mipLevel, 
00250       texOrig.x, texOrig.y,
00251       screenOrig.x, screenOrig.y,
00252       copySize.x, copySize.y );
00253    releaseDef();
00254 }
00255 
00256 /// CubeMap set function. 
00257 /// - setTexSize controls whether the texture size is set by this call
00258 //    * Set to "FALSE" if this is a subTexture replace
00259 void TexCube::setTexData( GLenum cubeFace, TexData* texData, int mipLevel, bool setTexSize )
00260 {
00261    /// Ensure that it is 2D data
00262    if( texData->texType() != GL_TEXTURE_2D ) {
00263       err() << "setTexData(...) Error:\n"
00264          << "\ttexData must be two dimensional.\n";
00265       exit(1);
00266    }
00267 
00268    /// Ensure that cubeFace is valid
00269    checkCubeFace( cubeFace );
00270 
00271    /// Set the texture data
00272    if( setTexSize ) {
00273       dataDimen( texData->dimen() );
00274    }
00275    bindDef();
00276    texData->setTexData(cubeFace, mipLevel);
00277    releaseDef();
00278 }
00279 
00280 /// Ensure that cubeFace is valid
00281 void TexCube::checkCubeFace( GLenum cubeFace )
00282 {
00283    if( cubeFace != GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB &&
00284       cubeFace != GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB &&
00285       cubeFace != GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB &&
00286       cubeFace != GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB &&
00287       cubeFace != GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB &&
00288       cubeFace != GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB )
00289    {
00290       err() << "checkCubeFace(...) Error:\n\tInvalid cubeFace.\n";
00291       exit(1);
00292    }
00293 }
00294 
00295 void TexCube::bindDef()
00296 {
00297    /// Bind this texture object
00298    CoreTex::bindDef();
00299 
00300    /// Bind the pbuffers for each face
00301    /// Note: If the 1st pbuffer is non-NULL, the constructor gaurantees that they are all non-NULL
00302    if( m_pbuff[0] && tryToBindPbuff() ) {
00303       for(uint i=0; i < m_pbuff.size(); i++) {
00304          m_pbuff[i]->bind();
00305       }
00306    }
00307 }
00308 
00309 void TexCube::releaseDef()
00310 {
00311    /// Release the pbuffers for each face
00312    if( m_pbuff[0] && tryToBindPbuff() ) {
00313       for(uint i=0; i < m_pbuff.size(); i++) {
00314          m_pbuff[i]->release();
00315       }
00316    }
00317 
00318    /// Release the texture object
00319    CoreTex::releaseDef();
00320 }
00321 
00322 

Send questions, comments, and bug reports to:
jmk