Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++: OpenGL, glm and struct padding

can I safely use the glm::* types (e.g. vec4, mat4) to fill a vertex buffer object ?

std::vector<glm::vec3> vertices;    
glBufferData(GL_ARRAY_BUFFER,  sizeof(glm::vec3) * vertices.size(), &vertices[0], GL_STATIC_DRAW);

I'm not quite sure about that since struct padding (member alignment) could cause some trouble in my opinion, though all compilers I've tested returns the expected sizes.

I'm developing for C++11 Compilers (maybe this make a difference).

like image 828
Kr0e Avatar asked Nov 26 '12 19:11

Kr0e


1 Answers

Define "safe".

C++ gives implementations wide latitude to pad structures as they see fit. So as far as ISO C++ is concerned, whether this "works" is implementation-dependent behavior.

It will work in general across a number of compilers for desktop platforms. I can't speak for ARM CPUs, but generally, glm::vec3 will be 3 floats in size. However, if you want to make sure, you can always perform a simple static_assert:

static_assert(sizeof(glm::vec3) == sizeof(GLfloat) * 3, "Platform doesn't support this directly.");
like image 60
Nicol Bolas Avatar answered Sep 30 '22 02:09

Nicol Bolas