问题
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).
回答1:
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.");
回答2:
Yes, glm is designed and built specifically for this purpose.
来源:https://stackoverflow.com/questions/13571898/c-opengl-glm-and-struct-padding