Skip to main content
gamedev.net
🔒 Locked

Tile-Based Speed Issues

Started by HUWUWA Dec 19, 2001 at 6:51 PM 1 replies 200+ views
Original Post
HUWUWA
HUWUWA
Hi guys, I''m kinda new to 3D programming. I made a nice terrain map using QUADS in a loop within Begin() but with a map of 256x256 tiles it lags a little, and it doesn''t even draw buildings yet. Am I supposed to only draw the tiles that are in the viewport ? Do I need a more efficient way to draw the tiles ? In any case, I used a cool random funtion and the maps look pretty awesome. Thanks.
Terran Marine
Terran Marine
You''re only supposed to draw the tiles you see. You have to use a for loop, here''s an example :

Here''s some tile-drawing code straight out of my engine :

void draw_tiles(char layer, int x, int y, GLfloat z)
{
int mapx=0, mapy=0;
int tile;
for(y=0;y<16*32;y+=32)
{
for(x=0;x<21*32;x+=32)
{
mapx = scrollx/32;
mapy = scrolly/32;

xoff = scrollx & 31;
yoff = scrolly & 31;

int y32, x32;
y32 = y/32;
x32 = x/32;

if(layer==1)
{
tile = map_layer1[y32 + mapy][x32 + mapx];
glBindTexture(GL_TEXTURE_2D,layer1[tile].texID);
}
else
{
if(layer==2)
{
tile = map_layer2[y32 + mapy][x32 + mapx];
glBindTexture(GL_TEXTURE_2D,layer2[tile].texID);
}
}

//Faster : Triangle Strips rendering code
glBegin(GL_TRIANGLE_STRIP);
glTexCoord2f(0.0, 0.0);
glVertex2f(x-xoff, y-yoff);

glTexCoord2f(0.0, 1.0);
glVertex2f(x-xoff, y-yoff + 32);

glTexCoord2f(1.0, 0.0);
glVertex2f(x-xoff + 32, y-yoff);

glTexCoord2f(1.0, 1.0);
glVertex2f(x-xoff + 32, y-yoff + 32);

glEnd();

}
}
}


BTW. Using triangle strips is faster than using quads.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.