Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

ShKernelLibImpl.hpp

Go to the documentation of this file.
00001 // Sh: A GPU metaprogramming language.
00002 //
00003 // Copyright (c) 2003 University of Waterloo Computer Graphics Laboratory
00004 // Project administrator: Michael D. McCool
00005 // Authors: Zheng Qin, Stefanus Du Toit, Kevin Moule, Tiberiu S. Popa,
00006 //          Bryan Chan, Michael D. McCool
00007 // 
00008 // This software is provided 'as-is', without any express or implied
00009 // warranty. In no event will the authors be held liable for any damages
00010 // arising from the use of this software.
00011 // 
00012 // Permission is granted to anyone to use this software for any purpose,
00013 // including commercial applications, and to alter it and redistribute it
00014 // freely, subject to the following restrictions:
00015 // 
00016 // 1. The origin of this software must not be misrepresented; you must
00017 // not claim that you wrote the original software. If you use this
00018 // software in a product, an acknowledgment in the product documentation
00019 // would be appreciated but is not required.
00020 // 
00021 // 2. Altered source versions must be plainly marked as such, and must
00022 // not be misrepresented as being the original software.
00023 // 
00024 // 3. This notice may not be removed or altered from any source
00025 // distribution.
00027 #ifndef SHUTIL_KERNELLIBIMPL_HPP 
00028 #define SHUTIL_KERNELLIBIMPL_HPP 
00029 
00030 #include <sstream>
00031 #include "ShSyntax.hpp"
00032 #include "ShPosition.hpp"
00033 #include "ShManipulator.hpp"
00034 #include "ShAlgebra.hpp"
00035 #include "ShProgram.hpp"
00036 #include "ShNibbles.hpp"
00037 #include "ShKernelLib.hpp"
00038 #include "ShFunc.hpp"
00039 #include "ShTexCoord.hpp"
00040 #include "ShVector.hpp"
00041 #include "ShPoint.hpp"
00042 #include "ShPosition.hpp"
00043 #include "ShNormal.hpp"
00044 
00049 namespace ShUtil {
00050 
00051 using namespace SH;
00052 
00053 template<int N, ShBindingType Binding, typename T>
00054 ShProgram ShKernelLib::shVsh(const ShMatrix<N, N, Binding, T> &mv,
00055                              const ShMatrix<N, N, Binding, T> &mvp,
00056                              int numTangents, int numLights)
00057 {
00058   int i;
00059   ShProgram generalVsh = SH_BEGIN_VERTEX_PROGRAM {
00060     // INPUTS
00061     ShInputTexCoord2f SH_NAMEDECL(u, "texcoord");  
00062     ShInputNormal3f SH_NAMEDECL(nm, "normal");     
00063     ShVector3f tgt; 
00064     ShVector3f tgt2; 
00065     if(numTangents > 0) {
00066       ShInputVector3f SH_NAMEDECL(inTangent, "tangent");
00067       tgt = inTangent;
00068       if( numTangents > 1) {
00069         ShInputVector3f SH_NAMEDECL(inTangent2, "tangent2");
00070         tgt2 = inTangent2;
00071       }  else {
00072         tgt2 = cross(nm, tgt);
00073       }
00074     }
00075     ShInputPoint3f* lpv = new ShInputPoint3f[numLights];                 
00076     for(i = 0; i < numLights; ++i) lpv[i].name(makeName("lightPos", i));
00077     ShInputPosition4f SH_NAMEDECL(pm, "posm");     
00078 
00079     // OUTPUTS
00080     ShOutputTexCoord2f SH_NAMEDECL(uo, "texcoord");  
00081     ShOutputPoint3f SH_NAMEDECL(pv, "posv");         
00082     ShOutputPoint4f SH_NAMEDECL(pmo, "posm");
00083     
00084     // VCS outputs
00085     ShOutputNormal3f SH_NAMEDECL(nv, "normal");      
00086     ShOutputVector3f SH_NAMEDECL(tv, "tangent");
00087     ShOutputVector3f SH_NAMEDECL(tv2, "tangent2");
00088     ShOutputVector3f SH_NAMEDECL(vv, "viewVec");     
00089     ShOutputVector3f* hv = new ShOutputVector3f[numLights];    
00090     for(i = 0; i < numLights; ++i) hv[i].name(makeName("halfVec", i).c_str());
00091     ShOutputVector3f* lv = new ShOutputVector3f[numLights];    
00092     for(i = 0; i < numLights; ++i) lv[i].name(makeName("lightVec", i).c_str()); 
00093 
00094     ShOutputPoint3f* lpo = new ShOutputPoint3f[numLights];    
00095     for(i = 0; i < numLights; ++i) lpo[i].name(makeName("lightPos", i).c_str()); 
00096 
00097     // TCS outputs
00098     ShOutputNormal3f SH_NAMEDECL(nvt, "normalt");      
00099     ShOutputVector3f SH_NAMEDECL(vvt, "viewVect");     
00100     ShOutputVector3f* hvt = new ShOutputVector3f[numLights];    
00101     for(i = 0; i < numLights; ++i) hvt[i].name(makeName("halfVect", i).c_str());
00102     ShOutputVector3f* lvt = new ShOutputVector3f[numLights];    
00103     for(i = 0; i < numLights; ++i) lvt[i].name(makeName("lightVect", i).c_str()); 
00104 
00105     ShOutputPosition4f SH_NAMEDECL(pd, "posh");      
00106 
00107     uo = u;
00108     pv = (mv | pm)(0,1,2); 
00109     pmo = pm;
00110 
00111     // VCS outputs
00112     nv = normalize(mv | nm); 
00113     vv = normalize(-pv);
00114     for(i = 0; i < numLights; ++i) {
00115       lv[i] = normalize(lpv[i] - pv); 
00116       hv[i] = normalize(vv + lv[i]); 
00117       lpo[i] = lpv[i];
00118     }
00119 
00120     // TCS outputs
00121     tv = mv | tgt;
00122     tv2 = mv | tgt2;
00123     nvt = normalize(changeBasis(nv, tv, tv2, nv));
00124     vvt = normalize(changeBasis(nv, tv, tv2, vv));
00125     for(i = 0; i < numLights; ++i) {
00126       hvt[i] = normalize(changeBasis(nv, tv, tv2, hv[i]));
00127       lvt[i] = normalize(changeBasis(nv, tv, tv2, lv[i])); 
00128     }
00129 
00130     pd = mvp | pm;
00131 
00132     delete [] lvt;
00133     delete [] hvt;
00134     delete [] lpo;
00135     delete [] lv;
00136     delete [] hv;
00137     delete [] lpv;
00138   } SH_END;
00139   return generalVsh;
00140 }
00141 
00142 }
00143 
00144 #endif

Generated on Mon Jan 24 18:36:32 2005 for Sh by  doxygen 1.4.1