float HandleDash( float offset )\n\ {\n\ float sum = 0.0; \n\ float dashMaxCount = float( textureSize( dashSampler, 0 ) ); \n\ int i; \n\ for(i=0; i<dashCount; ++i) sum += texture( dashSampler, i/dashMaxCount )[0]; \n\ offset -= sum * int(offset/sum); \n\ for(i=0; i<dashCount; ++i){ \n\ float dash = texture( dashSampler, i/dashMaxCount )[0]; \n\ if( offset < dash ) break; \n\ offset -= dash; \n\ } \n\ if( (i%2) > 0 ) discard; \n\ float dash = texture( dashSampler, i/dashMaxCount )[0]; \n\ float dx = dFdx( offset ); \n\ float dy = dFdy( offset ); \n\ // implicit lines: offset*(dash-offset) = 0 \n\ float fx = (dash - 2*offset) * dx; \n\ float fy = (dash - 2*offset) * dy; \n\ float sd = offset*(dash-offset) / sqrt( fx*fx + fy*fy ); \n\ \n\ float alpha = (sd - 0.5); \n\ if( alpha < 0.0 ) discard; \n\ if( alpha < 2.0 ) return 0.5*alpha; \n\ return 1.0; \n\ }\n\
// Derivative maps - bump mapping unparametrized surfaces by Morten Mikkelsen // http://mmikkelsen3d.blogspot.sk/2011/07/derivative-maps.html // Evaluate the derivative of the height w.r.t. screen-space using forward differencing (listing 2) vec2 dHdxy_fwd() { vec2 dSTdx = dFdx( vUv ); vec2 dSTdy = dFdy( vUv ); float hll = bumpScale * texture2D( tDisplacement, vUv ).x; float dBx = bumpScale * texture2D( tDisplacement, vUv + dSTdx ).x - hll; float dBy = bumpScale * texture2D( tDisplacement, vUv + dSTdy ).x - hll; return vec2( dBx, dBy ); }
// from [http://www.thetenthplanet.de/archives/1180] mat3 cotangent_frame( vec3 N, vec3 p, vec2 uv ) { // get edge vectors of the pixel triangle vec3 dp1 = dFdx( p ); vec3 dp2 = dFdy( p ); vec2 duv1 = dFdx( uv ); vec2 duv2 = dFdy( uv ); // solve the linear system vec3 dp2perp = cross( dp2, N ); vec3 dp1perp = cross( N, dp1 ); vec3 T = dp2perp * duv1.x + dp1perp * duv2.x; vec3 B = dp2perp * duv1.y + dp1perp * duv2.y; // construct a scale-invariant frame float invmax = inversesqrt( max( dot(T,T), dot(B,B) ) ); return mat3( T * invmax, B * invmax, N ); }
vec3 perturbNormalArb( vec3 surf_pos, vec3 surf_norm, vec2 dHdxy ) { vec3 vSigmaX = dFdx( surf_pos ); vec3 vSigmaY = dFdy( surf_pos ); vec3 vN = surf_norm; // normalized vec3 R1 = cross( vSigmaY, vN ); vec3 R2 = cross( vN, vSigmaX ); float fDet = dot( vSigmaX, R1 ); vec3 vGrad = sign( fDet ) * ( dHdxy.x * R1 + dHdxy.y * R2 ); return normalize( abs( fDet ) * surf_norm - vGrad ); }
void main(void) { float depth = vpPosition.z / vpPosition.w ; // depth = depth * 0.5 + 0.5; //Don't forget to move away from unit cube ([-1,1]) to [0,1] coordinate system float moment1 = depth; float moment2 = depth * depth; // Adjusting moments (this is sort of bias per pixel) using partial derivative float dx = dFdx(depth); float dy = dFdy(depth); moment2 += 0.25*(dx*dx+dy*dy); gl_FragColor = vec4( moment1,moment2, 0.0, 0.0 ); }
vec4 ComputeCubicBezier( vec3 p, vec4 color )\n\ {\n\ // Gradients: \n\ vec3 px = dFdx( p ); \n\ vec3 py = dFdy( p ); \n\ \n\ // Chain rule: \n\ float fx = (3*p.x*p.x)*px.x - p.y*px.y - p.z*px.z; \n\ float fy = (3*p.x*p.x)*py.x - p.y*py.y - p.z*py.z; \n\ \n\ // Signed distance: \n\ float sd = -(p.x*p.x*p.x - p.y*p.z) / sqrt(fx*fx + fy*fy); \n\ \n\ // Linear alpha: \n\ float alpha = 0.25 - sd; \n\ if( alpha < 0.0 ) discard; \n\ if( alpha < 0.5 ) color.a *= 2*alpha; \n\ return color; \n\ }\n\
// \n\ // Loop, Charles, and Jim Blinn: \n\ // GPU Gems 3: Chapter 25. Rendering Vector Art on the GPU. \n\ // http://http.developer.nvidia.com/GPUGems3/gpugems3_ch25.html \n\ // \n\ // Loop, Charles, and Jim Blinn. 2005: \n\ // Resolution Independent Curve Rendering using Programmable Graphics Hardware. \n\ // In ACM Transactions on Graphics (Proceedings of SIGGRAPH 2005) 24(3), pp. 1000–1008. \n\ // \n\ vec4 ComputeQuadraticBezier( vec2 p, vec4 color )\n\ {\n\ // Gradients: \n\ vec2 px = dFdx( p ); \n\ vec2 py = dFdy( p ); \n\ \n\ // Chain rule: \n\ float fx = (2*p.x)*px.x - px.y; \n\ float fy = (2*p.x)*py.x - py.y; \n\ \n\ // Signed distance: \n\ float sd = (p.x*p.x - p.y) / sqrt(fx*fx + fy*fy); \n\ \n\ // Linear alpha: \n\ float alpha = 0.5 - sd; \n\ if( alpha < 0.0 ) discard; \n\ if( alpha < 1.0 ) color.a *= alpha; \n\ return color; \n\ }\n\
__device__ __inline__ Vec4f dFdy (const Vec4f& v) const { return Vec4f(dFdy(v.x), dFdy(v.y), dFdy(v.z), dFdy(v.w)); }
__device__ __inline__ Vec3f dFdy (const Vec3f& v) const { return Vec3f(dFdy(v.x), dFdy(v.y), dFdy(v.z)); }
__device__ __inline__ Vec2f dFdy (const Vec2f& v) const { return Vec2f(dFdy(v.x), dFdy(v.y)); }
// do normal mapping using given texture coordinate // tangent space phong lighting with optional border clamp // normal X and Y stored in red and green channels vec4 normal_mapping(vec2 texcoord, out vec4 clr, out vec3 nml ) { // derivative texcoord from input texcoord vec2 dx = dFdx(texcoord); vec2 dy = dFdy(texcoord); // color map vec4 color = texture2DGrad(color_map, texcoord, dx, dy); clr=color; // vec4 color = texture2D(color_map, texcoord); // normal map vec3 normal = texture2DGrad(relief_map, texcoord, dx, dy); nml=normal; // vec4 normal = texture2D(relief_map, texcoord); #if 0 normal.xy = 2.0 * normal.xy - 1.0; normal.y = -normal.y; normal.z = sqrt( 1.0 - dot( normal.xy, normal.xy ) ); #endif normal = 2.0*normal-1.0; // light and view in tangent space vec3 l=normalize(light); vec3 v=normalize(eye); // compute diffuse and specular terms float diff = clamp( dot( l, normal.xyz ),0.0,1.0 ); float spec = clamp( dot( normalize( l - v ), normal.xyz ),0.0,1.0 ); // attenuation factor float att = 1.0 - max( 0.0, l.z ); att = 1.0 - att * att; // border clamp // if ( border_clamp ) { #if 0 if ( texcoord.x < 0.0 ) discard; if ( texcoord.y < 0.0 ) discard; if ( texcoord.x > 1.0 ) discard; if ( texcoord.y > 1.0 ) discard; #endif } // compute final color vec4 finalcolor; const float shine=32.0; const vec3 ambient=vec3(0.2,0.2,0.2); const vec3 diffuse=vec3(0.9,0.9,0.9); const vec3 specular=vec3(0.2,0.2,0.2); finalcolor.xyz = ambient * color.xyz + att * ( color.xyz * diffuse*diff + specular * pow( spec, shine ) ); finalcolor.w = 1.0; return finalcolor; }
void main() { vec4 color = gl_FrontLightModelProduct.sceneColor; vec3 N = normalize(normal); vec3 E = normalize(eyeVec); if ( gl_TexCoord[0].s > 0.0 && gl_TexCoord[0].t > 0.0 ) { const float maxVariance = 2.5; // Mess around with this value to increase/decrease normal perturbation (2.0 is standard) const float minVariance = maxVariance / 2.0; N = texture2D(normalmap, gl_TexCoord[0].st).rgb * maxVariance - minVariance; //calculate Tangtent vec3 Q1 = dFdx(vertex); vec3 Q2 = dFdy(vertex); vec2 st1 = dFdx(gl_TexCoord[0]).xy; vec2 st2 = dFdy(gl_TexCoord[0]).xy; vec3 t = normalize(Q1*st2.t - Q2*st1.t); vec3 b = normalize(-Q1*st2.s + Q2*st1.s); //smoothing t = normalize(cross(t, N)); b = normalize(cross(b, N)); // the transpose of texture-to-eye space matrix mat3 TBN = mat3(t, b, normal); N = N * TBN; N = normalize (normal + N); } for ( int l = 0; l < numLights; ++l ) { vec3 L = normalize(gl_LightSource[l].position.xyz - vertex); if ( dot(N, L) > 0.0 ) { vec3 R = reflect(-L, N); float shin = pow(max(dot(R, E), 0.0), gl_FrontMaterial.shininess); float fatt = gl_LightSource[l].constantAttenuation + gl_LightSource[l].linearAttenuation * length(L) + gl_LightSource[l].quadraticAttenuation * length(L) * length(L); if ( fatt != 0.0 ) fatt = 1.0 / fatt; else fatt = 1.0; color += fatt * (dot(N, L) * gl_LightSource[l].diffuse * gl_FrontMaterial.diffuse + gl_LightSource[l].specular * gl_FrontMaterial.specular * shin); } } gl_FragColor = color * texture2D(basemap, gl_TexCoord[0].st); //check, if a black border has to be drawn if ( abs(dot(normalize(eyeVec), normal)) < 0.2 ) { gl_FragColor = vec4(0.0, 0.0, 0.0, 1.0); return; } color = gl_FrontLightModelProduct.sceneColor; for ( int l = 0; l<numLights; ++l ) { float lambert = dot (normalize(normal), normalize(gl_LightSource[l].position.xyz - vertex)); if ( lambert < 0.0 ) lambert = 0.0; // Discretize the intensity, based on a few cutoff points if ( lambert > 0.85 ) color += vec4(1.0, 1.0, 1.0, 1.0) * gl_FragColor; else if ( lambert > 0.5 ) color += vec4(0.7, 0.7, 0.7, 1.0) * gl_FragColor; else if ( lambert > 0.05 ) color += vec4(0.35, 0.35, 0.35, 1.0) * gl_FragColor; else color += vec4(0.1, 0.1, 0.1, 1.0) * gl_FragColor; } gl_FragColor = color; }
vec3 reconstructNormalVS(vec3 positionVS) { return normalize(cross(dFdx(positionVS), dFdy(positionVS))); }