Optimized Tiling
Hallo,
i have a problem with tiling.
How can i optimize this part of code?
for (int y = 0; y < mapheight; y++)
{
for (int x = 0; x < mapwidth; x++)
{
glBegin(GL_QUADS);
glTexCoord2f(0.0f, 0.0f);glVertex3f(float(x), float(y), 0.0f);
glTexCoord2f(1.0f, 0.0f);glVertex3f(float(x + 1), float(y), 0.0f);
glTexCoord2f(1.0f, 1.0f);glVertex3f(float(x + 1), float(y + 1), 0.0f);
glTexCoord2f(0.0f, 1.0f);glVertex3f(float(x), float(y + 1), 0.0f);
glEnd();
}
}
You don't have to begin and end for every quad. This is fine:
float fx = 0.0f;float fy = 0.0f;glBegin(GL_QUADS);for (int y = 0; y < mapheight; y++, fy += 1.0f){ for (int x = 0; x < mapwidth; x++, fx += 1.0f) { glTexCoord2f(0.0f, 0.0f);glVertex3f(fx, fy, 0.0f); glTexCoord2f(1.0f, 0.0f);glVertex3f(fx + 1.0f, fy, 0.0f); glTexCoord2f(1.0f, 1.0f);glVertex3f(fx + 1.0f, fy + 1.0f, 0.0f); glTexCoord2f(0.0f, 1.0f);glVertex3f(fx, fy + 1.0f, 0.0f); }}glEnd();
Thank you for the very quick answer. This code works fine but it is not much faster
I have a second question.
Should i use Quadtree for large maps?
and if yes how can i do this in a 2d map?
[Edited by - Napster23 on July 18, 2005 4:40:04 AM]
I have a second question.
Should i use Quadtree for large maps?
and if yes how can i do this in a 2d map?
[Edited by - Napster23 on July 18, 2005 4:40:04 AM]
I'm assuming that code there draws all the tiles in your map. I doubt you'd need anything as complex as a quadtree, especially if your camera is at fixed zoom level. Simply determine which tiles are on screen and only render them. Hell, it can still be done with a variable zoom level.
i want to make a map in ortho mode with iso view.
can you give me a piece of code how i can only render the tiles in the viewport?
Would be really nice.
can you give me a piece of code how i can only render the tiles in the viewport?
Would be really nice.
Quote:Original post by Napster23
Hallo,
i have a problem with tiling.
How can i optimize this part of code?
Try this somewhere in your startup code (DO NOT call this every frame, just once at the beginning !):
unsigned int dlist = glGenLists(1);glNewList(dlist, GL_COMPILE);glBegin(GL_QUADS);for (int y = 0; y < mapheight; y++){ for (int x = 0; x < mapwidth; x++) { glTexCoord2f(0.0f, 0.0f);glVertex3f(float(x), float(y), 0.0f); glTexCoord2f(1.0f, 0.0f);glVertex3f(float(x + 1), float(y), 0.0f); glTexCoord2f(1.0f, 1.0f);glVertex3f(float(x + 1), float(y + 1), 0.0f); glTexCoord2f(0.0f, 1.0f);glVertex3f(float(x), float(y + 1), 0.0f); }}glEnd();glEndList();
Then, every frame you render your stuff by executing the display list:
glCallList(dlist);
Although faster than manual immediate mode, the real speed up will only come from using proper frustum culling and VBOs. But for starters, a display list is already a good approach.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement