#version 420 in vec2 Fragment_UV; in vec3 normal0; in vec3 toLightVector[4]; in vec3 toEyeVector; out vec4 out_color; uniform vec4 lightColor[4]; uniform vec3 attenuation[4]; uniform vec3 World_eyePosition; uniform sampler2D Texture_Background; uniform sampler2D Texture_R; uniform sampler2D Texture_G; uniform sampler2D Texture_B; uniform sampler2D Texture_BlendMap; void main() { vec4 totalDiffuse = vec4(0.0, 0.0, 0.0, 1.0); vec4 totalSpecular = vec4(0.0, 0.0, 0.0, 1.0); for(int i = 0; i < 4; i++) { /*Light Attenuation*/ float dist = length(toLightVector[i]); float attFactor = attenuation[i].x + (attenuation[i].y * dist) + (attenuation[i].z * dist * dist); vec3 unitToLightVector = normalize(toLightVector[i]); /*Diffuse lighting*/ /*La intensidad es el cos entre la normal y el vector hacia la luz*/ float brightness = dot(unitToLightVector, normalize(normal0)); /*No queremos luz negativa ni mayor a 1*/ brightness = max(brightness, 0.0); totalDiffuse = totalDiffuse + (brightness * lightColor[i]) / attFactor; /* Specular lighting */ /* Reflejamos el vector hacia la luz (su inverso) en la normal */ vec3 Vector_ReflectedLight = reflect(-unitToLightVector, normal0); /* La luz especular es el cos del angulo entre el vector hacia el ojo y la luz reflectada en la normal */ float specularity = clamp( dot(Vector_ReflectedLight, toEyeVector), 0, 1 ); float reflectivity = 0.5; /* Que tan grande es el specular highlight */ specularity = pow(specularity, 10); totalSpecular = totalSpecular + clamp( reflectivity * specularity * lightColor[i], 0, 1) / attFactor; } /* Ambient Light */ totalDiffuse = max(totalDiffuse, 0.2); /*Terrain color*/ /*The color of the current fragment from the blendmap*/ vec4 blendMapColor = texture(Texture_BlendMap, Fragment_UV); /*We want the background color when we have black in the blend map*/ float backTextureAmount = 1 - (blendMapColor.r + blendMapColor.g + blendMapColor.b); /*So we dont lose image quality from the tiles textures (Fragment_UV make the terrain look less HD)*/ vec2 Tiled_UV = Fragment_UV * 50.0f; /*We get the color of each map tile (soil1, soil2, etc)*/ vec4 backgroundTextureColor = texture(Texture_Background, Tiled_UV) * backTextureAmount; vec4 rTextureColor = texture(Texture_R, Tiled_UV) * blendMapColor.r; vec4 gTextureColor = texture(Texture_G, Tiled_UV) * blendMapColor.g; vec4 bTextureColor = texture(Texture_B, Tiled_UV) * blendMapColor.b; /*We mix them corresponding with the blendmap*/ vec4 totalColor = backgroundTextureColor + rTextureColor + gTextureColor + bTextureColor; /*Our final color for the fragment*/ out_color = totalDiffuse * totalColor + totalSpecular; }