techno
techno

Reputation: 833

how to map a texture onto a grid of tiles seamlessly?

I have a grid of 8 X 8 points and want to overlay an image on it. I divide the inner grid of 6 X 6 into 4 equal sub-parts of size 3 X 3 each. Let us call this sub-part as tile. The idea is to render these tiles along with their corresponding textures. I divide the grid into tiles because in future I want to test it on a larger grid. I have done it using following approach but I get seams along the edges. I can't figure out why the seams appear.

First the tiles have corresponding starting co-ords as {(0,0),(0,3),(3,0),(3,3)}.

I have an image of 8 X 8 size which I want to overlay on the super_grid and I store its rgba values in some super_data(8 X 8).

While rendering the tiles I draw them as quads and then overlay the corresponding texture on them. So, if (a,b) are starting co-ords for a particular tile, then I initialize the quad vertices ={(a-0.5,b-0.5),(a+3-0.5,b-0.5),(a+3-0.5,b+3-0.5),(a-0.5,b+3-0.5)} and if xs = 1/(tileWidth+1)=1/(3+1)=1/4 and ys =1/(tileHeight+1)=1/4, then texture Coordinates are {(xs/2,ys/2),(1-xs/2,ys/2),(1-xs/2,1-ys/2),(xs/2,1-ys/2)}

So, for the first tile,

quadVertices={(-0.5,-0.5),(3.5,-0.5),(3.5,3.5),(-0.5,3.5)}
textureCoords={(1/8,1/8),(1-1/8,1/8),(1-1/8,1-1/8),(1/8,1-1/8)}.

Before, passing a texture for the tile I initialize the texture : int image = new int [tileWidth+2] [tileHeight+2] by using the super_data. We take these dimensions because some part of tile texture overlap with the adjacent tiles(that is the reason why we account for this part while calculation of quadVertices).

Finally, to generate the texture, use

glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, tileWidth + 2, tileHeight + 2, 0, GL_RGBA, GL_UNSIGNED_BYTE, image); 
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);

Everything above works fine, I get a grid with image overlayed on it. However, I get seams at the edges. How can I get rid of the seams?

Upvotes: 2

Views: 1964

Answers (3)

genpfault
genpfault

Reputation: 52083

Try GL_CLAMP_TO_EDGE instead of GL_CLAMP.

Upvotes: 1

KleinBottle
KleinBottle

Reputation: 11

Have you got polygon smoothing on? Try turning it off:

glDisable(GL_POLYGON_SMOOTH)

Upvotes: 1

ssube
ssube

Reputation: 48267

You'll need to make sure the vertexes at the same location (or adjacent) use precise texture coordinates. Calculating based on world coordinates, as it appears you're doing, should be able to handle that.

The second part of the issue is the texture itself. Make sure the texture will tile properly when you place it. This becomes more difficult with overlapping tiles; typically you just make sure edges match properly.

Upvotes: 0

Related Questions