Advise on displaying textures

Hi, is the call to multiple glBegin() and glEnd() the most efficient way to display a grid based graphics?

For example, I have this code:

for (int i = 0; i < MAP_HEIGHT; i++) {
	for (int j = 0; j < MAP_WIDTH; j++) {
        //determine what kind of object is in this position
        //ex. if (node.object == grass) glBindTexture(GL_TEXTURE_2D, grass);
			glTexCoord2d(0, 1);
			glVertex2d(j * (window.GetWidth() / MAP_WIDTH), (i + 1) * (window.GetHeight() / MAP_HEIGHT));

			glTexCoord2d(1, 1);
			glVertex2d((j + 1) * (window.GetWidth() / MAP_WIDTH), (i + 1) * (window.GetHeight() / MAP_HEIGHT));

			glTexCoord2d(1, 0);
			glVertex2d((j + 1) * (window.GetWidth() / MAP_WIDTH), i * (window.GetHeight() / MAP_HEIGHT));

			glTexCoord2d(0, 0);
			glVertex2d(j * (window.GetWidth() / MAP_WIDTH), i * (window.GetHeight() / MAP_HEIGHT));

Or are there any other optimal solutions for this?

The first thing you should consider is using floats instead of doubles. This may speed up your code a little. You could also dispose the glTexCoord2* and glVertex2* with glTexCoord2v and glVertex2v, with this you pass only one parameter instead of two. A third optimization is to use vertex arrays or display lists and set the sizes using glTranslate and glScale.

It really depends on your GPU. For example, a shader-based solution on an openGL 3.0 GPU could simply store a heightfield and texture-fetch or vertex-buffer-fetch values based on x-y. Apart from that, and everything Godlike mentioned, a HUGE and simple optimization is to use triangle strips instead of quads(cuts down passed vertices to half). And do use vertex arrays(+vertex buffer objects if available), it IS a win!

The most efficient way would be to merge all quad draw calls into a single drawElements(), preferably using VBOs.

This assumes you can get rid of all state changes between multiple quads.

  • So, for example all textures have to been merged together into a large texture (atlas or array) and the texture coordinates have to been adjusted.
  • Only one (uber-)shader can be used for all quads.

Can you post good tutorials on how to use vertex buffer objects? And vertex arrays? Or can you post codes that has similar results with my code above that is more optimized than that?

Thanks a lot!

Good VBO tutorial:

About texture atlas: