Recently I'm trying to implement a render queue sorting system, i.e., ordering my renderable objects in an array in such a way that the overhead of OpenGL state changes are minumum. After some googling, the most ideal one (to me) is to generate an integer key value for each renderable object, and the sorting is based on that value. Once done sorting, objects that have similar rendering states (same shader, texture, distance, etc.) will be close to each other, so we can just loop through the object array and submit the draw calls for rendering.
Here's a detailed explanation: Order your graphics draw calls around!
My problem is, how do I loop through the renderable object's array to submit their draw calls for actual rendering? Do I need to keep track of the current OpenGL states manually?
What have come to my mind is:
(for each renderable object)
{
if(objects[i].getShader() != m_currentShader)
{
m_currentShader = objects[i].getShader();
m_currentShader.bind();
}
if(objects[i].getTexture() != m_currentTexture)
{
m_currentTexture = objects[i].getTexture();
m_currentTexture.bind();
}
// ... and so on
object[i].getMesh().draw();
}
Seems not so performance-friendly to me, but I can't find any further explanation on how to submit the actual draw calls form the object's array either on the article I've provided or other sites I can find. Perhaps there's something I have missed?