An Introduction to the OpenGL Shading Language Benj Lipchak Rob Simpson Bill Licea-Kane.

Slides:



Advertisements
Similar presentations
An Introduction to the OpenGL Shading Language Keith O’Conor.
Advertisements

Understanding the graphics pipeline Lecture 2 Original Slides by: Suresh Venkatasubramanian Updates by Joseph Kider.
EECS 700: Computer Modeling, Simulation, and Visualization Dr. Shontz Chapter 3: Lighting and Materials.
Introduction to GLSL Patrick Cozzi University of Pennsylvania CIS Fall 2014.
Informationsteknologi Wednesday, December 12, 2007Computer Graphics - Class 171 Today’s class OpenGL Shading Language.
MAT 594CM S10Fundamentals of Spatial ComputingAngus Forbes Week 4 : GLSL Shaders Topics: Shader programs, vertex & fragment shaders, passing data into.
MAT 594CM S10Fundamentals of Spatial ComputingAngus Forbes Week 5 : GLSL Shaders Topics: Shader syntax, passing textures into shaders, per-pixel lighting,
GLSL I May 28, 2007 (Adapted from Ed Angel’s lecture slides)
GLSL I Ed Angel Professor of Computer Science, Electrical and Computer Engineering, and Media Arts Director, Arts Technology Center University of New Mexico.
Mohan Sridharan Based on slides created by Edward Angel GLSL I 1 CS4395: Computer Graphics.
REAL-TIME VOLUME GRAPHICS Christof Rezk Salama Computer Graphics and Multimedia Group, University of Siegen, Germany Eurographics 2006 Real-Time Volume.
Foundations of Computer Graphics (Fall 2012) CS 184, Lecture 7: OpenGL Shading
What is ? Open Graphics Library A cross-language, multi-platform API for rendering 2D and 3D computer graphics. The API is used to interact with a Graphics.
Programmable Pipelines. Objectives Introduce programmable pipelines ­Vertex shaders ­Fragment shaders Introduce shading languages ­Needed to describe.
An Introduction to the OpenGL Shading Language Benj Lipchak Rob Simpson Bill Licea-Kane.
CIS 565 Fall 2011 Qing Sun
Computer Graphics 3 Lecture 5: OpenGL Shading Language (GLSL)
Computing & Information Sciences Kansas State University Lecture 13 of 42CIS 636/736: (Introduction to) Computer Graphics Lecture 13 of 42 Wednesday, 20.
OpenGL Shading Language (Advanced Computer Graphics) Ernest Tatum.
Programmable Pipelines. 2 Objectives Introduce programmable pipelines ­Vertex shaders ­Fragment shaders Introduce shading languages ­Needed to describe.
GL2 Shading Language Bill Licea-Kane ATI Research, Inc.
CSE 690: GPGPU Lecture 6: Cg Tutorial Klaus Mueller Computer Science, Stony Brook University.
Lecture by: Martin Deschamps CSE 4431
OpenGL Shading Language Jian Huang CS594, Spring 2005.
GPU Shading and Rendering: OpenGL Shading Language Marc Olano UMBC.
Programmable Pipelines Ed Angel Professor of Computer Science, Electrical and Computer Engineering, and Media Arts Director, Arts Technology Center University.
OpenGL Shader Language Vertex and Fragment Shading Programs.
Wilf Comp Shaders. Wilf Comp 4002 Shaders Shaders are programs that you can download onto the card Can be used.
OpenGL Shading Language Jian Huang Joshua New CS594, Spring 2006.
1 Introduction to Computer Graphics with WebGL Ed Angel Professor Emeritus of Computer Science Founding Director, Arts, Research, Technology and Science.
Shaders in OpenGL Marshall Hahn. Introduction to Shaders in OpenGL In this talk, the basics of OpenGL Shading Language will be covered. This includes.
Shading in OpenGL Ed Angel Professor Emeritus of Computer Science University of New Mexico 1 E. Angel and D. Shreiner: Interactive Computer Graphics 6E.
OpenGL Shading Language (GLSL)
CSE 381 – Advanced Game Programming GLSL. Rendering Revisited.
OpenGL Shading Language (GLSL)
GLSL II.
GLSL II Ed Angel Professor of Computer Science, Electrical and Computer Engineering, and Media Arts Director, Arts Technology Center University of New.
Week 3 Lecture 4: Part 2: GLSL I Based on Interactive Computer Graphics (Angel) - Chapter 9.
Programmable Pipelines. 2 Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009 Black Box View Geometry Processor Frame Buffer Fragment Processor.
Computing & Information Sciences Kansas State University Lecture 12 of 42CIS 636/736: (Introduction to) Computer Graphics CIS 636/736 Computer Graphics.
What are shaders? In the field of computer graphics, a shader is a computer program that runs on the graphics processing unit(GPU) and is used to do shading.
CS 4363/6353 SHADERS/GLSL. ANOTHER LOOK AT THE PIPELINE Vertex Processing Clipping/Assembly Rasterization Fragment Processing.
Background image by chromosphere.deviantart.com Fella in following slides by devart.deviantart.com DM2336 Programming hardware shaders Dioselin Gonzalez.
OpenGL Shading Language
OpenGL Shading Language (GLSL)
Programming with OpenGL Part 3: Shaders Ed Angel Professor of Emeritus of Computer Science University of New Mexico 1 E. Angel and D. Shreiner: Interactive.
OpenGl Shaders Lighthouse3d.com.
GLSL I.  Fixed vs. Programmable  HW fixed function pipeline ▪ Faster ▪ Limited  New programmable hardware ▪ Many effects become possible. ▪ Global.
Shaders, part 2 alexandri zavodny.
Shader.
CSE 381 – Advanced Game Programming GLSL Syntax
Programmable Pipelines
Carl Johan Gribel, PhD student
Chapter 6 GPU, Shaders, and Shading Languages
Objectives Simple Shaders Programming shaders with GLSL
GLSL I Ed Angel Professor of Computer Science, Electrical and Computer Engineering, and Media Arts Director, Arts Technology Center University of New Mexico.
Introduction to Computer Graphics with WebGL
Day 05 Shader Basics.
Chapter VI OpenGL ES and Shader
Introduction to Programmable Hardware
Introduction to Computer Graphics with WebGL
Shader Basics Chap. 2 of Orange Book.
Programming with OpenGL Part 3: Shaders
CS 480/680 Computer Graphics GLSL Overview.
Language Definitions Chap. 3 of Orange Book.
CS 480/680 Part 1: Model Loading.
Computer Graphics Shading in OpenGL
Shading in OpenGL Ed Angel Professor Emeritus of Computer Science
Introduction to Computer Graphics with WebGL
CS 480/680 Fall 2011 Dr. Frederick C Harris, Jr. Computer Graphics
Presentation transcript:

An Introduction to the OpenGL Shading Language Benj Lipchak Rob Simpson Bill Licea-Kane

An Introduction to the OpenGL Shading Language 2 January 2008 Fixed Functionality Pipeline API Transform and Lighting Transform and Lighting Rasterizer Primitive Assembly Primitive Assembly Texture Environment Texture Environment Depth Stencil Depth Stencil Color Sum Color Sum Alpha Test Alpha Test Fog Dither Color Buffer Blend Color Buffer Blend Vertex Buffer Objects Vertex Buffer Objects Vertices Triangles/Lines/Points Primitive Processing Primitive Processing Frame Buffer

An Introduction to the OpenGL Shading Language 3 January 2008 Programmable Shader Pipeline API Vertex Shader Vertex Shader Rasterizer Primitive Assembly Primitive Assembly Fragment Shader Fragment Shader Depth Stencil Depth Stencil Dither Color Buffer Blend Color Buffer Blend Vertex Buffer Objects Vertex Buffer Objects Vertices Triangles/Lines/Points Primitive Processing Primitive Processing Frame Buffer Alpha Test Alpha Test

An Introduction to the OpenGL Shading Language 4 January 2008 Programmer’s Model Vertex Shader Vertex Shader Fragment Shader Fragment Shader Primitive Assembly & Rasterize Primitive Assembly & Rasterize Per-Sample Operations Per-Sample Operations Attributes ( m * vec4) Attributes ( m * vec4) Vertex Uniforms ( p * vec4) Vertex Uniforms ( p * vec4) Varyings ( n * vec4) Varyings ( n * vec4) Fragment Uniforms ( q * vec4) Fragment Uniforms ( q * vec4)

An Introduction to the OpenGL Shading Language 5 January 2008 Vertex Shader Environment Attribute 0 Uniforms Textures Attribute 1 Attribute 2 Attribute 3 Attribute 4 Attribute 5 … … Attribute m Varying 0 Varying 1 Varying 2 Varying 3 Varying 4 Varying 5 … … Varying n Temporary variables Clip position Vertex Shader Point size

An Introduction to the OpenGL Shading Language 6 January 2008 Fragment Shader Environment Uniforms Textures Temporary variables Fragment Color(s) Varying 0 Varying 1 Varying 2 Varying 3 Varying 4 Varying 5 … … Varying n Fragment Shader Window coord Front facing flag Point coord Fragment Depth

An Introduction to the OpenGL Shading Language 7 January 2008 Precursors to GLSL Texture combiners  EXT_texture_env_combine Vendor-specific assembly-like programmable shaders  EXT_vertex_shader  ATI_fragment_shader, ATI_text_fragment_shader  NV_*_program* Standardized low-level programmable shaders  ARB_vertex_program  ARB_fragment_program Not to be confused with GLSL extensions!  ARB_vertex_shader  ARB_fragment_shader

An Introduction to the OpenGL Shading Language 8 January 2008 Hello World! void main(void) { // This is our Hello World vertex shader // Standard MVP transform gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex; } void main(void) { // This is our Hello World fragment shader // Set to a constant color (hint: look at it upside down) gl_FragColor = vec4(0.7734); }

An Introduction to the OpenGL Shading Language 9 January 2008 Language Basics: variables types Scalar  void float int bool Vector  Floating point: vec2 vec3 vec4  Integer: ivec2 ivec3 ivec4  Boolean: bvec2 bvec3 bvec4 Matrix  mat2 mat3 mat4 == mat2x2 mat3x3 mat4x4  mat2x3 mat2x4 mat3x2 mat3x4 mat4x2 mat4x3 Containers  Structures: struct  Arrays: []

An Introduction to the OpenGL Shading Language 10 January 2008 Language Basics: storage qualifiers const  Local constants defined within shader uniform  Constant shader parameters that can be changed between draws  Do not change per-vertex or per-fragment attribute  Per-vertex values (position, normal, color, etc.) varying  Values output by the vertex shader, input by the fragment shader  Interpolated during rasterization

An Introduction to the OpenGL Shading Language 11 January 2008 Grouping, function/constructor () Array/component indexing [] Component/member selection. Unary ! Binary * / + - Relational >= == != Logical && ^^ || Ternary conditional ?: Assignment = *= /= += -= Sequence, Language Basics: operators

An Introduction to the OpenGL Shading Language 12 January 2008  Used to initialize a structure or built-in type –Built-in type initialization: vec3 myRGB = vec3(0.25, 0.5, 0.75); –Structure initialization: struct S { int a; float b; }; S s = S(2, 3.5);  Provide enough components of correct type vec2 myYZ = vec2(0.5, 0.75); vec4 myPos = vec4(0.25, myYZ, 1.0);  Also provides explicit type conversions – no casting in GLSL! –Only int to float implicit conversions are allowed float numTexels = countTexels(); if (!bool(numTexels)) discard; // non-zero value -> true Language Basics: constructors

An Introduction to the OpenGL Shading Language 13 January 2008 Vector components vec2 v2; vec3 v3; vec4 v4; v2.x // is a float v2.z // wrong: undefined for type v4.rgba // is a vec4 v4.stp // is a vec3 v4.b // is a float v4.xy // is a vec2 v4.xgp // wrong: mismatched component sets

An Introduction to the OpenGL Shading Language 14 January 2008  Components from {xyzw}, {rgba}, or {stpq}  Writemask or swizzle during assignment vec4 foo = vec4(1.0); foo.xyz = vec3(0.25, 0.5, 0.75); foo.wzyx = foo; // reverse the components  Swizzle or replicate components on right hand side foo = foo.wzyx; // another way to reverse components foo = foo.xxyy; // components reusable on right side v2.yyyy // wrong: too many components for type  Use indexing for vector and matrix component selection mat4 myMatrix = mat4(1.0); foo.x = foo[2]; // same as foo.x = foo.z; foo = myMatrix[0]; // first column of matrix foo.x = myMatrix[0][0]; // first column, first row Language Basics: swizzles

An Introduction to the OpenGL Shading Language 15 January 2008 Language Basics: flow control  for while do –Loops can have break, continue  if else  Function calls –Can have return –More on function calls in next slide  The above can all be nested!  Note: no unstructured jumps (a.k.a goto )  discard –Only available in fragment shaders –“Kills” the fragment, no further processing in the pipeline

An Introduction to the OpenGL Shading Language 16 January 2008 Language Basics: VS built-in variables Inputs  attribute vec4 gl_Vertex  attribute vec3 gl_Normal  attribute vec4 gl_Color  attribute vec4 gl_SecondaryColor  attribute vec4 gl_MultiTexCoordn (0-7)  attribute float gl_FogCoord Outputs  vec4 gl_Position : must be written!  float gl_PointSize  vec4 gl_ClipVertex  varying vec4 gl_FrontColor  varying vec4 gl_BackColor  varying vec4 gl_FrontSecondaryColor  varying vec4 gl_BackSecondaryColor  varying vec4 gl_TexCoord[n]  varying float gl_FogFragCoord

An Introduction to the OpenGL Shading Language 17 January 2008 Language Basics: FS built-in variables Inputs  vec4 gl_FragCoord  bool gl_FrontFacing  varying vec4 gl_Color  varying vec4 gl_SecondaryColor  varying vec4 gl_TexCoord[n]  varying float gl_FogFragCoord  varying vec2 gl_PointCoord Outputs  vec4 gl_FragColor  vec4 gl_FragData[n]  float gl_FragDepth

An Introduction to the OpenGL Shading Language 18 January 2008 Built-in variables Attributes & uniforms For ease of programming OpenGL state mapped to variables Some special variables are required to be written to, others are optional

An Introduction to the OpenGL Shading Language 19 January 2008 Special built-ins Vertex shader vec4 gl_Position; // must be written vec4 gl_ClipPosition; // may be written float gl_PointSize; // may be written Fragment shader float gl_FragColor; // may be written float gl_FragDepth; // may be read/written vec4 gl_FragCoord; // may be read bool gl_FrontFacing; // may be read

An Introduction to the OpenGL Shading Language 20 January 2008 Attributes Built-in attribute vec4 gl_Vertex; attribute vec3 gl_Normal; attribute vec4 gl_Color; attribute vec4 gl_SecondaryColor; attribute vec4 gl_MultiTexCoordn; attribute float gl_FogCoord; User-defined attribute vec3 myTangent; attribute vec3 myBinormal; Etc…

An Introduction to the OpenGL Shading Language 21 January 2008 Built-in Uniforms uniform mat4 gl_ModelViewMatrix; uniform mat4 gl_ProjectionMatrix; uniform mat4 gl_ModelViewProjectionMatrix; uniform mat3 gl_NormalMatrix; uniform mat4 gl_TextureMatrix[n]; struct gl_MaterialParameters { vec4 emission; vec4 ambient; vec4 diffuse; vec4 specular; float shininess; }; uniform gl_MaterialParameters gl_FrontMaterial; uniform gl_MaterialParameters gl_BackMaterial;

An Introduction to the OpenGL Shading Language 22 January 2008 Built-in Uniforms struct gl_LightSourceParameters { vec4 ambient; vec4 diffuse; vec4 specular; vec4 position; vec4 halfVector; vec3 spotDirection; float spotExponent; float spotCutoff; float spotCosCutoff; float constantAttenuation float linearAttenuation float quadraticAttenuation }; Uniform gl_LightSourceParameters gl_LightSource[gl_MaxLights];

An Introduction to the OpenGL Shading Language 23 January 2008 Built-in Varyings varying vec4 gl_FrontColor // vertex varying vec4 gl_BackColor; // vertex varying vec4 gl_FrontSecColor; // vertex varying vec4 gl_BackSecColor; // vertex varying vec4 gl_Color; // fragment varying vec4 gl_SecondaryColor; // fragment varying vec4 gl_TexCoord[]; // both varying float gl_FogFragCoord; // both

An Introduction to the OpenGL Shading Language 24 January 2008 Language Basics: function calls Special storage qualifiers apply to function parameters, e.g.: bool f(in vec2 inputVec, out float retVal) {... } in : Parameter is copied in to the function but not copied out (default) const in : Parameter is copied in to the function and cannot change out : Parameter is copied out of the function but not copied in inout : Parameter is both copied in and copied out Notes  Recursion is strictly forbidden!  Functions can return a value or void

An Introduction to the OpenGL Shading Language 25 January 2008 Built-in functions Angles & Trigonometry  radians, degrees, sin, cos, tan, asin, acos, atan Exponentials  pow, exp2, log2, sqrt, inversesqrt Common  abs, sign, floor, ceil, fract, mod, min, max, clamp

An Introduction to the OpenGL Shading Language 26 January 2008 Built-in functions Interpolations  mix(x,y,a)x*( 1.0-a) + y*a)  step(edge,x)x <= edge ? 0.0 : 1.0  smoothstep(edge0,edge1,x) t = (x-edge0)/(edge1-edge0); t = clamp( t, 0.0, 1.0); return t*t*( *t);

An Introduction to the OpenGL Shading Language 27 January 2008 Built-in functions Geometric  length, distance, cross, dot, normalize, faceForward, reflect Matrix  matrixCompMult Vector relational  lessThan, lessThanEqual, greaterThan, greaterThanEqual, equal, notEqual, notEqual, any, all

An Introduction to the OpenGL Shading Language 28 January 2008 Built-in functions Texture  texture1D, texture2D, texture3D, textureCube  texture1DProj, texture2DProj, texture3DProj, textureCubeProj  shadow1D, shadow2D, shadow1DProj, shadow2Dproj Vertex  ftransform

An Introduction to the OpenGL Shading Language 29 January 2008 Starter Shaders: color manipulation // simple.fs // // copy primary color void main(void) { // Copy the primary color gl_FragColor = gl_Color; } // colorinvert.fs // // invert like a color negative void main(void) { // invert color components gl_FragColor.rgb = gl_Color.rgb; gl_FragColor.a = 1.0; }

An Introduction to the OpenGL Shading Language 30 January 2008 Starter Shaders: color manipulation // grayscale.fs // // convert RGB to grayscale void main(void) { // Convert to grayscale using NTSC conversion weights float gray = dot(gl_Color.rgb, vec3(0.299, 0.587, 0.114)); // replicate grayscale to RGB components gl_FragColor = vec4(gray, gray, gray, 1.0); } // sepia.fs // // convert RGB to sepia tone void main(void) { // Convert to grayscale using NTSC conversion weights float gray = dot(gl_Color.rgb, vec3(0.299, 0.587, 0.114)); // convert grayscale to sepia gl_FragColor = vec4(gray * vec3(1.2, 1.0, 0.8), 1.0); }

An Introduction to the OpenGL Shading Language 31 January 2008 Starter Shaders: color manipulation // heatsig.fs // // map grayscale to heat signature uniform sampler1D sampler0; void main(void) { // Convert to grayscale using NTSC conversion weights float gray = dot(gl_Color.rgb, vec3(0.299, 0.587, 0.114)); // look up heatsig value gl_FragColor = texture1D(sampler0, gray); }

An Introduction to the OpenGL Shading Language 32 January 2008 Starter Shaders: color manipulation // fog.fs // // per-pixel fog uniform float density; void main(void) { const vec4 fogColor = vec4(0.5, 0.8, 0.5, 1.0); // calculate 2nd order exponential fog factor // based on fragment's Z distance const float e = ; float fogFactor = (density * gl_FragCoord.z); fogFactor *= fogFactor; fogFactor = clamp(pow(e, -fogFactor), 0.0, 1.0); // Blend fog color with incoming color gl_FragColor = mix(fogColor, gl_Color, fogFactor); }

An Introduction to the OpenGL Shading Language 33 January 2008 Starter Shaders: convolution // passthrough.fs // // pass through a single texel value uniform sampler2D sampler0; void main(void) { gl_FragColor = texture2D(sampler0, gl_TexCoord[0].st); }

An Introduction to the OpenGL Shading Language 34 January 2008 Starter Shaders: convolution // blur.fs // // blur (low-pass) 3x3 kernel uniform sampler2D sampler0; uniform vec2 tc_offset[9]; void main(void) { vec4 sample[9]; for (int i = 0; i < 9; i++) { sample[i] = texture2D(sampler0, gl_TexCoord[0].st + tc_offset[i]); } // // / 13 // gl_FragColor = (sample[0] + (2.0*sample[1]) + sample[2] + (2.0*sample[3]) + sample[4] + (2.0*sample[5]) + sample[6] + (2.0*sample[7]) + sample[8]) / 13.0; }

An Introduction to the OpenGL Shading Language 35 January 2008 Starter Shaders: convolution Blur2 1 2 / Sharpen LaPlacian Dilationmax(kernel) Erosionmin(kernel)

An Introduction to the OpenGL Shading Language 36 January 2008 Starter Shaders: vertex shaders // simple.vs // // Generic vertex transformation, // copy primary color void main(void) { // normal MVP transform gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex; // Copy the primary color gl_FrontColor = gl_Color; }

An Introduction to the OpenGL Shading Language 37 January 2008 Starter Shaders: vertex shaders // diffuse.vs // // Generic vertex transformation, // diffuse lighting based on one // white light uniform vec3 lightPos[1]; void main(void) { // normal MVP transform gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex; vec3 N = normalize(gl_NormalMatrix * gl_Normal); vec4 V = gl_ModelViewMatrix * gl_Vertex; vec3 L = normalize(lightPos[0] - V.xyz); // output the diffuse color float NdotL = dot(N, L); gl_FrontColor = gl_Color * vec4(max(0.0, NdotL)); }

An Introduction to the OpenGL Shading Language 38 January 2008 Example: Fragment Shader varying vec4 diffuseColor; varying vec3 lightVector; varying vec3 fragNormal; void main(){ float perFragmentLighting=max(dot(lightVector,fragNormal),0.0); gl_FragColor = diffuseColor * lightingFactor; }

An Introduction to the OpenGL Shading Language 39 January 2008 Starter Shaders: vertex shaders // ptsize.vs // // Generic vertex transformation, // attenuated point size void main(void) { // normal MVP transform gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex; vec4 V = gl_ModelViewMatrix * gl_Vertex; gl_FrontColor = gl_Color; // calculate point size based on distance from eye float ptSize = length(V); ptSize = ptSize * ptSize * ptSize; gl_PointSize = / ptSize; }

