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

texConst.cpp

Go to the documentation of this file.
00001 /////////////////////////////////////////////////////////////////////
00002 // 6/28/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 <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 }; /// Mapping from [GLIFT_S, GLIFTQ] to [0, 3]
00032 static uint f_COORD_NAME[f_NUM_COORDS] = { 1, 2, 4, 8 };                                /// Mapping from [0, 3] to [GLIFT_S, GLIFT_Q]
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 // Return array of bools to show which texCoords are used by const
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 /// Find the maximum coordinate index that is being used
00090 /// TODO: There is a MUCH smarter way to do this!!
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 {       //Set the constants where requested.
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 

Send questions, comments, and bug reports to:
jmk