Advertisement
Guest User

OpenGL Terrain Lighting Shader Driver Bug

a guest
Jun 16th, 2015
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //Vertex Shader:
  2. #version 120
  3.  
  4. attribute vec3 aVertexPos;
  5. attribute vec2 aVertexTex;
  6. attribute vec3 aVertexNorm;
  7.  
  8. uniform mat4 uVMatrix;
  9. uniform mat4 uTMatrix;
  10. uniform mat4 uPMatrix;
  11. uniform vec3 uLightColor[5];
  12. uniform vec3 uLightPos[5];
  13. uniform vec3 uLightAtt[5];
  14.  
  15. varying vec3 vSurfaceNorm;
  16. varying vec3 vLightVector[5];
  17. varying vec3 vLightColor[5];
  18. varying vec3 vLightAtt[5];
  19. varying vec2 vVertexTex;
  20.  
  21. void main(){
  22.     vec4 lWorldPos = uTMatrix * vec4(aVertexPos, 1.0);
  23.     gl_Position = uPMatrix * uVMatrix * lWorldPos;
  24.    
  25.     for(int i = 0; i < 5; i++){
  26.         vLightVector[i] = uLightPos[i] - lWorldPos.xyz;
  27.         vLightColor[i] = uLightColor[i];
  28.         vLightAtt[i] = uLightAtt[i];
  29.     }
  30.  
  31.     vSurfaceNorm = (uTMatrix * vec4(aVertexNorm, 0.0)).xyz;
  32.     vVertexTex = aVertexTex * 128.0;
  33. }
  34.  
  35. //Fragment Shader:
  36. #version 120
  37.  
  38. uniform sampler2D diffuseTexs[4];
  39. uniform sampler2D blendTex;
  40.  
  41. varying vec3 vSurfaceNorm;
  42. varying vec3 vLightVector[5];
  43. varying vec3 vLightColor[5];
  44. varying vec3 vLightAtt[5];
  45. varying vec2 vVertexTex;
  46.  
  47. void main(){
  48.     vec4 texelDiffuse0 = texture2D(diffuseTexs[0], vVertexTex);
  49.     vec4 texelDiffuse1 = texture2D(diffuseTexs[1], vVertexTex);
  50.     vec4 texelDiffuse2 = texture2D(diffuseTexs[2], vVertexTex);
  51.     vec4 texelDiffuse3 = texture2D(diffuseTexs[3], vVertexTex);
  52.  
  53.     vec3 lTotalDiffuse = vec3(0.0);
  54.     for(int i = 0; i < 5; i++){
  55.         float lDist = length(vLightVector[i]);
  56.         float attFactor = 1.0 + (0.01 * lDist) + (0.002 * lDist * lDist);
  57.         vec3 lUnitNormal = normalize(vSurfaceNorm);
  58.         vec3 lUnitLightVector = normalize(vLightVector[i]);
  59.         float lNDotl = dot(lUnitNormal, lUnitLightVector);
  60.         float lBrightness = max(lNDotl, 0.0);
  61.         lTotalDiffuse = lTotalDiffuse + (lBrightness * vLightColor[i]) / attFactor;
  62.     }
  63.     lTotalDiffuse = max(lTotalDiffuse, 0.3);
  64.    
  65.     gl_FragColor = vec4(lTotalDiffuse, 1.0) * texelDiffuse0;
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement