Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- struct ObjectID{
- long int ID;
- uint ElementsSize;
- uint VerticesSize;
- };
- struct BufferData{
- std::vector<GLuint> Elements;
- std::vector<Vertex> Vertices;
- std::vector<long int> ElementsPositions;
- std::vector<long int> VerticesPositions;
- ObjectID AddData(std::vector<GLuint> NewElements,
- std::vector<Vertex> NewVertices);
- void RemoveData(ObjectID ID);
- };
- ObjectID BufferData::AddData(std::vector<GLuint> newelements,
- std::vector<Vertex> newvertices){
- //Set ID
- ObjectID ID;
- ID.ID = ElementsPositions.size();
- ID.ElementsSize = newelements.size();
- ID.VerticesSize = newvertices.size();
- //Ensure the incoming data makes some sense
- assert(newelements.size()%3 == 0);
- assert(newvertices.size()%3 == 0);
- //The object ID is its position in the elements/vertices positions vector
- ElementsPositions.push_back(Elements.size());
- VerticesPositions.push_back(Vertices.size());
- //Because the elements need to refer to the right vertices
- for(uint i = 0; i < newelements.size(); ++i){
- newelements[i] += Vertices.size();
- }
- Elements.insert(Elements.end(), newelements.begin(), newelements.end());
- Vertices.insert(Vertices.end(), newvertices.begin(), newvertices.end());
- return ID;
- }
- void BufferData::RemoveData(ObjectID ID){
- //Check that the Object data has not already been removed
- assert(ElementsPositions[ID.ID] != -1);
- assert(VerticesPositions[ID.ID] != -1);
- uint ElementsStart, ElementsEnd;
- uint VerticesStart, VerticesEnd;
- //Determine end point of the objects element data
- ElementsStart = ElementsPositions[ID.ID];
- ElementsEnd = ElementsPositions[ID.ID] + ID.ElementsSize;
- //Update the positions of the elements
- for(int i = ID.ID; i < ElementsPositions.size(); ++i){
- ElementsPositions[i] -= ElementsPositions[ID.ID];
- }
- //Change the ElementsPositions value to reflect the deletion
- ElementsPositions[ID.ID] = -1;
- //Update the value of the elements
- for(uint i = ElementsEnd; i < Elements.size(); ++i){
- Elements[i] -= ID.ElementsSize;
- }
- //Erase the elements of the object
- //(ElementsEnd being the last element erased)
- Elements.erase(Elements.begin()+ElementsStart, Elements.begin()+ElementsEnd);
- //Determine end point of the objects vertices data
- VerticesStart = VerticesPositions[ID.ID];
- VerticesEnd = VerticesPositions[ID.ID] + ID.VerticesSize;
- //Change the VerticesPositions value to reflect the deletion
- VerticesPositions[ID.ID] = -1;
- //Update the value of the vertices
- for(uint i = VerticesEnd; i < Vertices.size(); ++i){
- VerticesPositions[i] -= ID.VerticesSize;
- }
- //Erase the vertices of the object
- //(VerticesEnd being the last vertex erased)
- Vertices.erase(Vertices.begin()+VerticesStart, Vertices.begin()+VerticesEnd);
- //Check if the entire thing is empty
- bool IsEmpty = true;
- for(size_t i = 0; i < ElementsPositions.size(); ++i){
- IsEmpty = IsEmpty && (ElementsPositions[i] == -1);
- }
- if(IsEmpty){
- ElementsPositions.clear();
- VerticesPositions.clear();
- Elements.clear();
- Vertices.clear();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement