C++: OpenGL, glm and struct padding

大憨熊 提交于 2019-12-09 02:49:22

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!