Advertisement Jump to content
Sign in to follow this  

VBO offsets calculations...

This topic is 5184 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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]

Share this post

Link to post
Share on other sites

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.

Share this post

Link to post
Share on other sites
cheers, thats about what I thought it would have to be, glad to see my tiredness muddled brain wasnt too far off [grin]

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)

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using, you agree to our community Guidelines, Terms of Use, and Privacy Policy. is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!