This shader use the vertex height in the model to blend and to put tiled texture on a model.
For the texturing, it work perfectly as intented. I found some GLSL code in a tutorial site on GLSL and tried to add a directional light using GL_LightSource[0] and defined directly a position for this light in the shader.
The shader "work" kinda, on NVIDIA Hardware and on INTEL HD hardware. Tried it on AMD RADEON HD 7750 and the light channel give me a total black and darken the terrain completely.
Here is a screenshot with the light being properly cast over the terrain: (Render like this on NVIDIA and INTEL HD)
If I use the same on RADEON HD 7750, the image is all dark (rest of rendering is still ok, we can still see the blending of the textures)
EDIT: With some friends, we tested the shader with multiple AMD RADEON cards:
- NVIDIA GTX: work
- INTEL HD: work
- AMD RADEON 4800 SERIES: Work
- AMD MOBILITY 5400 SERIES: Work
- AMD RADEON 7000 SERIES: Fail (Compile but wrong rendering)
So far this problem seem to occur only on the RADEON 7000 series of cards. I don't know what to think now!
Most of it is done in the fragment/pixel shader, so here is the code:
Code: Select all
uniform sampler2D terrainLayer0;
uniform sampler2D terrainLayer1;
uniform sampler2D terrainLayer2;
uniform sampler2D terrainLayer3;
uniform float plateau;
uniform int terrainTextureScale;
uniform int terrainScale;
uniform int editingTerrain;
uniform vec4 AmbientLight;
varying vec3 normal;
varying vec3 position;
varying vec4 worldCoord;
void main()
{
float scale = float(terrainScale);
vec2 texCoord = vec2(gl_TexCoord[0]);
vec4 tex0 = texture2D( terrainLayer0, texCoord.xy*float(terrainTextureScale));
vec4 tex1 = texture2D( terrainLayer1, texCoord.xy*float(terrainTextureScale));
vec4 tex2 = texture2D( terrainLayer2, texCoord.xy*float(terrainTextureScale));
vec4 tex3 = texture2D( terrainLayer3, texCoord.xy*float(terrainTextureScale));
//tex1 = mix( tex0, tex1, min(1.0-normal.y,1.0) );
// First layer of composition soil & grass blending
float position2=position.y+200.0;
if (position2>128.0 && position2<256.0)
tex1 = mix( tex1, tex0, 1.0-(position2-128.0)/128.0);
if (position2<=128.0)
tex1=tex0;
// Second composition of first block and rock blending
if (position2>256.0 && position2<512.0)
tex2 = mix( tex1, tex2, ((position2-256.0)/256.0));
if (position2<=256.0)
tex2 = tex1;
// Third composition of first block and snow blending
if (position2>620.0 && position2<768.0)
tex3 = mix( tex2, tex3, ((position2-620.0)/148.0));
if (position2<=620.0)
tex3 = tex2;
vec4 tex10 = tex3;
//Plateau band
if(position.y>(plateau-1.5) && position.y<(plateau+1.5) && editingTerrain==1)
tex10=vec4(0.0,0.5,0.0,0.5);
// Directional light no attenuation (Sun)
vec3 norm = normalize(normal);
vec3 sunVector = normalize(vec3(0,50000,-50000) - worldCoord.xyz);
float sunDir = max(0.0, dot(norm, sunVector));
vec4 diffuse;
diffuse = (gl_LightSource[0].diffuse * sunDir*1.45); //1.45 brighten the whole texture
// Rendering with 1 directional light source
vec4 finalColor = ((0.45+diffuse) * vec4(tex10.rgb, 1.0))*AmbientLight; //0.45 diffuse the shadowing
// Rendering with no light source
//vec4 finalColor = (vec4(tex10.rgb, 1.0))*AmbientLight;
//Fog blending
float fog = (gl_Fog.end - gl_FogFragCoord) * gl_Fog.scale;
gl_FragColor = mix(gl_Fog.color,finalColor, fog);
}
Code: Select all
#version 150 compatibility
varying vec3 normal;
varying vec3 position;
varying vec4 worldCoord;
void main(void)
{
//gl_Position = ftransform();
gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
gl_TexCoord[0] = gl_TextureMatrix[0] * gl_MultiTexCoord0;
gl_FogFragCoord = gl_Position.z;
normal = gl_Normal.xyz;
position = gl_Vertex.xyz;
worldCoord = gl_ModelViewMatrix * gl_Vertex;
}
Code: Select all
// Directional light no attenuation (Sun)
vec3 norm = normalize(normal);
vec3 sunVector = normalize(vec3(0,50000,-50000) - worldCoord.xyz);
float sunDir = max(0.0, dot(norm, sunVector));
vec4 diffuse;
diffuse = (gl_LightSource[0].diffuse * sunDir*1.45); //1.45 brighten the whole texture
// Rendering with 1 directional light source
vec4 finalColor = ((0.45+diffuse) * vec4(tex10.rgb, 1.0))*AmbientLight; //0.45 diffuse the shadowing
With Irrlicht how GL_LigthSource[0] is used? (I have created a Irrlicht directional light in Irrlicht, but does'nt seem to link to that). Have someone have more information on GL_LightSource? I'm not sure I understand well how this work.
Would it be way to use the Irrlicht light information to be sent to the shader via UNIFORMS? So I could perhaps change that part of code here and hopefully have a OK rendering on AMD Hardware also? If I pass the light information via uniform, how to get the .diffuse?
Also got question about Irrlicht Directionnal light... Is there a command to increase the intensity? I barely see the light effect on the non-shader models.