refactor: change gl functions name

This commit is contained in:
2021-09-19 13:15:34 +02:00
parent a8b6a646af
commit bcde4278ab
7 changed files with 54 additions and 54 deletions

View File

@@ -22,7 +22,7 @@ public:
static constexpr float m_AnimationSpeed = 2.0f; static constexpr float m_AnimationSpeed = 2.0f;
struct Model { struct Model {
GL::VAO* vao; GL::VertexArray* vao;
glm::vec2 positon; glm::vec2 positon;
}; };
private: private:
@@ -41,7 +41,7 @@ public:
void prepare(); void prepare();
void resize(const int width, const int height); void resize(const int width, const int height);
void renderVAO(const GL::VAO& vao); void renderVAO(const GL::VertexArray& vao);
void renderModel(const Model& model); void renderModel(const Model& model);
void setZoom(float zoom); void setZoom(float zoom);

View File

@@ -21,44 +21,44 @@ struct VertexAttribPointer{
int m_Offset; int m_Offset;
}; };
class VBO{ class VertexBuffer{
private: private:
unsigned int m_ID, m_DataStride; unsigned int m_ID, m_DataStride;
std::vector<VertexAttribPointer> m_VertexAttribs; std::vector<VertexAttribPointer> m_VertexAttribs;
public: public:
REMOVE_COPY(VBO); REMOVE_COPY(VertexBuffer);
VBO(VBO&& other){ VertexBuffer(VertexBuffer&& other){
m_VertexAttribs = std::move(other.m_VertexAttribs); m_VertexAttribs = std::move(other.m_VertexAttribs);
m_ID = other.m_ID; m_ID = other.m_ID;
m_DataStride = other.m_DataStride; m_DataStride = other.m_DataStride;
other.m_ID = 0; other.m_ID = 0;
other.m_DataStride = 0; other.m_DataStride = 0;
} }
VBO(const std::vector<float>& data, unsigned int stride); VertexBuffer(const std::vector<float>& data, unsigned int stride);
~VBO(); ~VertexBuffer();
void bind() const; void bind() const;
void unbind() const; void unbind() const;
void addVertexAttribPointer(unsigned int index, unsigned int coordinateSize, unsigned int offset); void addVertexAttribPointer(unsigned int index, unsigned int coordinateSize, unsigned int offset);
void bindVertexAttribs() const; void bindVertexAttribs() const;
}; };
class VAO{ class VertexArray{
private: private:
unsigned int m_ID, m_VertexCount; unsigned int m_ID, m_VertexCount;
std::vector<VBO> m_Vbos; //use to destroy vbos when become unused std::vector<VertexBuffer> m_VertexBuffers; //use to destroy vbos when become unused
public: public:
REMOVE_COPY(VAO); REMOVE_COPY(VertexArray);
VAO(VAO&& other){ VertexArray(VertexArray&& other){
m_ID = other.m_ID; m_ID = other.m_ID;
m_VertexCount = other.m_VertexCount; m_VertexCount = other.m_VertexCount;
m_Vbos = std::move(other.m_Vbos); m_VertexBuffers = std::move(other.m_VertexBuffers);
other.m_VertexCount = 0; other.m_VertexCount = 0;
other.m_ID = 0; other.m_ID = 0;
} }
VAO(unsigned int vertexCount); VertexArray(unsigned int vertexCount);
~VAO(); ~VertexArray();
unsigned int getVertexCount() const {return m_VertexCount;} unsigned int getVertexCount() const {return m_VertexCount;}
void bindVBO(VBO& vbo); void bindVertexBuffer(VertexBuffer& vbo);
void bind() const; void bind() const;
void unbind() const; void unbind() const;
}; };

View File