An Introduction to the OpenGL Shading Language 40 January 2008 Starter Shaders: vertex shaders // stretch.vs // // Generic vertex transformation, // followed by squash/stretch uniform vec3 lightPos[1]; uniform vec3 squashStretch; void main(void) { // normal MVP transform, followed by squash/stretch vec4 stretchedCoord = gl_Vertex; stretchedCoord.xyz *= squashStretch; gl_Position = gl_ModelViewProjectionMatrix * stretchedCoord;... }

An Introduction to the OpenGL Shading Language 41 January 2008 Windows API Available now through 4 extensions  GL_ARB_shader_objects, GL_ARB_shading_language_100, GL_ARB_vertex_shader, GL_ARB_fragment_shader Will be core in OpenGL 2.0  With very minor modifications

An Introduction to the OpenGL Shading Language 42 January 2008 Basic method 2 basic object types  Shader object  Program object Create Vertex & Fragment Shader Objects Compile both Create program object & attach shaders Link program Use program

An Introduction to the OpenGL Shading Language 43 January 2008 Creating objects GLhandleARB glCreateProgramObjectARB(); GLhandleARB glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB); GLhandleARB glCreateShaderObjectARB(GL_FRAGMENT_SHADER_ARB);

An Introduction to the OpenGL Shading Language 44 January 2008 Compiling void glShaderSourceARB(GLhandleARB shader, GLsizei nstrings, const GLcharARB **strings, const GLint *lengths) //if lengths==NULL, assumed to be null-terminated void glCompileShaderARB(GLhandleARB shader);

An Introduction to the OpenGL Shading Language 45 January 2008 Attaching & Linking void glAttachObjectARB(GLhandleARB program, GLhandleARB shader); //twice, once for vertex shader & once for fragment shader void glLinkProgramARB(GLhandleARB program); //program now ready to use void glUseProgramObjectARB(GLhandleARB program); //switches on shader, bypasses FFP //if program==0, shaders turned off, returns to FFP

An Introduction to the OpenGL Shading Language 46 January 2008 In short… GLhandleARB programObject; GLhandleARB vertexShaderObject; GLhandleARB fragmentShaderObject; unsigned char *vertexShaderSource = readShaderFile(vertexShaderFilename); unsigned char *fragmentShaderSource = readShaderFile(fragmentShaderFilename); programObject=glCreateProgramObjectARB(); vertexShaderObject=glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB); fragmentShaderObject=glCreateShaderObjectARB(GL_FRAGMENT_SHADER_ARB); glShaderSourceARB(vertexShaderObject,1,(const char**)&vertexShaderSource,NULL); glShaderSourceARB(fragmentShaderObject,1,(const char**)&fragmentShaderSource,NULL); glCompileShaderARB(vertexShaderObject); glCompileShaderARB(fragmentShaderObject); glAttachObjectARB(programObject, vertexShaderObject); glAttachObjectARB(programObject, fragmentShaderObject); glLinkProgramARB(programObject); glUseProgramObjectARB(programObject);

An Introduction to the OpenGL Shading Language 47 January 2008 Other functions Clean-up void glDetachObjectARB(GLhandleARB container, GLhandleARB attached); void glDeleteObjectARB(GLhandleARB object); Info Log void glGetInfoLogARB(GLhandleARB object, GLsizei maxLength, GLsizei *length, GLcharARB *infoLog);  Returns compile & linking information, errors

An Introduction to the OpenGL Shading Language 48 January 2008 Loading Uniforms void glUniform{1|2|3|4}{f|i}ARB(GLint location,…); Location obtained with GLint glGetUniformLocationARB(GLhandleARB program, const GLcharARB *name); Shader must be enabled with glUseProgramObjectARB() before uniforms can be loaded

