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

texCoordPerturb.cpp

Go to the documentation of this file.
00001 /////////////////////////////////////////////////////////////////////
00002 // 6/26/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/texCoordPerturb.h>
00021 #include <iostream>
00022 
00023 using namespace std;
00024 using namespace gutz;
00025 
00026 using namespace glift;
00027 
00028 /*TexCoordPerturb::TexCoordPerturb( uint numTexUnits, const Vecarrayo1f& bias, const VecTexConst& texConst )
00029 {
00030 checkTexUnitMin( numTexUnits );
00031 
00032 /// Create transforms from bias vectors.
00033 VecTexTrans texTransform( bias.size() );
00034 for(uint i=0; i < texTransform.size(); i++) {
00035 texTransform[i] = TexTrans( bias[i] );
00036 }
00037 
00038 /// Initialize the perturbations
00039 initTexTrans( numTexUnits, texTransform );
00040 initTexConst( numTexUnits, texConst );
00041 }
00042 */
00043 TexCoordPerturb::TexCoordPerturb( uint numTexUnits, const VecTexConst& texConst )
00044 {
00045    checkTexUnitMin( numTexUnits );
00046 
00047    /// Initialize the perturbations
00048    initTexConst( numTexUnits, texConst );
00049 
00050    //Identity transform for all texture units.
00051    m_texTrans = VecVecTexTrans(numTexUnits); 
00052    for(uint i=0; i < m_texTrans.size(); i++) {
00053       m_texTrans[i] = VecTexTrans(1,TexTrans());
00054    }
00055 }
00056 
00057 TexCoordPerturb::TexCoordPerturb( uint numTexUnits, const VecTexTrans& texTransform, const VecTexConst& texConst )
00058 {
00059    checkTexUnitMin( numTexUnits );
00060 
00061    /// Initialize the perturbations
00062    initTexTrans( numTexUnits, texTransform );
00063    initTexConst( numTexUnits, texConst );
00064 }
00065 
00066 void TexCoordPerturb::initTexConst( uint numTexUnits, const VecTexConst& texConst )
00067 {
00068    m_texConst = VecVecTexConst( numTexUnits );   /// Create "no-op" texture constants for all texture units
00069    arrayo1ui usedCoords(numTexUnits,(uint)0); /// Track which coords have been set for each texture unit
00070 
00071    /// Assign constants to the requested texture units 
00072    for(uint i=0; i < texConst.size(); i++) {
00073       uint t = texConst[i].texUnit();
00074       uint c = texConst[i].coords();            
00075 
00076       /// Ensure that 't' is in range.
00077       checkTexUnit( "initTexConst(...)", "texConst", t, i, numTexUnits );
00078 
00079       /// Set the constant for this texture unit unless one has already been set
00080       /// - Also ensure that the texture coordinates used do not overlap
00081       if( (usedCoords[t] & c)==0 ) {
00082          m_texConst[t].push_back( texConst[i] );
00083          usedCoords[t] |= c;
00084       }
00085       else {
00086          err() << "initTexConst(...) Error:\n"
00087             << "\tTexture unit " << t << " has conflicting constants assigned to it.\n";
00088          exit(1);
00089       }
00090    }
00091 }
00092 
00093 void TexCoordPerturb::initTexTrans( uint numTexUnits , const VecTexTrans& texTransform )
00094 {
00095    m_texTrans = VecVecTexTrans( numTexUnits ); /// Create Identity transforms for all texture units
00096    VecBool hasConst(numTexUnits, false);         /// Track which texUnits have coord transforms to prevent multiples.
00097 
00098    for(uint i=0; i < texTransform.size(); i++) {
00099       uint t = texTransform[i].texUnit();
00100 
00101       /// Ensure that 't' is in range.
00102       checkTexUnit( "initTexTrans(...)", "texTransform", t, i, numTexUnits );
00103 
00104       /// Set the transform for this unit unless one has already been set.
00105       if( hasConst[t] == false ) {
00106          m_texTrans[t].push_back( texTransform[i] );
00107          hasConst[t]   = true;
00108       }
00109       else {
00110          err() << "initTexTrans(...) Error:\n"
00111             << "\tTexture unit " << t << " cannot have more than one coordinate transform assigned to it.\n";
00112          exit(1);
00113       }
00114    }
00115 }
00116 
00117 void TexCoordPerturb::checkTexUnitMin( uint numTexUnits )
00118 {
00119    if( numTexUnits < 1 ) {
00120       err() << "checkTexUnitMin(...) Error:\n"
00121          << "\tnumTexUnits must be > 1.\n";
00122       exit(1);
00123    }
00124 }
00125 
00126 /// TODO: These 'char*'s should become std::strings when everything is moved over to std::
00127 void TexCoordPerturb::checkTexUnit( const char* callingFuncName, const char* inputVecName,
00128                                    uint texUnit, uint vecIndex, uint maxTexUnits )
00129 {
00130    if( texUnit >= maxTexUnits ) {
00131       err() << callingFuncName << " Error:\n"
00132          << "\t" << inputVecName << "[" << vecIndex << "] specifies a texture unit (" << texUnit
00133          << ") that is larger than\n" << "\t TexCoordPerturb::numTexUnits (" 
00134          << maxTexUnits << ").\n";
00135       exit(1);
00136    }
00137 }
00138 
00139 /// Generate appropriate texture coordinates, taking into account 
00140 /// multiTexturing, texCoordTransforms, and texture contants
00141 MultiTexCoord* TexCoordPerturb::genTexCoords( const arrayw2f& rawTexCoord, const vec3f& primScale )
00142 {
00143    uint numTexUnits       = m_texConst.size(); /// Constructor ensures texConst.size() == texTrans.size()
00144    int  numVerts                  = rawTexCoord.dim(0);
00145    int  rawTexCoordDimen = rawTexCoord.dim(1);
00146 
00147    /// Create return data...
00148    MultiTexCoord* texCoordPtr = new MultiTexCoord( numTexUnits );
00149    MultiTexCoord& texCoords   = *texCoordPtr;
00150 
00151    /// Generate texture coords for each texture unit.
00152    /// - There are up to "n" texTrans and "m" texConsts for each texUnit
00153    for(uint t=0; t < numTexUnits; t++) {
00154       bool initialized = false;
00155 
00156       for(uint n=0; n < m_texTrans[t].size(); n++) {
00157          if(!initialized) {
00158             texCoords[t] = m_texTrans[t][n]( rawTexCoord,  primScale ); /// Apply transform
00159             initialized = true;
00160          }
00161          else {
00162             texCoords[t] = m_texTrans[t][n]( texCoords[t],  primScale );/// Apply transform
00163          }
00164       }
00165 
00166       for(uint m=0; m < m_texConst[t].size(); m++) {
00167          if(!initialized) {
00168             texCoords[t] = m_texConst[t][m]( rawTexCoord, primScale );  /// Apply constant
00169             initialized = true;
00170          }
00171          else {
00172             texCoords[t] = m_texConst[t][m]( texCoords[t], primScale ); /// Apply constant
00173          }
00174       }
00175    }
00176 
00177    return texCoordPtr;
00178 }
00179 
00180 

Send questions, comments, and bug reports to:
jmk