Upgrade to Pro — share decks privately, control downloads, hide ads and more …

An iOS Developer's OpenGL Primer

An iOS Developer's OpenGL Primer

Slides from my talk at NSScotland in October 2011. An explanatory introduction to OpenGL for iOS programmers.

More information here: http://www.blog.montgomerie.net/slides-from-an-ios-developers-opengl-primer

A word of warning: the slides are designed as an aid to the talk, not the other way around. There’s lots of information that was in the talk that’s not on the slides, and that might make them confusing or hard to follow. If there was video from the talk, that would be much better than slides, but there’s not, so slides it is.

Avatar for Jamie Montgomerie

Jamie Montgomerie

February 09, 2012
Tweet

Other Decks in Programming

Transcript

  1. Where do the pixels come from? • Fragment shader. •

    Simple program that runs on the GPU.
  2. Where do the pixels come from? • Fragment shader. •

    Simple program that runs on the GPU. • Computes the shade for each fragment, or pixel.
  3. Where do the pixels come from? • Fragment shader. •

    Simple program that runs on the GPU. • Computes the shade for each fragment, or pixel. • Run once for every pixel simultaneously.
  4. Where do the pixels come from? • Fragment shader. •

    Simple program that runs on the GPU. • Computes the shade for each fragment, or pixel. • Run once for every fragment independently.
  5. Where do the pixels come from? • Fragment shader. •

    Simple program that runs on the GPU. • Computes the shade for each fragment, or pixel. • Run once for every pixel simultaneously.
  6. OpenGL ES 2.0 Shading Language uniform lowp sampler2D sTexture; varying

    highp vec2 vTextureCoordinate; void main() { gl_FragColor = texture2D(sTexture, vTextureCoordinate); }
  7. Scalars float int bool Vectors [i,b]vec2 [i,b]vec3 [i,b]vec4 Matrices mat2

    mat3 mat4 Samplers sampler2d samplerCube Shading Language Basic Types
  8. float i = 1.0; const float too = 2.0; Shading

    Language Variable Initialisation
  9. float i = 1.0; const float too = 2.0; Shading

    Language Variable Initialisation
  10. float i = 1.0; const float too = 2.0; int

    j = 1; Shading Language Variable Initialisation
  11. vec4 v = vec4(1.0, 2.0, 3.0, 4.0); mat2 m =

    mat2(1.0, 0.0, // 1st Column 0.0, 1.0); // 2nd Column Shading Language Variable Initialisation
  12. OpenGL ES 2.0 Shading Language uniform lowp sampler2D sTexture; varying

    highp vec2 vTextureCoordinate; void main() { gl_FragColor = texture2D(sTexture, vTextureCoordinate); }
  13. Colours.fsh uniform lowp sampler2D sTexture; varying highp vec2 vTextureCoordinate; void

    main() { gl_FragColor = texture2D(sTexture, vTextureCoordinate); }
  14. Colours.fsh uniform lowp sampler2D sTexture; varying highp vec2 vTextureCoordinate; void

    main() { gl_FragColor = texture2D(sTexture, vTextureCoordinate); }
  15. Colours.fsh uniform lowp sampler2D sTexture; varying highp vec2 vTextureCoordinate; void

    main() { gl_FragColor = texture2D(sTexture, vTextureCoordinate); }
  16. Colours.fsh uniform lowp sampler2D sTexture; varying highp vec2 vTextureCoordinate; void

    main() { gl_FragColor = texture2D(sTexture, vTextureCoordinate); }
  17. Colours.fsh uniform lowp sampler2D sTexture; varying highp vec2 vTextureCoordinate; void

    main() { gl_FragColor = texture2D(sTexture, vTextureCoordinate); }
  18. Colours.fsh uniform lowp sampler2D sTexture; varying highp vec2 vTextureCoordinate; void

    main() { gl_FragColor = texture2D(sTexture, vTextureCoordinate); }
  19. Colours.fsh uniform lowp sampler2D sTexture; varying highp vec2 vTextureCoordinate; void

    main() { gl_FragColor = texture2D(sTexture, vTextureCoordinate); }
  20. Colours.fsh uniform lowp sampler2D sTexture; varying highp vec2 vTextureCoordinate; void

    main() { gl_FragColor = texture2D(sTexture, vTextureCoordinate); }
  21. Colours.fsh uniform lowp sampler2D sTexture; varying highp vec2 vTextureCoordinate; void

    main() { gl_FragColor = texture2D(sTexture, vTextureCoordinate); }
  22. Colours.fsh uniform lowp sampler2D sTexture; varying highp vec2 vTextureCoordinate; void

    main() { gl_FragColor = texture2D(sTexture, vTextureCoordinate); }
  23. Colours.fsh uniform lowp sampler2D sTexture; varying highp vec2 vTextureCoordinate; void

    main() { gl_FragColor = texture2D(sTexture, vTextureCoordinate); }
  24. Colours.fsh uniform lowp sampler2D sTexture; varying highp vec2 vTextureCoordinate; void

    main() { gl_FragColor = texture2D(sTexture, vTextureCoordinate); }
  25. Colours.fsh uniform lowp sampler2D sTexture; varying highp vec2 vTextureCoordinate; void

    main() { gl_FragColor = texture2D(sTexture, vTextureCoordinate); }
  26. Colours.fsh uniform lowp sampler2D sTexture; varying highp vec2 vTextureCoordinate; void

    main() { gl_FragColor = texture2D(sTexture, vTextureCoordinate); }
  27. Where are these inputs coming from? • uniforms • Your

    app code • Objective-C (or other language - C, C++, Java etc.)
  28. Where are these inputs coming from? • varyings • Vertex

    shader • Simple program that runs on the GPU.
  29. Where are these inputs coming from? • varyings • Vertex

    shader • Simple program that runs on the GPU. • OpenGL ES 2.0 Shading Language code.
  30. Where are these inputs coming from? • varyings • Vertex

    shader • Simple program that runs on the GPU. • OpenGL ES 2.0 Shading Language code. • Run once for every vertex simultaneously.
  31. Where are these inputs coming from? • varyings • Vertex

    shader • Simple program that runs on the GPU. • OpenGL ES 2.0 Shading Language code. • Run once for every vertex independently.
  32. Where are these inputs coming from? • varyings • Vertex

    shader • Simple program that runs on the GPU. • OpenGL ES 2.0 Shading Language code. • Run once for every vertex simultaneously.
  33. What’s a Vertex? • A vertex is a point •

    Represents • Disconnected point
  34. What’s a Vertex? • A vertex is a point •

    Represents • Disconnected point • Line end point
  35. What’s a Vertex? • A vertex is a point •

    Represents • Disconnected point • Line end point • Triangle corner point
  36. What’s a Vertex? • A vertex is a point •

    Represents • Disconnected point • Line end point • Triangle corner point • Two or three dimensional (vec2, vec3)
  37. What’s a Triangle Strip? • An array of points... ...an

    array of vec2s or vec3s, in Shading Language terms.
  38. 1

  39. 1 2

  40. Where do the Fragment Shader inputs come from? • varyings

    • Vertex shader • Simple program that runs on the GPU. • OpenGL ES 2.0 Shading Language code. • Run once for every vertex simultaneously.
  41. Where do the Fragment Shader inputs come from? • varyings

    • Vertex shader • Simple program that runs on the GPU. • OpenGL ES 2.0 Shading Language code. • Run once for every vertex simultaneously. • Calculates the position of the vertex...
  42. Where do the Fragment Shader inputs come from? • varyings

    • Vertex shader • Simple program that runs on the GPU. • OpenGL ES 2.0 Shading Language code. • Run once for every vertex simultaneously. • Calculates the position of the vertex... • ...and arbitrary other inputs to pass to the fragment shaders.
  43. Vertex Shader attribute vec4 aPosition; attribute vec2 aTextureCoordinate; varying vec2

    vTextureCoordinate; void main() { vTextureCoordinate = aTextureCoordinate; gl_Position = aPosition; }
  44. Colours.vsh attribute vec4 aPosition; attribute vec2 aTextureCoordinate; varying vec2 vTextureCoordinate;

    void main() { vTextureCoordinate = aTextureCoordinate; gl_Position = aPosition; }
  45. Colours.vsh attribute vec4 aPosition; attribute vec2 aTextureCoordinate; varying vec2 vTextureCoordinate;

    void main() { vTextureCoordinate = aTextureCoordinate; gl_Position = aPosition; }
  46. Colours.vsh attribute vec4 aPosition; attribute vec2 aTextureCoordinate; varying vec2 vTextureCoordinate;

    void main() { vTextureCoordinate = aTextureCoordinate; gl_Position = aPosition; }
  47. Colours.vsh attribute vec4 aPosition; attribute vec2 aTextureCoordinate; varying vec2 vTextureCoordinate;

    void main() { vTextureCoordinate = aTextureCoordinate; gl_Position = aPosition; }
  48. Colours.vsh attribute vec4 aPosition; attribute vec2 aTextureCoordinate; varying vec2 vTextureCoordinate;

    void main() { vTextureCoordinate = aTextureCoordinate; gl_Position = aPosition; }
  49. Colours.vsh attribute vec4 aPosition; attribute vec2 aTextureCoordinate; varying vec2 vTextureCoordinate;

    void main() { vTextureCoordinate = aTextureCoordinate; gl_Position = aPosition; }
  50. Colours.vsh attribute vec4 aPosition; attribute vec2 aTextureCoordinate; varying vec2 vTextureCoordinate;

    void main() { vTextureCoordinate = aTextureCoordinate; gl_Position = aPosition; }
  51. What does the app code do? • OpenGL content is

    presented in a CALayer... • ...specifically, a CAEAGLLayer.
  52. What does the app code do? • OpenGL content is

    presented in a CALayer... • ...specifically, a CAEAGLLayer. • Use a UIView backed by a CAEAGLLayer...
  53. What does the app code do? • OpenGL content is

    presented in a CALayer... • ...specifically, a CAEAGLLayer. • Use a UIView backed by a CAEAGLLayer... • ...like Xcode template and sample code’s EAGLView.
  54. What does the app code do? • OpenGL content is

    presented in a CALayer... • ...specifically, a CAEAGLLayer. • Use a UIView backed by a CAEAGLLayer... • ...like Xcode template and sample code’s EAGLView. • Use the C OpenGL API to draw.
  55. Integrating with UIKit • You can’t use the setNeedsDisplay model.

    • Draw explicitly when you have something to display.
  56. Integrating with UIKit • You can’t use the setNeedsDisplay model.

    • Draw explicitly when you have something to display. • Draw on a timer (e.g. 30 frames per second).
  57. Integrating with UIKit • You can’t use the setNeedsDisplay model.

    • Draw explicitly when you have something to display. • Draw on a timer (e.g. 30 frames per second). • Use the CADisplayLink timer class.
  58. C OpenGL API • Context based • Like CoreGraphics’ CGContext.

    • No state save/restore. • Not thread safe.
  59. C OpenGL API • Context based • Like CoreGraphics’ CGContext.

    • No state save/restore. • Not thread safe. • Use from one thread at a time.
  60. C OpenGL API • Context based • Like CoreGraphics’ CGContext.

    • No state save/restore. • Not thread safe. • Use from one thread at a time. • Use share pools to share resources between threads.
  61. C OpenGL API • Functions to upload things to the

    GPU, e.g.: • Shader programs • Textures
  62. C OpenGL API • Functions to upload things to the

    GPU, e.g.: • Shader programs • Textures • Shader program uniforms, attributes
  63. C OpenGL API • Functions to upload things to the

    GPU, e.g.: • Shader programs • Textures • Shader program uniforms, attributes • Functions to set context state, e.g.:
  64. C OpenGL API • Functions to upload things to the

    GPU, e.g.: • Shader programs • Textures • Shader program uniforms, attributes • Functions to set context state, e.g.: • Active texture
  65. C OpenGL API • Functions to upload things to the

    GPU, e.g.: • Shader programs • Textures • Shader program uniforms, attributes • Functions to set context state, e.g.: • Active texture • Clear colour
  66. C OpenGL API • Functions to upload things to the

    GPU, e.g.: • Shader programs • Textures • Shader program uniforms, attributes • Functions to set context state, e.g.: • Active texture • Clear colour • Functions to perform rendering
  67. What about 3D stuff? • Do positioning maths in the

    vertex shaders • One uniform mat4 to position each strip - a model matrix.
  68. What about 3D stuff? • Do positioning maths in the

    vertex shaders • One uniform mat4 to position each strip - a model matrix. • One uniform mat4 to position the world around the camera, calculate perspective - a projection matrix.
  69. What about 3D stuff? • Do lighting maths in the

    vertex shaders. • Upload • light information
  70. What about 3D stuff? • Do lighting maths in the

    vertex shaders. • Upload • light information • arrays of vertex normals
  71. What about 3D stuff? • Do lighting maths in the

    vertex shaders. • Upload • light information • arrays of vertex normals • Calculate per-vertex lighting brightness, color
  72. What about 3D stuff? • Do lighting maths in the

    vertex shaders. • Upload • light information • arrays of vertex normals • Calculate per-vertex lighting brightness, color • Formulas online
  73. What about 3D stuff? • Do lighting maths in the

    vertex shaders. • Upload • light information • arrays of vertex normals • Calculate per-vertex lighting brightness, color • Formulas online • Pass as varying to fragment shader.
  74. What about 3D stuff? • Do texturing, other per-pixel effects

    in the fragment shaders. • Multiply the pixel colour with the light colour.
  75. Learning More • OpenGL ES Programming Guide for iOS; Apple

    • OpenGL ES 2.0 Programming Guide; Munshi, Ginsburg & Shreiner
  76. Learning More • OpenGL ES Programming Guide for iOS; Apple

    • OpenGL ES 2.0 Programming Guide; Munshi, Ginsburg & Shreiner • PowerVR Insider web site
  77. Learning More • OpenGL ES Programming Guide for iOS; Apple

    • OpenGL ES 2.0 Programming Guide; Munshi, Ginsburg & Shreiner • PowerVR Insider web site • Watch out for great new things in iOS 5!