BatchRenderer2D: OpenGL Core profile requires the use of VAO, so implement that.
This commit is contained in:
parent
4bc2b3fa56
commit
93f869acc6
2 changed files with 21 additions and 25 deletions
|
|
@ -58,6 +58,7 @@ protected :
|
||||||
|
|
||||||
std::vector<const Renderable2D*> m_queue;
|
std::vector<const Renderable2D*> m_queue;
|
||||||
|
|
||||||
|
unsigned int m_VAO;
|
||||||
unsigned int m_IBO;
|
unsigned int m_IBO;
|
||||||
unsigned int m_VBO;
|
unsigned int m_VBO;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -30,8 +30,23 @@ BatchRenderer2D::BatchRenderer2D()
|
||||||
glGenBuffers(1, &m_IBO);
|
glGenBuffers(1, &m_IBO);
|
||||||
glGenBuffers(1, &m_VBO);
|
glGenBuffers(1, &m_VBO);
|
||||||
|
|
||||||
|
// Generate and bind Vertex Array Object
|
||||||
|
glGenVertexArrays(1, &m_VAO);
|
||||||
|
glBindVertexArray(m_VAO);
|
||||||
|
|
||||||
setBatchSize(10000);
|
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();
|
m_textShader.create();
|
||||||
if (!m_textShader.loadFromFile("assets/shaders/text.shader.glsl")) {
|
if (!m_textShader.loadFromFile("assets/shaders/text.shader.glsl")) {
|
||||||
std::cout << "Failed to load shader: " << m_textShader.getLastError() << std::endl;
|
std::cout << "Failed to load shader: " << m_textShader.getLastError() << std::endl;
|
||||||
|
|
@ -55,6 +70,7 @@ BatchRenderer2D::~BatchRenderer2D()
|
||||||
{
|
{
|
||||||
glDeleteBuffers(1, &m_VBO);
|
glDeleteBuffers(1, &m_VBO);
|
||||||
glDeleteBuffers(1, &m_IBO);
|
glDeleteBuffers(1, &m_IBO);
|
||||||
|
glDeleteVertexArrays(1, &m_VAO);
|
||||||
}
|
}
|
||||||
|
|
||||||
void BatchRenderer2D::setBatchSize(unsigned short size)
|
void BatchRenderer2D::setBatchSize(unsigned short size)
|
||||||
|
|
@ -82,12 +98,12 @@ void BatchRenderer2D::setBatchSize(unsigned short size)
|
||||||
// Upload to GPU
|
// Upload to GPU
|
||||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_IBO);
|
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_IBO);
|
||||||
glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size() * sizeof(unsigned short), &indices[0], GL_STATIC_DRAW);
|
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.
|
// Allocate memory for vertex buffer.
|
||||||
glBindBuffer(GL_ARRAY_BUFFER, m_VBO);
|
glBindBuffer(GL_ARRAY_BUFFER, m_VBO);
|
||||||
glBufferData(GL_ARRAY_BUFFER, m_size * sizeof(Vertex2D), NULL, GL_DYNAMIC_DRAW);
|
glBufferData(GL_ARRAY_BUFFER, m_size * sizeof(Vertex2D), NULL, GL_DYNAMIC_DRAW);
|
||||||
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
//glBindBuffer(GL_ARRAY_BUFFER, 0);
|
||||||
|
|
||||||
// Log
|
// Log
|
||||||
log("BatchRenderer - BatchSize\n");
|
log("BatchRenderer - BatchSize\n");
|
||||||
|
|
@ -124,21 +140,8 @@ void BatchRenderer2D::render()
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Bind buffers.
|
// Bind VAO
|
||||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_IBO);
|
glBindVertexArray(m_VAO);
|
||||||
glBindBuffer(GL_ARRAY_BUFFER, m_VBO);
|
|
||||||
|
|
||||||
// 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.
|
// Set shader uniforms.
|
||||||
m_textShader.enable();
|
m_textShader.enable();
|
||||||
|
|
@ -151,14 +154,6 @@ void BatchRenderer2D::render()
|
||||||
|
|
||||||
prepareQueue();
|
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_queue.clear();
|
||||||
m_state.cleanup();
|
m_state.cleanup();
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue