Skip to main content
GameDev.net gamedev.net
🔒 Locked

Help Needed with Multitexturing

Started by Abhishek Bansal Jun 20, 2011 at 10:10 AM 4 replies 650+ views
Original Post
Abhishek Bansal
Abhishek Bansal
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);
SuperVGA
SuperVGA
The edges are very sharp. Try making the heightRatio intervals larger, and a higher resolution texture would look good also.
Even with this, the terrain might look a little curious. You might want to add some environment or bump mapping, or add unique
texturing to some areas. What are you trying to acheive: Maybe you can post links to those images of other terrains that you refer to?


EDIT: You should compute a blend factor for each layer on separate lines, (from (distance between map height and ideal texture height with) * height coverage). Multiply the facor with the fragment for the texture in question. Your fading is preset, resulting in a very sharp transition.
Anyways I think the terrain looks good so far! Good job!
challenge
challenge
Instead of hardcodeing values in shader, you can also use an 1-dimensional texture with smooth transition from one color to another (yellow - green - brown - gray - white for example). In order to give detail to this terrain (there will be just plain colors yellow on low height and white on high height) use a grayscale texture to modulate it. Using multiple grayscale textures with different sizes over terrain(one texture covering whole terrain, another one covering it for 32x32, etc.) can result in pretty good looking terrain.
Abhishek Bansal
Abhishek Bansal
Anyways I think the terrain looks good so far! Good job!

First of all very very thank you for your motivation ... i really appreciate it....


You might want to add some environment or bump mapping, or add unique
texturing to some areas.


why would one use environment mapping in terrains can you please elaborate...(i thought environment mapping is for simulating reflections)


EDIT: You should compute a blend factor for each layer on separate lines, (from (distance between map height and ideal texture height with) * height coverage). Multiply the facor with the fragment for the texture in question. Your fading is preset, resulting in a very sharp transition.


i changed my shader to this...and achieved a really pretty terrain ...also i am using four textures instead of three now...


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;
vec3 rockColor = texture2D(rock,gl_TexCoord[2].st).rgb;

float maxHeight1 = 0.0;
float lowerBound = 0.0;
float height1 = 0.0;
if(minHeight < 0)
{
lowerBound = abs(minHeight);
maxHeight1 = maxHeight + lowerBound;
height1 = height + lowerBound;
}
else if (minHeight > 0)
{
maxHeight1=maxHeight-minHeight;
height1 = height - lowerBound;
}
else
{
maxHeight1=maxHeight;
height1 = height;
}

float interval = maxHeight1/3.0;

float grassWeight = clamp(1.0 -(abs(height1 - 0.0)/interval),0.0,1.0);
float sandWeight = clamp(1.0 -(abs(height1 - interval)/interval),0.0,1.0);
float rockWeight = clamp(1.0 -(abs(height1 - (2.0 * interval))/interval),0.0,1.0);
float iceWeight =clamp(1.0 -(abs(height1 - maxHeight1 * .8)/interval),0.0,1.0);

float totalWeight = grassWeight + sandWeight + iceWeight + rockWeight;

grassWeight = grassWeight/totalWeight;
sandWeight = sandWeight/totalWeight;
iceWeight = iceWeight/totalWeight;
rockWeight= rockWeight / totalWeight;

gl_FragColor = vec4((grassWeight * grassColor.rgb + sandWeight * sandColor.rgb + rockWeight * rockColor.rgb + iceWeight * iceColor.rgb),1.0);
SuperVGA
SuperVGA
Wow that looks much better! I think the grass and sand needs higher resolution to match the overall quality, though. I meant env mapping as when it's used for ambient occlusion. I'm on the go now, and can't find it easily, but one other guy in here had a post briefly explaining how he did it on landscapes. But that was more meant as a side note next to general lighting. -It looks like you got some sort of lighting too, how do you get that? It's as if you have a very high degree of detail on those summits. :-)
Abhishek Bansal
Abhishek Bansal

W I meant env mapping as when it's used for ambient occlusion. It looks like you got some sort of lighting too, how do you get that? It's as if you have a very high degree of detail on those summits.

well i know nothing about ambient occlusion....

W I meant env mapping as when it's used for ambient occlusion. It looks like you got some sort of lighting too, how do you get that? It's as if you have a very high degree of detail on those summits.

and i dont have any lights..in that scene all effects are from texture itself...

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.