OpenGL Shading Language (GLSL) - PowerPoint PPT Presentation

1 / 44
About This Presentation
Title:

OpenGL Shading Language (GLSL)

Description:

Title: PowerPoint Presentation Last modified by: u31hts00 Created Date: 1/1/1601 12:00:00 AM Document presentation format: (4:3) Other titles – PowerPoint PPT presentation

Number of Views:1881
Avg rating:3.0/5.0
Slides: 45
Provided by: cggmcsieN
Category:

less

Transcript and Presenter's Notes

Title: OpenGL Shading Language (GLSL)


1
OpenGL Shading Language (GLSL)
2
OpenGL Fixed Pipeline
Vertex pipeline
Transformation Lighting
Viewport culling clipping
Primitive assembly
Rasterizer setup
Pixel pipeline
Texture blending
Per Fragment operations
Buffer operations
Framebuffer
3
Vertex Shader
  • Vertex transformation
  • Normal transformation normalization
  • Texture coordinate generation transformation
  • Per-vertex lighting

Transformation Lighting
Viewport culling clipping
Primitive assembly
Vertex processor
Rasterizer setup
4
Geometry Shader
  • Add/remove primitives
  • Add/remove vertices
  • Edit vertex position
  • Supported by OpenGL Extension (Glew 1.4) or DX10

Stream output
Viewport culling clipping
Vertex processor
Primitive assembly
Geometry processor
Rasterizer setup
5
Fragment (pixel) Shader
  • Operations on interpolated values
  • Texture access
  • Texture application
  • Fog
  • Color sum

Rasterizer setup
Texture blending
Per Fragment operations
Buffer operations
Fragment processor
6
Programmable Pipeline
Vertex pipeline
Viewport culling clipping
Vertex Shader
Geometry Shader
Rasterizer setup
Pixel pipeline
Pixel Shader
Per Fragment operations
Buffer operations
Framebuffer
7
Qualifiers
  • Used to management the input and output of
    shaders.
  • attribute
  • Communicate frequently changing variables from
    the application to a vertex shader.
  • uniform
  • Communicate infrequently changing variables from
    the application to any shader.
  • varying
  • Communicate interpolated variables from a vertex
    shader to a fragment shader

8
Qualifiers in pipeline
(x,y,z)
(x,y,z)
Vertex Shader
FragmentShader
attribute
rasterizer
Buffer Op
varying
varying
uniform
9
Qualifiers in pipeline
(x,y,z)
Vertex Shader
FragmentShader
Geometry Shader
attribute
rasterizer
varying out
varying in
uniform
10
Vertex Shader
Build-in Attributes gl_Color gl_Normal gl_Vertex
gl_MultiTexCoord0 gl_MultiTexCoord1
User-defined Attributes Tangent Bitangent
Build-in Uniform gl_ModelviewMatrix gl_Projection
Matrix gl_LightSource0n gl_FrontMaterial
User-defined Uniform Texture EyePosition Time Sca
leValue
Vertex processer
Special Output gl_Position gl_PointSize
Build-in Varying gl_FrontColor gl_TexCoord0n
User-defined Varying normal tangent
11
Fragment Shader
Build-in Varying gl_TexCoord0n gl_FragCoord gl
_FrontColor
User-defined Varying normal tangent
Build-in Uniform gl_ModelviewMatrix gl_Projection
Matrix gl_LightSource0n gl_FrontMaterial
User-defined Uniform Texture EyePosition Time Sca
leValue
Fragment processer
Special Output gl_FragColor / gl_FragData gl_Fr
agDepth
12
Geometry Shader
Resterization Info. gl_PointSizeIngl_VerticesIn
gl_ClipVertexIngl_VerticesIn
Vertex Color gl_FrontColorIngl_VerticesIn gl_B
ackColorIngl_VerticesIn gl_FrontSecondaryColorI
ngl_VerticesIn gl_BackSecondaryColorIngl_Verti
cesIn gl_FogFragCoordIngl_VerticesIn
Vertex Coord. gl_TexCoordIngl_VerticesIn gl_
PositionIngl_VerticesIn
Geometry processor
Number of Vertices gl_VerticesIn
Color gl_FrontColor gl_BackColor gl_FrontSeconda
ryColor gl_BackSecondaryColor gl_FogFragCoord
Coord. gl_Position gl_TexCoord
13
GLSL Language Definition
  • Data Type Description
  • int Integer
  • float Floating-point
  • bool Boolean (true or false).
  • vec2 Vector with two floats.
  • vec3 Vector with three floats.
  • vec4 Vector with four floats.
  • mat2 2x2 floating-point matrix.
  • mat3 3x3 floating-point matrix.
  • mat4 4x4 floating-point matrix.

14
Vector
  • Vector is like a class
  • You can use following to access
  • .r .g .b .a
  • .x .y .z .w
  • .s .t .p .q
  • Example
  • vec4 color
  • color.rgb vec3(1.0 , 1.0 , 0.0 ) color.a
    0.5
  • or color vec4(1.0 , 1.0 , 0.0 , 0.5)
  • or color.xy vec2(1.0 , 1.0) color.zw vec2(0.0
    , 0.5)

