Using Shaders
In "Modern" OpenGL
|
Prof. David Bernstein
James Madison University
|
|
Computer Science Department
|
bernstdh@jmu.edu
|
Review
-
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:
This Lecture
- The "Internal" Build Process:
- Loading the shaders
- Compiling the shaders
- Creating the shader program
- Putting it all Together:
Loading Shaders
- 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.)
The Specification
openglexamples/modern/fileUtilities.h
Loading Shaders (cont.)
The Implementation
openglexamples/modern/fileUtilities.cpp
Compiling Shaders
- 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.)
The Specification
openglexamples/modern/shaderUtilities.h
(Fragment: buildShader)
Compiling Shaders (cont.)
The Implementation
openglexamples/modern/shaderUtilities.cpp
(Fragment: buildShader)
Building Programs
- 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.)
The Specification
openglexamples/modern/shaderUtilities.h
(Fragment: buildProgram)
Building Programs (cont.)
The Implementation
openglexamples/modern/shaderUtilities.cpp
(Fragment: buildProgram)
The Application that Invokes the OpenGL Drawing Commands
- Initialize the environment (e.g., GLUT, GLEW, and OpenGL)
- Read the shaders and create the program
- Create the transformation matrices, lights, etc...
- Create the vertex stream
- Create the vertex buffer object
- In the display loop, make the data available to the shaders
and invoke the drawing commands
The Complete Application (cont.)
Initializing the Environment
openglexamples/modern/model.cpp
(Fragment: environment)
The Complete Application (cont.)
Read the Shaders and Create the Program
openglexamples/modern/model.cpp
(Fragment: program)
The Complete Application (cont.)
Create the Transformation Matrices, Lights etc...
openglexamples/modern/model.cpp
(Fragment: init)
The Complete Application (cont.)
Create the Vertex Stream
openglexamples/modern/trifileUtilities.cpp
The Complete Application (cont.)
Create the Vertex Buffer Object
openglexamples/modern/model.cpp
(Fragment: vbo)
The Complete Application (cont.)
The Display Loop
openglexamples/modern/model.cpp
(Fragment: display)
There's Always More to Learn