Original Post
Hi! I'm rendering a terrain using GLSL for texture splatting and PPL, but depending on the camera position/angle I get some ugly "artifacts"/results:
Shaders One thing left to say: I don't get this errors, when I disable texture mipmapping, but the results without mipmapping (lot of noise) are even less useable... Oh and, how can I use more than 3-4 textures for splatting? Thanks!
Fragment Shader
uniform sampler2D Grass;
uniform sampler2D Stone;
uniform sampler2D Rock;
uniform sampler2D Alpha;
varying vec4 diffuse,ambientGlobal, ambient;
varying vec3 normal,lightDir,halfVector;
varying float dist;
vec4 cursorColor = vec4(1,0,0,1);
uniform float cursorSize;
uniform vec2 cursorPosition;
void main(void)
{
//Texture Splatting
vec2 texUV = gl_TexCoord[0].xy;
vec4 alpha = texture2D( Alpha, texUV.xy );
vec4 tex0 = texture2D( Grass, texUV.xy * 64.0 ); // Tile
vec4 tex1 = texture2D( Rock, texUV.xy * 64.0 ); // Tile
vec4 tex2 = texture2D( Stone, texUV.xy * 64.0 ); // Tile
tex0 *= alpha.r; // Red channel
tex1 = mix( tex0, tex1, alpha.g ); // Green channel
vec4 outColor = mix( tex1, tex2, alpha.b ); // Blue channel
//Drawing a Editing-Brush for the Terrain
float distRing = distance(texUV.xy, cursorPosition);
if(distRing < cursorSize)
{
float strength = max(0.0, mix(-1.5, 0.5, distRing / cursorSize));
outColor += cursorColor * strength;
}
//gl_FragColor = outColor;
//Per Pixel Lighting
float startAlpha = gl_Color.w;
vec3 n,halfV,viewV,ldir;
float NdotL,NdotHV;
vec4 color = ambientGlobal;
float att;
n = normalize(normal);
NdotL = max(dot(n,normalize(lightDir)),0.0);
if (NdotL > 0.0)
{
att = 1.0 / (gl_LightSource[0].constantAttenuation +
gl_LightSource[0].linearAttenuation * dist + gl_LightSource[0].quadraticAttenuation * dist * dist);
color += att * (diffuse * NdotL + ambient);
halfV = normalize(halfVector);
NdotHV = max(dot(n,halfV),0.0);
color += att * gl_FrontMaterial.specular * gl_LightSource[0].specular *
pow(NdotHV,gl_FrontMaterial.shininess);
}
color.w = startAlpha;
gl_FragColor = color*outColor;
}
Vertex Shader
varying vec4 diffuse,ambientGlobal,ambient;
varying vec3 normal,lightDir,halfVector;
varying float dist;
void main()
{
gl_TexCoord[0] = gl_MultiTexCoord0;
//Per Pixel Lighting
vec4 ecPos;
vec3 aux;
normal = normalize(gl_NormalMatrix * gl_Normal);
ecPos = gl_ModelViewMatrix * gl_Vertex;
aux = vec3(gl_LightSource[0].position-ecPos);
lightDir = normalize(aux);
dist = length(aux);
halfVector = normalize(gl_LightSource[0].halfVector.xyz);
diffuse = gl_FrontMaterial.diffuse * gl_LightSource[0].diffuse;
ambient = gl_FrontMaterial.ambient * gl_LightSource[0].ambient;
ambientGlobal = gl_LightModel.ambient * gl_FrontMaterial.ambient;
gl_Position = ftransform();
}