diff --git a/include/Spectre/Graphics/BatchRenderer2D.h b/include/Spectre/Graphics/BatchRenderer2D.h index ad49098..7fe7d16 100644 --- a/include/Spectre/Graphics/BatchRenderer2D.h +++ b/include/Spectre/Graphics/BatchRenderer2D.h @@ -58,6 +58,7 @@ protected : std::vector m_queue; + unsigned int m_VAO; unsigned int m_IBO; unsigned int m_VBO; diff --git a/source/Graphics/BatchRenderer2D.cpp b/source/Graphics/BatchRenderer2D.cpp index 6b7adb5..73d3119 100644 --- a/source/Graphics/BatchRenderer2D.cpp +++ b/source/Graphics/BatchRenderer2D.cpp @@ -30,8 +30,23 @@ BatchRenderer2D::BatchRenderer2D() glGenBuffers(1, &m_IBO); glGenBuffers(1, &m_VBO); + // Generate and bind Vertex Array Object + glGenVertexArrays(1, &m_VAO); + glBindVertexArray(m_VAO); + setBatchSize(10000); + // Setup position. + glEnableVertexAttribArray(VertexAttribPosition); + glVertexAttribPointer(VertexAttribPosition, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex2D), (void*)0); + + // Setup color + glEnableVertexAttribArray(VertexAttribColor0); + glVertexAttribPointer(VertexAttribColor0, 4, GL_FLOAT, GL_FALSE, sizeof(Vertex2D), (void*)8); + + glEnableVertexAttribArray(VertexAttribTexCoord0); + glVertexAttribPointer(VertexAttribTexCoord0, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex2D), (void*)24); + m_textShader.create(); if (!m_textShader.loadFromFile("assets/shaders/text.shader.glsl")) { std::cout << "Failed to load shader: " << m_textShader.getLastError() << std::endl; @@ -55,6 +70,7 @@ BatchRenderer2D::~BatchRenderer2D() { glDeleteBuffers(1, &m_VBO); glDeleteBuffers(1, &m_IBO); + glDeleteVertexArrays(1, &m_VAO); } void BatchRenderer2D::setBatchSize(unsigned short size) @@ -82,12 +98,12 @@ void BatchRenderer2D::setBatchSize(unsigned short size) // Upload to GPU glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_IBO); glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size() * sizeof(unsigned short), &indices[0], GL_STATIC_DRAW); - glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); + //glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); // Allocate memory for vertex buffer. glBindBuffer(GL_ARRAY_BUFFER, m_VBO); glBufferData(GL_ARRAY_BUFFER, m_size * sizeof(Vertex2D), NULL, GL_DYNAMIC_DRAW); - glBindBuffer(GL_ARRAY_BUFFER, 0); + //glBindBuffer(GL_ARRAY_BUFFER, 0); // Log log("BatchRenderer - BatchSize\n"); @@ -124,22 +140,9 @@ void BatchRenderer2D::render() return; } - // Bind buffers. - glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_IBO); - glBindBuffer(GL_ARRAY_BUFFER, m_VBO); + // Bind VAO + glBindVertexArray(m_VAO); - // Setup position. - glEnableVertexAttribArray(VertexAttribPosition); - glVertexAttribPointer(VertexAttribPosition, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex2D), (void*) 0); - - - // Setup color - glEnableVertexAttribArray(VertexAttribColor0); - glVertexAttribPointer(VertexAttribColor0, 4, GL_FLOAT, GL_FALSE, sizeof(Vertex2D), (void*) 8); - - glEnableVertexAttribArray(VertexAttribTexCoord0); - glVertexAttribPointer(VertexAttribTexCoord0, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex2D), (void*) 24); - // Set shader uniforms. m_textShader.enable(); m_textShader.setUniform("u_MVP", MVP); @@ -151,14 +154,6 @@ void BatchRenderer2D::render() prepareQueue(); - glDisableVertexAttribArray(VertexAttribPosition); - glDisableVertexAttribArray(VertexAttribColor0); - glDisableVertexAttribArray(VertexAttribTexCoord0); - - // Unbind all buffers. - glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); - glBindBuffer(GL_ARRAY_BUFFER, 0); - m_queue.clear(); m_state.cleanup(); }