15
Texture
  • Sampler
  • sampler1,2,3D
  • sampler1,2DShadow
  • samplerCube
  • sampler1,2,3D
  • Texture unit to access the content of texture.
  • samplerDShadow
  • The depth texture for shadow map.
  • samplerCube
  • The cube map.

16
Addition information
  • Array
  • Similar to C.
  • No union, enum, class
  • Static cast by function
  • float()
  • int()

17
Phong Shading
  • Use varying variable to save the vertex normal or
    other information.
  • Compute the Phong lighting in pixel shader with
    the information.

18
Vertex Shader Code Example
  • varying vec3 normal, lightDir, eyeDir
  • void main()
  • normal gl_NormalMatrix gl_Normal
  • vec3 vVertex vec3(gl_ModelViewMatrix
    gl_Vertex)
  • lightDir vec3(gl_LightSource0.position.xyz -
    vVertex)
  • eyeDir -vVertex
  • gl_Position ftransform()
  • //gl_Position gl_ProjectionMatrixgl_ModelViewM
    atrixgl_Vertex

19
Fragment Shader Code Example
  • varying vec3 normal, lightDir, eyeDir
  • void main (void)
  • vec4 final_color
  • (gl_FrontLightModelProduct.sceneColor
    gl_FrontMaterial.ambient)
  • (gl_LightSource0.ambient gl_FrontMaterial.amb
    ient)
  • vec3 N normalize(normal)
  • vec3 L normalize(lightDir)
  • float lambertTerm dot(N,L)
  • if(lambertTerm gt 0.0)
  • final_color gl_LightSource0.diffuse
    gl_FrontMaterial.diffuse lambertTerm
  • vec3 E normalize(eyeDir)
  • vec3 R reflect(-L, N)
  • float specular pow( max(dot(R, E), 0.0),
    gl_FrontMaterial.shininess )
  • final_color gl_LightSource0.specular
    gl_FrontMaterial.specular specular

20
Result
OpenGL Gouraud Shading
GLSL Phong Shading
21
Geometry Shader
  • It can change the primitive.
  • Add/remove primitives
  • Add/remove vertices
  • Edit vertex position
  • Application

22
Geometry Shader Example Code
  • void main(void)
  • vec2 lineDir gl_PositionIn1.xy -
    gl_PositionIn0.xy
  • vec2 normal vec2(-lineDir.y, lineDir.x) //CCW
    90 degree
  • vec4 v4
  • v0 gl_PositionIn1
  • v1 gl_PositionIn1
  • v2 gl_PositionIn0
  • v3 gl_PositionIn0
  • v0.xy - normal0.125
  • v1.xy normal0.125
  • v2.xy normal0.125
  • v3.xy - normal0.125

23
  • gl_Position v0
  • EmitVertex()
  • gl_Position v1
  • EmitVertex()
  • gl_Position v2
  • EmitVertex()
  • gl_Position v3
  • EmitVertex()
  • gl_Position v0
  • EmitVertex()
  • EndPrimitive() // GL_LINE_STRIP

24
Result
Original input primitive
Output primitive
25
New input primitives
  • GL_LINES_ADJACENCY
  • GL_LINE_STRIP_ADJACENCY
  • GL_TRIANGLES_ADJACENCY
  • GL_TRIANGLE_STRIP_ADJECENCY

26
Applications
27
Applications
28
Use GLSL in OpenGL
  • You need those head and library files
  • glew.h
  • wglew.h
  • glew32.lib
  • glew32s.lib
  • glew32.dll

29
Use the shader code in C/C
  • Initialize the shader.
  • Use the shader you made.
  • Draw what you want.

30
Shader Initialization
Vertex Shader
glCreateShade
Vertex Shader Code
glShaderSource
glCreateProgram
glCompileShader
glAttachShader
Fragment Shader
glAttachShader
glCreateShade
glLinkProgram
Fragment Shader Code
glShaderSource
glUseProgram
glCompileShader
31
Part of Example Code (C)
  • int main(int argc, char argv)
  • glutInit(argc, argv)
  • glutInitDisplayMode(GLUT_DOUBLE GLUT_RGBA)
  • glutInitWindowPosition(100,100)
  • glutInitWindowSize(320,320)
  • glutCreateWindow("GPU")
  • .
  • .
  • .
  • glewInit()
  • setShaders()
  • glutMainLoop()
  • return 0

