- Forward


Vertex Shaders
In "Modern" OpenGL


Prof. David Bernstein
James Madison University

Computer Science Department
bernstdh@jmu.edu

Print

Overview
Back SMYC Forward
  • Purpose:
    • Transform vertices into a post-projection space
  • 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 vertex (except when vertices have the same inputs, in which case the output may be re-used
User-Defined Inputs
Back SMYC Forward
  • Source:
    • A drawing command issued when a vertex array object is bound
  • Process:
    • Each user-defined input is assigned an attribute index that can be retrieved and assigned using a layout statement in the shader (e.g., layout(location = 0) in vec4 position;)
Built-In Inputs
Back SMYC Forward
  • in int gl_VertexID:
    • The index of the vertex being processed
  • in int gl_InstanceID:
    • The index of the instance of the mesh when multiple copies of the same mesh are rendered in different locations
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 information to the next part of the pipeline
  • User-Defined:
    • Declared using an out type identifier statement
  • Pre-Defined Outputs:
    • vec4 gl_Position - The clip-space position of the vertex
    • float gl_PointSize - The width and height of a pixel (only meaningful for point primitives)
    • float gl_ClipDistance - Distance to the clipping plane (negative for outside)
Examples
Back SMYC Forward

A Vertex Shader that "Forwards" the Position

openglexamples/modern/position.vert
 
Examples (cont.)
Back SMYC Forward

A Vertex Shader that "Forwards" the Position and Color

openglexamples/modern/positionandcolor.vert
 
Examples (cont.)
Back SMYC Forward

A Model-View-Projection Vertex Shader

openglexamples/modern/mvp.vert
 
Examples (cont.)
Back SMYC Forward

A Vertex Shader that Provides Vectors for a Lighting Model

openglexamples/modern/lighting.vert
 
There's Always More to Learn
Back -