PDA

View Full Version : Help needed with multi texturing



abhishek bansal
06-20-2011, 04:07 AM
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 &amp;&amp; heightRatio <= 0.6)
gl_FragColor = vec4(grassColor*0.5 + sandColor*0.5, 1.0);
else if (heightRatio > 0.6 &amp;&amp; heightRatio <= 0.8)
gl_FragColor = vec4(grassColor * 0.2 + sandColor * 0.4 + iceColor * 0.4, 1.0);
else if(heightRatio > 0.8 &amp;&amp; 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);

Ilian Dinev
06-20-2011, 12:01 PM
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);

abhishek bansal
06-20-2011, 11:24 PM
hmmm that is great..!!!

Thank you very much for your help...:)