00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include <texture/subTex.h>
00021 #include <texture/texNd.h>
00022 #include <util/gliftUtil.h>
00023 #include <iostream>
00024
00025 using namespace std;
00026 using namespace gutz;
00027
00028 using namespace glift;
00029
00030 #ifndef NDEBUG
00031
00032 #endif
00033
00034 SubTex::SubTex( SingleTex* tex, GLenum channel )
00035 : m_createdTex(false)
00036 {
00037 SubPBuff* subPBuff = dynamic_cast<SubPBuff*>(tex->pbuff());
00038
00039
00040 vec3i origin = subPBuff ? (vec3i)subPBuff->origin() : vec3i(0,0);
00041 vec3i dimen = subPBuff ? (vec3i)subPBuff->subDimen() : tex->dimen();
00042
00043 initMembers( tex, origin, dimen, channel );
00044 }
00045
00046 SubTex::SubTex( const MultiTexOState& texState, SubPBuff* subPBuff, GLenum channel )
00047 : m_createdTex(true)
00048 {
00049 SingleTex* tex = new Tex2D(texState, subPBuff);
00050 vec3i origin = (vec3i)subPBuff->origin();
00051 vec3i dimen = (vec3i)subPBuff->subDimen();
00052
00053 initMembers( tex, origin, dimen, channel );
00054 }
00055
00056 SubTex::SubTex( SingleTex* tex, int origin, int dimen, GLenum channel )
00057 : m_createdTex(false)
00058 {
00059 initMembers( tex, vec3i(origin), vec3i(dimen), channel );
00060 }
00061
00062 SubTex::SubTex( SingleTex* tex, const vec2i& origin,
00063 const vec2i& dimen, GLenum channel )
00064 : m_createdTex(false)
00065 {
00066 initMembers( tex, vec3i(origin), vec3i(dimen), channel );
00067 }
00068
00069 SubTex::SubTex( SingleTex* tex, const vec3i& origin,
00070 const vec3i& dimen, GLenum channel )
00071 : m_createdTex(false)
00072 {
00073 initMembers( tex, vec3i(origin), vec3i(dimen), channel );
00074 }
00075
00076 void SubTex::initMembers( SingleTex* tex, const vec3i& origin,
00077 const vec3i& dimen, GLenum channel )
00078 {
00079 m_tex = tex;
00080 m_origin = origin;
00081 m_dimen = dimen;
00082 m_channel = channel;
00083
00084
00085 vec3f fullTexDimen= vec3f( (float)tex->dimen().x, (float)tex->dimen().y, (float)tex->dimen().z);
00086 vec3f subTexDimen = vec3f( (float)dimen.x, (float)dimen.y, (float)dimen.z);
00087 vec3f orig = vec3f( (float)origin.x, (float)origin.y, (float)origin.z);
00088
00089 m_scale.x = (tex->dimen().x != 0) ? subTexDimen.x / fullTexDimen.x : 1.0f;
00090 m_scale.y = (tex->dimen().y != 0) ? subTexDimen.y / fullTexDimen.y : 1.0f;
00091 m_scale.z = (tex->dimen().z != 0) ? subTexDimen.z / fullTexDimen.z : 1.0f;
00092
00093 m_bias.x = (tex->dimen().x != 0) ? orig.x / fullTexDimen.x : 0.0f;
00094 m_bias.y = (tex->dimen().y != 0) ? orig.y / fullTexDimen.y : 0.0f;
00095 m_bias.z = (tex->dimen().z != 0) ? orig.z / fullTexDimen.z : 0.0f;
00096
00097
00098 if( m_channel != GL_NONE &&
00099 m_channel != GL_RED &&
00100 m_channel != GL_GREEN &&
00101 m_channel != GL_BLUE &&
00102 m_channel != GL_ALPHA &&
00103 m_channel != GL_RGB &&
00104 m_channel != GL_RGBA )
00105 {
00106 err() << "initMembers(...) Error:\n"
00107 << "\tThe 'channel' given is not supported.\n\n";
00108 exit(1);
00109 }
00110 }
00111
00112 SubTex::~SubTex()
00113 {
00114 if( m_createdTex ) {
00115 delete m_tex;
00116 }
00117 }
00118
00119 void SubTex::getDataub( GLenum format, int mipLevel, arraybub& data )
00120 {
00121 m_tex->getDataub(format, mipLevel, data);
00122 }
00123
00124 void SubTex::getDataf( GLenum format, int mipLevel, arraybf& data )
00125 {
00126 m_tex->getDataf(format, mipLevel, data);
00127 }
00128
00129
00130
00131 void SubTex::adjustTexCoord( gutz::arrayw2f& texCoord )
00132 {
00133
00134 int maxCoord = 0;
00135 switch( m_tex->texType() ) {
00136 case GL_TEXTURE_1D: maxCoord = 1; break;
00137 case GL_TEXTURE_2D:
00138 case GL_TEXTURE_CUBE_MAP_ARB: maxCoord = 2; break;
00139 case GL_TEXTURE_3D: maxCoord = 3; break;
00140 }//No default necessary b/c SingleTex's ctor checked for validity already
00141
00142 vec3f fullTexDimen= vec3f(m_tex->dimen().x, m_tex->dimen().y, m_tex->dimen().z);
00143 vec3f subTexDimen = vec3f(m_dimen.x, m_dimen.y, m_dimen.z);
00144 vec3f orig = vec3f(m_origin.x, m_origin.y, m_origin.z);
00145
00146 vec3f scale;
00147 scale.x = (m_tex->dimen().x != 0) ? subTexDimen.x / fullTexDimen.x : 1.0f;
00148 scale.y = (m_tex->dimen().y != 0) ? subTexDimen.y / fullTexDimen.y : 1.0f;
00149 scale.z = (m_tex->dimen().z != 0) ? subTexDimen.z / fullTexDimen.z : 1.0f;
00150
00151 vec3f bias;
00152 bias.x = (m_tex->dimen().x != 0) ? orig.x / fullTexDimen.x : 0.0f;
00153 bias.y = (m_tex->dimen().y != 0) ? orig.y / fullTexDimen.y : 0.0f;
00154 bias.z = (m_tex->dimen().z != 0) ? orig.z / fullTexDimen.z : 0.0f;
00155
00156 for(int v=0; v < texCoord.dim(0); v++) {
00157 for(int c=0; c < maxCoord; c++) {
00158 texCoord[v][c] = (texCoord[v][c] * scale[c]) + bias[c];
00159 }
00160 }
00161 }
00162 */
00163 void SubTex::bindDef()
00164 {
00165 m_tex->bind();
00166
00167
00168
00169 glMatrixMode(GL_TEXTURE);
00170 glPushMatrix();
00171 glTranslatef( m_bias.x, m_bias.y, m_bias.z );
00172 glScalef( m_scale.x, m_scale.y, m_scale.z );
00173 glMatrixMode(GL_MODELVIEW);
00174
00175 #ifndef NDEBUG
00176 #ifdef SPEW
00177 err() << "bindDef() Info: bias = (" << m_bias.x << ", " << m_bias.y << ", " << m_bias.z
00178 << "), scale = (" << m_scale.x << ", " << m_scale.y << ", " << m_scale.z << ")\n";
00179 #endif
00180 #endif
00181
00182
00183 }
00184
00185 void SubTex::releaseDef()
00186 {
00187
00188
00189
00190 glMatrixMode(GL_TEXTURE);
00191 glPopMatrix();
00192 glMatrixMode(GL_MODELVIEW);
00193
00194 m_tex->release();
00195 }
00196
00197