@@ -8,9 +8,9 @@ namespace render {
namespace WorldLoader { namespace WorldLoader {
GL::VAO loadMobModel(); GL::VertexArray loadMobModel();
GL::VAO loadWorldModel(const td::game::World* world); GL::VertexArray loadWorldModel(const td::game::World* world);
GL::VAO loadTileSelectModel(); GL::VertexArray loadTileSelectModel();
} // namespace WorldLoader } // namespace WorldLoader

View File

@@ -51,7 +51,7 @@ bool Renderer::init(){
return true; return true;
} }
void Renderer::renderVAO(const GL::VAO& vao){ void Renderer::renderVAO(const GL::VertexArray& vao){
m_WorldShader->start(); m_WorldShader->start();
vao.bind(); vao.bind();
glDrawArrays(GL_TRIANGLES, 0, vao.getVertexCount()); glDrawArrays(GL_TRIANGLES, 0, vao.getVertexCount());

View File

@@ -11,9 +11,9 @@ namespace render {
void WorldRenderer::loadModels(){ void WorldRenderer::loadModels(){
std::cout << "World Created !\n"; std::cout << "World Created !\n";
m_WorldVao = std::make_unique<GL::VAO>(std::move(WorldLoader::loadWorldModel(m_World))); m_WorldVao = std::make_unique<GL::VertexArray>(std::move(WorldLoader::loadWorldModel(m_World)));
m_MobVao = std::make_unique<GL::VAO>(std::move(WorldLoader::loadMobModel())); m_MobVao = std::make_unique<GL::VertexArray>(std::move(WorldLoader::loadMobModel()));
m_SelectTileVao = std::make_unique<GL::VAO>(std::move(WorldLoader::loadTileSelectModel())); m_SelectTileVao = std::make_unique<GL::VertexArray>(std::move(WorldLoader::loadTileSelectModel()));
std::cout << "Vertex Count : " << m_WorldVao->getVertexCount() << std::endl; std::cout << "Vertex Count : " << m_WorldVao->getVertexCount() << std::endl;
} }

View File

@@ -13,35 +13,35 @@ using namespace gl;
namespace GL{ namespace GL{
VAO::~VAO(){ VertexArray::~VertexArray(){
if(m_ID != 0) if(m_ID != 0)
glDeleteVertexArrays(1, &m_ID); glDeleteVertexArrays(1, &m_ID);
} }
VAO::VAO(unsigned int vertexCount) : m_VertexCount(vertexCount){ VertexArray::VertexArray(unsigned int vertexCount) : m_VertexCount(vertexCount){
glGenVertexArrays(1, &m_ID); glGenVertexArrays(1, &m_ID);
} }
void VAO::bind() const{ void VertexArray::bind() const{
glBindVertexArray(m_ID); glBindVertexArray(m_ID);
} }
void VAO::unbind() const{ void VertexArray::unbind() const{
glBindVertexArray(0); glBindVertexArray(0);
} }
void VAO::bindVBO(VBO& vbo){ void VertexArray::bindVertexBuffer(VertexBuffer& VertexBuffer){
vbo.bind(); VertexBuffer.bind();
vbo.bindVertexAttribs(); VertexBuffer.bindVertexAttribs();
m_Vbos.push_back(std::move(vbo)); m_VertexBuffers.push_back(std::move(VertexBuffer));
} }
VBO::~VBO(){ VertexBuffer::~VertexBuffer(){
if(m_ID != 0) if(m_ID != 0)
glDeleteBuffers(1, &m_ID); glDeleteBuffers(1, &m_ID);
} }
VBO::VBO(const std::vector<float>& data, unsigned int stride) : m_DataStride(stride){ VertexBuffer::VertexBuffer(const std::vector<float>& data, unsigned int stride) : m_DataStride(stride){
glGenBuffers(1, &m_ID); glGenBuffers(1, &m_ID);
bind(); bind();
glBufferData(GL_ARRAY_BUFFER, data.size() * sizeof(float), nullptr, GL_STATIC_DRAW); glBufferData(GL_ARRAY_BUFFER, data.size() * sizeof(float), nullptr, GL_STATIC_DRAW);
@@ -49,15 +49,15 @@ namespace GL{
unbind(); unbind();
} }
void VBO::bind() const{ void VertexBuffer::bind() const{
glBindBuffer(GL_ARRAY_BUFFER, m_ID); glBindBuffer(GL_ARRAY_BUFFER, m_ID);
} }
void VBO::unbind() const{ void VertexBuffer::unbind() const{
glBindBuffer(GL_ARRAY_BUFFER, 0); glBindBuffer(GL_ARRAY_BUFFER, 0);
} }
void VBO::addVertexAttribPointer(unsigned int index, unsigned int coordinateSize, unsigned int offset){ void VertexBuffer::addVertexAttribPointer(unsigned int index, unsigned int coordinateSize, unsigned int offset){
VertexAttribPointer pointer; VertexAttribPointer pointer;
pointer.m_Index = index; pointer.m_Index = index;
pointer.m_Size = coordinateSize; pointer.m_Size = coordinateSize;
@@ -65,7 +65,7 @@ namespace GL{
m_VertexAttribs.push_back(pointer); m_VertexAttribs.push_back(pointer);
} }
void VBO::bindVertexAttribs() const{ void VertexBuffer::bindVertexAttribs() const{
for(const VertexAttribPointer& pointer : m_VertexAttribs){ for(const VertexAttribPointer& pointer : m_VertexAttribs){
glEnableVertexAttribArray(pointer.m_Index); glEnableVertexAttribArray(pointer.m_Index);
glVertexAttribPointer(pointer.m_Index, pointer.m_Size, GL_FLOAT, false, m_DataStride * sizeof(float), (void*)(intptr_t) pointer.m_Offset); glVertexAttribPointer(pointer.m_Index, pointer.m_Size, GL_FLOAT, false, m_DataStride * sizeof(float), (void*)(intptr_t) pointer.m_Offset);

View File

@@ -10,7 +10,7 @@ namespace render {
namespace WorldLoader { namespace WorldLoader {
GL::VAO loadMobModel(){ GL::VertexArray loadMobModel(){
std::vector<float> positions = { std::vector<float> positions = {
-0.5, -0.5, -0.5, -0.5,
0.5, -0.5, 0.5, -0.5,
@@ -35,20 +35,20 @@ GL::VAO loadMobModel(){
yellowFloat yellowFloat
}; };
GL::VBO positionVBO(positions, 2); GL::VertexBuffer positionVBO(positions, 2);
positionVBO.addVertexAttribPointer(0, 2, 0); positionVBO.addVertexAttribPointer(0, 2, 0);
GL::VBO colorVBO(colors, 1); GL::VertexBuffer colorVBO(colors, 1);
colorVBO.addVertexAttribPointer(1, 1, 0); colorVBO.addVertexAttribPointer(1, 1, 0);
GL::VAO mobVao(colors.size()); // each pos = 1 color GL::VertexArray mobVao(colors.size()); // each pos = 1 color
mobVao.bind(); mobVao.bind();
mobVao.bindVBO(positionVBO); mobVao.bindVertexBuffer(positionVBO);
mobVao.bindVBO(colorVBO); mobVao.bindVertexBuffer(colorVBO);
mobVao.unbind(); mobVao.unbind();
return mobVao; return mobVao;
} }
GL::VAO loadWorldModel(const td::game::World* world){ GL::VertexArray loadWorldModel(const td::game::World* world){
std::vector<float> positions; std::vector<float> positions;
std::vector<float> colors; std::vector<float> colors;
@@ -143,20 +143,20 @@ GL::VAO loadWorldModel(const td::game::World* world){
} }
} }
GL::VBO positionVBO(positions, 2); GL::VertexBuffer positionVBO(positions, 2);
positionVBO.addVertexAttribPointer(0, 2, 0); positionVBO.addVertexAttribPointer(0, 2, 0);
GL::VBO colorVBO(colors, 1); GL::VertexBuffer colorVBO(colors, 1);
colorVBO.addVertexAttribPointer(1, 1, 0); colorVBO.addVertexAttribPointer(1, 1, 0);
GL::VAO worldVao(positions.size() / 2); // each pos = 2 vertecies GL::VertexArray worldVao(positions.size() / 2); // each pos = 2 vertecies
worldVao.bind(); worldVao.bind();
worldVao.bindVBO(positionVBO); worldVao.bindVertexBuffer(positionVBO);
worldVao.bindVBO(colorVBO); worldVao.bindVertexBuffer(colorVBO);
worldVao.unbind(); worldVao.unbind();
return worldVao; return worldVao;
} }
GL::VAO loadTileSelectModel(){ GL::VertexArray loadTileSelectModel(){
std::vector<float> positions = { std::vector<float> positions = {
0, 0, 0, 0,
1, 0, 1, 0,
@@ -174,15 +174,15 @@ GL::VAO loadTileSelectModel(){
std::vector<float> colors(6, colorFloat); std::vector<float> colors(6, colorFloat);
GL::VBO positionVBO(positions, 2); GL::VertexBuffer positionVBO(positions, 2);
positionVBO.addVertexAttribPointer(0, 2, 0); positionVBO.addVertexAttribPointer(0, 2, 0);
GL::VBO colorVBO(colors, 1); GL::VertexBuffer colorVBO(colors, 1);
colorVBO.addVertexAttribPointer(1, 1, 0); colorVBO.addVertexAttribPointer(1, 1, 0);
GL::VAO tileSelectVao(positions.size() / 2); // each pos = 2 vertecies GL::VertexArray tileSelectVao(positions.size() / 2); // each pos = 2 vertecies
tileSelectVao.bind(); tileSelectVao.bind();
tileSelectVao.bindVBO(positionVBO); tileSelectVao.bindVertexBuffer(positionVBO);
tileSelectVao.bindVBO(colorVBO); tileSelectVao.bindVertexBuffer(colorVBO);
tileSelectVao.unbind(); tileSelectVao.unbind();
return tileSelectVao; return tileSelectVao;