Hi everyone,
I'm programming a simple game using the LWJGL. The game uses a tile-based map.
I implemented an Atlas for my tile textures and wrote some code to generate the proper texture coordinates.
Now, this all seems to be working perfectly fine. The right textures are loaded every time and no bleeding occurs.
When I scroll the map, however, (I do this by translating the scene over a PAN_X and PAN_Y variable), some strange graphical glitches occurr every now and then. I have attached a screenshot of the problem. (the screenshot is of low quality, just a compression result, you will clearly see the graphical glitch I refer to, though.)
Does this look familiar to anyone? What could cause this phenomenon?
I call the general rendering routine as follows:
GL11.glClear(GL11.GL_COLOR_BUFFER_BIT);
GL11.glPushMatrix();
Texture texture = TextureUtils.ATLAS;
texture.bind();
world.renderTiles();
GL11.glPopMatrix();
The tiles are then rendered as such:
GL11.glPushMatrix();
GL11.glTranslatef(PAN_X, PAN_Y, 0f);
GL11.glScalef(World.ZOOM, World.ZOOM, 0f);
for (int y = tileY0; y < tileY1; y++)
{
for (int x = tileX0; x < tileX1; x++)
{
if ((x > MAP_SIZE-1) || (y > MAP_SIZE-1) || (x < 0) || (y < 0))
{
// Do not render
}
else
{
GraphicsUtils.renderTileGraphic(tiles[x][y]);
}
}
}
GL11.glPopMatrix();
I use the following basic rendering code for the tiles:
public static void renderTileGraphic(IRenderable renderObject)
{
int x = renderObject.getLocation().x;
int y = renderObject.getLocation().y;
Point coordinates = TextureUtils.getTileTextureCoordinates(renderObject.getTexture());
double tx0 = coordinates.x*TextureUtils.TEXTURE_STEP_TILE;
double tx1 = (coordinates.x+1)*TextureUtils.TEXTURE_STEP_TILE;
double ty0 = coordinates.y *TextureUtils.TEXTURE_STEP_TILE;
double ty1 = (coordinates.y+1)*TextureUtils.TEXTURE_STEP_TILE;
GL11.glPushMatrix();
GL11.glTranslatef(x, y, 0);
GL11.glRotatef(renderObject.getRotation(), 0f, 0f, 1f);
GL11.glTranslatef(-x, -y, 0);
GL11.glBegin(GL11.GL_QUADS);
GL11.glTexCoord2d(tx0,ty0);
GL11.glVertex2d(x - (renderObject.getWidth()/2), y - (renderObject.getHeight()/2));
GL11.glTexCoord2d(tx0,ty1);
GL11.glVertex2d(x - (renderObject.getWidth()/2), y + (renderObject.getHeight()/2));
GL11.glTexCoord2d(tx1,ty1);
GL11.glVertex2d(x + (renderObject.getWidth()/2), y + (renderObject.getHeight()/2));
GL11.glTexCoord2d(tx1,ty0);
GL11.glVertex2d(x + (renderObject.getWidth()/2), y - (renderObject.getHeight()/2));
GL11.glEnd();
GL11.glPopMatrix();
}