- Forward


Fragment Shaders
In "Modern" OpenGL


Prof. David Bernstein
James Madison University

Computer Science Department
bernstdh@jmu.edu

Print

Overview
Back SMYC Forward
  • Purpose:
    • Determine the colors (e.g., the diffuse color, the specular color) and depth for a fragment (e.g., a pixel) in a raster
  • Structure:
    • Programs written in a C-like language
    • Invariant with respect to inputs (i.e., the same input will produce the same output)
  • Invocation Frequency:
    • Essentially once per fragment generated by the rasterizer
User-Defined Inputs
Back SMYC Forward
  • Source:
    • Prior stages in the pipeline
  • Process:
    • User-defined inputs are commonly interpolated across the surface/interior of the graphics primitive/shape
Built-In Inputs
Back SMYC Forward
  • in vec4 gl_FragCoord:
    • The location of the fragment in window space
  • in bool gl_frontFacing:
    • TRUE if this fragment was generated by the front face of the primitive
  • in vec2 gl_PointCoord:
    • The location within a point primitive that defines the position of the fragment relative to the size of the point
Uniforms
Back SMYC Forward
  • Purpose:
    • A global variable that does not change from one invocation of the shader to the next (within a rendering call)
  • Tasks Before Invoking the Draw Command:
    • glGetUniformLocation() to get a handle/name/ID
    • glUniform____() to initialize
  • Declaration and Assignment Within a Shader:
    • uniform type identifier;
Outputs
Back SMYC Forward
  • Purpose:
    • Pass color and depth information to the next part of the pipeline
  • Specified Within the Shader:
    • Declared using a layout(location=index) out type identifier statement (Note: 0 is the default index if the layout() clause is omitted.)
  • Specified Before Linking:
    • void glBindFragDataLocation(GLuint programID​, GLuint colorNumber​, const char* name​);
  • Automatic Specification (Not Recommended):
    • If neither of the other two methods is used then the fragment color is automatically assigned when the program is linked
Examples
Back SMYC Forward

A Red Fragment Shader

openglexamples/modern/red.frag
 
Examples (cont.)
Back SMYC Forward

A Color Pass-Through Fragment Shader

openglexamples/modern/color.frag
 
Examples (cont.)
Back SMYC Forward

A Fragment Shader that Implements a Simple Lighting Model

openglexamples/modern/lighting.frag
 
Examples (cont.)
Back SMYC Forward

A Fragment Shader that Implements a Simple Lighting Model

fragment-shader_lighting_sphere fragment-shader_lighting_fighter
Examples (cont.)
Back SMYC Forward

A Fragment Shader that Implements a Cartoon Lighting Model

openglexamples/modern/cartoonlighting.frag
 
Examples (cont.)
Back SMYC Forward

A Fragment Shader that Implements a Cartoon Lighting Model

fragment-shader_cartoonlighting_sphere
There's Always More to Learn
Back -