Skip to content

Instantly share code, notes, and snippets.

@emctague
Last active September 26, 2019 23:50
Show Gist options
  • Save emctague/a60bfe0cf511e069599931c1de401bb6 to your computer and use it in GitHub Desktop.
Save emctague/a60bfe0cf511e069599931c1de401bb6 to your computer and use it in GitHub Desktop.
OpenGL Attrib Pointers Template System
/** Base case of OpenGL attrib pointer template tool. */
template<typename P, typename T>
inline void attribPointers(int n = 0, size_t s = 0) {
glEnableVertexAttribArray(n);
glVertexAttribPointer(n, sizeof(T)/sizeof(float), GL_FLOAT, GL_FALSE, sizeof(P), (void*)s);
}
/** Applies all OpenGL attrib pointers given as type arguments (first argument is vertex class). */
template<typename P, typename T, typename S, typename ...Args>
inline void attribPointers(int n = 0, size_t s = 0) {
attribPointers<P, T>(n, s);
attribPointers<P, S, Args...>(n + 1, s + sizeof(T));
}
// Your vertex type
struct Vertex {
glm::vec3 position;
glm::vec3 normal;
glm::vec2 uv;
};
// Wherever you need to define attribs, you do this:
attribPointers<Vertex, glm::vec3, glm::vec3, glm::vec2>();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment