I need to draw couple of millions of rectangles, and I would like to get some advice on optimization, because currently it gets very slow (can notice lag when starting, zooming or dragging). The code is as follows:
const GLfloat vertexes[] =
{
x, y, 0.0,
x + x_delta, y, 0.0,
x, y + y_delta, 0.0,
x + x_delta, y + y_delta, 0.0,
};
int vertexLocation = m_program.attributeLocation("aVertex");
m_program.enableAttributeArray(vertexLocation);
m_program.setAttributeArray(vertexLocation, GL_FLOAT, vertexes, 3);
const GLfloat colors[] =
{
0.2, 0.6, 1.0, 0.3,
0.2, 0.6, 1.0, 0.3,
0.2, 0.6, 1.0, 0.3,
0.2, 0.6, 1.0, 0.3,
};
int colorLocation = m_program.attributeLocation("aColor");
m_program.enableAttributeArray(colorLocation);
m_program.setAttributeArray(colorLocation, GL_FLOAT, colors, 4);
const GLushort indices[] =
{
0, 1, 2, // first triangle
3, 3, 2, // second triangle
};
glDrawElements(GL_TRIANGLES, sizeof(indices)/sizeof(indices[0]), GL_UNSIGNED_SHORT, indices);
m_program.disableAttributeArray( vertexLocation );
m_program.disableAttributeArray( colorLocation );
These rectangles are actually very small and they appear as lines (code for drawing lines is almost the same), so to certain zoom level I draw only lines, still is very slow. I am not sure if such performance is ok when drawing so many of rectangles. Maybe I am expecting too much, or there is a mistake in code. Anyway, I would like to hear some hints on performance optimization.
Already using glEnable(GL_DEPTH_TEST); glEnable(GL_CULL_FACE);, drawing only in 2D. All rectangles are of unique dimensions.
Thanks you in advance.