开发者

How to Keep Track of the Index of a Non-Unique Element in a Vector?

I'm most likely going about this the wrong way, but I was wondering if anyone has some advice for how to keep track of where a non-unique element is within a vector?

I'm using glDrawArraysInstanced and using a vector to store translations (i.e. the first element is the x translation, the second y, the third z, repeat) that offset the position of a particular instance. This vector is put into a buffer and the drawing works as expected with a good performance boost from using the instancing. I don't want to set the positions based on gl_InstanceID because the ID shifts depending on the number of instances drawn but I need the instances to stay consistent.

The problem comes in when trying to make the number of instances dynamic; I need to be able to add and remove elements in the vector efficiently without rebuilding the entire vector each time something changes. I'm not sure how to determine exactly where the translation is inside of the vector, however.


As an example:

  • Instance 0 uses translations 0,1,2
  • Instance 1 uses translations 3,4,5
  • Instance 2 uses translat开发者_开发问答ions 6,7,8

Want to delete Instance 1:

I can store the index of the x component of the translation when the translations are first constructed, so I know I need to .erase 3 elements starting at 3.

Want to delete Instance 2:

How do I know what the starting index is for this instance? The indices stored during construction are no longer valid.


I know this case is trivial and I can just subtract from the initial stored index, but if I have say 40,000 instances is there an efficient way to always know where the specific translation for that instance is within the vector? After several additions/deletions of instances the indices can shift around a fair amount.

Note: I want to use the vector because OpenGL needs float* to fill the buffer. I could easily use a map between an instance and it's positions, but at some point I'd still need to get all of the positions together for rendering. If I'm missing something obvious on this point though please let me know. Swapping out the buffer doesn't seem to impact performance much, but rebuilding the entire positions vector when something changes is far too slow.

Hope this isn't too much of a wall of text.. any help would be appreciated.


Use "GLDrawElementsInstanced" instead so that there is no need to change the translation vector (just reorder the indices).

Update: (Real answer:)

Bind the transformation buffer (with ALL transformations including invisible instances) as TBO, then access it as a texture in a shader. Supply an index attribute per instance (glVertexAttribDivisor=1) to use as a coordinate to fetch from your translation texture.

This way you'll need to modify only the index buffer and append new translations to the translation buffer. At some points you can perform a cleanup procedure of the translation buffer by removing unused values.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