refactor: change gl functions name
This commit is contained in:
@@ -22,7 +22,7 @@ public:
|
||||
static constexpr float m_AnimationSpeed = 2.0f;
|
||||
|
||||
struct Model {
|
||||
GL::VAO* vao;
|
||||
GL::VertexArray* vao;
|
||||
glm::vec2 positon;
|
||||
};
|
||||
private:
|
||||
@@ -41,7 +41,7 @@ public:
|
||||
void prepare();
|
||||
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 setZoom(float zoom);
|
||||
|
||||
@@ -21,44 +21,44 @@ struct VertexAttribPointer{
|
||||
int m_Offset;
|
||||
};
|
||||
|
||||
class VBO{
|
||||
class VertexBuffer{
|
||||
private:
|
||||
unsigned int m_ID, m_DataStride;
|
||||
std::vector<VertexAttribPointer> m_VertexAttribs;
|
||||
public:
|
||||
REMOVE_COPY(VBO);
|
||||
VBO(VBO&& other){
|
||||
REMOVE_COPY(VertexBuffer);
|
||||
VertexBuffer(VertexBuffer&& other){
|
||||
m_VertexAttribs = std::move(other.m_VertexAttribs);
|
||||
m_ID = other.m_ID;
|
||||
m_DataStride = other.m_DataStride;
|
||||
other.m_ID = 0;
|
||||
other.m_DataStride = 0;
|
||||
}
|
||||
VBO(const std::vector<float>& data, unsigned int stride);
|
||||
~VBO();
|
||||
VertexBuffer(const std::vector<float>& data, unsigned int stride);
|
||||
~VertexBuffer();
|
||||
void bind() const;
|
||||
void unbind() const;
|
||||
void addVertexAttribPointer(unsigned int index, unsigned int coordinateSize, unsigned int offset);
|
||||
void bindVertexAttribs() const;
|
||||
};
|
||||
|
||||
class VAO{
|
||||
class VertexArray{
|
||||
private:
|
||||
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:
|
||||
REMOVE_COPY(VAO);
|
||||
VAO(VAO&& other){
|
||||
REMOVE_COPY(VertexArray);
|
||||
VertexArray(VertexArray&& other){
|
||||
m_ID = other.m_ID;
|
||||
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_ID = 0;
|
||||
}
|
||||
VAO(unsigned int vertexCount);
|
||||
~VAO();
|
||||
VertexArray(unsigned int vertexCount);
|
||||
~VertexArray();
|
||||
unsigned int getVertexCount() const {return m_VertexCount;}
|
||||
void bindVBO(VBO& vbo);
|
||||
void bindVertexBuffer(VertexBuffer& vbo);
|
||||
void bind() const;
|
||||
void unbind() const;
|
||||
};
|
||||
|
||||
@@ -8,9 +8,9 @@ namespace render {
|
||||
|
||||
namespace WorldLoader {
|
||||
|
||||
GL::VAO loadMobModel();
|
||||
GL::VAO loadWorldModel(const td::game::World* world);
|
||||
GL::VAO loadTileSelectModel();
|
||||
GL::VertexArray loadMobModel();
|
||||
GL::VertexArray loadWorldModel(const td::game::World* world);
|
||||
GL::VertexArray loadTileSelectModel();
|
||||
|
||||
} // namespace WorldLoader
|
||||
|
||||
|
||||
@@ -51,7 +51,7 @@ bool Renderer::init(){
|
||||
return true;
|
||||
}
|
||||
|
||||
void Renderer::renderVAO(const GL::VAO& vao){
|
||||
void Renderer::renderVAO(const GL::VertexArray& vao){
|
||||
m_WorldShader->start();
|
||||
vao.bind();
|
||||
glDrawArrays(GL_TRIANGLES, 0, vao.getVertexCount());
|
||||
|
||||
@@ -11,9 +11,9 @@ namespace render {
|
||||
|
||||
void WorldRenderer::loadModels(){
|
||||
std::cout << "World Created !\n";
|
||||
m_WorldVao = std::make_unique<GL::VAO>(std::move(WorldLoader::loadWorldModel(m_World)));
|
||||
m_MobVao = std::make_unique<GL::VAO>(std::move(WorldLoader::loadMobModel()));
|
||||
m_SelectTileVao = std::make_unique<GL::VAO>(std::move(WorldLoader::loadTileSelectModel()));
|
||||
m_WorldVao = std::make_unique<GL::VertexArray>(std::move(WorldLoader::loadWorldModel(m_World)));
|
||||
m_MobVao = std::make_unique<GL::VertexArray>(std::move(WorldLoader::loadMobModel()));
|
||||
m_SelectTileVao = std::make_unique<GL::VertexArray>(std::move(WorldLoader::loadTileSelectModel()));
|
||||
std::cout << "Vertex Count : " << m_WorldVao->getVertexCount() << std::endl;
|
||||
}
|
||||
|
||||
|
||||
@@ -13,35 +13,35 @@ using namespace gl;
|
||||
|
||||
namespace GL{
|
||||
|
||||
VAO::~VAO(){
|
||||
VertexArray::~VertexArray(){
|
||||
if(m_ID != 0)
|
||||
glDeleteVertexArrays(1, &m_ID);
|
||||
}
|
||||
|
||||
VAO::VAO(unsigned int vertexCount) : m_VertexCount(vertexCount){
|
||||
VertexArray::VertexArray(unsigned int vertexCount) : m_VertexCount(vertexCount){
|
||||
glGenVertexArrays(1, &m_ID);
|
||||
}
|
||||
|
||||
void VAO::bind() const{
|
||||
void VertexArray::bind() const{
|
||||
glBindVertexArray(m_ID);
|
||||
}
|
||||
|
||||
void VAO::unbind() const{
|
||||
void VertexArray::unbind() const{
|
||||
glBindVertexArray(0);
|
||||
}
|
||||
|
||||
void VAO::bindVBO(VBO& vbo){
|
||||
vbo.bind();
|
||||
vbo.bindVertexAttribs();
|
||||
m_Vbos.push_back(std::move(vbo));
|
||||
void VertexArray::bindVertexBuffer(VertexBuffer& VertexBuffer){
|
||||
VertexBuffer.bind();
|
||||
VertexBuffer.bindVertexAttribs();
|
||||
m_VertexBuffers.push_back(std::move(VertexBuffer));
|
||||
}
|
||||
|
||||
VBO::~VBO(){
|
||||
VertexBuffer::~VertexBuffer(){
|
||||
if(m_ID != 0)
|
||||
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);
|
||||
bind();
|
||||
glBufferData(GL_ARRAY_BUFFER, data.size() * sizeof(float), nullptr, GL_STATIC_DRAW);
|
||||
@@ -49,15 +49,15 @@ namespace GL{
|
||||
unbind();
|
||||
}
|
||||
|
||||
void VBO::bind() const{
|
||||
void VertexBuffer::bind() const{
|
||||
glBindBuffer(GL_ARRAY_BUFFER, m_ID);
|
||||
}
|
||||
|
||||
void VBO::unbind() const{
|
||||
void VertexBuffer::unbind() const{
|
||||
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;
|
||||
pointer.m_Index = index;
|
||||
pointer.m_Size = coordinateSize;
|
||||
@@ -65,7 +65,7 @@ namespace GL{
|
||||
m_VertexAttribs.push_back(pointer);
|
||||
}
|
||||
|
||||
void VBO::bindVertexAttribs() const{
|
||||
void VertexBuffer::bindVertexAttribs() const{
|
||||
for(const VertexAttribPointer& pointer : m_VertexAttribs){
|
||||
glEnableVertexAttribArray(pointer.m_Index);
|
||||
glVertexAttribPointer(pointer.m_Index, pointer.m_Size, GL_FLOAT, false, m_DataStride * sizeof(float), (void*)(intptr_t) pointer.m_Offset);
|
||||
|
||||
@@ -10,7 +10,7 @@ namespace render {
|
||||
|
||||
namespace WorldLoader {
|
||||
|
||||
GL::VAO loadMobModel(){
|
||||
GL::VertexArray loadMobModel(){
|
||||
std::vector<float> positions = {
|
||||
-0.5, -0.5,
|
||||
0.5, -0.5,
|
||||
@@ -35,20 +35,20 @@ GL::VAO loadMobModel(){
|
||||
yellowFloat
|
||||
};
|
||||
|
||||
GL::VBO positionVBO(positions, 2);
|
||||
GL::VertexBuffer positionVBO(positions, 2);
|
||||
positionVBO.addVertexAttribPointer(0, 2, 0);
|
||||
GL::VBO colorVBO(colors, 1);
|
||||
GL::VertexBuffer colorVBO(colors, 1);
|
||||
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.bindVBO(positionVBO);
|
||||
mobVao.bindVBO(colorVBO);
|
||||
mobVao.bindVertexBuffer(positionVBO);
|
||||
mobVao.bindVertexBuffer(colorVBO);
|
||||
mobVao.unbind();
|
||||
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> 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);
|
||||
GL::VBO colorVBO(colors, 1);
|
||||
GL::VertexBuffer colorVBO(colors, 1);
|
||||
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.bindVBO(positionVBO);
|
||||
worldVao.bindVBO(colorVBO);
|
||||
worldVao.bindVertexBuffer(positionVBO);
|
||||
worldVao.bindVertexBuffer(colorVBO);
|
||||
worldVao.unbind();
|
||||
return worldVao;
|
||||
}
|
||||
|
||||
GL::VAO loadTileSelectModel(){
|
||||
GL::VertexArray loadTileSelectModel(){
|
||||
std::vector<float> positions = {
|
||||
0, 0,
|
||||
1, 0,
|
||||
@@ -174,15 +174,15 @@ GL::VAO loadTileSelectModel(){
|
||||
|
||||
std::vector<float> colors(6, colorFloat);
|
||||
|
||||
GL::VBO positionVBO(positions, 2);
|
||||
GL::VertexBuffer positionVBO(positions, 2);
|
||||
positionVBO.addVertexAttribPointer(0, 2, 0);
|
||||
GL::VBO colorVBO(colors, 1);
|
||||
GL::VertexBuffer colorVBO(colors, 1);
|
||||
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.bindVBO(positionVBO);
|
||||
tileSelectVao.bindVBO(colorVBO);
|
||||
tileSelectVao.bindVertexBuffer(positionVBO);
|
||||
tileSelectVao.bindVertexBuffer(colorVBO);
|
||||
tileSelectVao.unbind();
|
||||
|
||||
return tileSelectVao;
|
||||
|
||||
Reference in New Issue
Block a user