JMU
Vertex Shaders
In "Modern" OpenGL


Prof. David Bernstein
James Madison University

Computer Science Department
bernstdh@jmu.edu


Overview
User-Defined Inputs
Built-In Inputs
Uniforms
Outputs
Examples

A Vertex Shader that "Forwards" the Position

openglexamples/modern/position.vert
        #version 300 es

/**
 * A vertex shader that simply outputs the positions that
 * are passed to it as input.
 *
 * @author Prof. David Bernstein, James Madison University
 */


/**
 * Inputs that are different for each invocation of this shader.
 */
layout(location = 0) in vec3 position_modelspace; // The vertex in model space


/**
 * Process the inputs and generate the outputs.
 * In this case, the only output is the pre-defined
 * variable gl_Position.
 */
void main(){
  gl_Position.xyz = position_modelspace;
  gl_Position.w = 1.0;

  //     Another way to accomplish the same thing
  //gl_Position =  vec4(position_modelspace, 1);
}

        
Examples (cont.)

A Vertex Shader that "Forwards" the Position and Color

openglexamples/modern/positionandcolor.vert
        #version 300 es


/**
 * A vertex shader that simply outputs the positions and
 * colors that are passed to it as inputs.
 *
 * @author Prof. David Bernstein, James Madison University
 */



/**
 * Inputs that are different for each invocation of this shader.
 */
layout(location = 0) in vec3 position_modelspace; // The position in model space
layout(location = 1) in vec3 color;               // The color


/**
 * Outputs.
 */
out vec3 fragmentColor;


/**
 * Process the inputs and generate the outputs.
 * In this case, the only output is the pre-defined
 * variable gl_Position.
 */
void main(){
  // Output the position of the vertex
  gl_Position =  vec4(position_modelspace, 1);

  // Output the vertex color (which will be interpolated with the
  // other vertex colors before it is given to the fragment)
  fragmentColor = color;
}

        
Examples (cont.)

A Model-View-Projection Vertex Shader

openglexamples/modern/mvp.vert
        #version 300 es

/**
 * A vertex shader that applies the MVP transform to the
 * position.
 *
 * @author Prof. David Bernstein, James Madison University
 */


/**
 * Inputs that are different for each invocation of this shader.
 */
layout(location = 0) in vec3 position_modelspace; // The position in model space

/**
 * Inputs that (may) stay constant for all vertices.
 */
uniform mat4 MVP;   // The model-view-projection matrix

/**
 * Process the inputs and generate the outputs.
 * In this case, apply the MVP matrix.
 */
void main(){
  gl_Position =  MVP * vec4(position_modelspace, 1);
}

        
Examples (cont.)

A Vertex Shader that Provides Vectors for a Lighting Model

openglexamples/modern/lighting.vert
        #version 300 es

/**
 * A vertex shader that can be used with a fragment shader models lighting.
 *
 * Note: This vertex shader assumes that M is the identity matrix.
 *
 * @author Prof. David Bernstein, James Madison University
 */

precision mediump float;

/**
 * Inputs that are different for each invocation of this shader.
 */
layout(location = 0) in vec3 position_modelspace; // The position in model space
layout(location = 1) in vec3 color;               // The color
layout(location = 2) in vec3 normal_modelspace;   // The normal at the vertex

/**
 * Output data (which will be interpolated for each fragment)
 */
out vec3 vertexColor;
out vec3 position_worldspace;
out vec3 normal_cameraspace;
out vec3 eyeDirection_cameraspace;
out vec3 lightDirection_cameraspace;

/**
 * Inputs that (may) stay constant for all vertices.
 */
uniform mat4 M;                        // The model matrix
uniform mat4 V;                        // The view matrix
uniform mat4 MVP;                      // The model-view-projection matrix
uniform vec3 lightPosition_worldspace; // The light position in world space

/**
 * Process the inputs and generate the outputs.
 */
void main(){

  // M is assumed to be the identity matrix!

  // The transformed position of the vertex
  gl_Position =  MVP * vec4(position_modelspace, 1);
        
  // Position of the vertex in worldspace
  position_worldspace = (M * vec4(position_modelspace, 1)).xyz;

  vec3 cameraposition_cameraspace = vec3(0,0,0);

  vec3 position_cameraspace = ( V * M * vec4(position_modelspace,1)).xyz;
  eyeDirection_cameraspace =  cameraposition_cameraspace - position_cameraspace;

  vec3 lightPosition_cameraspace = ( V * vec4(lightPosition_worldspace,1)).xyz;
  lightDirection_cameraspace = lightPosition_cameraspace + eyeDirection_cameraspace;
        
  // Note: This is only correct because M does not scale the model.
  //       Use its inverse transpose if M scales the model.
  normal_cameraspace = ( V * M * vec4(normal_modelspace,0)).xyz;
        
  vertexColor = color;
}