OpenGL ES 2.0 shader setup

> Coding, hacking, computer graphics, game dev, and such...
User avatar
fips
Site Admin
Posts: 166
Joined: Wed Nov 12, 2008 9:49 pm
Location: Prague
Contact:

OpenGL ES 2.0 shader setup

Post by fips »

As I've found myself not remembering all this stuff, I've decided to put together a piece of pseudo code showing a typical command sequence for OpenGL ES 2.0 shader initialization and drawing. (Note that there's an alternative way to obtain vertex attribute locations using glGetAttribLocation(), but I rather prefer explicit binding using glBindAttribLocation()).

---Init---

Code: Select all

glCreateShader() => vertShader
glShaderSource(vertShader, vertSource)
glCompileShader(vertShader)

glCreateShader() => fragShader
glShaderSource(fragShader, fragSource)
glCompileShader(fragShader)

glCreateProgram() => program

glAttachShader(program, vertShader)
glAttachShader(program, fragShader)

glBindAttribLocation(program, attribLoc0, attribName0)
...
glBindAttribLocation(program, attribLocN, attribNameN)

glLinkProgram(program)

glGetUniformLocation(program, unifName0) => unifLoc0
...
glGetUniformLocation(program, unifNameN) => unifLocN
---Draw---

Code: Select all

glBindBuffer(GL_ARRAY_BUFFER, vtxBuffer)
glVertexAttribPointer(attribLoc0, ...)
glEnableVertexAttribArray(attribLoc0)
...
glVertexAttribPointer(attribLocN, ...)
glEnableVertexAttribArray(attribLocN)

glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, idxBuffer)

glUseProgram(program)

glUniform*(unifLoc0, unifValue0)
...
glUniform*(unifLocN, unifValueN)

glDrawElements(...)
References:
OpenGL ES 2.0 API Reference (khronos.org)
Vertex Shader Attribute Mapping in GLSL (stackoverflow.com)