[gameprogrammer] Re: Repeating part of a texture in GL


On Nov 2, 2006, at 11:54 AM, Sami Näätänen wrote:

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.

Y'know, when I first read that, I thought to myself "what?? why go that far?" But now, having seen/read the rest of your message I realize I'm saying to myself instead "I KNEW there was a reason I liked fragment shaders aside from specular-maps and normal-maps etc...!" This is ridiculously amazing to me. Thank you very much for renewing my interest in GLSL and shaders. ^_^ I gotta go buy a book on this stuff! ^_^

--Scott

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. :)

PS:  Fun!
---------------------
To unsubscribe go to http://gameprogrammer.com/mailinglist.html


Other related posts: