Advertisement
Guest User

cvbo.h/.cpp

a guest
Oct 30th, 2013
208
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.45 KB | None | 0 0
  1. //=======================================CVBO.H==================================
  2. #ifndef __C_VBO_H__
  3. #define __C_VBO_H__
  4.  
  5. #include <vector>
  6.  
  7. //A wrapper class for an OpenGL VBO
  8. class CVBObject
  9. {
  10. private:
  11.     unsigned int vaoID; //The VAO object handle
  12.     unsigned int vboID; //The VBO object handle
  13.  
  14.     std::vector<signed char> data; //The vector to hold all of the data before it is uploaded
  15.     unsigned int dataSize; //The size of the last batch of data to be uploaded
  16.  
  17.     bool dataUploaded; //If the data from this vbo is in video memory
  18.     bool dirty; //If the data stored in system memory does not match the data stored in video memory
  19.     bool created; //If the vbo has been initialized
  20.  
  21.     int bufferType; //The buffer type for this vbo, usually GL_ARRAY_BUFFER
  22.  
  23. public:
  24.     CVBObject(int vao = -1); //Defaults to creating a new VAO, -1 = create new VAO
  25.     ~CVBObject();
  26.  
  27.     void CreateVBO(int type = 0x8892); //Creates the vbo
  28.     void ReleaseVBO(); //Releases the vbo
  29.     void BindVAO(); //Binds the VAO object
  30.     void UnbindVAO(); //Binds the 0 VAO object
  31.     void BindVBO(); //Binds the VBO object, Defaults to GL_ARRAY_BUFFER
  32.     void UnbindVBO(); //Binds the 0 VBO object for the buffer type
  33.  
  34.     unsigned int GetDataSize(); //Returns the size of the data most recently sent to the GPU
  35.  
  36.     bool IsDirty(); //Gets if this vbo is dirty
  37.  
  38.     void* MapBufferToMemory(int usage); //Maps the buffer to memory and return the pointer to the start of that memory. usage = GL_READ_ONLY, GL_WRITE_ONLY,ect...
  39.     void* MapSubBufferToMemory(int usage, unsigned int offset, unsigned int length); //Maps a part of the buffer to memory. usage same as MapBufferToMemory()
  40.     void UnmapBuffer(); //Unmaps the buffer from memory
  41.  
  42.     void AddData(void* data, unsigned int size); //Data to add to this vbo, and the length of data
  43.     void UploadData(int usage); //Uploads the data to the GPU
  44.  
  45.     void* GetDataPointer(); //Only before uploaded, gets the data in system memory
  46.  
  47.     unsigned int getVAO(); //Returns the VAO handle
  48.     int getVBO(); //Returns the VBO handle
  49. };
  50.  
  51. #endif
  52.  
  53. //======================================VCBO.CPP==========================================
  54. #include "cvbo.h"
  55. #include <GL\glew.h>
  56.  
  57. CVBObject::CVBObject(int vao)
  58. {
  59.     if (vao < 0)
  60.         glGenVertexArrays(1, &vaoID);
  61.     else
  62.         vaoID = vao;
  63.  
  64.     bufferType = 0x8892;
  65.     dataUploaded = false;
  66.     dirty = true;
  67.     created = false;
  68.  
  69.     data = std::vector<signed char>();
  70.     dataSize = 0;
  71. }
  72.  
  73. CVBObject::~CVBObject()
  74. {
  75.     if (created)
  76.         ReleaseVBO();
  77.  
  78.     data.clear();
  79. }
  80.  
  81. void CVBObject::CreateVBO(int type)
  82. {
  83.     glBindVertexArray(vaoID);
  84.     glGenBuffers(1, &vboID);
  85.     glBindBuffer(type, vboID);
  86.     glBindVertexArray(0);
  87.  
  88.     bufferType = type;
  89.     created = true;
  90. }
  91.  
  92. void CVBObject::ReleaseVBO()
  93. {
  94.     if (!created) return;
  95.     glDeleteBuffers(1, &vboID);
  96.     dataUploaded = false;
  97.     dirty = false;
  98.     created = false;
  99. }
  100.  
  101. void CVBObject::BindVAO()
  102. {
  103.     if (!created) return;
  104.     glBindVertexArray(vaoID);
  105. }
  106.  
  107. void CVBObject::UnbindVAO()
  108. {
  109.     glBindVertexArray(0);
  110. }
  111.  
  112. void CVBObject::BindVBO()
  113. {
  114.     if (!created) return;
  115.     glBindBuffer(bufferType, vboID);
  116. }
  117.  
  118. void CVBObject::UnbindVBO()
  119. {
  120.     glBindBuffer(bufferType, 0);
  121. }
  122.  
  123. unsigned int CVBObject::GetDataSize()
  124. {
  125.     return dataSize;
  126. }
  127.  
  128. bool CVBObject::IsDirty()
  129. {
  130.     return dirty;
  131. }
  132.  
  133. void* CVBObject::MapBufferToMemory(int usage)
  134. {
  135.     if (!dataUploaded || !created) return nullptr;
  136.  
  137.     void* res = glMapBuffer(bufferType, usage);
  138.     return res;
  139. }
  140.  
  141. void* CVBObject::MapSubBufferToMemory(int usage, unsigned int offset, unsigned int length)
  142. {
  143.     if (!dataUploaded || !created) return nullptr;
  144.  
  145.     void* res = glMapBufferRange(bufferType, offset, length, usage);
  146.     return res;
  147. }
  148.  
  149. void CVBObject::UnmapBuffer()
  150. {
  151.     if (!created) return;
  152.  
  153.     glUnmapBuffer(bufferType);
  154. }
  155.  
  156. void CVBObject::AddData(void* data, unsigned int size)
  157. {
  158.     if (!created) return;
  159.  
  160.     this->data.insert(this->data.end(), (signed char*)data, (signed char*)data + size);
  161.     dirty = true;
  162. }
  163.  
  164. void CVBObject::UploadData(int usage)
  165. {
  166.     if (!created || !dirty) return;
  167.     if (data.size() <= 0) return;
  168.  
  169.     glBindBuffer(bufferType, vboID);
  170.     glBufferData(bufferType, data.size(), &data[0], usage);
  171.     glBindBuffer(bufferType, 0);
  172.  
  173.     dataSize = data.size();
  174.  
  175.     dataUploaded = true;
  176.     dirty = false;
  177.     data.clear();
  178. }
  179.  
  180. void* CVBObject::GetDataPointer()
  181. {
  182.     if (!created || !dirty) return nullptr;
  183.    
  184.     return (void*)data[0];
  185. }
  186.  
  187. unsigned int CVBObject::getVAO()
  188. {
  189.     return vaoID;
  190. }
  191.  
  192. int CVBObject::getVBO()
  193. {
  194.     if (!created) return -1;
  195.  
  196.     return vboID;
  197. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement