Sign in to follow this  
Napster23

Optimized Tiling

Recommended Posts

Napster23    122
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(); } }

Share this post


Link to post
Share on other sites
Puppet    181
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();


Share this post


Link to post
Share on other sites
Napster23    122
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]

Share this post


Link to post
Share on other sites
ghosted    445
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.

Share this post


Link to post
Share on other sites
Napster23    122
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.


Share this post


Link to post
Share on other sites
Yann L    1802
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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this