32
  • void setShaders()
  • //a few strings
  • // will hold onto the file read in!
  • char vs NULL, fs NULL, gs NULL
  • //First, create our shaders
  • v glCreateShader(GL_VERTEX_SHADER)
  • f glCreateShader(GL_FRAGMENT_SHADER)
  • g glCreateShader(GL_GEOMETRY_SHADER_EXT)
  • //Read in the programs
  • vs textFileRead("../GeometryShader/ShaderCode/s
    hader.vert")
  • fs textFileRead("../GeometryShader/ShaderCode/s
    hader.frag")
  • gs textFileRead("../GeometryShader/ShaderCode/s
    hader.geom")

33
  • //Setup a few constant pointers for below
  • const char ff fs
  • const char vv vs
  • const char gg gs
  • glShaderSource(v, 1, vv, NULL)
  • glShaderSource(f, 1, ff, NULL)
  • glShaderSource(g, 1, gg, NULL)
  • free(vs)free(fs)free(gs)
  • glCompileShader(v)
  • glCompileShader(f)
  • glCompileShader(g)
  • p glCreateProgram()

34
  • glAttachShader(p,f)
  • glAttachShader(p,v)
  • glAttachShader(p,g)
  • glProgramParameteri(p,GL_GEOMETRY_INPUT_TYPE,GL_L
    INES)
  • glProgramParameteri(p,GL_GEOMETRY_OUTPUT_TYPE,GL_
    LINE_STRIP)
  • int temp
  • glGetIntegerv(GL_MAX_GEOMETRY_OUTPUT_VERTICES,te
    mp)
  • glProgramParameteri(p,GL_GEOMETRY_VERTICES_OUT,te
    mp)
  • glLinkProgram(p)
  • glUseProgram(p)

35
Send texture to shader
  • Active texture channel
  • Bind the texture
  • Get location of the variable in shader
  • Set the value

36
Multi-texture
Pixel Color
Op 0
glActiveTexture( GL_TEXTURE0 ) glEnable(GL_TEXTUR
E_2D) glBindTexture(GL_TEXTURE_2D, )
Op 1
glActiveTexture( GL_TEXTURE1 ) glEnable(GL_TEXTUR
E_2D) glBindTexture(GL_TEXTURE_2D, )
Op 2
glActiveTexture( GL_TEXTURE2 ) glEnable(GL_TEXTUR
E_2D) glBindTexture(GL_TEXTURE_2D, )
Op n
final color
glActiveTexture( GL_TEXTUREn ) glEnable(GL_TEXTUR
E_2D) glBindTexture(GL_TEXTURE_2D, )
37
Shader
Get location by name Assign channel number
Shader
glActiveTexture( GL_TEXTURE0 ) glBindTexture(GL_T
EXTURE_2D, )
glActiveTexture( GL_TEXTURE1 ) glBindTexture(GL_T
EXTURE_2D, )
final color
glActiveTexture( GL_TEXTURE2 ) glBindTexture(GL_T
EXTURE_2D, )
glActiveTexture( GL_TEXTURE3 ) glBindTexture(GL_T
EXTURE_2D, )
38
Get location
  • Glint glGetUniformLocationARB(GLhandleARBprogram,
    const GLcharARB name)
  • Return an integer to represent the location of
    a specific uniform variable.
  • name is the name of the uniform variable in the
    shader.
  • The location of a variable is assigned in link
    time, so this function should be called after the
    link stage.

39
Set value
  • Following functions are used to assign values for
    uniform variables
  • Void glUniform1,2,3,4f,iARB(Glint
    location, TYPE v)
  • Void glUniform1,2,3,4f,ivARB(Glint
    location, Gluint count, TYPE v)
  • Void glUniformMatrix2,3,4fvARB(Glint
    location,GLuint count, GLboolean transpose,
    const GLfloat v)
  • location is the value obtained using
    glGetUniformLocationARB().
  • v is the value to be assigned.

40
Texture Mapping C Code
  • glUseProgramObject(MyShader)
  • glActiveTexture( GL_TEXTURE0 )
  • glBindTexture(GL_TEXTURE_2D, texObject0)
  • GLint location glGetUniformLocation(MyShader,
    "colorTexture")
  • if(location -1) printf("Cant find texture
    name colorTexture\n")
  • else glUniform1i(location, 0)
  • int i,j
  • for (i0i lt object-gtfTotali)
  • glBegin(GL_POLYGON)
  • for (j0jlt3j)
  • glMultiTexCoord2fv(GL_TEXTURE0,
    object-gttListobject-gtfaceListij.t.ptr)
  • glNormal3fv(object-gtnListobject-gtfaceListij
    .n.ptr)
  • glVertex3fv(object-gtvListobject-gtfaceListij
    .v.ptr)
  • glEnd()

41
Texture Mapping Vertex Shader Code
  • void main()
  • gl_TexCoord0.xy gl_MultiTexCoord0.xy
  • gl_Position ftransform()

42
Texture Mapping Pixel Shader Code
  • uniform sampler2D colorTexture
  • void main (void)
  • gl_FragColor texture2D(colorTexture,gl_TexCoord
    0.xy).rgba

43
gl_TexCoord0
Vertex Shader
FragmentShader (colorTexture)
gl_MultiTexCoord0
rasterizer
glGetUniformLocation
glActiveTexture
location
GL_TEXTURE0
glBindTexture(..)
glUniform1i(location, 0)
id
GL_TEXTURE1
Texture Content

GL_TEXTUREn
44
Result 2
Write a Comment
User Comments (0)
About PowerShow.com