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

ShLibGeometryImpl.hpp

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 // 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 SHLIBGEOMETRYIMPL_HPP 00028 #define SHLIBGEOMETRYIMPL_HPP 00029 00030 #include "ShLibClamp.hpp" 00031 #include "ShAttrib.hpp" 00032 #include "ShInstructions.hpp" 00033 00034 namespace SH { 00035 00036 template<typename T> 00037 inline 00038 ShGeneric<3, T> cross(const ShGeneric<3, T>& left, const ShGeneric<3, T>& right) 00039 { 00040 ShAttrib<3, SH_TEMP, T> t; 00041 shXPD(t, left, right); 00042 return t; 00043 } 00044 00045 template<typename T> 00046 inline 00047 ShGeneric<3, T> operator^(const ShGeneric<3, T>& left, const ShGeneric<3, T>& right) 00048 { 00049 return cross(left, right); 00050 } 00051 00052 template<int N, typename T> 00053 inline 00054 ShGeneric<N, T> normalize(const ShGeneric<N, T>& var) 00055 { 00056 ShAttrib<N, SH_TEMP, T> t; 00057 shNORM(t, var); 00058 return t; 00059 } 00060 00061 template<int N, typename T> 00062 inline 00063 ShGeneric<1, T> dot(const ShGeneric<N, T>& left, const ShGeneric<N, T>& right) 00064 { 00065 ShAttrib<1, SH_TEMP, T> t; 00066 shDOT(t, left, right); 00067 return t; 00068 } 00069 00070 template<int N, typename T> 00071 inline 00072 ShGeneric<1, T> operator|(const ShGeneric<N, T>& left, const ShGeneric<N, T>& right) 00073 { 00074 return dot(left, right); 00075 } 00076 00077 template<int N, typename T> 00078 inline 00079 ShGeneric<N, T> reflect(const ShGeneric<N, T>& a, const ShGeneric<N, T>& b) 00080 { 00081 ShGeneric<N, T> bn = normalize(b); 00082 return 2.0 * dot(a, b) * b - a; 00083 } 00084 00085 template<int N, typename T> 00086 ShGeneric<N, T> refract(const ShGeneric<N, T>& v, const ShGeneric<N, T>& n, 00087 const ShGeneric<1, T>& theta) 00088 { 00089 ShGeneric<N, T> vn = normalize(v); 00090 ShGeneric<N, T> nn = normalize(n); 00091 ShAttrib1f c = (vn|nn); 00092 ShAttrib1f k = c*c - 1.0f; 00093 k = 1.0f + theta*theta*k; 00094 k = clamp(k, 0.0f, 1.0f); 00095 ShAttrib1f a = theta; 00096 ShAttrib1f b = theta*c + sqrt(k); 00097 return (a*vn + b*nn); 00098 } 00099 00100 template<int N, typename T> 00101 inline 00102 ShGeneric<N, T> faceforward(const ShGeneric<N, T>& a, const ShGeneric<N, T>& b) 00103 { 00104 return (2.0 * (dot(a, b) > 0.0) - 1.0) * b; 00105 } 00106 00107 template<typename T> 00108 inline 00109 ShGeneric<4, T> lit(const ShGeneric<1, T>& a, 00110 const ShGeneric<1, T>& b, 00111 const ShGeneric<1, T>& c) 00112 { 00113 ShAttrib<4, SH_TEMP, T> r; 00114 r(0,3) = ShConstAttrib2f(1.0, 1.0); 00115 r(1) = pos(a); 00116 r(2) = (a < 0 && b < 0) * pow(b, c); 00117 return r; 00118 } 00119 00120 template<int N, typename T> 00121 ShGeneric<1, T> distance(const ShGeneric<N, T>& a, const ShGeneric<N, T>& b) 00122 { 00123 return length(a-b); 00124 } 00125 00126 template<int N, typename T> 00127 ShGeneric<1, T> distance_1(const ShGeneric<N, T>& a, const ShGeneric<N, T>& b) 00128 { 00129 return length_1(a-b); 00130 } 00131 00132 template<int N, typename T> 00133 ShGeneric<1, T> distance_inf(const ShGeneric<N, T>& a, const ShGeneric<N, T>& b) 00134 { 00135 return length_inf(a-b); 00136 } 00137 00138 template<int N, typename T> 00139 ShGeneric<1, T> length(const ShGeneric<N, T>& a) 00140 { 00141 return sqrt(dot(a, a)); 00142 } 00143 00144 template<int N, typename T> 00145 ShGeneric<1, T> length_1(const ShGeneric<N, T>& a, const ShGeneric<N, T>& b) 00146 { 00147 return sum(abs(a)); 00148 } 00149 00150 template<int N, typename T> 00151 ShGeneric<1, T> length_inf(const ShGeneric<N, T>& a, const ShGeneric<N, T>& b) 00152 { 00153 return max(abs(a)); 00154 } 00155 00156 00157 } 00158 00159 #endif

Generated on Mon Oct 18 14:17:39 2004 for Sh by doxygen 1.3.7