1 / 24

GL Shading Language

GL Shading Language. Marc Neveu. http://www.opengl.org/documentation/glsl/ http://www.opengl.org/documentation/current_version/. OpenGL 2.0. extensions ARB et OpenGL 2.0. Vertex Processor. Exécute les “vertex shaders”. En entrée : les données sommets (position, couleur, normales, etc,)

cargan
Download Presentation

GL Shading Language

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. GL Shading Language Marc Neveu http://www.opengl.org/documentation/glsl/ http://www.opengl.org/documentation/current_version/

  2. OpenGL 2.0 • extensions ARB et OpenGL 2.0

  3. Vertex 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.

  4. Fragment 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.

  5. Avec OpenGL • On a : • un “ vertex shader” • et un “ fragment shader” • à utiliser dans une applicationOpenGL

  6. OpenGL 2.0 disponible ? #include <GL/glew.h> #include <GL/glut.h> 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(); glutMainLoop(); } #include <GL/glew.h> #include <GL/glut.h> void main(int argc, char **argv) { glutInit(&argc, argv); ... glewInit(); if (GLEW_ARB_vertex_shader && GLEW_ARB_fragment_shader) printf("Ready for GLSL\n"); else { printf("Not totally ready :( \n"); exit(1); } setShaders(); glutMainLoop(); } OpenGL 2.0 Extension ARB

  7. KoiKona?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.

  8. Création d’un 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.

  9. En 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.

  10. Création d’un 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

  11. void 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(); • glAttachShader(p,v); • glAttachShader(p,f); • glLinkProgram(p); • glUseProgram(p); • }

  12. En 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

  13. Idem 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); }

  14. Ca 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 • maxLen - The maximum number of chars to retrieve from the InfoLog. • len - returns the actual length of the retrieved InfoLog. • log - The log itself.

  15. Desallouer (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.

  16. Communication OpenGL -> 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 glUniform{1,2,3,4}fv(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 glUniformMatrix{2,3,4}fv(GLint location, GLsizei count, GLboolean transpose, GLfloat *v);

  17. uniform float specIntensity; uniform vec4 specColor; uniform float t[2]; uniform vec4 colors[3]; Dans une application OpenGL 2.0 : GLint loc1,loc2,loc3,loc4; float specIntensity = 0.98; float sc[4] = {0.8,0.8,0.8,1.0}; float threshold[2] = {0.5,0.25}; float colors[12] = {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"); glUniform1fv(loc3,2,threshold); loc4 = glGetUniformLocation(p,"colors"); glUniform4fv(loc4,3,colors);

  18. Communication OpenGL -> 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 glVertexAttrib{1,2,3,4}fv(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"

  19. loc = glGetAttribLocation(p,"height"); • Le rendu en OpenGL 2.0 : • glBegin(GL_TRIANGLE_STRIP); • glVertexAttrib1f(loc,2.0); • glVertex2f(-1,1); • glVertexAttrib1f(loc,2.0); • glVertex2f(1,1); • glVertexAttrib1f(loc,-2.0); • glVertex2f(-1,-1); • glVertexAttrib1f(loc,-2.0); • glVertex2f(1,-1); • glEnd();

  20. Types & Variables • float, bool, int • vec{2,3,4}, bvec{2,3,4}, ivec{2,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

  21. Instructions 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

  22. Exemple « 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();

  23. Exemple 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; }

  24. Des exemples • http://www.lighthouse3d.com/opengl/glsl/examples/ • http://developer.3dlabs.com/downloads/index.htm • Etc…………….

More Related