[gameprogrammer] Re: Repeating part of a texture in GL
- From: Sami Näätänen <sn.ml@xxxxxxxxxxxx>
- To: gameprogrammer@xxxxxxxxxxxxx
- Date: Thu, 2 Nov 2006 20:54:31 +0200
On Wednesday 01 November 2006 00:47, Alan Wolfe wrote:
> heya
>
> If i have a texture loaded into memory that has alot of various tiles
> on it, how would i go about texturing one of those tiles onto a quad
> and having the texture repeat.
>
> I know i could do it by just making a bunch of quads, or by making
> the tile i want into it's own texture and using texture coordinates
> to make it repeat but is there any other way thats just quick and
> easy?
I think only way is to use fragment shaders.
Fragment shaders can do so much more.
For Tiling example:
Normally one would use one quad for each tile.
Not needed anymore.
With one fullscreen quad one can draw the tile map to the screen.
One can also blend the tiles together without any tile borders.
These are the requirements for this kind of tile system.
At least one texture, where the tiles are kept.
One texture, which holds the map.
The position in the map.
The size of the map.
Texture coordinates will be from 0.0 to n.0 for x-direction and
0.0 to m.0 for y-direction. So the used part of the tile map is n*m.
How it works:
One will first pick the texture coordinates for the fragment.
The map offset value is added and this value is saved as the fragments
map position.
Then the fragment position is rounded to tile border and divided by the
map size. This is saved as the normalized tile position.
The fragment position is now truncated to the fraction part. This is
then diveded by the directional tile counts in the Tiles texture.
This is the offset of the fragment inside the tile.
Texture look up is used to get the tiles position in the Tiles texture
from the Map texture.
Next the fragment position inside the tile is added to that.
This is then used to get the color of the fragment from the Tiles
texture.
Fragment shader:
uniform vec2 MapPosition; // upper left corner map position
uniform vec2 MapSize; // Map size
uniform vec2 TileGrid; // How many tiles the Tiles texture has
uniform sampler2D Map; // Tile map with 2 components (Red & Alpha)
uniform sampler2D Tiles; // Tiles side by side
void
main() {
vec2 frag_pos = vec2( gl_TexCoord[0].st + MapPosition );
vec2 map_pos = vec2( floor( frag_pos ) / MapSize );
frag_pos = fract(frag_pos) / TileGrid;
frag_pos += texture2D( Map, map_pos ).ra;
gl_FragColor = texture2D( Tiles, frag_pos );
}
PS. This is in no way complete rendering system description, but more
like a proof of concept. One would extend this by computing the areas
that this pixel occupies and then blend these colors accordingly.
This is needed, because one pixel can be part of up to four tiles.
This kind of renderer will produce good looking free zooming without any
visible tile borders.
PS. PS. But all of that is left as an exercise. :)
---------------------
To unsubscribe go to http://gameprogrammer.com/mailinglist.html
- Follow-Ups:
- [gameprogrammer] Re: Repeating part of a texture in GL
- From: Scott Harper
Other related posts:
- » [gameprogrammer] Repeating part of a texture in GL
- » [gameprogrammer] Re: Repeating part of a texture in GL
- » [gameprogrammer] Re: Repeating part of a texture in GL
- » [gameprogrammer] Re: Repeating part of a texture in GL
- [gameprogrammer] Re: Repeating part of a texture in GL
- From: Scott Harper