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

DroidconUK 2017 - OpenGL - A noob's guide for Android developers

DroidconUK 2017 - OpenGL - A noob's guide for Android developers

OpenGL is often regarded as too complex, obscure or even too low level to be considered when developing an application. Most people will even try to go around it and develop what they want using Views and Canvas to avoid developing with it. Nevertheless, it's not always possible nor the best solution.

As someone who have been going through all the learning of OpenGL recently, Benjamin has encountered a lot of frustration but also a lot of “aha!“ moment. In this talk, Benjamin will try to demystified OpenGL and share the basics of what it is and how to use it in Android to make applications perform better. Also, you will learn all the pitfalls and key concepts that you should know when starting with OpenGL.

Benjamin Monjoie

October 26, 2017
Tweet

More Decks by Benjamin Monjoie

Other Decks in Programming

Transcript

  1. Roadmap • What is OpenGL ? • What about with

    Android ? • OpenGL’s internals • Shaders • Enter the matrix • Advices • Bonus
  2. Roadmap • What is OpenGL ? • What about with

    Android ? • OpenGL’s internals • Shaders • Enter the matrix • Advices • Bonus
  3. OpenGL & Android • OpenGL ES 1.0 & 1.1 since

    Android 1.0 (API 4) • OpenGL ES 2.0 since Android 2.2 (API 8)
  4. OpenGL & Android • OpenGL ES 1.0 & 1.1 since

    Android 1.0 (API 4) • OpenGL ES 2.0 since Android 2.2 (API 8) • OpenGL ES 3.0 since Android 4.3 (API 18) (almost)
  5. OpenGL & Android • OpenGL ES 1.0 & 1.1 since

    Android 1.0 (API 4) • OpenGL ES 2.0 since Android 2.2 (API 8) • OpenGL ES 3.0 since Android 4.3 (API 18) (almost) • OpenGL ES 3.1 since Android 5.0 (API 21)
  6. OpenGL & Android • OpenGL ES 1.0 & 1.1 since

    Android 1.0 (API 4) • OpenGL ES 2.0 since Android 2.2 (API 8) • OpenGL ES 3.0 since Android 4.3 (API 18) (almost) • OpenGL ES 3.1 since Android 5.0 (API 21)
  7. Roadmap • What is OpenGL ? • What about with

    Android ? • OpenGL’s internals • Shaders • Enter the matrix • Advices • Bonus
  8. What about with Android ? GLSurfaceView & GLSurfaceView.Renderer onSurfaceCreated (GL10

    gl, EGLConfig config) onSurfaceChanged (GL10 gl, int width, int height) onDrawFrame (GL10 gl)
  9. Roadmap • What is OpenGL ? • What about with

    Android ? • OpenGL’s internals • Shaders • Enter the matrix • Advices • Bonus
  10. Roadmap • What is OpenGL ? • What about with

    Android ? • OpenGL’s internals • Shaders • Enter the matrix • Advices • Bonus
  11. Vertex shader - Example precision mediump float; uniform mat4 uMVPMatrix;

    attribute vec4 vPosition; attribute vec4 vTextureCoordinate; varying vec2 position; void main() { gl_Position = uMVPMatrix * vPosition; position = vTextureCoordinate.xy; }
  12. Vertex shader - Example precision mediump float; uniform mat4 uMVPMatrix;

    attribute vec4 vPosition; attribute vec4 vTextureCoordinate; varying vec2 position; void main() { gl_Position = uMVPMatrix * vPosition; position = vTextureCoordinate.xy; }
  13. Vertex shader - Example precision mediump float; uniform mat4 uMVPMatrix;

    attribute vec4 vPosition; attribute vec4 vTextureCoordinate; varying vec2 position; void main() { gl_Position = uMVPMatrix * vPosition; position = vTextureCoordinate.xy; }
  14. Vertex shader - Example precision mediump float; uniform mat4 uMVPMatrix;

    attribute vec4 vPosition; attribute vec4 vTextureCoordinate; varying vec2 position; void main() { gl_Position = uMVPMatrix * vPosition; position = vTextureCoordinate.xy; }
  15. Vertex shader - Example precision mediump float; uniform mat4 uMVPMatrix;

    attribute vec4 vPosition; attribute vec4 vTextureCoordinate; varying vec2 position; void main() { gl_Position = uMVPMatrix * vPosition; position = vTextureCoordinate.xy; }
  16. Fragment shader - Example precision mediump float; uniform sampler2D uTexture;

    varying vec2 position; void main() { gl_FragColor = texture2D(uTexture, position); }
  17. Fragment shader - Example precision mediump float; uniform sampler2D uTexture;

    varying vec2 position; void main() { gl_FragColor = texture2D(uTexture, position); }
  18. Fragment shader - Example precision mediump float; uniform sampler2D uTexture;

    varying vec2 position; void main() { gl_FragColor = texture2D(uTexture, position); }
  19. Fragment shader - Example precision mediump float; uniform sampler2D uTexture;

    varying vec2 position; void main() { gl_FragColor = texture2D(uTexture, position); }
  20. Attribute vs Uniform vs Varying Attribute Attribute of a vertex

    (position, color, etc). Uniform Value passed by the application to the shader. Constant during a draw call. Varying Information passed from the vertex shader to the fragment shader.
  21. Attribute vs Uniform vs Varying Attribute Uniform Varying Vertex Shader

    ✔ ✔ ✔ Fragment Shader ✔ ✔ Can change ~
  22. Roadmap • What is OpenGL ? • What about with

    Android ? • OpenGL’s internals • Shaders • Enter the matrix • Advices • Bonus
  23. The strip POSITION_MATRIX = { -1,-1, 1, // X1,Y1,Z1 1,-1,

    1, // X2,Y2,Z2 -1, 1, 1, // X3,Y3,Z3 1, 1, 1, // X4,Y4,Z4 }; TEXTURE_COORDS[] = { 0, 1, // X1,Y1 1, 1, // X2,Y2 0, 0, // X3,Y3 1, 0, // X4,Y4 };
  24. Roadmap • What is OpenGL ? • What about with

    Android ? • OpenGL’s internals • Shaders • Enter the matrix • Advices • Bonus
  25. Advices • Abstract as much as possible • Crash often

    but crash well ! • Test, a lot ! • Use Google’s Play Store’s Alpha & Beta channels
  26. Roadmap • What is OpenGL ? • What about with

    Android ? • OpenGL’s internals • Shaders • Enter the matrix • Advices • Bonus
  27. Roadmap • What is OpenGL ? • What about with

    Android ? • OpenGL’s internals • Shaders • Enter the matrix • Advices • GPUEffect
  28. GPUEffect • Inspired by CyberAgent’s GPUImage • Written in Kotlin

    • As close as possible to OpenGL • Fail often and fail early
  29. GPUImage ? • Applies filters to images • Works with

    Picasso, Glide and Fresco • Works with SurfaceView • Not maintained for several years & too opinionated
  30. Written in Kotlin • Allows to write shaders with “““

    • Properties for cleaner code • Operators overloading (“+=”, “+”, ..) • Infix
  31. As close as possible to OpenGL • Interfaces like :

    Texture, Program and FrameBuffer • Classes like : BitmapTexture, GLSLProgram, FBOTexture