Advertisement
Guest User

BufferData

a guest
Mar 11th, 2014
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.02 KB | None | 0 0
  1. struct ObjectID{
  2.     long int ID;
  3.     uint ElementsSize;
  4.     uint VerticesSize;
  5.  
  6. };
  7.  
  8. struct BufferData{
  9.     std::vector<GLuint> Elements;
  10.     std::vector<Vertex> Vertices;
  11.     std::vector<long int> ElementsPositions;
  12.     std::vector<long int> VerticesPositions;
  13.  
  14.     ObjectID AddData(std::vector<GLuint> NewElements,
  15.             std::vector<Vertex> NewVertices);
  16.  
  17.     void RemoveData(ObjectID ID);
  18.  
  19. };
  20.  
  21.  
  22. ObjectID BufferData::AddData(std::vector<GLuint> newelements,
  23.         std::vector<Vertex> newvertices){
  24.  
  25.     //Set ID
  26.     ObjectID ID;
  27.     ID.ID = ElementsPositions.size();
  28.     ID.ElementsSize = newelements.size();
  29.     ID.VerticesSize = newvertices.size();
  30.  
  31.     //Ensure the incoming data makes some sense
  32.     assert(newelements.size()%3 == 0);
  33.     assert(newvertices.size()%3 == 0);
  34.  
  35.     //The object ID is its position in the elements/vertices positions vector
  36.     ElementsPositions.push_back(Elements.size());
  37.     VerticesPositions.push_back(Vertices.size());
  38.  
  39.  
  40.     //Because the elements need to refer to the right vertices
  41.     for(uint i = 0; i < newelements.size(); ++i){
  42.         newelements[i] += Vertices.size();
  43.     }
  44.     Elements.insert(Elements.end(), newelements.begin(), newelements.end());
  45.     Vertices.insert(Vertices.end(), newvertices.begin(), newvertices.end());
  46.  
  47.     return ID;
  48. }
  49.  
  50. void BufferData::RemoveData(ObjectID ID){
  51.     //Check that the Object data has not already been removed
  52.     assert(ElementsPositions[ID.ID] != -1);
  53.     assert(VerticesPositions[ID.ID] != -1);
  54.  
  55.     uint ElementsStart, ElementsEnd;
  56.     uint VerticesStart, VerticesEnd;
  57.  
  58.     //Determine end point of the objects element data
  59.     ElementsStart = ElementsPositions[ID.ID];
  60.     ElementsEnd = ElementsPositions[ID.ID] + ID.ElementsSize;
  61.  
  62.     //Update the positions of the elements
  63.     for(int i = ID.ID; i < ElementsPositions.size(); ++i){
  64.         ElementsPositions[i] -= ElementsPositions[ID.ID];
  65.     }
  66.  
  67.     //Change the ElementsPositions value to reflect the deletion
  68.     ElementsPositions[ID.ID] = -1;
  69.  
  70.     //Update the value of the elements
  71.     for(uint i = ElementsEnd; i < Elements.size(); ++i){
  72.         Elements[i] -= ID.ElementsSize;
  73.     }
  74.  
  75.     //Erase the elements of the object
  76.     //(ElementsEnd being the last element erased)
  77.     Elements.erase(Elements.begin()+ElementsStart, Elements.begin()+ElementsEnd);
  78.  
  79.  
  80.     //Determine end point of the objects vertices data
  81.     VerticesStart = VerticesPositions[ID.ID];
  82.     VerticesEnd = VerticesPositions[ID.ID] + ID.VerticesSize;
  83.  
  84.     //Change the VerticesPositions value to reflect the deletion
  85.     VerticesPositions[ID.ID] = -1;
  86.  
  87.     //Update the value of the vertices
  88.     for(uint i = VerticesEnd; i < Vertices.size(); ++i){
  89.         VerticesPositions[i] -= ID.VerticesSize;
  90.     }
  91.  
  92.     //Erase the vertices of the object
  93.     //(VerticesEnd being the last vertex erased)
  94.     Vertices.erase(Vertices.begin()+VerticesStart, Vertices.begin()+VerticesEnd);
  95.  
  96.  
  97.     //Check if the entire thing is empty
  98.     bool IsEmpty = true;
  99.     for(size_t i = 0; i < ElementsPositions.size(); ++i){
  100.         IsEmpty = IsEmpty && (ElementsPositions[i] == -1);
  101.     }
  102.     if(IsEmpty){
  103.         ElementsPositions.clear();
  104.         VerticesPositions.clear();
  105.         Elements.clear();
  106.         Vertices.clear();
  107.     }
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement