VBO offsets calculations...

Started by
2 comments, last by _the_phantom_ 19 years, 6 months ago
ok, i've had a mild brain fart and cant work something out, so if someone could point me in the right direction on how to do what I need todo I'll be greatfull :) Basically, I've got aload of vertex data in a VBO (consiting of location and colour data), so i need to render by setting the pointers, which is easy enuff and works fine if I do it this way :

struct terraindata {
	float x,y,z;
	GLubyte r,g,b,a;	// use four bytes so we dont botch the alignment up
};
glVertexPointer(3, GL_FLOAT, sizeof(terraindata), BUFFER_OFFSET(0));
glColorPointer(4,GL_UNSIGNED_BYTE,sizeof(terraindata),BUFFER_OFFSET(12));	// offset of 12 to skip first 3 floats (4 bytes each)


Which is all well and good but not very... gracefull, as if I monkey around with order (which I probably will do) or add things I've goto work out the magic numbers again... yey. Now, with normal VA you can do this :

glVertexPointer(3, GL_FLOAT, sizeof(terraindata), &vElements[0].x);
glColorPointer(4, GL_FLOAT, sizeof(terraindata), &vElements[0].r);


and ofcourse all your addresses will be worked out nicely, but with VBOs they are from an offset of 0, not a real address. so, anyone got any ideas as to how I could simulate this? hmmm infact typing this post out has given me an idea, I could take the address of the first element, subtract it from the address of the 'r' member and that should give me my offset.. anyone see any huge holes in that plan? I'm falling into bed in a few mins, so I'll probably get on it tomorrow, so if anyone has any ideas in the meantime, or even just to say i'm not going crazy with my idea, then I'd appricate it [smile]
Advertisement
try
&terraindata::x
&terraindata::r
Heya.

Funnily enough, I was messing about with this yesterday (changing from glInterleavedArrays to glEnableClientState / gl___Pointer for VBO - after finding out glInterleavedArrays doesn't play well with multitexture - can't set TU 1+'s texcoords. :().

What you propose will work. At least, it's working for me -

glVertexPointer(3, GL_FLOAT, sizeof(terraindata), (&vElements[0].x) - (&vElements));glColorPointer(4, GL_FLOAT, sizeof(terraindata), (&vElements[0].r) - (&vElements));


(Now if I can just get my damned detail textures to behave, I'll be happy with my terrain...)

Anyways, good luck.
cheers, thats about what I thought it would have to be, glad to see my tiredness muddled brain wasnt too far off [grin]

edit:
quick update, that worked perfectly (once I'd demangled my VBO allocation slightly, note to self its TOTAL size not number of elements), just had to adjust it as VS.Net03 was complaining about mismatching pointers, so ended doing it as:
terraindata temp;vertoffset = &temp.x - (float*)&temp;coloroffset = &temp.r - (GLubyte*)&temp;// I should probably use C++ casts there tbh.. 

(my vertex data is in an std::vector, I could work it out from that instead of making a temp but as its only happening once on startup its not a problem, plus it cuts down on one more thing to go wrong)

This topic is closed to new replies.

Advertisement