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

Slides:



Advertisements
Similar presentations
Fragment level programmability in OpenGL Evan Hart
Advertisements

Week 4 Lecture 1: OpenGL 3.x & 4.x. 2 Objectives Changes in OpenGL 3.x 4.x Changes in GLSL 1.3/4/5 4.x.
Perspective aperture ygyg yryr n zgzg y s = y g (n/z g ) ysys y s = y r (n/z r ) zrzr.
GLSL Basics Discussion Lecture for CS 418 Spring 2015 TA: Zhicheng Yan, Sushma S Kini, Mary Pietrowicz.
EECS 700: Computer Modeling, Simulation, and Visualization Dr. Shontz Chapter 3: Lighting and Materials.
Informationsteknologi Wednesday, December 12, 2007Computer Graphics - Class 171 Today’s class OpenGL Shading Language.
The Programmable Graphics Hardware Pipeline Doug James Asst. Professor CS & Robotics.
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.
Programmable Shading May 21, Motivation Recall what are done in the graphics pipeline: –Front End: Transformations (Modeling, Viewing, and Projection)
Under the Hood: 3D Pipeline. Motherboard & Chipset PCI Express x16.
Cg – C for Graphics Eric Vidal CS 280. History General graphics processing languages – Renderman shading language (1988) Assembly languages for graphics.
REAL-TIME VOLUME GRAPHICS Christof Rezk Salama Computer Graphics and Multimedia Group, University of Siegen, Germany Eurographics 2006 Real-Time Volume.
COMP 261 Lecture 14 3D Graphics 2 of 2. Doing better than 3x3? Translation, scaling, rotation are different. Awkward to combine them. Use homogeneous.
An Introduction to the OpenGL Shading Language Benj Lipchak Rob Simpson Bill Licea-Kane.
Under the Hood: 3D Pipeline. Motherboard & Chipset PCI Express x16.
GPU Programming Robert Hero Quick Overview (The Old Way) Graphics cards process Triangles Graphics cards process Triangles Quads.
Programmable Pipelines. Objectives Introduce programmable pipelines ­Vertex shaders ­Fragment shaders Introduce shading languages ­Needed to describe.
Real-time Graphical Shader Programming with Cg (HLSL)
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.
1 Introduction to Computer Graphics with WebGL Ed Angel Professor Emeritus of Computer Science Founding Director, Arts, Research, Technology and Science.
CSE 690: GPGPU Lecture 6: Cg Tutorial Klaus Mueller Computer Science, Stony Brook University.
GAM532 DPS932 – Week 2 Vertex Shaders. The Shader Pipeline Vertex Processing Primitive Assembly / Processing Rasterization Fragment Process Pixel Output.
OpenGL Shading Language Jian Huang CS594, Spring 2005.
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.
Shaders in OpenGL Marshall Hahn. Introduction to Shaders in OpenGL In this talk, the basics of OpenGL Shading Language will be covered. This includes.
CS418 Computer Graphics John C. Hart
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)
Review on Graphics Basics. Outline Polygon rendering pipeline Affine transformations Projective transformations Lighting and shading From vertices to.
OpenGL Shading Language (GLSL)
Week 3 Lecture 4: Part 2: GLSL I Based on Interactive Computer Graphics (Angel) - Chapter 9.
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.
OpenGL Shading Language
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.
Advanced Texture Mapping Bump Mapping & Environment Mapping (Reflection)
An Introduction to the Cg Shading Language Marco Leon Brandeis University Computer Science Department.
GLSL Review Monday, Nov OpenGL pipeline Command Stream Vertex Processing Geometry processing Rasterization Fragment processing Fragment Ops/Blending.
COMP 175 | COMPUTER GRAPHICS Remco Chang1/XX13 – GLSL Lecture 13: OpenGL Shading Language (GLSL) COMP 175: Computer Graphics April 12, 2016.
Shader.
CSE 381 – Advanced Game Programming GLSL Syntax
Programmable Pipelines
Carl Johan Gribel, PhD student
GLSL I Ed Angel Professor of Computer Science, Electrical and Computer Engineering, and Media Arts Director, Arts Technology Center University of New Mexico.
Day 05 Shader Basics.
Chapter VI OpenGL ES and Shader
Introduction to Programmable Hardware
Introduction to Computer Graphics with WebGL
ICG 2018 Fall Homework1 Guidance
Programmable Shading May 15, 2006.
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
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 Keith O’Conor

Outline How the fixed function pipeline worksHow the fixed function pipeline works How it’s replaced by GLSLHow it’s replaced by GLSL Structure & syntax nitty-grittyStructure & syntax nitty-gritty How to integrate GLSL into OpenGL appsHow to integrate GLSL into OpenGL apps Some simple examplesSome simple examples ResourcesResources

Fixed Function Vertex Processor Transform [MVP],[MV],[MV] -T Lighting [0,1] [0,1] Texgen Texture Matrixn Color SecondaryColor TexCoordn EdgeFlag Normal Vertex (object) TexCoordn EdgeFlag Vertex (eye) Vertex (clip) Front&Back Color Front&Back SecondaryColor

GL2 Vertex Processor Attributen Attribute1 Attribute0 Attribute2 EdgeFlag Temporaries Vertex Shader Uniform EdgeFlag Texture … Varyingn Varying1 Varying0 Varying2 … ClipVertex Position PointSize

Fixed Function Fragment Processor FrontFacing Color Coord Depth Tex n TE n SumFog [0,1] Coord FrontFacing Color SecondaryColor TexCoord[n] zz e z (|z e |,f ) Depth

GL2 Fragment Processor FrontFacing FragCoord FrontFacing FragColor FragCoord FragDepth Temporaries Fragment Shader Uniform Texture Varyingn Varying1 Varying0 Varying2 …

In General… Vertex processes bypassedVertex processes bypassed –Vertex Transformation –Normal Transformation, Normalization –Lighting –Texture Coordinate Generation and Transformation Fragment processes bypassedFragment processes bypassed –Texture accesses & application –Fog

Previous programmability Texture ShadersTexture Shaders Register CombinersRegister Combiners Assembly programsAssembly programs –ARB_vertex_program –ARB_fragment_program –Messy! Needed general, readable & maintainable languageNeeded general, readable & maintainable language

Types void float vec2 vec3 vec4 mat2 mat3 mat4 int ivec2 ivec3 ivec4 bool bvec2 bvec3 bvec4 samplernD, samplerCube, samplerShadownD

Types StructsStructs ArraysArrays –One dimensional –Constant size (ie float array[4]; ) Reserved typesReserved types –half hvec2 hvec3 hvec4 –fixed fvec2 fvec3 fvec4 –double dvec2 dvec3 dvec4

Type qualifiers attributeattribute –Changes per-vertex eg. position, normal etc.eg. position, normal etc. uniformuniform –Does not change between vertices of a batch eg light position, texture unit, other constantseg light position, texture unit, other constants varyingvarying –Passed from VS to FS, interpolated eg texture coordinates, vertex coloreg texture coordinates, vertex color

Operators grouping: ()grouping: () array subscript: []array subscript: [] function call and constructor: ()function call and constructor: () field selector and swizzle:.field selector and swizzle:. postfix: ++ --postfix: prefix: !prefix: !

Operators binary: * / + -binary: * / + - relational: >=relational: >= equality: == !=equality: == != logical: && ^^ ||logical: && ^^ || selection: ?:selection: ?: assignment: = *= /= += -=assignment: = *= /= += -=

Reserved Operators prefix: ~prefix: ~ binary: %binary: % bitwise: > & ^ |bitwise: > & ^ | assignment: %= >= &= ^= |=assignment: %= >= &= ^= |=

Scalar/Vector Constructors No castingNo casting float f; int i; bool b; vec2 v2; vec3 v3; vec4 v4; vec2(1.0,2.0) vec3(0.0,0.0,1.0) vec4(1.0,0.5,0.0,1.0) vec4(1.0) // all 1.0 vec4(v2,v2) vec4(v3,1.0) float(i)int(b)

Matrix Constructors vec4 v4; mat4 m4; mat4( 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10., 11., 12., 9.0, 10., 11., 12., 13., 14., 15., 16.) // row major 13., 14., 15., 16.) // row major mat4( v4, v4, v4, v4) mat4( 1.0) // identity matrix mat3( m4) // upper 3x3 vec4( m4) // 1st column float( m4) // upper 1x1

Accessing components component accessor for vectorscomponent accessor for vectors –xyzw rgba stpq [i] component accessor for matricescomponent accessor for matrices –[i] [i][j]

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

Swizzling & Smearing R-valuesR-values vec2 v2; vec3 v3; vec4 v4; v4.wzyx // swizzles, is a vec4 v4.bgra // swizzles, is a vec4 v4.xxxx // smears x, is a vec4 v4.xxx // smears x, is a vec3 v4.yyxx // duplicates x and y, is a vec4 v2.yyyy // wrong: too many components for type

Vector Components L-valuesL-values vec4 v4 = vec4( 1.0, 2.0, 3.0, 4.0); v4.xw = vec2( 5.0, 6.0); // (5.0, 2.0, 3.0, 6.0) v4.wx = vec2( 7.0, 8.0); // (8.0, 2.0, 3.0, 7.0) v4.xx = vec2( 9.0,10.0); // wrong: x used twice v4.yz = 11.0; // wrong: type mismatch v4.yz = vec2( 12.0 ); // (8.0,12.0,12.0, 7.0)

Flow Control expression ? trueExpression : falseExpressionexpression ? trueExpression : falseExpression if, if-elseif, if-else for, while, do-whilefor, while, do-while return, break, continuereturn, break, continue discard (fragment only)discard (fragment only)

Built-in variables Attributes & uniformsAttributes & uniforms For ease of programmingFor ease of programming OpenGL state mapped to variablesOpenGL state mapped to variables Some special variables are required to be written to, others are optionalSome special variables are required to be written to, others are optional

