OpenGL and STL?

I use openGL and currently pass it an array of vertices. The problem is that I have to create many vertices and add them together (for ordering). This means using a regular array is quite annoying / inefficient.

I want to use the data structure from STL so that I can efficiently (and easily) place new vertices at any index. The problem is that openGL expects a regular array.

Does anyone know how to do this? Is there an easy way to convert from an STL vector to an array?

I am using openGL 1.1

thanks

+6
c ++ stl opengl
source share
4 answers

You can use a pointer to the first address of the vector as an array pointer. STL vectors are guaranteed to store their elements in continuous memory. So you can just do something like:

&vertices[0] 

where vertices is your vector.

+15
source share

OpenGL requires a continuous array of elements. There are apparently obvious reasons, there is no efficient way to insert one element into an adjacent array. This is necessarily at least O (N).

However, you could add N elements less than O (N ^ 2), which the vector reaches for N random inserts.

For example, if you don’t actually add new vertices β€œat any index” but are always close to the previous one, you can add all the elements to std :: list (O (1) for the element, O (N)), then copy std :: list in std :: vector. In fact, it should not be the previous element, just the previous element, so if the order is based on a recursive traversal of a tree, then you can still do it.

If new vertices are added at an index defined by some linear order, add all the elements in std :: map or std :: multi_map (O (log N) to the element, common O (N log N)), then copy it to the vector.

Thus, the method of performing the lowest complexity depends on how the order is determined. Whether these solutions with lower complexity are actually faster than the vector depends on N. They have much higher overhead (O (N) distribution instead of O (log N) for the vector), so N can be quite large up to asymptotic behavior is triggered.

If you use any of the solutions described by us, then a simple / effective way to copy a list or map into a vector is as follows:

 std::vector<glVertex3f> vec; vec.reserve(listormap.size()); vec.insert(vec.begin(), listormap.begin(), listormap.end()); 
+3
source share
 vector<int> array; ..... functionThatAcceptsArray(&array[0]); // yes, this is standard 
+1
source share

It depends on how much control you need over the intermediate representations of the data, and how much you can count.
For a trade-off between freedom and memory usage, read about the Winged Edge data structure. The structure provides quick access and traversal between vertices, edges, and faces and works as a double linked list. If you implement a concept and implement an iterator for it, you can use std :: copy to copy data to any STL container.
As mentioned above, use std :: vector as the final view when OpenGL needs data. And finally:
Do not be afraid to have multiple copies of the same data!

+1
source share

All Articles