00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
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