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