Weird problem combining multiple textures

I am trying to render a piece of terrain with a texture calculated based on the slope and height at each point. I use the standard fixed function pipeline for the vertex processing and then apply a fragment shader to perform the texturing. I have successfully got it to load in textures of the height and slope maps, as well as various ground textures, and a texture that is used to select which ground texture to apply to a point based on height and slope values. This produces squares of ground texture all over the map which are not blended together.
To do the blending, I find the height and slope values at the 4 nearest vertices, and then use these to calculate the ground texture at each. Next I combine the two colours on one side of the square, and the two on the other side, using the distance along the line between the two points the fragment is, i.e.

firstcolor = fract.y*color1+(1.0-fract.y*color2)

, and the same to get the second colour. I then try to blend these two colours together based on the distance the fragment is in the other direction,

firstcolor *= fract.x;
secondcolor *= (1.0-fract.x);
gl_FragColor = firstcolor+secondcolor;

If I simply use the first colour or the second, I see the terrain textured as I would expect, but of course blending to black across every quad. When I introduce the sum, something very strange happens. The texture no longer bears any resemblance to the height or slope maps. Instead it is divided into four squares, with diagonally opposite squares being textured with the same texture, and the two textures used are the one corresponding to zero slope and height and maximum slope and height. I have no idea why this would be the case, but I have been experimenting with it, changing variables and trying different things and I have discovered that if I combine just one colour channel, e.g. only firstcolor.g+secondcolor.g it behaves as expected, but if I introduce a second component it breaks. Also the code


produces a crazy mess of random pattern rather than being the same as firstcolor as you might expect.
If anyone has any idea what might be wrong with this I would be very grateful for some thoughts, as I am completely confused.

Take a screenshot and we might be able to help you in a more eficient way.

A screenshot as requested. You can see that the two colours individually blend correctly, so when combined the entire terrain should be blended, but instead

Ok, the way you are doing it looks a little complicated, what you should do is to make a precalcylated coverage texture(based on the height and slope maps) and just use that directly to blend the different ground textures.

this line

firstcolor = fract.y*color1+(1.0-fract.y*color2)

Looks suspect to me, perhaps whis will work

firstcolor = (fract.y*color1)+(1.0-fract.y*color2)

anyway, mess around with the values, when you find the one that matters, then look up how it is created

This topic was automatically closed 183 days after the last reply. New replies are no longer allowed.