Page 1 of 1

OpenGL ES 2.0 shader setup

Posted: Tue Aug 09, 2011 3:06 pm
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)