1
play

1 7 January 2008 An Introduction to the OpenGL Shading Language 8 - PDF document

Traditional Graphics Pipeline Traditional Graphics Pipeline Xform Lighting Per Vertex Per Vertex Projection Polynomial Operations & Polynomial Operations & Primitive Primitive Evaluator Evaluator Clipping Assembly Assembly


  1. Traditional Graphics Pipeline Traditional Graphics Pipeline Xform Lighting Per Vertex Per Vertex Projection Polynomial Operations & Polynomial Operations & Primitive Primitive Evaluator Evaluator Clipping Assembly Assembly etc Display Per Fragment Frame Display Per Fragment Frame Application Application Rasterization Rasterization List Operations Buffer List Operations Buffer Texture Texture Memory Memory CPU Pixel CPU Pixel Operations Operations 1 January 2008 An Introduction to the OpenGL Shading Language 2 January 2008 An Introduction to the OpenGL Shading Language Traditional Graphics Pipeline Video Application Transform Rasterizer Shade Memory (Textures) CPU GPU A simplified graphics pipeline ! ! Note that pipe widths vary ! ! Many caches, FIFOs, and so on not shown 3 January 2008 An Introduction to the OpenGL Shading Language 4 January 2008 An Introduction to the OpenGL Shading Language 5 January 2008 An Introduction to the OpenGL Shading Language 6 January 2008 An Introduction to the OpenGL Shading Language 1

  2. 7 January 2008 An Introduction to the OpenGL Shading Language 8 January 2008 An Introduction to the OpenGL Shading Language 9 January 2008 An Introduction to the OpenGL Shading Language 10 January 2008 An Introduction to the OpenGL Shading Language 11 January 2008 An Introduction to the OpenGL Shading Language 12 January 2008 An Introduction to the OpenGL Shading Language 2

  3. 13 January 2008 An Introduction to the OpenGL Shading Language 14 January 2008 An Introduction to the OpenGL Shading Language 15 January 2008 An Introduction to the OpenGL Shading Language 16 January 2008 An Introduction to the OpenGL Shading Language Fixed Functionality Pipeline Programmable Shader Pipeline Triangles/Lines/Points Triangles/Lines/Points Transform Primitive Vertex Primitive Primitive Primitive Rasterizer Rasterizer Vertices and Vertices Processing Assembly Processing Shader Assembly Lighting API API Vertex Vertex Buffer Buffer Objects Objects Fragment Texture Color Fog Environment Sum Shader Color Color Alpha Depth Alpha Depth Buffer Dither Frame Buffer Buffer Dither Frame Buffer Test Stencil Test Stencil Blend Blend 17 January 2008 An Introduction to the OpenGL Shading Language 18 January 2008 An Introduction to the OpenGL Shading Language 3

  4. Programmer’s Model Attributes ( m * vec4) Vertex Uniforms Vertex ( p * vec4) Shader Primitive Assembly & Rasterize Varyings ( n * vec4) Fragment Uniforms Fragment ( q * vec4) Shader Per-Sample Operations 19 January 2008 An Introduction to the OpenGL Shading Language 20 January 2008 An Introduction to the OpenGL Shading Language Vertex Shader Environment Uniforms Textures Attribute 0 Varying 0 Attribute 1 Varying 1 Attribute 2 Varying 2 Attribute 3 Varying 3 Vertex Shader Attribute 4 Varying 4 Attribute 5 Varying 5 … … Attribute m Varying n Clip position Point size Temporary variables 21 January 2008 An Introduction to the OpenGL Shading Language 22 January 2008 An Introduction to the OpenGL Shading Language Fragment Shader Environment Uniforms Textures Varying 0 Varying 1 Varying 2 Fragment Color(s) Varying 3 Varying 4 Fragment Shader Varying 5 Fragment Depth … Varying n Window coord Front facing flag Point coord Temporary variables 23 January 2008 An Introduction to the OpenGL Shading Language 24 January 2008 An Introduction to the OpenGL Shading Language 4

  5. 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); } 25 January 2008 An Introduction to the OpenGL Shading Language 26 January 2008 An Introduction to the OpenGL Shading Language Basic method Creating Shaders 2 basic object types ! ! Shader object ! ! Program object Create Vertex & Fragment Shader Objects Compile both Create program object & attach shaders Link program Use program 27 January 2008 An Introduction to the OpenGL Shading Language 28 January 2008 An Introduction to the OpenGL Shading Language Compiling Attaching & Linking void glShaderSource(GLuint shader, GLsizei nstrings, const GLchar **strings, void glAttachShader(GLuint program, GLuint shader); const GLint *lengths) //twice, once for vertex shader & once for fragment shader //if lengths==NULL, assumed to be null-terminated void glLinkProgram(GLuint program); //program now ready to use void glCompileShader (GLuint shader); void glUseProgram( GLuint program); //switches on shader, bypasses FFP //if program==0, shaders turned off, returns to FFP 29 January 2008 An Introduction to the OpenGL Shading Language 30 January 2008 An Introduction to the OpenGL Shading Language 5

  6. In short… Example GLuint programObject; void setShaders() { char *vs,*fs; GLuint vertexShaderObject; GLuint fragmentShaderObject; v = glCreateShader(GL_VERTEX_SHADER); f = glCreateShader(GL_FRAGMENT_SHADER); unsigned char *vertexShaderSource = readShaderFile(vertexShaderFilename); unsigned char *fragmentShaderSource = readShaderFile(fragmentShaderFilename); vs = textFileRead("toon.vert"); fs = textFileRead("toon.frag"); programObject=glCreateProgram (); const char * vv = vs; vertexShaderObject=glCreateShader (GL_VERTEX_SHADER); const char * ff = fs; fragmentShaderObject=glCreateShader (GL_FRAGMENT_SHADER); glShaderSource(v, 1, &vv,NULL); glShaderSource (vertexShaderObject,1,(const char**)&vertexShaderSource,NULL); glShaderSource(f, 1, &ff,NULL); glShaderSource (fragmentShaderObject,1,(const char**)&fragmentShaderSource,NULL); free(vs);free(fs); glCompileShader (vertexShaderObject); glCompileShader(v); glCompileShader (fragmentShaderObject); glCompileShader(f); glAttachObject (programObject, vertexShaderObject); p = glCreateProgram(); glAttachObject (programObject, fragmentShaderObject); glAttachShader(p,v); glAttachShader(p,f); glLinkProgram (programObject); glLinkProgram(p); glUseProgram (programObject); glUseProgram(p); } 31 January 2008 An Introduction to the OpenGL Shading Language 32 January 2008 An Introduction to the OpenGL Shading Language Other functions Useful References Clean-up http://www.3dshaders.com/ void glDetachObject (GLuint container, GLuint attached); ! ! Home page for the “orange book” focused solely on GLSL void glDeleteObject (GLuint object); http://www.opengl.org/sdk/ Info Log ! ! OpenGL SDK, including links to the below resources void glGetInfoLog (GLuint object, GLsizei maxLength, GLsizei http://www.opengl.org/sdk/libs/OpenSceneGraph/glsl_quickref.pdf *length, GLchar *infoLog); ! ! one double-sided page cheat sheet to GLSL – indispensible! ! ! Returns compile & linking information, errors http://www.opengl.org/registry/doc/GLSLangSpec.Full.1.20.8.pdf ! ! This is the ultimate authority: the GLSL specification document http://www.opengl.org/sdk/docs/books/SuperBible/ ! ! Full reference and tutorial to OpenGL 2.1 ! ! All sample code downloadable for Windows, Mac OS X, and Linux 33 January 2008 An Introduction to the OpenGL Shading Language 34 January 2008 An Introduction to the OpenGL Shading Language 6

Recommend


More recommend