PDA

View Full Version : Advise on displaying textures



racumin
11-02-2009, 11:11 PM
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);
glBegin(GL_QUADS);
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));
glEnd();
}
}


Or are there any other optimal solutions for this?

Godlike
11-03-2009, 01:32 AM
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 glTexCoord2*v and glVertex2*v, 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.

Y-tension
11-03-2009, 07:08 AM
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!

Nighthawk
11-03-2009, 07:49 AM
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.

racumin
11-03-2009, 06:59 PM
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!

Nighthawk
11-04-2009, 03:25 AM
Good VBO tutorial:
http://www.songho.ca/opengl/gl_vbo.html

About texture atlas:
http://en.wikipedia.org/wiki/Texture_atlas
http://www.gamasutra.com/features/20060126/ivanov_01.shtml