Title: GL Shading Language
1GL Shading Language
http//www.opengl.org/documentation/glsl/ http//w
ww.opengl.org/documentation/current_version/
2OpenGL 2.0
- extensions ARB et OpenGL 2.0
3Vertex Processor
- Exécute les vertex shaders.
- En entrée les données sommets (position,
couleur, normales, etc,) - glBegin(...)
- glColor3f(0.2,0.4,0.6)
- glVertex3f(-1.0,1.0,2.0)
- glColor3f(0.2,0.4,0.8)
- glVertex3f(1.0,-1.0,2.0)
- glEnd()
vertex shader envoie au moins une variable
gl_Position, Transformation du sommet par les
matrices modelview et projection.
4Fragment Processor
- Exécute les fragment shaders
- Calcul des couleurs, et coordonnées texture par
pixel - Application de texture
- Brouillard
- Calcul de normales (illumination par pixel )
- Entrée pour chaque sommet positions,
couleurs, normales, etc... - Calculs par sommets ? pour les fragments valeurs
interpolées.
5Avec OpenGL
- On a
- un vertex shader
- et un fragment shader
- Ã utiliser dans une application OpenGL
6OpenGL 2.0 disponible ?
include ltGL/glew.hgt include ltGL/glut.hgt void
main(int argc, char argv) glutInit(argc,
argv) ... glewInit() if
(glewIsSupported("GL_VERSION_2_0")) printf("Ready
for OpenGL 2.0\n") else printf("OpenGL
2.0 not supported\n") exit(1) setShaders() g
lutMainLoop()
include ltGL/glew.hgt include ltGL/glut.hgt void
main(int argc, char argv) glutInit(argc,
argv) ... glewInit() if (GLEW_ARB_vertex_shade
r GLEW_ARB_fragment_shader) printf("Ready for
GLSL\n") else printf("Not totally ready (
\n") exit(1) setShaders() glutMainLoop()
OpenGL 2.0
Extension ARB
7KoiKona?KelKart?
--------------------------- GLEW Extension
Info --------------------------- GLEW version
1.3.2 Reporting capabilities of pixelformat
1 Running on a GeForce4 MX 440 with
AGP8X/AGP/SSE2 from NVIDIA Corporation OpenGL
version 1.4.0 is supported GL_VERSION_1_1
OK
--------------- GL_VERSION_1_2
OK
--------------- glCopyTexSubImage3D
OK
GL_VERSION_1_3
OK ---------------
glActiveTexture
OK glClientActiveTexture
OK GL_VERSION_1_4
OK
--------------- glBlendColor
OK
glBlendEquation
OK glBlendFuncSeparate
OK
glFogCoordPointer
OK glFogCoordd
OK GL_VERSION_1_5
MISSING --------------- glBeginQuery
MISSING
glBindBuffer
MISSING glBufferData
MISSING GL_VERSION_2_0
MISSING ---------------
glAttachShader
MISSING glBindAttribLocation
MISSING
glBlendEquationSeparate
MISSING glCompileShader
MISSING
glCreateProgram
MISSING glCreateShader
MISSING
2. What NVIDIA Drivers and GPUs support OpenGL
2.0? NVIDIA support for OpenGL 2.0 begins with
the Release 75 series of drivers. GeForceFX
(NV3x), GeForce 6 Series (NV4x), NV3xGL-based
Quadro FX and NV4xGL-based Quadro FX GPUs, and
all future NVIDIA GPUs support OpenGL 2.0. Prior
to Release 75, drivers for these OpenGL
2.0-capable GPUs advertised OpenGL 1.5 support
but also exposed the feature set of OpenGL 2.0
through the corresponding extensions listed in
section 1. Earlier GPUs (such as GeForce2,
GeForce3, and GeForce4) continue to support
OpenGL1.5 with no plans to ever support OpenGL
2.0 because the hardware capabilities of these
GPUs are not sufficient to accelerate the OpenGL
2.0 feature set properly. However, NVIDIA
provides an option with Release 75 drivers to
emulate OpenGL 2.0 features on these earlier
GPUs. This option is further discussed in section
5. This emulation option is not recommended for
general users because OpenGL 2.0 features will be
emulated in software very, very slowly. OpenGL
2.0 emulation may be useful for developers and
students without access to the latest NVIDIA GPU
hardware.
8Création dun shader
GLuint glCreateShader(GLenum shaderType)
Parameter shaderType - GL_VERTEX_SHADER or
GL_FRAGMENT_SHADER.
void glShaderSource(GLuint shader, int
numOfStrings, const char strings, int
lenOfStrings) Parameters shader - the
handler to the shader. numOfStrings - the number
of strings in the array. strings - the array of
strings. lenOfStrings - an array with the length
of each string, or NULL, meaning that the strings
are NULL terminated.
void glCompileShader(GLuint shader) Parameters
shader - the handler to the shader.
9En ARB OpenGL 2.0
- GLhandleARB glCreateShaderObjectARB(GLenum
shaderType) - Parameter
- shaderType - GL_VERTEX_SHADER_ARB or
GL_FRAGMENT_SHADER_ARB. - void glShaderSourceARB(GLhandleARB shader, int
numOfStrings, const char strings, int
lenOfStrings) - Parameters
- shader - the handler to the shader.
- numOfStrings - the number of strings in the
array. - strings - the array of strings.
- lenOfStrings - an array with the length of each
string, or NULL, meaning that the strings are
NULL terminated. - void glCompileShaderARB(GLhandleARB shader)
- Parameters
- shader - the handler to the shader.
10Création dun Programme
GLuint glCreateProgram(void)
void glAttachShader(GLuint program, GLuint
shader) Parameters program - the handler to
the program. shader - the handler to the shader
you want to attach.
void glLinkProgram(GLuint program) Parameters
program - the handler to the program.
void glUseProgram(GLuint prog) Parameters
prog - the handler to the program you want to
use, or zero to return to fixed functionality
11void setShaders() char vs,fs v
glCreateShader(GL_VERTEX_SHADER) f
glCreateShader(GL_FRAGMENT_SHADER) vs
textFileRead("toon.vert") fs
textFileRead("toon.frag") const char vv
vs const char ff fs glShaderSource(v, 1,
vv,NULL) glShaderSource(f, 1,
ff,NULL) free(vs)free(fs) glCompileShader(v)
glCompileShader(f) p glCreateProgram() gl
AttachShader(p,v) glAttachShader(p,f) glLinkPro
gram(p) glUseProgram(p)
12En ARB
- GLhandleARB glCreateProgramObjectARB(void)
- void glAttachObjectARB(GLhandleARB program,
GLhandleARB shader) - program - the handler to the program.
- shader - the handler to the shader you want to
attach. - void glLinkProgramARB(GLhandleARB program)
- program - the handler to the program.
- void glUseProgramObjectARB(GLhandleARB prog)
- prog - the handler to the program you want to
use, or zero to return to fixed functionality
13Idem
- void setShaders()
- char vs,fs
- v glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB)
- f glCreateShaderObjectARB(GL_FRAGMENT_SHADER_ARB
) - vs textFileRead("toon.vert")
- fs textFileRead("toon.frag")
- const char vv vs
- const char ff fs
- glShaderSourceARB(v, 1, vv,NULL)
- glShaderSourceARB(f, 1, ff,NULL)
- free(vs)free(fs)
- glCompileShaderARB(v)
- glCompileShaderARB(f)
- p glCreateProgramObjectARB()
- glAttachObjectARB(p,v)
- glAttachObjectARB(p,f)
- glLinkProgramARB(p)
- glUseProgramObjectARB(p)
-
14Ca coince ? InfoLog
- Debug difficile (pas de printf )
- Etapes de compilation vérifiables avec
- void glGetShaderiv(GLuint object, GLenum type,
int param) - Parameters
- object - the handler to the object. Either a
shader or a program - type - GL_COMPILE_STATUS.
- param - the return value, GL_TRUE if OK, GL_FALSE
otherwise. -
- Etapes de link vérifiables avec
- void glGetProgramiv(GLuint object, GLenum type,
int param) - Parameters
- object - the handler to the object. Either a
shader or a program - type - GL_LINK_STATUS.
- void glGetShaderInfoLog(GLuint object, int
maxLen, int len, char log) - void glGetProgramInfoLog(GLuint object, int
maxLen, int len, char log) - Parameters
- object - the handler to the object. Either a
shader or a program
15Desallouer (Cleaning Up)
- void glDetachShader(GLuint program, GLuint
shader) - Parameter
- program - The program to detach from.
- shader - The shader to detach.
- void glDeleteShader(GLuint id)
- void glDeleteProgram(GLuint id)
- Parameter
- id - The handler of the shader or program to
delete.
16Communication OpenGL -gt Shaders
- Variable Uniform globale
- sa valeur est modifiée par une primitive (ce ne
peut être dans un glBegin-glEnd, ce ne peut être
un attribut de sommet) - Pour récupérer une variable uniforme
- GLint glGetUniformLocation(GLuint program, const
char name) - Parameters
- program - the handler to the program
- name - the name of the variable.
- GLint glUniform1,2,3,4fv(GLint location,
GLsizei count, GLfloat v) - Parameters
- location - the previously queried location.
- v0,v1,v2,v3 - float values.
- count - the number of elements in the array
- v - an array of floats.
- Idem avec le type integer, avec "i au lieu de
"f" - Idem avec le type matrix GLint
glUniformMatrix2,3,4fv(GLint location, GLsizei
count, GLboolean transpose, GLfloat v)
17uniform float specIntensity uniform vec4
specColor uniform float t2 uniform vec4
colors3 Dans une application OpenGL 2.0
GLint loc1,loc2,loc3,loc4 float
specIntensity 0.98 float sc4
0.8,0.8,0.8,1.0 float threshold2
0.5,0.25 float colors12
0.4,0.4,0.8,1.0,0.2,0.2,0.4,1.0,0.1,0.1,0.1,1.0
loc1 glGetUniformLocation(p,"specIntensity")
glUniform1f(loc1,specIntensity) loc2
glGetUniformLocation(p,"specColor") glUniform4fv(
loc2,1,sc) loc3 glGetUniformLocation(p,"t") g
lUniform1fv(loc3,2,threshold) loc4
glGetUniformLocation(p,"colors") glUniform4fv(loc
4,3,colors)
18Communication OpenGL -gt Shaders
- Variable Attribute locale
- pour définir une variable par sommet
- GLint glGetAttribLocation(GLuint program,char
name) - Parameters
- program - the handle to the program.
- Name - the name of the variable
- GLint glVertexAttrib1,2,3,4fv(GLint location,
GLfloat v) - Parameters
- location - the previously queried location.
- v0,v1,v2,v3 - float values.
- v - an array of floats.
- Idem avec le type integer, avec "i au lieu de
"f"
19loc glGetAttribLocation(p,"height") Le rendu
en OpenGL 2.0 glBegin(GL_TRIANGLE_STRIP) glVer
texAttrib1f(loc,2.0) glVertex2f(-1,1) glVertexAt
trib1f(loc,2.0) glVertex2f(1,1) glVertexAttrib1f
(loc,-2.0) glVertex2f(-1,-1) glVertexAttrib1f(lo
c,-2.0) glVertex2f(1,-1) glEnd()
20Types Variables
- float, bool, int
- vec2,3,4, bvec2,3,4, ivec2,3,4 2,3,ou 4
float, bool, integer - mat2, mat3 , mat4 matrices 2x2, 3x3 et 4x4
- sampler1D, sampler2D, sampler3D pour textures
1D, 2D, 3D - samplerCube pour les textures cube map
- sampler1DShadow, sampler2DShadow pour shadow
maps - Structures
- struct dirlight
- vec3 direction
- vec3 color
-
- Variables ? comme en C
- Qualificatifs de Variables const -attribute
-uniform -varying
21Instructions et Fonctions
- ? C
- if (expression booléenne) ... else ...
- for (initialization expression booléenne
expression de boucle) ... - while (expression booléenne) ...
- do ... while (expression booléenne)
- Au moins une fonction main void main()
- Elle peut retourner un type qcq (sauf array)
- Paramètres
- in
- out
- inout
22Exemple Concon
- Transforme les sommets et trace les primitives
avec une seule couleur - Vertex Shader
- La transfo vTrans projection modelview
incomingVertex - uniform mat4 gl_ModelViewMatrix
- uniform mat4 gl_ProjectionMatrix
- attribute vec4 gl_Vertex
- void main()
- gl_Position gl_ProjectionMatrix
gl_ModelViewMatrix gl_Vertex -
- Fragment Shader
- void main()
- gl_FragColor vec4(0.4,0.4,0.8,1.0)
-
gl_Position gl_ModelViewProjectionMatrix
gl_Vertex gl_Position ftransform()
23Exemple Couleur
- Accès à la couleur spécifiée par glColor.
-
- 1) OpenGL envoie la couleur glColor
- 2) Le vertex shader reçoit la couleur (attribute
gl_Color) - 3) Le vertex shader calcule les couleurs face
avant et face arrière (gl_FrontColor,
gl_BackColor) - 4) Le fragment shader reçoit une couleur
interpolée (varying gl_Color) - 5) Le fragment shader assigne gl_FragColor Ã
partir de gl_Color
- attribute vec4 gl_Color
- varying vec4 gl_FrontColor // writable on the
vertex shader - varying vec4 gl_BackColor // writable on the
vertex shader - varying vec4 gl_Color // readable on the
fragment shader - void main() // Vertex Shader
- gl_FrontColor gl_Color
- gl_Position ftransform()
-
- void main() // Fragment Shader gl_FragColor
gl_Color -
24Des exemples
- http//www.lighthouse3d.com/opengl/glsl/examples/
- http//developer.3dlabs.com/downloads/index.htm
- Etc.