Slide 1

Slide 1 text

@chrismiles http://chrismiles.info/ Chris Miles OpenGL ES with iOS 5 Part 1: Learning to draw 1

Slide 2

Slide 2 text

SWIPE CONFERENCE 2012 @chrismiles OpenGL ES Open standard for 2D & 3D graphics programming Hardware accelerated by GPU Cross platform C-based API ES: Embedded Systems variant OpenGL ES 1.1 and 2.0 2

Slide 3

Slide 3 text

SWIPE CONFERENCE 2012 @chrismiles GLKit Framework added to iOS 5 Helps simplify OpenGL ES integration GLKViewController, GLKView GLKBaseEffect GLKMath 3

Slide 4

Slide 4 text

SWIPE CONFERENCE 2012 @chrismiles Our Goal Learn enough to understand the “OpenGL Game” template project 4

Slide 5

Slide 5 text

SWIPE CONFERENCE 2012 @chrismiles GLKViewController GLKView 5

Slide 6

Slide 6 text

SWIPE CONFERENCE 2012 @chrismiles #import @interface OGDTrianglesViewController : GLKViewController @end 6

Slide 7

Slide 7 text

SWIPE CONFERENCE 2012 @chrismiles - (void)viewDidLoad { [super viewDidLoad]; self.context = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2]; GLKView *view = (GLKView *)self.view; view.context = self.context; view.drawableDepthFormat = GLKViewDrawableDepthFormat24; self.preferredFramesPerSecond = 60; [self setupGL]; } 7

Slide 8

Slide 8 text

SWIPE CONFERENCE 2012 @chrismiles #pragma mark - GLKViewController frame update - (void)update { // Update frame data: transforms, physics, vertex animations, etc } #pragma mark - GLKViewDelegate methods - (void)glkView:(GLKView *)view drawInRect:(CGRect)rect { // Draw frame using OpenGL ES } 8

Slide 9

Slide 9 text

SWIPE CONFERENCE 2012 @chrismiles GLKViewController Implements an OpenGL ES rendering loop Automatically pause when inactive or backgrounded Recommended way to support device rotation Works with a GLKView 9

Slide 10

Slide 10 text

SWIPE CONFERENCE 2012 @chrismiles GLKView Creates and manages an OpenGL ES framebuffer object Draw by overriding drawRect: or by assigning a delegate object Supports colour, depth & stencil buffers Supports 4x multisampling 10

Slide 11

Slide 11 text

SWIPE CONFERENCE 2012 @chrismiles Vertex Data 11

Slide 12

Slide 12 text

SWIPE CONFERENCE 2012 @chrismiles OpenGLESDrawing GL_TRIANGLES 12

Slide 13

Slide 13 text

SWIPE CONFERENCE 2012 @chrismiles 1 2 3 4 1 2 1 2 3 4 5 6 13

Slide 14

Slide 14 text

SWIPE CONFERENCE 2012 @chrismiles #define kNumVertices 24 static GLfloat gTrianglesBoxVertexData[kNumVertices*3] = { // position x, position y, position z // Front -0.5f, -0.5f, 0.5f, // 1 0.5f, 0.5f, 0.5f, // 2 -0.5f, 0.5f, 0.5f, // 3 -0.5f, -0.5f, 0.5f, // 1 0.5f, -0.5f, 0.5f, // 4 0.5f, 0.5f, 0.5f, // 2 . . . }; 14

Slide 15

Slide 15 text

SWIPE CONFERENCE 2012 @chrismiles glGenVertexArraysOES(1, &_trianglesVertexArray); glBindVertexArrayOES(_trianglesVertexArray); glGenBuffers(1, &_trianglesVertexBuffer); glBindBuffer(GL_ARRAY_BUFFER, _trianglesVertexBuffer); glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat)*kNumVertices*3, _vertexData, GL_DYNAMIC_DRAW); glEnableVertexAttribArray(GLKVertexAttribPosition); glVertexAttribPointer(GLKVertexAttribPosition, 3, GL_FLOAT, GL_FALSE, 0, (char *)0); Vertex Array Object (VAO) ➔Vertex data configuration Vertex Buffer Object (VBO) ➔Vertex data buffer Position vertex attribute ➔Enable and configure 15

Slide 16

Slide 16 text

SWIPE CONFERENCE 2012 @chrismiles Vertex Array Object Encapsulates vertex data configuration OpenGL driver can optimise for the saved state glGenVertexArraysOES() glBindVertexArrayOES() 16

Slide 17

Slide 17 text

SWIPE CONFERENCE 2012 @chrismiles Vertex Buffer Object Stores vertex data in GPU memory Avoids copying vertex data for every frame glGenBuffers() glBindBuffer() glBufferData() 17

Slide 18

Slide 18 text

SWIPE CONFERENCE 2012 @chrismiles Vertex Attribute Configuration Enable an attribute with glEnableVertexAttribArray() Configure attribute data with glVertexAttribPointer() Use attribute indices provided by GLKit: GLKVertexAttribPosition GLKVertexAttribNormal GLKVertexAttribColor GLKVertexAttribTexCoord0 18

Slide 19

Slide 19 text

SWIPE CONFERENCE 2012 @chrismiles GLKBaseEffect 19

Slide 20

Slide 20 text

SWIPE CONFERENCE 2012 @chrismiles OpenGLESDrawing GL_TRIANGLES 20

Slide 21

Slide 21 text

SWIPE CONFERENCE 2012 @chrismiles - (void)setupGL { self.effect = [[GLKBaseEffect alloc] init]; } - (void)update { self.effect.transform.projectionMatrix = projectionMatrix; self.effect.transform.modelviewMatrix = modelViewMatrix; } GLKBaseEffect 21

Slide 22

Slide 22 text

SWIPE CONFERENCE 2012 @chrismiles - (void)glkView:(GLKView *)view drawInRect:(CGRect)rect { glClearColor(0.0f, 0.0f, 0.0f, 1.0f); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); [self.effect prepareToDraw]; glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat)*kNumVertices*3, _vertexData, GL_DYNAMIC_DRAW); glDrawArrays(GL_TRIANGLES, 0, kNumVertices); } Clear colour & depth buffers Enable GLKBaseEffect shader Start rendering Update vertex data 22

Slide 23

Slide 23 text

SWIPE CONFERENCE 2012 @chrismiles GLKBaseEffect Takes care of rendering effects By writing shaders for us Handles: projection & model transforms, vertex colours, texturing, lighting and fog Replicates OpenGL ES 1.1 effects 23

Slide 24

Slide 24 text

SWIPE CONFERENCE 2012 @chrismiles Vertex Colours 24

Slide 25

Slide 25 text

SWIPE CONFERENCE 2012 @chrismiles OpenGLESDrawing GL_TRIANGLES Coloured 25

Slide 26

Slide 26 text

SWIPE CONFERENCE 2012 @chrismiles self.effect = [[GLKBaseEffect alloc] init]; self.effect.constantColor = GLKVector4Make(1.0f, 1.0f, 0.0f, 1.0f); // yellow self.effect.useConstantColor = YES; glEnableVertexAttribArray(GLKVertexAttribColor); Use vertex colours Use constant colour across all vertices OR 26

Slide 27

Slide 27 text

SWIPE CONFERENCE 2012 @chrismiles Interleaved Vertex Array (IVA) {x, y, z, r, g, b, a, x, y, z, r, g, b, a, x, y, z, r, g, b, a} Vertex 1 Vertex 2 Vertex 3 Position (3 floats) Colour (4 ubytes) 27 • Offset and Stride in bytes • Greatly helps with memory locality

Slide 28

Slide 28 text

SWIPE CONFERENCE 2012 @chrismiles typedef struct _vertexStruct { GLfloat position[3]; GLubyte color[4]; } vertexStruct; #define kBytesPerVertex (sizeof(vertexStruct)) 28

Slide 29

Slide 29 text