An Introduction to the OpenGL Shading Language 49 January 2008 Loading Attributes void glVertexAttrib{1234}{sfd}ARB(GLint index,…); Index obtained with GLint glGetAttribLocationARB(GLhandleARB program, const GLcharARB *name); Alternate method void glBindAttribLocationARB(GLhandleARB program, GLuint index, const GLcharARB *name);  Program must be linked after binding attrib locations

An Introduction to the OpenGL Shading Language 50 January 2008 Loading Textures Bind textures to different units as usual glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D,myFirstTexture); glActiveTexture(GL_TEXTURE1); glBindTexture(GL_TEXTURE_2D,mySecondTexture); Then load corresponding sampler with texture unit that texture is bound to glUniform1iARB(glGetUniformLocationARB( programObject,”myFirstSampler”),0); glUniform1iARB(glGetUniformLocationARB( programObject,”mySecondSampler”),1);

An Introduction to the OpenGL Shading Language 51 January 2008 Ivory – vertex shader uniform vec4 lightPos; varying vec3 normal; varying vec3 lightVec; varying vec3 viewVec; void main(){ gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex; vec4 vert = gl_ModelViewMatrix * gl_Vertex; normal = gl_NormalMatrix * gl_Normal; lightVec = vec3(lightPos - vert); viewVec = -vec3(vert); }

An Introduction to the OpenGL Shading Language 52 January 2008 Ivory – fragment shader varying vec3 normal; varying vec3 lightVec; varying vec3 viewVec; void main(){ vec3 norm = normalize(normal); vec3 L = normalize(lightVec); vec3 V = normalize(viewVec); vec3 halfAngle = normalize(L + V); float NdotL = dot(L, norm); float NdotH = clamp(dot(halfAngle, norm), 0.0, 1.0); // "Half-Lambert" technique for more pleasing diffuse term float diffuse = 0.5 * NdotL + 0.5; float specular = pow(NdotH, 64.0); float result = diffuse + specular; gl_FragColor = vec4(result); }

An Introduction to the OpenGL Shading Language 53 January 2008 Gooch – vertex shader uniform vec4 lightPos; varying vec3 normal; varying vec3 lightVec; varying vec3 viewVec; void main(){ gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex; vec4 vert = gl_ModelViewMatrix * gl_Vertex; normal = gl_NormalMatrix * gl_Normal; lightVec = vec3(lightPos - vert); viewVec = -vec3(vert); }

An Introduction to the OpenGL Shading Language 54 January 2008 Gooch – fragment shader uniform vec3 ambient; varying vec3 normal; varying vec3 lightVec; varying vec3 viewVec; void main(){ const float b = 0.55; const float y = 0.3; const float Ka = 1.0; const float Kd = 0.8; const float Ks = 0.9; vec3 specularcolor = vec3(1.0, 1.0, 1.0); vec3 norm = normalize(normal); vec3 L = normalize (lightVec); vec3 V = normalize (viewVec); vec3 halfAngle = normalize (L + V);

An Introduction to the OpenGL Shading Language 55 January 2008 Gooch – fragment shader (2) vec3 orange = vec3(.88,.81,.49); vec3 purple = vec3(.58,.10,.76); vec3 kCool = purple; vec3 kWarm = orange; float NdotL = dot(L, norm); float NdotH = clamp(dot(halfAngle, norm), 0.0, 1.0); float specular = pow(NdotH, 64.0); float blendval = 0.5 * NdotL + 0.5; vec3 Cgooch = mix(kWarm, kCool, blendval); vec3 result = Ka * ambient + Kd * Cgooch + specularcolor * Ks * specular; gl_FragColor = vec4(result, 1.0); }

An Introduction to the OpenGL Shading Language 56 January 2008 Useful References  Home page for the “orange book” focused solely on GLSL  OpenGL SDK, including links to the below resources  one double-sided page cheat sheet to GLSL – indispensible!  This is the ultimate authority: the GLSL specification document  Full reference and tutorial to OpenGL 2.1  All sample code downloadable for Windows, Mac OS X, and Linux