Special built-ins Vertex shaderVertex shader vec4 gl_Position; // must be written vec4 gl_ClipPosition; // may be written float gl_PointSize; // may be written Fragment shaderFragment 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

Attributes Built-inBuilt-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-definedUser-defined attribute vec3 myTangent; attribute vec3 myBinormal; Etc…

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 emission; vec4 ambient; vec4 ambient; vec4 diffuse; vec4 diffuse; vec4 specular; vec4 specular; float shininess; float shininess;}; uniform gl_MaterialParameters gl_FrontMaterial; uniform gl_MaterialParameters gl_BackMaterial;

Built-in Uniforms struct gl_LightSourceParameters { vec4 ambient; vec4 ambient; vec4 diffuse; vec4 diffuse; vec4 specular; vec4 specular; vec4 position; vec4 position; vec4 halfVector; vec4 halfVector; vec3 spotDirection; vec3 spotDirection; float spotExponent; float spotExponent; float spotCutoff; float spotCutoff; float spotCosCutoff; float spotCosCutoff; float constantAttenuation float constantAttenuation float linearAttenuation float linearAttenuation float quadraticAttenuation float quadraticAttenuation}; Uniform gl_LightSourceParameters gl_LightSource[gl_MaxLights];

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

Built-in functions Angles & TrigonometryAngles & Trigonometry –radians, degrees, sin, cos, tan, asin, acos, atan ExponentialsExponentials –pow, exp2, log2, sqrt, inversesqrt CommonCommon –abs, sign, floor, ceil, fract, mod, min, max, clamp

Built-in functions InterpolationsInterpolations –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);

Built-in functions GeometricGeometric –length, distance, cross, dot, normalize, faceForward, reflect MatrixMatrix –matrixCompMult Vector relationalVector relational –lessThan, lessThanEqual, greaterThan, greaterThanEqual, equal, notEqual, notEqual, any, all

Built-in functions TextureTexture –texture1D, texture2D, texture3D, textureCube –texture1DProj, texture2DProj, texture3DProj, textureCubeProj –shadow1D, shadow2D, shadow1DProj, shadow2Dproj VertexVertex –ftransform

Example: Vertex Shader varying vec4 diffuseColor; varying vec3 fragNormal; varying vec3 lightVector; uniform vec3 eyeSpaceLightVector; void main(){ vec3 eyeSpaceVertex= vec3(gl_ModelViewMatrix * gl_Vertex); lightVector= vec3(normalize(eyeSpaceLightVector - eyeSpaceVertex)); fragNormal = normalize(gl_NormalMatrix * gl_Normal); diffuseColor = gl_Color; gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex; }

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; }

Windows API Available now through 4 extensionsAvailable 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.0Will be core in OpenGL 2.0 –With very minor modifications

Basic method 2 basic object types2 basic object types –Shader object –Program object Create Vertex & Fragment Shader ObjectsCreate Vertex & Fragment Shader Objects Compile bothCompile both Create program object & attach shadersCreate program object & attach shaders Link programLink program Use programUse program

Creating objects GLhandleARB glCreateProgramObjectARB(); GLhandleARB glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB); GLhandleARB glCreateShaderObjectARB(GL_FRAGMENT_SHADER_ARB);

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);

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

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);

Other functions Clean-upClean-up void glDetachObjectARB(GLhandleARB container, GLhandleARB attached); void glDeleteObjectARB(GLhandleARB object); Info LogInfo Log void glGetInfoLogARB(GLhandleARB object, GLsizei maxLength, GLsizei *length, GLcharARB *infoLog); –Returns compile & linking information, errors

Loading Uniforms void glUniform{1|2|3|4}{f|i}ARB(GLint location,…); Location obtained withLocation obtained with GLint glGetUniformLocationARB(GLhandleARB program, const GLcharARB *name); Shader must be enabled with glUseProgramObjectARB() before uniforms can be loadedShader must be enabled with glUseProgramObjectARB() before uniforms can be loaded

Loading Attributes void glVertexAttrib{1234}{sfd}ARB(GLint index,…); Index obtained withIndex obtained with GLint glGetAttribLocationARB(GLhandleARB program, const GLcharARB *name); Alternate methodAlternate method void glBindAttribLocationARB(GLhandleARB program, GLuint index, const GLcharARB *name); –Program must be linked after binding attrib locations

Loading Textures Bind textures to different units as usualBind textures to different units as usualglActiveTexture(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 toThen load corresponding sampler with texture unit that texture is bound to glUniform1iARB(glGetUniformLocationARB( programObject,”myFirstSampler”),0); glUniform1iARB(glGetUniformLocationARB( programObject,”mySecondSampler”),1);

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); }

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); float NdotH = clamp(dot(halfAngle, norm), 0.0, 1.0); // "Half-Lambert" technique for more pleasing diffuse term // "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); }

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); }

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);

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); }