SWIPE CONFERENCE 2012 @chrismiles GLsizei stride = kBytesPerVertex; glEnableVertexAttribArray(GLKVertexAttribPosition); glVertexAttribPointer(GLKVertexAttribPosition, 3, GL_FLOAT, GL_FALSE, stride, (char *)offsetof(vertexStruct, position)); glEnableVertexAttribArray(GLKVertexAttribColor); glVertexAttribPointer(GLKVertexAttribColor, 4, GL_UNSIGNED_BYTE, GL_TRUE, stride, (char *)offsetof(vertexStruct, color)); Stride = bytes per vertex IVA Offset to attribute data in bytes 29

Slide 30

Slide 30 text

SWIPE CONFERENCE 2012 @chrismiles Lighting 30

Slide 31

Slide 31 text

SWIPE CONFERENCE 2012 @chrismiles OpenGLESDrawing GL_TRIANGLES with Lighting 31

Slide 32

Slide 32 text

SWIPE CONFERENCE 2012 @chrismiles self.effect = [[GLKBaseEffect alloc] init]; self.effect.light0.enabled = GL_TRUE; self.effect.light0.ambientColor = GLKVector4Make(0.1f, 0.1f, 0.1f, 1.0f); self.effect.light0.diffuseColor = GLKVector4Make(1.0f, 1.0f, 1.0f, 1.0f); self.effect.light0.position = GLKVector4Make(0.0f, 0.0f, 1.0f, 0.0f); self.effect.lightModelTwoSided = YES; Enable first light Configure lighting calculation properties 32 • GLKEffectPropertyLight is identical to the lighting model implemented in OpenGL ES 1.1. • W=0.0: Directional Light, infinitely far away, position is direction to the light (i.e. negative of light travel vector) • W>0.0 and spotCutoff=180.0: Point Light, emits light in all directions from specific point, can set attenuation • W>0.0 and spotCutoff<180.0: Spot Light • Ambient Light: “background” non-directional light • Diffuse Light: directional reflected light

Slide 33

Slide 33 text

SWIPE CONFERENCE 2012 @chrismiles Reflected Light Surface Normal 33

Slide 34

Slide 34 text

SWIPE CONFERENCE 2012 @chrismiles Surface Normals v1 v2 v3 typedef struct _vertexStruct { GLfloat position[3]; GLfloat normals[3]; GLubyte color[4]; } vertexStruct; 34

Slide 35

Slide 35 text

SWIPE CONFERENCE 2012 @chrismiles Shaders 35

Slide 36

Slide 36 text

SWIPE CONFERENCE 2012 @chrismiles Vertex Shader Fragment Shader Linear Interpolation Vertex attributes Varyings Uniforms 36

Slide 37

Slide 37 text

SWIPE CONFERENCE 2012 @chrismiles Shaders Vertex Shader: Vertex calculations E.g. project vertices on to 2D plane Executes shader program per vertex 37

Slide 38

Slide 38 text

SWIPE CONFERENCE 2012 @chrismiles Shaders Fragment Shader Receives interpolated values from vertex shader output Calculates output pixel colours Executes shader program per output pixel 38

Slide 39

Slide 39 text

SWIPE CONFERENCE 2012 @chrismiles 39

Slide 40

Slide 40 text

SWIPE CONFERENCE 2012 @chrismiles 40

Slide 41

Slide 41 text

SWIPE CONFERENCE 2012 @chrismiles Summary GLKViewController & GLKView Vertex Data, VAOs, VBOs GLKBaseEffect Colours Lighting Shaders 41

Slide 42

Slide 42 text

SWIPE CONFERENCE 2012 @chrismiles Part 2 Preview Advanced GLKit effects & texturing OpenGL ES debugging & profiling tools Performance & best practices for iOS devices Fancy segue transitions using OpenGL ES More demos! 42

Slide 43

Slide 43 text

SWIPE CONFERENCE 2012 @chrismiles 43

Slide 44

Slide 44 text

SWIPE CONFERENCE 2012 Chris Miles iOS Specialist Software Engineer [email protected] @chrismiles Thank you http://chrismiles.info/ https://github.com/chrismiles/SwipeOpenGLTriangles 44