5

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.

genpfault
  • 51,148
  • 11
  • 85
  • 139
deniz
  • 2,427
  • 4
  • 27
  • 38
  • Related : http://stackoverflow.com/questions/12548465/use-offsetof-with-glm-opengl-maths – Grimmy Jun 19 '13 at 17:22
  • it seems not 'answered', at least not checked – deniz Jun 19 '13 at 17:24
  • @deniz: "*it seems not 'answered', at least not checked*" That's not what that means. It simply means that the OP didn't accept the answer. Whether it's due to not liking the answer, the answer not being helpful, or simple neglect is impossible to determine. – Nicol Bolas Jun 19 '13 at 17:53

1 Answers1

8

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.

Community
  • 1
  • 1
Nicol Bolas
  • 449,505
  • 63
  • 781
  • 982
  • It definitely works for me. I use glm types all over the place (uniforms and arrays) – Grimmy Jun 19 '13 at 17:58
  • @Nicol Is it too early to use offsetof or do you recommend it ? – deniz Jun 19 '13 at 18:09
  • @deniz: ... how could it be "too early" to use a macro? – Nicol Bolas Jun 19 '13 at 18:10
  • @Nicol, I mean standard, will it cause any unexpected behavior under C++11 compilers ? – deniz Jun 19 '13 at 18:10
  • 2
    @deniz: Did you bother to read what I wrote? "In a conforming C++11 compiler, the GLM types will be considered standard layout types, and therefore offsetof has well-defined behavior." I cannot state it any more directly than that. – Nicol Bolas Jun 19 '13 at 18:12
  • 1
    @deniz I never had any problems be it uniforms, arrays of glm types or arrays of structs with glm types. – Grimmy Jun 19 '13 at 18:15
  • @Grimmy, thanks. I will be cautious this time. By the way, did you use glm::value_ptr for them ? – deniz Jun 19 '13 at 18:17