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 2003-2005 Serious Hack Inc.
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., 51 Franklin Street, Fifth Floor, Boston, 
00018 // MA  02110-1301, USA
00020 #ifndef SHUTIL_KERNELLIBIMPL_HPP 
00021 #define SHUTIL_KERNELLIBIMPL_HPP 
00022 
00023 #include <sstream>
00024 #include "ShSyntax.hpp"
00025 #include "ShPosition.hpp"
00026 #include "ShManipulator.hpp"
00027 #include "ShAlgebra.hpp"
00028 #include "ShProgram.hpp"
00029 #include "ShNibbles.hpp"
00030 #include "ShKernelLib.hpp"
00031 #include "ShFunc.hpp"
00032 #include "ShTexCoord.hpp"
00033 #include "ShVector.hpp"
00034 #include "ShPoint.hpp"
00035 #include "ShPosition.hpp"
00036 #include "ShNormal.hpp"
00037 
00042 namespace ShUtil {
00043 
00044 using namespace SH;
00045 
00046 template<int N, ShBindingType Binding, typename T>
00047 ShProgram ShKernelLib::shVsh(const ShMatrix<N, N, Binding, T> &mv,
00048                              const ShMatrix<N, N, Binding, T> &mvp,
00049                              int numTangents, int numLights)
00050 {
00051   int i;
00052   ShProgram generalVsh = SH_BEGIN_VERTEX_PROGRAM {
00053     // INPUTS
00054     ShInputTexCoord2f SH_NAMEDECL(u, "texcoord");  
00055     ShInputNormal3f SH_NAMEDECL(nm, "normal");     
00056     ShVector3f tgt; 
00057     ShVector3f tgt2; 
00058     if(numTangents > 0) {
00059       ShInputVector3f SH_NAMEDECL(inTangent, "tangent");
00060       tgt = inTangent;
00061       if( numTangents > 1) {
00062         ShInputVector3f SH_NAMEDECL(inTangent2, "tangent2");
00063         tgt2 = inTangent2;
00064       }  else {
00065         tgt2 = cross(nm, tgt);
00066       }
00067     }
00068     ShInputPoint3f* lpv = new ShInputPoint3f[numLights];                 
00069     for(i = 0; i < numLights; ++i) lpv[i].name(makeName("lightPos", i));
00070     ShInputPosition4f SH_NAMEDECL(pm, "posm");     
00071 
00072     // OUTPUTS
00073     ShOutputTexCoord2f SH_NAMEDECL(uo, "texcoord");  
00074     ShOutputPoint3f SH_NAMEDECL(pv, "posv");         
00075     ShOutputPoint4f SH_NAMEDECL(pmo, "posm");
00076     
00077     // VCS outputs
00078     ShOutputNormal3f SH_NAMEDECL(nv, "normal");      
00079     ShOutputVector3f SH_NAMEDECL(tv, "tangent");
00080     ShOutputVector3f SH_NAMEDECL(tv2, "tangent2");
00081     ShOutputVector3f SH_NAMEDECL(vv, "viewVec");     
00082     ShOutputVector3f* hv = new ShOutputVector3f[numLights];    
00083     for(i = 0; i < numLights; ++i) hv[i].name(makeName("halfVec", i).c_str());
00084     ShOutputVector3f* lv = new ShOutputVector3f[numLights];    
00085     for(i = 0; i < numLights; ++i) lv[i].name(makeName("lightVec", i).c_str()); 
00086 
00087     ShOutputPoint3f* lpo = new ShOutputPoint3f[numLights];    
00088     for(i = 0; i < numLights; ++i) lpo[i].name(makeName("lightPos", i).c_str()); 
00089 
00090     // TCS outputs
00091     ShOutputNormal3f SH_NAMEDECL(nvt, "normalt");      
00092     ShOutputVector3f SH_NAMEDECL(vvt, "viewVect");     
00093     ShOutputVector3f* hvt = new ShOutputVector3f[numLights];    
00094     for(i = 0; i < numLights; ++i) hvt[i].name(makeName("halfVect", i).c_str());
00095     ShOutputVector3f* lvt = new ShOutputVector3f[numLights];    
00096     for(i = 0; i < numLights; ++i) lvt[i].name(makeName("lightVect", i).c_str()); 
00097 
00098     ShOutputPosition4f SH_NAMEDECL(pd, "posh");      
00099 
00100     uo = u;
00101     pv = (mv | pm)(0,1,2); 
00102     pmo = pm;
00103 
00104     // VCS outputs
00105     nv = normalize(mv | nm); 
00106     vv = normalize(-pv);
00107     for(i = 0; i < numLights; ++i) {
00108       lv[i] = normalize(lpv[i] - pv); 
00109       hv[i] = normalize(vv + lv[i]); 
00110       lpo[i] = lpv[i];
00111     }
00112 
00113     // TCS outputs
00114     tv = mv | tgt;
00115     tv2 = mv | tgt2;
00116     nvt = normalize(changeBasis(nv, tv, tv2, nv));
00117     vvt = normalize(changeBasis(nv, tv, tv2, vv));
00118     for(i = 0; i < numLights; ++i) {
00119       hvt[i] = normalize(changeBasis(nv, tv, tv2, hv[i]));
00120       lvt[i] = normalize(changeBasis(nv, tv, tv2, lv[i])); 
00121     }
00122 
00123     pd = mvp | pm;
00124 
00125     delete [] lvt;
00126     delete [] hvt;
00127     delete [] lpo;
00128     delete [] lv;
00129     delete [] hv;
00130     delete [] lpv;
00131   } SH_END;
00132   return generalVsh;
00133 }
00134 
00135 }
00136 
00137 #endif

Generated on Thu Jul 28 17:33:02 2005 for Sh by  doxygen 1.4.3-20050530