Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //=======================================CVBO.H==================================
- #ifndef __C_VBO_H__
- #define __C_VBO_H__
- #include <vector>
- //A wrapper class for an OpenGL VBO
- class CVBObject
- {
- private:
- unsigned int vaoID; //The VAO object handle
- unsigned int vboID; //The VBO object handle
- std::vector<signed char> data; //The vector to hold all of the data before it is uploaded
- unsigned int dataSize; //The size of the last batch of data to be uploaded
- bool dataUploaded; //If the data from this vbo is in video memory
- bool dirty; //If the data stored in system memory does not match the data stored in video memory
- bool created; //If the vbo has been initialized
- int bufferType; //The buffer type for this vbo, usually GL_ARRAY_BUFFER
- public:
- CVBObject(int vao = -1); //Defaults to creating a new VAO, -1 = create new VAO
- ~CVBObject();
- void CreateVBO(int type = 0x8892); //Creates the vbo
- void ReleaseVBO(); //Releases the vbo
- void BindVAO(); //Binds the VAO object
- void UnbindVAO(); //Binds the 0 VAO object
- void BindVBO(); //Binds the VBO object, Defaults to GL_ARRAY_BUFFER
- void UnbindVBO(); //Binds the 0 VBO object for the buffer type
- unsigned int GetDataSize(); //Returns the size of the data most recently sent to the GPU
- bool IsDirty(); //Gets if this vbo is dirty
- 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...
- void* MapSubBufferToMemory(int usage, unsigned int offset, unsigned int length); //Maps a part of the buffer to memory. usage same as MapBufferToMemory()
- void UnmapBuffer(); //Unmaps the buffer from memory
- void AddData(void* data, unsigned int size); //Data to add to this vbo, and the length of data
- void UploadData(int usage); //Uploads the data to the GPU
- void* GetDataPointer(); //Only before uploaded, gets the data in system memory
- unsigned int getVAO(); //Returns the VAO handle
- int getVBO(); //Returns the VBO handle
- };
- #endif
- //======================================VCBO.CPP==========================================
- #include "cvbo.h"
- #include <GL\glew.h>
- CVBObject::CVBObject(int vao)
- {
- if (vao < 0)
- glGenVertexArrays(1, &vaoID);
- else
- vaoID = vao;
- bufferType = 0x8892;
- dataUploaded = false;
- dirty = true;
- created = false;
- data = std::vector<signed char>();
- dataSize = 0;
- }
- CVBObject::~CVBObject()
- {
- if (created)
- ReleaseVBO();
- data.clear();
- }
- void CVBObject::CreateVBO(int type)
- {
- glBindVertexArray(vaoID);
- glGenBuffers(1, &vboID);
- glBindBuffer(type, vboID);
- glBindVertexArray(0);
- bufferType = type;
- created = true;
- }
- void CVBObject::ReleaseVBO()
- {
- if (!created) return;
- glDeleteBuffers(1, &vboID);
- dataUploaded = false;
- dirty = false;
- created = false;
- }
- void CVBObject::BindVAO()
- {
- if (!created) return;
- glBindVertexArray(vaoID);
- }
- void CVBObject::UnbindVAO()
- {
- glBindVertexArray(0);
- }
- void CVBObject::BindVBO()
- {
- if (!created) return;
- glBindBuffer(bufferType, vboID);
- }
- void CVBObject::UnbindVBO()
- {
- glBindBuffer(bufferType, 0);
- }
- unsigned int CVBObject::GetDataSize()
- {
- return dataSize;
- }
- bool CVBObject::IsDirty()
- {
- return dirty;
- }
- void* CVBObject::MapBufferToMemory(int usage)
- {
- if (!dataUploaded || !created) return nullptr;
- void* res = glMapBuffer(bufferType, usage);
- return res;
- }
- void* CVBObject::MapSubBufferToMemory(int usage, unsigned int offset, unsigned int length)
- {
- if (!dataUploaded || !created) return nullptr;
- void* res = glMapBufferRange(bufferType, offset, length, usage);
- return res;
- }
- void CVBObject::UnmapBuffer()
- {
- if (!created) return;
- glUnmapBuffer(bufferType);
- }
- void CVBObject::AddData(void* data, unsigned int size)
- {
- if (!created) return;
- this->data.insert(this->data.end(), (signed char*)data, (signed char*)data + size);
- dirty = true;
- }
- void CVBObject::UploadData(int usage)
- {
- if (!created || !dirty) return;
- if (data.size() <= 0) return;
- glBindBuffer(bufferType, vboID);
- glBufferData(bufferType, data.size(), &data[0], usage);
- glBindBuffer(bufferType, 0);
- dataSize = data.size();
- dataUploaded = true;
- dirty = false;
- data.clear();
- }
- void* CVBObject::GetDataPointer()
- {
- if (!created || !dirty) return nullptr;
- return (void*)data[0];
- }
- unsigned int CVBObject::getVAO()
- {
- return vaoID;
- }
- int CVBObject::getVBO()
- {
- if (!created) return -1;
- return vboID;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement