Due to weak design, I had to rewrite my WHOLE math library and things related with math. Instead, I found out that I can use GLM. If you have encountered my previous questions, I have been dealing with skeletal animation so I have to upload large quantities of mat4s, vec3, vec2s. Here is my previous vertex struct :
struct Vertex {
Vec3 pos;
Vec3 normal;
Vec2 uv;
Vec4 boneindex;
Vec4 weightbias;
};
Unfortunately I have found out that my math library was not POD and I tried to upload the whole struct and I sure got weird results. Same for my final bone matrices defined as :
Mat4 bones[numBones];
and I also tried to upload that at once.
I just wonder If I replace my Vec3s, Vec2s and Mat4s with glm::vec3, vec2, mat4 and directly upload the whole Vertex struct (number of vertices times) and set an offset for each data using:
glBindBuffer(GL_ARRAY_BUFFER,everythingVBO);
glVertexAttribPointer(xx,xx,xx,xx, BUFFER_OFFSET(0)) // pos
glVertexAttribPointer(xx,xx,xx,xx, BUFFER_OFFSET(sizeof(glm::vec3)) // norm
..
..
same for matrix
glUniformMatrix4fv(xx, numBones, xx, glm::mat4 value)
.
Will it work as intended ? I just don't want to re-write whole thing and this time I must make sure that It works because I don't want to face another failure again.
This is both a very simple and very complex question. So I'll start from the simple way first.
If you're looking to build some struct
out of GLM types, upload them to a buffer, and access them via glVertexAttribPointer
, you can probably get away with it on most C++98/03 compilers. Assuming you get offsets properly. It won't be "portable C++"; as far as the standard is concerned, it will be implementation-defined behavior (or undefined behavior if you try to use the offsetof
macro. Granted, anything you might use in place of offsetof
will be no less well defined by the standard).
But it will generally work.
In a conforming C++11 compiler, the GLM types will be considered standard layout types, and therefore offsetof
has well-defined behavior. And as long as your Vertex
structure is also standard layout, offsetof
will work.
However, that doesn't mean that the internal structure of any of these types will not have arbitrary padding. A glm::vec3
is not required to be layout-compatible with a float[3]
, which is what OpenGL would expect. The C++ standard won't protect you.
That being said, it's pretty safe to assume for most compilers you will encounter that GLM's types will be equivalent to arrays. GLM even provides the type_ptr
function to get a pointer to the contents of one of its types as an array, under the expectation that the compiler will make them compatible.
Similarly, the C++ standard provides no guarantees that a float[16*n]
will be layout-compatible with an array of glm::mat4[n]
. But again, it's fairly safe to assume that it will just work.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With