OpenGL Shading Language Course Chapter 1 – Introduction to GLSL
Computer Graphics 3 Lecture 5: OpenGL Shading Language (GLSL)
description
Transcript of Computer Graphics 3 Lecture 5: OpenGL Shading Language (GLSL)
![Page 1: Computer Graphics 3 Lecture 5: OpenGL Shading Language (GLSL)](https://reader035.fdocuments.in/reader035/viewer/2022062309/56814ab6550346895db7c889/html5/thumbnails/1.jpg)
Computer Graphics 3Lecture 5:OpenGL
Shading Language(GLSL)
Benjamin Mora 1University of Wales
Swansea
Dr. Benjamin Mora
![Page 2: Computer Graphics 3 Lecture 5: OpenGL Shading Language (GLSL)](https://reader035.fdocuments.in/reader035/viewer/2022062309/56814ab6550346895db7c889/html5/thumbnails/2.jpg)
Content
2Benjamin MoraUniversity of Wales
Swansea
• Introduction.
• How to include GLSL programs in your code.– GLEW, API
• Variables and Types.
• Vertex Processor.
• Fragment Processor.
• Samplers
• Built-in functions.
• Applications.
![Page 3: Computer Graphics 3 Lecture 5: OpenGL Shading Language (GLSL)](https://reader035.fdocuments.in/reader035/viewer/2022062309/56814ab6550346895db7c889/html5/thumbnails/3.jpg)
Introduction
3Benjamin MoraUniversity of Wales
Swansea
![Page 4: Computer Graphics 3 Lecture 5: OpenGL Shading Language (GLSL)](https://reader035.fdocuments.in/reader035/viewer/2022062309/56814ab6550346895db7c889/html5/thumbnails/4.jpg)
Vertex and Fragment Programs
4Benjamin MoraUniversity of Wales
Swansea
Setup
Rasterization
Frame Buffer Blending
Texture Fetch, Fragment Shading
Tests (z, stencil…)
Vertices
Transform And Lighting
Vertex Programs:User-Defined Vertex
Processing
Fragment Programs:User-Defined
Per-Pixel Processing
![Page 5: Computer Graphics 3 Lecture 5: OpenGL Shading Language (GLSL)](https://reader035.fdocuments.in/reader035/viewer/2022062309/56814ab6550346895db7c889/html5/thumbnails/5.jpg)
Introduction
5Benjamin MoraUniversity of Wales
Swansea
• Introduced with OpenGL 2.0.• High Level Language.
– Real shaders implementation hidden inside drivers.
• C/C++ like coding.– Some differences.– Stronger typing.– Language still heavily influenced by current hardware design.– Still somehow messy…
• Compatible with future pipelines.• Replaces fixed vertex and pixel pipelines.
– Geometry shader available as an extension.– OpenGL 3.0 adds some functionalities.
![Page 6: Computer Graphics 3 Lecture 5: OpenGL Shading Language (GLSL)](https://reader035.fdocuments.in/reader035/viewer/2022062309/56814ab6550346895db7c889/html5/thumbnails/6.jpg)
How to code/debug
6Benjamin MoraUniversity of Wales
Swansea
• Understanding of the whole pipeline needed.• Thorough analysis of the algorithm/solution first.
– Favor simple solutions unless big performance issues.
• Start with a very simple shader that achieves a simple task.– Test and iterate your code until program is finalized.
• Frame rate proportional to code efficiency.– Thoroughly analyze your problem again…– Check for redundancies, memory access, etc…– Use textures for emulating/pre-computing complex
functions
![Page 7: Computer Graphics 3 Lecture 5: OpenGL Shading Language (GLSL)](https://reader035.fdocuments.in/reader035/viewer/2022062309/56814ab6550346895db7c889/html5/thumbnails/7.jpg)
How to code/debug
7Benjamin MoraUniversity of Wales
Swansea
• Debugging: – Again difficult.– Can test variables/temporary results by returning specific
colors.
• Tools:– RenderMonkey (ATI).– glslDevil
• http://www.vis.uni-stuttgart.de/glsldevil/– gDEBugger (30-days trial version).
![Page 8: Computer Graphics 3 Lecture 5: OpenGL Shading Language (GLSL)](https://reader035.fdocuments.in/reader035/viewer/2022062309/56814ab6550346895db7c889/html5/thumbnails/8.jpg)
Shader Loading
8Benjamin MoraUniversity of Wales
Swansea
• Shaders should be ideally stored in a separate text file.• Needs to be loaded into your program as a string (char *)
variable, and then sent to the API.– Several different shaders can be stored by the API and interchanged
during rendering.– Cannot be changed between glBegin(…) and glEnd() calls.
…Char *myVertexProgram;LoadText(myVertexProgram,
“VertexProgram.shader”);//Use now the OpenGL 2.0 API //to compile and enable the program…
myProgram.c
void main(){ gl_Position=gl_ModelviewProjectionMatrix
* gl_Vertex;}
VertexProgram.shader
![Page 9: Computer Graphics 3 Lecture 5: OpenGL Shading Language (GLSL)](https://reader035.fdocuments.in/reader035/viewer/2022062309/56814ab6550346895db7c889/html5/thumbnails/9.jpg)
Shader Loading
9Benjamin MoraUniversity of Wales
Swansea
• Loading a shader object (vertex or fragment) requires several steps:– Create a shader object
• glCreateShader.
– Associate the source code to the shader object• glShaderSource.
– Compile the shader.• glCompileShader
– Attach the shader to a program object (container for shaders)• glAttachShader
– Link the compiled shader to a program.• glLinkShader
– Replace the fixed pipeline with the program object.• glUseProgram.
![Page 10: Computer Graphics 3 Lecture 5: OpenGL Shading Language (GLSL)](https://reader035.fdocuments.in/reader035/viewer/2022062309/56814ab6550346895db7c889/html5/thumbnails/10.jpg)
Shader Loading:Example
10Benjamin MoraUniversity of Wales
Swansea
char *myVertexProgram;
char *myFragmentProgram;
GLuint vShader, fShader, program;
…
vShader=glCreateShader(GL_VERTEX_SHADER);
fShader=glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(vShader, 1, & myVertexProgram, NULL);
glShaderSource(fShader, 1, & myFragmentProgram, NULL);
glCompileShader(vShader);
glCompileShader(fShader);
//Source strings can now be deleted
![Page 11: Computer Graphics 3 Lecture 5: OpenGL Shading Language (GLSL)](https://reader035.fdocuments.in/reader035/viewer/2022062309/56814ab6550346895db7c889/html5/thumbnails/11.jpg)
Shader Loading:Example
11Benjamin MoraUniversity of Wales
Swansea
program=glCreateProgram(); //Creates a program object
glAttachShader(program, vShader);
glAttachShader(program, fShader);
glLinkProgram(program);
glUseProgram(program);
//can come back to a fixed pipeline by passing NULL instead
…
//Don’t forget :
//Objects must be deleted when not needed anymore
![Page 12: Computer Graphics 3 Lecture 5: OpenGL Shading Language (GLSL)](https://reader035.fdocuments.in/reader035/viewer/2022062309/56814ab6550346895db7c889/html5/thumbnails/12.jpg)
Variables and Types
12Benjamin MoraUniversity of Wales
Swansea
![Page 13: Computer Graphics 3 Lecture 5: OpenGL Shading Language (GLSL)](https://reader035.fdocuments.in/reader035/viewer/2022062309/56814ab6550346895db7c889/html5/thumbnails/13.jpg)
Types
13Benjamin MoraUniversity of Wales
Swansea
• Simple types.• Structures (struct keyword) and arrays
possible. • No Pointer!• Implicit conversion generally not possible.• Scalar types:
– float, bool, int• int at least in the range [-65535, 65535]
– Declaration:• float f,g=1.0;
![Page 14: Computer Graphics 3 Lecture 5: OpenGL Shading Language (GLSL)](https://reader035.fdocuments.in/reader035/viewer/2022062309/56814ab6550346895db7c889/html5/thumbnails/14.jpg)
Types
14Benjamin MoraUniversity of Wales
Swansea
• Vector types: – vec2, vec3, vec4: Vectors of floats.– bvec2, bvec3, bvec4: Vectors of booleans.– ivec2, ivec3, ivec4: Vectors of integers.– Declaration: vec3 v=vec3(1.0,0.0,3.0);
• Vector components:– .xyzw, for vectors representing positions.– .rgba, for vectors representing colors– .stqp, for vectors representing texture coordinates. – Designation not compulsory.
![Page 15: Computer Graphics 3 Lecture 5: OpenGL Shading Language (GLSL)](https://reader035.fdocuments.in/reader035/viewer/2022062309/56814ab6550346895db7c889/html5/thumbnails/15.jpg)
Types
15Benjamin MoraUniversity of Wales
Swansea
• Swizzling examples: – float f;
vec4 v;
vec2 v2=v.ww;
vec3 v3=v.xzy;
v2=vec2(3.0,-1.0);
v2=texture1D(sampler,coordinate).xy;
v=v+f; //f is added to the 4 components of v!
v+=v; //Component-wise addition
![Page 16: Computer Graphics 3 Lecture 5: OpenGL Shading Language (GLSL)](https://reader035.fdocuments.in/reader035/viewer/2022062309/56814ab6550346895db7c889/html5/thumbnails/16.jpg)
Types
16Benjamin MoraUniversity of Wales
Swansea
• Matrices (of floats, square):– mat2, mat3, mat4;– mat4 m;
vec4 v=m[2];
float f=m[2][2];
• Row and columns inverted in OpenGL conventions!– m[2] is the third column of the matrix.
• Don’t use oversized vector and matrices if not required.
![Page 17: Computer Graphics 3 Lecture 5: OpenGL Shading Language (GLSL)](https://reader035.fdocuments.in/reader035/viewer/2022062309/56814ab6550346895db7c889/html5/thumbnails/17.jpg)
Types
17Benjamin MoraUniversity of Wales
Swansea
• Structure :– Struct light {
vec3 position;
vec3 color;
float watt; //could be actually stored with color
}
light myLight;
• No typedef!
![Page 18: Computer Graphics 3 Lecture 5: OpenGL Shading Language (GLSL)](https://reader035.fdocuments.in/reader035/viewer/2022062309/56814ab6550346895db7c889/html5/thumbnails/18.jpg)
Types
18Benjamin MoraUniversity of Wales
Swansea
• Arrays:– vec3 vertices[20];– vec3 vertices2[];
//Also possible. Size must be determinable at compilation //time. See manual & specs.
• Special case: texture coordinate array.– Internally declared as:– varying vec4 gl_TexCoord[];
![Page 19: Computer Graphics 3 Lecture 5: OpenGL Shading Language (GLSL)](https://reader035.fdocuments.in/reader035/viewer/2022062309/56814ab6550346895db7c889/html5/thumbnails/19.jpg)
Types
19Benjamin MoraUniversity of Wales
Swansea
• Samplers– Texture variables.
• sampler1D• sampler2D• sampler3D• samplerCube• sampler1DShadow• sampler2DShadow
– Declaration:• Uniform sampler2D brick;
vec4 col=texture2D(brick, texCoordinate);
![Page 20: Computer Graphics 3 Lecture 5: OpenGL Shading Language (GLSL)](https://reader035.fdocuments.in/reader035/viewer/2022062309/56814ab6550346895db7c889/html5/thumbnails/20.jpg)
Types: Samplers
20Benjamin MoraUniversity of Wales
Swansea
• Example– C/C++ core program:
glActiveTexture(GL_TEXTURE0);glBindTexture(GL_TEXTURE_2D, marbleTex);texLoc=glGetUniformLocation(myProgram,
“marbleTexture”);glUniform1i(texLoc,0);
– Vertex Program: varying vec2 coord; …coord = gl_MultiTexCoord0.st; //Get the tex coordinates.
![Page 21: Computer Graphics 3 Lecture 5: OpenGL Shading Language (GLSL)](https://reader035.fdocuments.in/reader035/viewer/2022062309/56814ab6550346895db7c889/html5/thumbnails/21.jpg)
Types: Samplers
21Benjamin MoraUniversity of Wales
Swansea
• Example– Fragment Program:
varying vec2 coord;
uniform sampler2D marbleTexture; //texture object.
…
gl_FragColor = texture2D(marbleTexture, coord);
![Page 22: Computer Graphics 3 Lecture 5: OpenGL Shading Language (GLSL)](https://reader035.fdocuments.in/reader035/viewer/2022062309/56814ab6550346895db7c889/html5/thumbnails/22.jpg)
Types
22Benjamin MoraUniversity of Wales
Swansea
• Qualifiers:– attribute
• For frequently changing variables, typically what would be passed to OpenGL between glBegin(…) and glEnd().
• Built-in attributes include gl_Vertex, gl_Normal,…
– uniform• For not-so-frequently changing variables, typically
what would be passed to OpenGL outside of a glBegin(…)/glEnd() section.
• At most changed once per primitive. • Read-only.
![Page 23: Computer Graphics 3 Lecture 5: OpenGL Shading Language (GLSL)](https://reader035.fdocuments.in/reader035/viewer/2022062309/56814ab6550346895db7c889/html5/thumbnails/23.jpg)
Types
23Benjamin MoraUniversity of Wales
Swansea
• Qualifiers:– varying
• For variables passed from the vertex shader to the fragment shader. These variables undergo a linear interpolation.
• Variable declation must be consistent across the vertex and fragment programs.
• Perspectively correct.
– const • Variable value fixed at compilation time. Cannot be modifier
• The first 3 qualifiers must be global variables.• No qualifier means a read/write variable local to the
shader.
![Page 24: Computer Graphics 3 Lecture 5: OpenGL Shading Language (GLSL)](https://reader035.fdocuments.in/reader035/viewer/2022062309/56814ab6550346895db7c889/html5/thumbnails/24.jpg)
Types
24Benjamin MoraUniversity of Wales
Swansea
• Functions:– Functions can be written locally.– Call by value-return.– Parameter qualifiers:
• in• out• inout• const
– In addition to previous qualifiers
• Example:– float norm(in vec3 v) {…
![Page 25: Computer Graphics 3 Lecture 5: OpenGL Shading Language (GLSL)](https://reader035.fdocuments.in/reader035/viewer/2022062309/56814ab6550346895db7c889/html5/thumbnails/25.jpg)
Built-In Variables
25Benjamin MoraUniversity of Wales
Swansea
• GLSL pre-declares many (useful) variables.
• Input/Output variables are used for communication between programs.
• Additional attributes can also be specified.– Implementation dependent. – A minimum number defined by OpenGL.
• glGet(GL_MAX_VERTEX_ATTRIBS);
– See later.
![Page 26: Computer Graphics 3 Lecture 5: OpenGL Shading Language (GLSL)](https://reader035.fdocuments.in/reader035/viewer/2022062309/56814ab6550346895db7c889/html5/thumbnails/26.jpg)
Predefined Vertex Variables
26Benjamin MoraUniversity of Wales
Swansea
• attribute vec4 gl_Color;
• attribute vec4 gl_SecondaryColor;
• attribute vec3 gl_Normal;
• attribute vec4 gl_MultiTexCoord0;
• attribute vec4 gl_MultiTexCoord1;– const int gl_MaxTextureCoords;
• …
• attribute vec4 gl_FogCoord;
![Page 27: Computer Graphics 3 Lecture 5: OpenGL Shading Language (GLSL)](https://reader035.fdocuments.in/reader035/viewer/2022062309/56814ab6550346895db7c889/html5/thumbnails/27.jpg)
Vertex Ouput Variables
27Benjamin MoraUniversity of Wales
Swansea
• vec4 gl_Position;
• vec4 gl_ClipVertex;
• float gl_PointSize;
![Page 28: Computer Graphics 3 Lecture 5: OpenGL Shading Language (GLSL)](https://reader035.fdocuments.in/reader035/viewer/2022062309/56814ab6550346895db7c889/html5/thumbnails/28.jpg)
Vertex Varying Ouput Variables
28Benjamin MoraUniversity of Wales
Swansea
• varying vec4 gl_FrontColor;
• varying vec4 gl_BackColor;
• varying vec4 gl_FrontSecondary;
• varying vec4 gl_BackSecondary;
• varying vec4 gl_TexCoord[];
• float gl_FogFragCoord;
![Page 29: Computer Graphics 3 Lecture 5: OpenGL Shading Language (GLSL)](https://reader035.fdocuments.in/reader035/viewer/2022062309/56814ab6550346895db7c889/html5/thumbnails/29.jpg)
Special Fragment Input Variables
29Benjamin MoraUniversity of Wales
Swansea
• varying vec4 gl_Color;
• varying vec4 gl_SecondaryColor;
• varying vec4 gl_TexCoord[];
• varying float gl_FogFragCoord;
![Page 30: Computer Graphics 3 Lecture 5: OpenGL Shading Language (GLSL)](https://reader035.fdocuments.in/reader035/viewer/2022062309/56814ab6550346895db7c889/html5/thumbnails/30.jpg)
Special Fragment Input Variables
30Benjamin MoraUniversity of Wales
Swansea
• bool gl_FrontFacing;
• vec4 gl_FragCoord;
![Page 31: Computer Graphics 3 Lecture 5: OpenGL Shading Language (GLSL)](https://reader035.fdocuments.in/reader035/viewer/2022062309/56814ab6550346895db7c889/html5/thumbnails/31.jpg)
Fragment Output Variables
31Benjamin MoraUniversity of Wales
Swansea
• vec4 gl_FragColor;
• vec4 gl_FragData;
• float gl_FragDepth;– //gl_FragCoord.z by default
• These variables have a global scope.
![Page 32: Computer Graphics 3 Lecture 5: OpenGL Shading Language (GLSL)](https://reader035.fdocuments.in/reader035/viewer/2022062309/56814ab6550346895db7c889/html5/thumbnails/32.jpg)
Built-In Constants
32Benjamin MoraUniversity of Wales
Swansea
• const int gl_MaxClipPlanes;• const int gl_MaxCombinedTextureImageUnits;• const int gl_MaxFragmentUniformComponents;• const int gl_MaxVertexAttribs;• const int gl_MaxVaryingFloats;• const int gl_MaxDrawBuffers;• const int gl_MaxTextureCoords;• const int gl_MaxTextureUnits;• const int gl_MaxTextureImageUnits;• const int gl_MaxVertexTextureImageUnits;• const int gl_MaxLights;
![Page 33: Computer Graphics 3 Lecture 5: OpenGL Shading Language (GLSL)](https://reader035.fdocuments.in/reader035/viewer/2022062309/56814ab6550346895db7c889/html5/thumbnails/33.jpg)
Built-In Uniform Variables
33Benjamin MoraUniversity of Wales
Swansea
• uniform mat4 gl_ModelViewMatrix;• uniform mat4 gl_ModelViewProjectionMatrix;• uniform mat4 gl_ProjectionMatrix;• uniform mat4
gl_TextureMatrix[gl_MaxTextureCoords];• uniform mat4 gl_ModelViewMatrixInverse;• uniform mat4
gl_ModelViewProjectionMatrixInverse;• uniform mat4 gl_ProjectionMatrixInverse;• uniform mat4
gl_TextureMatrixInverse[gl_MaxTextureCoords];
![Page 34: Computer Graphics 3 Lecture 5: OpenGL Shading Language (GLSL)](https://reader035.fdocuments.in/reader035/viewer/2022062309/56814ab6550346895db7c889/html5/thumbnails/34.jpg)
Built-In Uniform Variables
34Benjamin MoraUniversity of Wales
Swansea
• uniform mat4 gl_ModelViewMatrixTranspose;• uniform mat4 gl_ModelViewProjectionMatrixTranspose;• uniform mat4 gl_ProjectionMatrixTranspose;• uniform mat4
gl_TextureMatrixTranspose[gl_MaxTextureCoords];• uniform mat4 gl_ModelViewMatrixInverseTranspose;• uniform mat4
gl_ModelViewProjectionMatrixInverseTranspose;• uniform mat4 gl_ProjectionMatrixInverseTranspose;• uniform mat4
gl_TextureMatrixInverseTranspose[gl_MaxTextureCoords];• uniform mat3 gl_NormalMatrix;• uniform float gl_NormalScale;
![Page 35: Computer Graphics 3 Lecture 5: OpenGL Shading Language (GLSL)](https://reader035.fdocuments.in/reader035/viewer/2022062309/56814ab6550346895db7c889/html5/thumbnails/35.jpg)
Built-In Uniform Variables
35Benjamin MoraUniversity of Wales
Swansea
• 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];• etc…
![Page 36: Computer Graphics 3 Lecture 5: OpenGL Shading Language (GLSL)](https://reader035.fdocuments.in/reader035/viewer/2022062309/56814ab6550346895db7c889/html5/thumbnails/36.jpg)
Vertex and FragmentProcessors
36Benjamin MoraUniversity of Wales
Swansea
![Page 37: Computer Graphics 3 Lecture 5: OpenGL Shading Language (GLSL)](https://reader035.fdocuments.in/reader035/viewer/2022062309/56814ab6550346895db7c889/html5/thumbnails/37.jpg)
Vertex and Fragment Processors
37Benjamin MoraUniversity of Wales
Swansea
• Replaces the fixed pipeline.
– Input/Ouput Data: Attribute or Uniform variables.
• Built-In or User defined.
• Uses “Varying Data” for the communication of Linearly interpolated Values between the vertex and the fragment program.
![Page 38: Computer Graphics 3 Lecture 5: OpenGL Shading Language (GLSL)](https://reader035.fdocuments.in/reader035/viewer/2022062309/56814ab6550346895db7c889/html5/thumbnails/38.jpg)
Vertex Processor
38Benjamin MoraUniversity of Wales
Swansea
• Modelview and projection matrices not applied.
• Normals not transformed to eye-coordinate.
• Normals not normalized.
• Texture coordinates not processed.
• Lighting not performed.
• Color material computations not performed.
• …
![Page 39: Computer Graphics 3 Lecture 5: OpenGL Shading Language (GLSL)](https://reader035.fdocuments.in/reader035/viewer/2022062309/56814ab6550346895db7c889/html5/thumbnails/39.jpg)
Vertex Processor
39Benjamin MoraUniversity of Wales
Swansea
• After the vertex program, the following fixed functionalities are still applied:– Color clamping.– Perspective division.– Viewport mapping.– Depth range scaling.
• Additional Vertex Attributes can be send from the main program.– Additional colors, tangents, curvatures…
![Page 40: Computer Graphics 3 Lecture 5: OpenGL Shading Language (GLSL)](https://reader035.fdocuments.in/reader035/viewer/2022062309/56814ab6550346895db7c889/html5/thumbnails/40.jpg)
Passing More Vertex Attributes
40Benjamin MoraUniversity of Wales
Swansea
Main C/C++ program:• Texture coordinates can be used.
– Not best.
• glVertexAttrib function.– void glVertexAttrib2dv(GLuint index, const GLdouble *v);– void glVertexAttrib4s(GLuint index, GLshort v0, GLshort v1, GLshort
v2, GLshort v3) ;– void glVertexAttrib4fv(GLuint index, const GLfloat *v);– etc…
– Index at least in the range [0..16]• Attrib 0 indicates the completion of a vertex.
– Version for normalized data available…
![Page 41: Computer Graphics 3 Lecture 5: OpenGL Shading Language (GLSL)](https://reader035.fdocuments.in/reader035/viewer/2022062309/56814ab6550346895db7c889/html5/thumbnails/41.jpg)
Passing More Vertex Attributes
41Benjamin MoraUniversity of Wales
Swansea
• How to associate a fragment program variable with an attribute index in the C/C++ program?– Use glBindAttribLocation function.– void glBindAttribLocation(GLuint program, GLuint
index, const GLchar *name); – glBindAttribLocation(myProgram, 1,
“objectTangent”);
• Must be done before calling the linker.
![Page 42: Computer Graphics 3 Lecture 5: OpenGL Shading Language (GLSL)](https://reader035.fdocuments.in/reader035/viewer/2022062309/56814ab6550346895db7c889/html5/thumbnails/42.jpg)
Passing More Vertex Attributes
42Benjamin MoraUniversity of Wales
Swansea
Main C/C++ program:• glVertexAttribPointer.
– void glVertexAttribPointer(GLuint index, GLint size, GLenum type, GLboolean normalized, GLsizei stride, const GLvoid *pointer);
– Similar to vertex arrays. Arrays can now be stored in video memory and processed optimally.
– Vertex attrib arrays possible.
• Enabling/Disabling Attrib arrays:– void glEnableVertexAttribArray(GLuint index);
– void glDisableVertexAttribArray(GLuint index);
• Arrays used when making a call to glDrawArrays, glDrawElements, etc…
![Page 43: Computer Graphics 3 Lecture 5: OpenGL Shading Language (GLSL)](https://reader035.fdocuments.in/reader035/viewer/2022062309/56814ab6550346895db7c889/html5/thumbnails/43.jpg)
Passing More Vertex Attributes
43Benjamin MoraUniversity of Wales
Swansea
Uniform variables:• Setup in a different way than attribute
variables.– After linking the program, the main application (C/C++)
must query the location of the uniform variable, and then set its value.
• GLint glGetUniformLocation (GLuint program, const GLchar *name) :– Look for a specific variable. – Returns the location.
• void glUniform{1|2|3|4}{f|i} (Glint location, TYPE v);– Set the uniform value. Should not happen between
glBegin/glEnd.
![Page 44: Computer Graphics 3 Lecture 5: OpenGL Shading Language (GLSL)](https://reader035.fdocuments.in/reader035/viewer/2022062309/56814ab6550346895db7c889/html5/thumbnails/44.jpg)
Fragment Processor
44Benjamin MoraUniversity of Wales
Swansea
• The fragment program mainly processes interpolated information generated from the vertex program.– e.g. gl_Color.
• The fragment program must replace/code:– Texture mapping environments & functions.– Texture application.– Color application/generation.– Shading. – Fog application.
![Page 45: Computer Graphics 3 Lecture 5: OpenGL Shading Language (GLSL)](https://reader035.fdocuments.in/reader035/viewer/2022062309/56814ab6550346895db7c889/html5/thumbnails/45.jpg)
Built-In Functions
45Benjamin MoraUniversity of Wales
Swansea
![Page 46: Computer Graphics 3 Lecture 5: OpenGL Shading Language (GLSL)](https://reader035.fdocuments.in/reader035/viewer/2022062309/56814ab6550346895db7c889/html5/thumbnails/46.jpg)
Built-In Functions
46Benjamin MoraUniversity of Wales
Swansea
• Easy Shader Development.– Readability.– Simplicity.– Common functions needed for graphics.
• Mask the actual hardware implementation.– The compiler has to be efficient/clever.
• No warranty that a function is hardware accelerated.– Non-accelerated functions could be slower.– Most of them available from both programs.
![Page 47: Computer Graphics 3 Lecture 5: OpenGL Shading Language (GLSL)](https://reader035.fdocuments.in/reader035/viewer/2022062309/56814ab6550346895db7c889/html5/thumbnails/47.jpg)
Built-In Functions
47Benjamin MoraUniversity of Wales
Swansea
• genType = float | vec2 | vec3 | vec4• Trigonometry Functions.
– genType sin( genType );– genType cos( genType );– genType tan( genType );– genType asin( genType );– genType acos( genType );– genType atan( genType, genType );– genType atan( genType );– genType radians( genType );– genType degrees( genType );
![Page 48: Computer Graphics 3 Lecture 5: OpenGL Shading Language (GLSL)](https://reader035.fdocuments.in/reader035/viewer/2022062309/56814ab6550346895db7c889/html5/thumbnails/48.jpg)
Built-In Functions
48Benjamin MoraUniversity of Wales
Swansea
• Inverse, Exponential and square root functions.– genType pow( genType, genType );– genType exp( genType );– genType log( genType );– genType exp2( genType );– genType log2( genType );– genType sqrt( genType );– genType inversesqrt( genType );
![Page 49: Computer Graphics 3 Lecture 5: OpenGL Shading Language (GLSL)](https://reader035.fdocuments.in/reader035/viewer/2022062309/56814ab6550346895db7c889/html5/thumbnails/49.jpg)
Built-In Functions
49Benjamin MoraUniversity of Wales
Swansea
• Common functions– Min, Max, Clamping, Linear interpolation (Mix),
modulo, floor, frac, step functions.– genType abs( genType );– genType ceil( genType );– genType clamp( genType, genType, genType );– genType clamp( genType, float, float );– genType floor( genType );– genType fract( genType );– genType max( genType, genType );– genType max( genType, float );
![Page 50: Computer Graphics 3 Lecture 5: OpenGL Shading Language (GLSL)](https://reader035.fdocuments.in/reader035/viewer/2022062309/56814ab6550346895db7c889/html5/thumbnails/50.jpg)
Built-In Functions
50Benjamin MoraUniversity of Wales
Swansea
• Common functions– genType mix( genType, genType, genType );– genType mix( genType, genType, float );– genType mod( genType, genType );– genType mod( genType, float );– genType sign( genType );– genType smoothstep( genType, genType,
genType );– genType smoothstep( float, float, genType );– genType step( genType, genType );– genType step( float, genType );
![Page 51: Computer Graphics 3 Lecture 5: OpenGL Shading Language (GLSL)](https://reader035.fdocuments.in/reader035/viewer/2022062309/56814ab6550346895db7c889/html5/thumbnails/51.jpg)
Built-In Functions
51Benjamin MoraUniversity of Wales
Swansea
• 3D functions and Matrix functions.– dot product, length, multiplications…– vec4 ftransform(); //Vertex ONLY. Same transform as //done with
a fixed pipeline. A direct ModelviewProjection //multiplication may lead to a slightly different result.
– vec3 cross( vec3, vec3 );– float distance( genType, genType );– float dot( genType, genType );– genType faceforward ( genType V, genType I, genType N );– float length( genType );– genType normalize( genType );– genType reflect( genType I, genType N );– genType refract( genType I, genType N, float eta );– mat matrixCompMult( mat, mat );
![Page 52: Computer Graphics 3 Lecture 5: OpenGL Shading Language (GLSL)](https://reader035.fdocuments.in/reader035/viewer/2022062309/56814ab6550346895db7c889/html5/thumbnails/52.jpg)
Built-In Functions
52Benjamin MoraUniversity of Wales
Swansea
• Texture Lookup functions– //Optional bias term is Fragment ONLY– vec4 texture1D( sampler1D, float [,float bias] );– vec4 texture1DProj( sampler1D, vec2 [,float bias] );– vec4 texture1DProj( sampler1D, vec4 [,float bias] );– vec4 texture2D( sampler2D, vec2 [,float bias] );– vec4 texture2DProj( sampler2D, vec3 [,float bias] );– vec4 texture2DProj( sampler2D, vec4 [,float bias] );
![Page 53: Computer Graphics 3 Lecture 5: OpenGL Shading Language (GLSL)](https://reader035.fdocuments.in/reader035/viewer/2022062309/56814ab6550346895db7c889/html5/thumbnails/53.jpg)
Built-In Functions
53Benjamin MoraUniversity of Wales
Swansea
• Texture Lookup functions– vec4 texture3D( sampler3D, vec3 [,float bias] );– vec4 texture3DProj( sampler3D, vec4 [,float bias] );– vec4 textureCube( samplerCube, vec3 [,float bias] );– vec4 shadow1D( sampler1DShadow, vec3 [,float bias] );– vec4 shadow2D( sampler2DShadow, vec3 [,float bias] );– vec4 shadow1DProj( sampler1DShadow, vec4 [,float
bias] );– vec4 shadow2DProj( sampler2DShadow, vec4 [,float
bias] );
![Page 54: Computer Graphics 3 Lecture 5: OpenGL Shading Language (GLSL)](https://reader035.fdocuments.in/reader035/viewer/2022062309/56814ab6550346895db7c889/html5/thumbnails/54.jpg)
Built-In Functions
54Benjamin MoraUniversity of Wales
Swansea
• Texture Lookup functions– //Vertex ONLY; ensure
//GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS > 0– vec4 texture1DLod( sampler1D, float, float lod );– vec4 texture1DProjLod( sampler1D, vec2, float lod );– vec4 texture1DProjLod( sampler1D, vec4, float lod );– vec4 texture2DLod( sampler2D, vec2, float lod );– vec4 texture2DProjLod( sampler2D, vec3, float lod );– vec4 texture2DProjLod( sampler2D, vec4, float lod );
![Page 55: Computer Graphics 3 Lecture 5: OpenGL Shading Language (GLSL)](https://reader035.fdocuments.in/reader035/viewer/2022062309/56814ab6550346895db7c889/html5/thumbnails/55.jpg)
Built-In Functions
55Benjamin MoraUniversity of Wales
Swansea
• Texture Lookup functions– //Vertex ONLY; ensure
//GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS > 0– vec4 texture3DProjLod( sampler3D, vec4, float lod );
– vec4 textureCubeLod( samplerCube, vec3, float lod );
– vec4 shadow1DLod( sampler1DShadow, vec3, float lod );
– vec4 shadow2DLod( sampler2DShadow, vec3, float lod );
– vec4 shadow1DProjLod( sampler1DShadow, vec4, float lod );
– vec4 shadow2DProjLod( sampler2DShadow, vec4, float lod );
![Page 56: Computer Graphics 3 Lecture 5: OpenGL Shading Language (GLSL)](https://reader035.fdocuments.in/reader035/viewer/2022062309/56814ab6550346895db7c889/html5/thumbnails/56.jpg)
Built-In Functions
56Benjamin MoraUniversity of Wales
Swansea
• Other functions:– float noise1( genType );– vec2 noise2( genType );– vec3 noise3( genType );– vec4 noise4( genType );– genType dFdx( genType );– genType dFdy( genType );– genType fwidth( genType );– …
![Page 57: Computer Graphics 3 Lecture 5: OpenGL Shading Language (GLSL)](https://reader035.fdocuments.in/reader035/viewer/2022062309/56814ab6550346895db7c889/html5/thumbnails/57.jpg)
Application: Phong Shading
57Benjamin MoraUniversity of Wales
Swansea
Ian Fergusson, https://www.cis.strath.ac.uk/teaching/ug/classes/52.359/lect13.pdf