00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include <texCoordGen/texConst.h>
00021 #include <GL/glew.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 static const uint f_NUM_COORDS = 4;
00031 static uint f_COORD_NUM[9] = { 0, 0, 1, 0, 2, 0, 0, 0, 3 };
00032 static uint f_COORD_NAME[f_NUM_COORDS] = { 1, 2, 4, 8 };
00033
00034 TexConst::TexConst() : m_val(arrayo1f()), m_texUnit(0), m_coords(GLIFT_NONE), m_size(0), m_maxCoordDimen(0)
00035 {}
00036
00037 TexConst::TexConst( float val, uint texUnit, uint coords )
00038 : m_val(arrayo1f(1,val)), m_texUnit(texUnit),
00039 m_coords(coords), m_size( numOnesInWord(coords) ),
00040 m_maxCoordDimen( maxCoordDimen(coords) )
00041 {
00042 if( m_size != 1 ) {
00043 err() << "TexConst(...) Error:\n"
00044 << "\tScalar constant constructor must specify exactly 1 texture coordinate destination.\n";
00045 exit(1);
00046 }
00047
00048 checkCoords();
00049 }
00050
00051 TexConst::TexConst( const vec2f& val, uint texUnit, uint coords )
00052 : m_val(arrayo1f(2,val.v())), m_texUnit(texUnit), m_coords(coords),
00053 m_size( numOnesInWord(coords) ), m_maxCoordDimen( maxCoordDimen(coords) )
00054 {
00055 if( m_size != 2 ) {
00056 err() << "TexConst(...) Error:\n"
00057 << "\tvec2f constant constructor must specify exactly 2 texture coordinate destinations.\n";
00058 exit(1);
00059 }
00060
00061 checkCoords();
00062 }
00063
00064 TexConst::TexConst( const vec3f& val, uint texUnit, uint coords )
00065 : m_val(arrayo1f(3,val.v())), m_texUnit(texUnit), m_coords(coords),
00066 m_size( numOnesInWord(coords) ), m_maxCoordDimen( maxCoordDimen(coords) )
00067 {
00068 if( m_size != 3 ) {
00069 err() << "TexConst(...) Error:\n"
00070 << "\tvec3f constant constructor must specify exactly 3 texture coordinate destinations.\n";
00071 exit(1);
00072 }
00073
00074 checkCoords();
00075 }
00076
00077
00078 vec4<bool> TexConst::usedTexCoords() const
00079 {
00080 vec4<bool> usedCoords(false);
00081
00082 for(int c=0; c < f_NUM_COORDS; c++) {
00083 usedCoords[c] = (m_coords & f_COORD_NAME[c]) > 0;
00084 }
00085
00086 return usedCoords;
00087 }
00088
00089
00090
00091 uint TexConst::maxCoordDimen( uint coords )
00092 {
00093 uint maxCoord=0;
00094 if( GLIFT_Q_BIT & coords ) {
00095 maxCoord = f_COORD_NUM[ GLIFT_Q_BIT ];
00096 }
00097 else if( GLIFT_R_BIT & coords ) {
00098 maxCoord = f_COORD_NUM[ GLIFT_R_BIT ];
00099 }
00100 else if( GLIFT_T_BIT & coords ) {
00101 maxCoord = f_COORD_NUM[ GLIFT_T_BIT ];
00102 }
00103 else if( GLIFT_S_BIT & coords ) {
00104 maxCoord = f_COORD_NUM[ GLIFT_S_BIT ];
00105 }
00106
00107 return maxCoord + 1;
00108 }
00109
00110 void TexConst::checkCoords()
00111 {
00112 if( ((GLIFT_S_BIT & m_coords) && (f_COORD_NUM[GLIFT_S_BIT] > (uint)getLimitsGL().maxTexCoords)) ||
00113 ((GLIFT_T_BIT & m_coords) && (f_COORD_NUM[GLIFT_T_BIT] > (uint)getLimitsGL().maxTexCoords)) ||
00114 ((GLIFT_R_BIT & m_coords) && (f_COORD_NUM[GLIFT_R_BIT] > (uint)getLimitsGL().maxTexCoords)) ||
00115 ((GLIFT_Q_BIT & m_coords) && (f_COORD_NUM[GLIFT_Q_BIT] > (uint)getLimitsGL().maxTexCoords)) ) {
00116 err() << "checkCoords() Error:\n"
00117 << "\tOnly " << getLimitsGL().maxTexCoords << " are supported on this card.\n";
00118 exit(1);
00119 }
00120 }
00121
00122 arrayo2f TexConst::operator()( const arrayw2f& rawTexCoord, const vec3f& primScale )
00123 {
00124 int numVerts = rawTexCoord.dim(0);
00125 int coordDimen = max( (int)m_maxCoordDimen, rawTexCoord.dim(1) );
00126 arrayo2f texCoords( numVerts, coordDimen, (float)0 );
00127
00128 if( m_coords==GLIFT_NONE ) {
00129 texCoords = rawTexCoord;
00130 }
00131 else {
00132 for(int v=0; v < numVerts; v++) {
00133 int index=0;
00134 for(int c=0; c < coordDimen; c++) {
00135 if( f_COORD_NAME[c] & m_coords ) {
00136 texCoords[v][c] = m_val[index];
00137 index++;
00138 }
00139 else {
00140 texCoords[v][c] = rawTexCoord[v][c];
00141 }
00142 }
00143 }
00144 }
00145
00146 return texCoords;
00147 }
00148
00149
00150