the fragment shader
play

The Fragment Shader CS418 Computer Graphics John C. Hart Fragment - PowerPoint PPT Presentation

The Fragment Shader CS418 Computer Graphics John C. Hart Fragment Pipeline Rasterization Model Vertex Clip Clip & Window Viewport Coords W2V Interpolation Coords Shader Coords Divide Coords Depth Fragment Pixels Fragments


  1. The Fragment Shader CS418 Computer Graphics John C. Hart

  2. Fragment Pipeline Rasterization Model Vertex Clip Clip & Window Viewport Coords W2V Interpolation Coords Shader Coords Divide Coords Depth Fragment Pixels Fragments Fragments Test Shader

  3. Model Coords GLSL Fragment Shader Vertex Shader Clip Coords Inputs: Clip/Div/W2V • vec4 gl_FragCoord (viewport coordinates) • bool gl_FrontFacing Viewport Coords • vec4 gl_Color, gl_SecondaryColor Rasterize • vec4 gl_TexCoord[gl_MaxTextureCoords] • float gl_FogFragCoord Fragments Fragment Shader Outputs: Fragments • vec4 gl_FragColor Depth Test • vec4 gl_FragData[gl_MaxDrawBuffers] • float gl_FragDepth (= glFragCoord.z) Pixels

  4. clamp(x,a,b) b GLSL Functions a x a b • sin, cos, tan, asin, acos, atan, atan(n,d) mix(x,y,a) y • radians(deg), degrees(rads) x • pow, exp, log, sqrt a 1 0 • exp2, log2, inversesqrt(x) step(a,x) • abs, ceil, floor, fract, max, min, mod, sign, 1 • clamp(x,a,b) = min(max(x,a),b) x 0 a • mix(x,y,a) = (1.-a)*x + a*y smoothstep(a,b,x) • step(a,x) = (x < a) ? 0.0 : 1.0 1 0.0 if x < a x 0 a b • smoothstep(a,b,x) = 1.0 if x > b else t = clamp((x-a)/(b-a),0,1), t*t*(3-2*t)

  5. GLSL Vector Math • dot(a,b) = a.x*b.x + a.y*b.y + a.z*b.z + … • length(a) = sqrt(dot(a,a)) • distance(a,b) = length(b – a) • cross(a,b) = vec3(a.y*b.z – a.z*b.y, …) • normalize(a) = a/length(a) = a*inversesqrt(dot(a,a)) • faceforward(n,v,nref) = dot(nref,v) < 0.0 ? -n : n • reflect(l,n) = l – 2*dot(l,n)*n • refract(l,n,eta) – refracts a vector l through a surface w/normal n and index of refraction eta – derivation in CS419 Production Graphics

  6. Specular Reflection Phong Blinn n n h r l l f q q q f v q v r = 2( n  l ) n – l h = ( l + v )/|| l + v || = L i k s c s cos n f = L i k s c s cos n f L o L o = L i k s c s ( v  r ) n = L i k s c s ( n  h ) n

  7. Blinn Vertex Shader void main() { /* compute unit normal, vertex position, light vector and view vector in viewing coordinates */ vec3 n = normalize(gl_NormalMatrix*gl_Normal); vec3 p = gl_ModelViewMatrix*gl_Vertex; vec3 l = normalize(gl_LightSource[0].position – p.xyz); vec3 v = -normalize(p); /* eye is at origin */ /* compute halfway vector */ vec3 h = normalize(l + v); /* initialize color with reflection of ambient light */ frontColor = gl_FrontMaterial.ambient*gl_LightSource[0].ambient; /* f indicates if vertex faces light (f=1) or on dark side (f=0) */ float f = (dot(n,l) > 0.0) ? 1.0 : 0.0; /* add Lambertian diffuse reflection of direct light */ frontColor += f*dot(n,l)*gl_FrontMaterial.diffuse*gl_LightSource[0].diffuse; /* add Blinn specular reflection of direct light */ frontColor += f*pow(dot(n,h),gl_FrontMaterial.shininess) * gl_FrontMaterial.specular*gl_LightSource[0].specular; gl_Position = gl_ModelViewProjectionMatrix*gl_Vertex; }

  8. Blinn Fragment Shader varying vec3 n; varying vec4 p; void main() { n = gl_NormalMatrix*gl_Normal; Vertex Shader p = gl_ModelViewMatrix*gl_Vertex; gl_Position = gl_ModelViewProjectionMatrix*gl_Vertex; } varying vec3 n; varying vec4 p; void main() { vec3 nhat = normalize(n); vec3 l = normalize(gl_LightSource[0].position – p.xyz); vec3 v = -normalize(p); /* eye is at origin */ vec3 h = normalize(l + v); vec4 c = gl_FrontMaterial.ambient*gl_LightSource[0].ambient; c += max(0,dot(n,l))*gl_FrontMaterial.diffuse*gl_LightSource[0].diffuse; int f; if (dot(n,l) > 0.0) c += pow(max(0,dot(n,h)),gl_FrontMaterial.shininess) * gl_FrontMaterial.specular*gl_LightSource[0].specular; gl_FragColor = c; }

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend