INTRODUCTION TO SHADERS JEFF CHASTINE 1 QUICK BACKGROUND
INTRODUCTION TO SHADERS JEFF CHASTINE 1
QUICK BACKGROUND • A vertex is a 3 D point (x, y, z) • A triangle • is made from 3 vertices • has a normal • Note: vertices can have normals too! JEFF CHASTINE 2
QUICK BACKGROUND • Models/Meshes are made from triangles • Open. GL doesn’t support Quads (4 vertices) JEFF CHASTINE 3
QUICK BACKGROUND • Hardware has changed! • Was “fixed” • More of the graphics pipeline is programmable • Open. GL has changed! • Was “fixed” • Is now shader-based JEFF CHASTINE 4
INTRO TO SHADERS • A shader is a program • Has source code (text file) • Cg, HLSL and GLSL • GLSL is very “C-like” • Is compiled into a program • We get back IDs, which are just ints! JEFF CHASTINE 5
INTRO TO SHADERS • Two primary types of shaders: • Vertex shader • Changes the position of a vertex (trans/rot/skew) • May determine color of the vertex • Fragment shader • Determines the color of a pixel • Uses lighting, materials, normals, etc… JEFF CHASTINE 6
MAKING A SHADER PROGRAM • Compile a vertex shader (get an ID) • Compile a fragment shader (get an ID) • Check for compilation errors • Link those two shaders together (get an ID) • Keep that ID! • Use that ID before you render triangles • Can have separate shaders for each model JEFF CHASTINE 7
EXAMPLES in vec 4 s_v. Position; void main () { // Look, Ma! I avoided any matrix multiplication! // The value of s_v. Position should be between -1. 0 and +1. 0 gl_Position = s_v. Position; } ----------------------------------out vec 4 s_v. Color; void main () { // No matter what, color the pixel red! f. Color = vec 4 (1. 0, 0. 0, 1. 0); } JEFF CHASTINE 8
COMPILING VERTEX AND FRAGMENT SHADERS • <GLuint> gl. Create. Shader (<type>) • Creates an ID (a GLuint) of a shader • Example: GLuint ID = gl. Create. Shader(GL_VERTEX_SHADER); • gl. Shader. Source (<id>, <count>, <src code>, <lengths>) • Binds the source code to the shader • Happens before compilation • gl. Compile. Shader (<id>) • Used to make part the shader program JEFF CHASTINE 9
CREATING/LINKING/USING THE SHADER • <GLuint> gl. Create. Program() • Returns an ID – keep this for the life of the program • gl. Attach. Shader (<prog ID>, <shader ID>) • Do this for both the vertex and fragment shaders • gl. Link. Program(<prog ID>) • Actually makes the shader program • gl. Use. Program(<prog ID>) • Use this shader when you’re about to draw triangles JEFF CHASTINE 10
END WITH A DRAGON! JEFF CHASTINE 11
- Slides: 11