1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228
|
// represents all data members stored ine vtkLight
struct vtkLight
{
uniform float3 Position;
uniform float Intensity;
};
// represents all data members stored ine vtkProperty
struct vtkProperty
{
uniform float3 AmbientColor;
uniform float3 DiffuseColor;
uniform float3 SpecularColor;
uniform float Ambient;
uniform float Diffuse;
uniform float Specular;
uniform float SpecularPower;
uniform float Opacity;
};
// represents all data members stored ine vtkCamera
struct vtkCamera
{
uniform float3 Position;
};
// Central location for functions commonly used when computing reflectance
float Phong(float alpha, float c)
{
// c1
// D1 = (N.H)
//
return max( 0, pow(cos(alpha),c) );
}
float GaussianMicroFacets(float alpha, float c)
{
// 2
// -(alpha * C)
// D2 = e
//
return exp( -pow(alpha*c,2) );
}
float BlinnMicroFacets(float alpha, float c)
{
// AKA Blinn shading function
// / \
// | 2 |
// | C3 |
// D3 = | ---------------------------- |
// | 2 2 |
// | cos (alpha) * (C3 - 1) + 1 |
// \ /
//
c3sq = pow(c,2);
float den = pow( cos(alpha), 2 ) * (c3sq-1.0) + 1.0;
if( den != 0.0 )
{
return pow( c3sq/den, 2 );
}
return 0.0;
}
float TrowbridgeMicroFacets( float alpha, float c )
{
float c2 = pow(c,2);
float c2a = pow(cos(alpha),2);
return pow( c2/(c2a*(c2-1)+1), 2 );
}
float BeckmannMicroFacets( float m, float a )
{
// a = dot(N,H)
//
// / \ 2
// | tan(a) |
// - | ------ |
// | m |
// 1 \ /
// --------------- * e
// 2 4
// m * cos a
//
float den = pow(m,2) * pow(cos(a),4 );
float value = 0.0;
if( abs(den) > 0.00001 )
{
value = exp( -1.0 * pow((tan(a)/m),2))/den;
}
return 0.0;
}
// Rendering variables
//
// V - unit vector in direction of viewer
// L - unit vector in direction of light source
// H - unit vector that bisects V and L
// N - unit vector in normal direction
//
// theta - angle between H and V or H and L
// alpha - angle betwee N and H
//
// GAF = Geometric Attenuation Factor
//
// MicroFacets as VShaped grooves
float VGrooveGAF( float3 E, float3 H, float3 L, float3 N )
{
float Ga = 1.0;
float Gb = Ga;
float Gc = Ga;
float nh = dot(N,H);
float eh = dot(E,H);
if( abs(dot(E,H)) > 0.00001 )
{
Gb = 2.0 * nh * dot(N,E) / eh;
Gc = 2.0 * nh * dot(N,L) / eh;
}
return min( 1.0, min(Gb,Gc));
}
// c = dot(E,H)
// n = refractive index
float Fresnel( float3 E, float3 H, float n )
{
float zero = 0.000001;
float c = dot(E,H);
float g = sqrt( c*c + n*n - 1 );
float num = pow((c*(g+c)-1.0),2);
float den = pow((c*(g-c)+1.0),2);
float f = 0.0;
if( abs(pow(g+c,2)) > zero )
{
f = pow((g-c),2)/pow((g+c),2);
}
if( abs(den) > zero )
{
f *= (1.0 + num/den) * 0.5;
}
else
{
f = 0.0;
}
return f;
}
// Compute the diffuse and specular intesity of a light source
// N, L, E should all be normalized
float3 ReflectedIntensity( float3 N, // unit surface normal
float3 E, // eye vector
float3 L, // eye vector
float3 H, // eye vector
vtkLight light, // vtkLight in question
vtkProperty property, // vtkProperties
float facetConst, // const for micro-facet distribution
float RefractiveIndex // surface property
)
{
#if 1
float mf = GaussianMicroFacets( dot(H,N), facetConst );
#else
float mf = TrowbridgeMicroFacets( dot(H,N), facetConst );
#endif
float gaf = VGrooveGAF(E,H,L,N);
float fresnel = Fresnel(E,H,RefractiveIndex);
// don't let these values go negative
float Rd = max(0.0, dot(N,L) );
#if 1
float Rs = max(0.0, mf*gaf*fresnel/dot(N,E) );
#else
float Rs = max(0.0, pow(dot(N,H),property.SpecularPower));
#endif
return light.Intensity * (Rd * property.Diffuse * property.DiffuseColor +
Rs * property.Specular * property.SpecularColor);
}
// don't reference TEXCOORD0, it's and alias for POSITION
void fragment_program( in float4 pos : TEXCOORD0,
in float4 normal : TEXCOORD1,
in float4 col : COLOR0,
uniform float facetConst,
uniform float RefractiveIndex,
uniform vtkLight light0,
uniform vtkLight light1,
uniform vtkCamera camera,
uniform vtkProperty property,
out float4 color : COLOR
)
{
float3 N = normalize( normal.xyz );
float3 E = normalize( camera.Position - pos.xyz );
color.rgb = float3( 0.0, 0.0, 0.0 );
// Ambient Term
#if 1
color.rgb += property.Ambient * property.AmbientColor;
#endif
#if 1
// Diffuse and Specular - light0
float3 L = normalize( light0.Position - pos.xyz );
float3 H = normalize(L+E);
color.rgb += ReflectedIntensity( N, E, L, H, light0, property, facetConst, RefractiveIndex);
#endif
#if 1
// Diffuse and Specular - light1
L = normalize( light1.Position - pos.xyz );
H = normalize(L+E);
color.rgb += ReflectedIntensity( N, E, L, H, light1, property, facetConst, RefractiveIndex);
#endif
color.a = property.Opacity;
}
|