00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include <texture/singleTex.h>
00021
00022 #include <iostream>
00023 #include <GL/glUtil.h>
00024 #include <util/gliftUtil.h>
00025
00026 using namespace std;
00027 using namespace gutz;
00028
00029 using namespace glift;
00030
00031 CoreTex::CoreTex( GLenum texType, const MultiTexOState& texState, TexData* texData, PBuffGlift* pbuff)
00032 : m_texType(texType),
00033
00034 m_pbuff(pbuff),
00035 m_texNum(0),
00036 m_tryToBindPbuff(true),
00037 m_pbuffBound(false),
00038 m_dataDimen( m_pbuff ? m_pbuff->dimen() : vec2i(0)),
00039 m_tcNum(0)
00040 {
00041 checkTexType( m_texType );
00042
00043
00044 glErr(estr(),"CoreTex() 1");
00045 glGenTextures(1, &m_texNum);
00046 glErr(estr(),"CoreTex","CoreTex() 2");
00047 if( m_texNum==0 ) {
00048 err() << "CoreTexture(...) Error:\n\tglGenTextures(...) failed.\n";
00049 exit(1);
00050 }
00051
00052
00053 setTexState( texState );
00054
00055 if( texData ) {
00056 bindDef();
00057 setTexData( texData, 0 );
00058 releaseDef();
00059 }
00060
00061 glErr(estr(),"CoreTex","CoreTex");
00062 }
00063
00064 CoreTex::~CoreTex()
00065 {
00066 if( m_texNum > 0 ) {
00067 glDeleteTextures( 1, &m_texNum );
00068 }
00069 m_texNum = 0;
00070 }
00071
00072 void CoreTex::checkTexType( GLenum texType )
00073 {
00074 if( texType != GL_TEXTURE_1D &&
00075 texType != GL_TEXTURE_2D &&
00076 texType != GL_TEXTURE_3D &&
00077 texType != GL_TEXTURE_CUBE_MAP_ARB &&
00078 texType != GL_TEXTURE_RECTANGLE_NV &&
00079 texType != GL_TEXTURE_RECTANGLE_EXT ) {
00080 err() << "checkTexType(...) Error:\n"
00081 << "\ttexType is not valid.\n";
00082 exit(1);
00083 }
00084 }
00085
00086 void CoreTex::bindDef()
00087 {
00088 assert( m_texNum != 0 );
00089
00090 if(m_texType!=GL_TEXTURE_RECTANGLE_NV) {
00091 glEnable(m_texType);
00092 }
00093 glErr(estr(),"CoreTex","bindDef 1");
00094 glBindTexture( m_texType, m_texNum );
00095 glErr(estr(),"CoreTex","bindDef 2");
00096
00097 if( m_pbuff && m_tryToBindPbuff ) {
00098 m_pbuff->bind();
00099 m_pbuffBound = true;
00100 }
00101 }
00102
00103 void CoreTex::releaseDef()
00104 {
00105 if( m_pbuff && m_pbuffBound ) {
00106 m_pbuff->release();
00107 m_pbuffBound = false;
00108 }
00109
00110 glBindTexture( m_texType, 0);
00111 if(m_texType!=GL_TEXTURE_RECTANGLE_NV) {
00112 glDisable(m_texType);
00113 }
00114 }
00115
00116
00117 void CoreTex::getDataub( GLenum format, int mipLevel, arraybub& data )
00118 {
00119 vec3i dim = m_dataDimen;
00120 int numChannels = getNumChannels( format );
00121
00122
00123 if( m_texType == GL_TEXTURE_1D ) {
00124 assert( data.dim(0) == dim.x );
00125 assert( data.dim(1) == numChannels );
00126 }
00127 else if( m_texType == GL_TEXTURE_2D ) {
00128 assert( data.dim(0) == dim.y );
00129 assert( data.dim(1) == dim.x );
00130 assert( data.dim(2) == numChannels );
00131 }
00132 else if( m_texType == GL_TEXTURE_3D ) {
00133 assert( data.dim(0) == dim.z );
00134 assert( data.dim(1) == dim.y );
00135 assert( data.dim(2) == dim.x );
00136 assert( data.dim(3) == numChannels );
00137 }
00138 else {
00139 err() << "getDataub(...) Error:\n\t"
00140 << "Only 1D, 2D, and 3D textures are supported.\n\t"
00141 << "TexCube must define its own getData*(...) functions.\n";
00142 exit(1);
00143 }
00144
00145
00146 if( m_pbuff == NULL ) {
00147 bindDef();
00148 glGetTexImage( m_texType, mipLevel, format, GL_UNSIGNED_BYTE, data.data() );
00149 releaseDef();
00150 }
00151 else {
00152
00153
00154
00155
00156
00157 m_pbuff->release();
00158 m_pbuff->enable(true);
00159 glReadPixels(0, 0, dim.x, dim.y, format, GL_UNSIGNED_BYTE, data.data() );
00160 m_pbuff->disable();
00161 }
00162 }
00163
00164
00165 void CoreTex::getDataf( GLenum format, int mipLevel, arraybf& data )
00166 {
00167 vec3i dim = m_dataDimen;
00168 int numChannels = getNumChannels( format );
00169
00170
00171 if( m_texType == GL_TEXTURE_1D ) {
00172 assert( data.dim(0) == dim.x );
00173 assert( data.dim(1) == numChannels );
00174 }
00175 else if( m_texType == GL_TEXTURE_2D ) {
00176 assert( data.dim(0) == dim.y );
00177 assert( data.dim(1) == dim.x );
00178 assert( data.dim(2) == numChannels );
00179 }
00180 else if( m_texType == GL_TEXTURE_3D ) {
00181 assert( data.dim(0) == dim.z );
00182 assert( data.dim(1) == dim.y );
00183 assert( data.dim(2) == dim.x );
00184 assert( data.dim(3) == numChannels );
00185 }
00186 else {
00187 err() << "CoreTex::getDataf(...) Error:\n\t"
00188 << "Only 1D, 2D, and 3D textures are supported.\n\t"
00189 << "TexCube must define its own getData*(...) functions.\n";
00190 exit(1);
00191 }
00192
00193
00194 if( m_pbuff == NULL ) {
00195 bindDef();
00196 glGetTexImage( m_texType, mipLevel, format, GL_FLOAT, data.data() );
00197 releaseDef();
00198 }
00199 else {
00200 m_pbuff->enable(true);
00201 glReadPixels(0, 0, dim.x, dim.y, format, GL_FLOAT, data.data() );
00202 m_pbuff->disable();
00203 }
00204
00205 }
00206
00207
00208
00209 void CoreTex::setTexState( TexObjState* state )
00210 {
00211
00212 bindDef();
00213 state->bind( m_texType );
00214 releaseDef();
00215 }
00216
00217
00218 void CoreTex::setTexState( const MultiTexOState& texState )
00219 {
00220
00221
00222 bindDef();
00223 texState.bind( m_texType );
00224 releaseDef();
00225 }
00226
00227
00228
00229 void CoreTex::setTexData( TexData* texData, int mipLevel, bool setTexSize)
00230 {
00231 checkTexType( m_texType, texData->texType() );
00232
00233 if( setTexSize ) {
00234 m_dataDimen = texData->dimen();
00235 }
00236 bindDef();
00237 texData->setTexData( m_texType, mipLevel );
00238 releaseDef();
00239 }
00240
00241
00242 void CoreTex::checkTexType( GLenum objTexType, GLenum dataTexType )
00243 {
00244 if( objTexType == dataTexType )
00245 {
00246 return;
00247 }
00248 else {
00249 err() << "checkTexType(...) Error:\n\tIncompatible texture object and data texture target types.\n";
00250 exit(1);
00251 }
00252 }
00253
00254
00255