Help needed with multi texturing

Hello guys…
I am trying to implement a multitextured terrain…i successfully implemented it…but my problem is i don’t know which technique should i use for blending different textures for final fragment color computation…presently i m computing fragment colors based as addition of different texture colors depending upon terrain height…but this does not look gud…final result is not as pretty as i see on the internet…
I am attaching my results

and here is my fragment shader


vec3 sandColor	= texture2D(sand,gl_TexCoord[0].st).rgb;
	vec3 grassColor	= texture2D(grass,gl_TexCoord[1].st).rgb;
	vec3 iceColor	= texture2D(ice,gl_TexCoord[2].st).rgb;
	
	float lowerBound = abs(minHeight);
	float maxHeight1 = maxHeight + lowerBound;
	float height1 = height + lowerBound;
	float heightRatio = height1/maxHeight1;
	
	if(heightRatio <= .3)
		gl_FragColor = vec4(grassColor, 1.0);
	else if (heightRatio > .3 && heightRatio <= 0.6)
		gl_FragColor = vec4(grassColor*0.5 + sandColor*0.5, 1.0);
	else if (heightRatio > 0.6 && heightRatio <= 0.8)
		gl_FragColor = vec4(grassColor * 0.2 + sandColor * 0.4 + iceColor * 0.4, 1.0);
	else if(heightRatio > 0.8 && heightRatio < 0.9)
		gl_FragColor = vec4(grassColor *0.0 + sandColor * 0.3 + iceColor * 0.7, 1.0);
	else if(heightRatio >= 0.9)
		gl_FragColor = vec4(iceColor, 1.0);

You expect gradual/linear blending results, but notice how in none of your formulas you use the heightRatio .


float ComputeAlpha(float hei, float hmin,float hmax){
	float hmid = (hmin+hmax)*0.5;
	float hspan = (hmax-hmin)*0.5;
	float hdiff = abs(hei - hmid)/hspan;
	
	return 1.0 - hdiff;
}


float grassAlpha =  ComputeAlpha(heightRatio, -0.6,0.6);
float sandAlpha  =  ComputeAlpha(heightRatio, 0.2,0.8);
float iceAlpha   =  ComputeAlpha(heightRatio, 0.5,1.5);
// note that grassAlpha+sandAlpha+iceAlpha != 1.0  , so we have to normalize later

vec3 outColor = grassAlpha*grassColor + sandAlpha*sandColor + iceAlpha*iceColor); // sum them up

outColor /= grassAlpha+sandAlpha+iceAlpha; // normalize


gl_FragColor = vec4(outColor, 1.0);

hmmm that is great…!!!

Thank you very much for your help…:slight_smile: