Texture Blending in a Terrain

Hello all,

I am currently trying to make a tile based engine, each type of tile having it’s own texture, I wondered how it is possible, when two different type of tiles meet, to make a progressive blend in between the two, using some sort of interpolation.
I have already played(without much success) with the ARB extention, I managed to mix 2 textures. But it’s not really what I seek.

I have heard about 3 things which maybe could help me,splatting alpha maps and multipass rendering, I have searched in vain.
So, if anybody could enlighten me, or just point out some simple tutorial, I would really apreciate.

Thanks for your time.

PS:Without going that far, my idea was to be able to do something that at least looks a bit like this:

Right-o. What I would try to do would be to create a texture that is the transitional texture, and use it where appropriate. You can do that easily by transferring 2 textures from GPU to CPU (get them as an array using glGetTexImage), then do your calculations for the transition texture), and then turn that back into a texture to feed to the GPU (glTexSubImage2D). This may be kind of slow, but if you did those calculations when your game loads, that would be OK…might be fine for static-terrain environments.

Thanks, I’ve just though(with what you said),using SDL(which I already do to load textures and display my OpenGL render) I could parse each terrain(when loading it) for every possible combination, register them as matrices* then make corresponding SDL Surfaces, that I can turn into GL textures, and perhaps also create a map(STL) :slight_smile:

eg 2x2 matrix:
[ R R
R G ]
Where R = Rock and G = Grass

Would it work? Or is it too slow?

Suppose you have a matrix that represents your tiles. For sake of example, suppose your entire landscape is a 2x2 matrix (in your application, maybe 100x100 or 1000x1000). Store an enumerated type or a similar kind of thing in this matrix.

Now, each tile is a square (at least the texture is, suppose). Consider each vertex of that square. Get the type of each vertex by determining what tiles it touches.


consider a 2x2 world:


where R denotes rock and G denotes grass.

The 3x3 grid of vertexes is then (separated by commas, new rows on new lines):

100% R (1 R divided by 1 total), 50% R and 50% G(1 R & 1 G div. by 2 total, 100% G
50% R and 50% G, 50% R and 50% G (2/4 and 2/4), 50% R and 50% G
100% G, 50% R and 50% G, 100% R

The “type” of a vertex is a floating point vector summing to 1, basically saying: this vertex is about X% rock and Y% grass and Z% water, etc.

Then find where the transitional tiles are. This is easy. All tiles bordering tiles of a different type - and by bordering, I’m using the minesweeper definition: one diagonal step away or bordering on a side - are transitional tiles. You only need to calculate these “vertexes” as I’ve defined them for these transitional tiles.

Now, using the vertexes, interpolate at each pixel of the transitional tile you are creating to find the amount of each of your standard textures’ contributions to that tile.


Say I want to make a tile that has vertexes labeled
50%R/50%G 100%R
50%R/50%G 100%R

I might wind up with a tile that looks like this (each entry is a pixel, although there’d be more pixels):

50%R/50%G, 51%R/49%G, 52%R/48%G, … , 99%R/1%G, 100%R
50%R/50%G, 51%R/49%G, 52%R/48%G, … , 99%R/1%G, 100%R

50%R/50%G, 51%R/49%G, 52%R/48%G, … , 99%R/1%G, 100%R

where 50%R/50%G just means take the value at that pixel of the rock tile, multiply it by 50%, and add it to 50% of the value at that pixel of the grass tile.

If I’m confusing the hell out of you, post again at the end of the week. I’m solving the same problem too, so I might be able to hand you a full algorithm.

Also, this could be too slow for you. Depends on your application. If you only need to create your map once, I don’t think it is unreasonable.

hehe, you’re not confusing the hell out of me, the only thing I don’t really understand is how to really interpolate the pixels, and how to store all the textures so that you do it once and for all, not at every frame…