- Forward


Using Shaders
In "Modern" OpenGL


Prof. David Bernstein
James Madison University

Computer Science Department
bernstdh@jmu.edu

Print

Review
Back SMYC Forward
  • Vertex Specification:
    • Using vertex buffer objects and vertex array objects to specify primitives/shapes
  • The Pipeline:
    • Initiation using drawing commands
    • The stages
    • The shaders used at different stages
  • Some Important Shaders:
    • Vertex Shaders
    • Fragment Shaders
This Lecture
Back SMYC Forward
  • The "Internal" Build Process:
    • Loading the shaders
    • Compiling the shaders
    • Creating the shader program
  • Putting it all Together:
    • A complete application
Loading Shaders
Back SMYC Forward
  • Review:
    • Shaders are written in a C-like language and stored in a text file
  • What's Needed:
    • A way to read shaders into the program that invokes the OpenGL drawing commands
Loading Shaders (cont.)
Back SMYC Forward

The Specification

openglexamples/modern/fileUtilities.h
 
Loading Shaders (cont.)
Back SMYC Forward

The Implementation

openglexamples/modern/fileUtilities.cpp
 
Compiling Shaders
Back SMYC Forward
  • What's Needed:
    • A way to compile the source code within the program that invokes the OpenGL drawing commands
  • The Steps in the Process:
    • Create a handle/name/ID using GLuint glCreateShader(GLenum shaderType)
    • Associate the source with the ID using void glShaderSource(GLuint shaderID, GLsizei count, const GLchar **source, const GLint *length)
    • Compile the source using void glCompileShader(GLuint shaderID)
    • Check the status of the compilation using void glGetShaderiv(GLuint shaderID, GLenum paramname, GLint *params)
    • Get the error messages (if any) using void glGetShaderInfoLog(GLuint shaderID, GLsizei maxLength, GLsizei *actualLength, GLchar *message)
Compiling Shaders (cont.)
Back SMYC Forward

The Specification

openglexamples/modern/shaderUtilities.h (Fragment: buildShader)
 
Compiling Shaders (cont.)
Back SMYC Forward

The Implementation

openglexamples/modern/shaderUtilities.cpp (Fragment: buildShader)
 
Building Programs
Back SMYC Forward
  • What's Needed:
    • A way to link the various shaders into a coherent whole
  • The Steps in the Process:
    • Create a handle/name/ID using GLuint glCreateProgram(void)
    • Associate the shaders with with the ID using void glAttachShader(GLuint programID, GLuint shaderID)
    • Link the pieces together using void glLinkProgram(GLuint programID)
    • Check the status of the compilation using void glGetProgramiv(GLuint programID, GLenum paramname, GLint *params)
    • Get the error messages (if any) using void glGetProgramInfoLog(GLuint programID, GLsizei maxLength, GLsizei *actualLength, GLchar *message)
    • Cleanup using void glDetachShader(GLuint programID, GLuint shaderID) and void glDeleteShader(GLuint id)
Building Programs (cont.)
Back SMYC Forward

The Specification

openglexamples/modern/shaderUtilities.h (Fragment: buildProgram)
 
Building Programs (cont.)
Back SMYC Forward

The Implementation

openglexamples/modern/shaderUtilities.cpp (Fragment: buildProgram)
 
The Application that Invokes the OpenGL Drawing Commands
Back SMYC Forward
  1. Initialize the environment (e.g., GLUT, GLEW, and OpenGL)
  2. Read the shaders and create the program
  3. Create the transformation matrices, lights, etc...
  4. Create the vertex stream
  5. Create the vertex buffer object
  6. In the display loop, make the data available to the shaders and invoke the drawing commands
The Complete Application (cont.)
Back SMYC Forward

Initializing the Environment

openglexamples/modern/model.cpp (Fragment: environment)
 
The Complete Application (cont.)
Back SMYC Forward

Read the Shaders and Create the Program

openglexamples/modern/model.cpp (Fragment: program)
 
The Complete Application (cont.)
Back SMYC Forward

Create the Transformation Matrices, Lights etc...

openglexamples/modern/model.cpp (Fragment: init)
 
The Complete Application (cont.)
Back SMYC Forward

Create the Vertex Stream

openglexamples/modern/trifileUtilities.cpp
 
The Complete Application (cont.)
Back SMYC Forward

Create the Vertex Buffer Object

openglexamples/modern/model.cpp (Fragment: vbo)
 
The Complete Application (cont.)
Back SMYC Forward

The Display Loop

openglexamples/modern/model.cpp (Fragment: display)
 
There's Always More to Learn
Back -