PhoenixMee

Untitled

Apr 11th, 2019
123
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // VERTEX SHADER
  2. #version 330
  3.  
  4. // Matrices
  5. uniform mat4 matrixProjection;
  6. uniform mat4 matrixView;
  7. uniform mat4 matrixModelView;
  8. uniform mat4 matrixViewInvert;
  9.  
  10. // Materials
  11. uniform vec3 materialAmbient;
  12. uniform vec3 materialDiffuse;
  13. uniform float fogThickness;
  14.  
  15. // Water
  16. uniform float waterLevel;
  17. uniform int flipTextures;
  18.  
  19. // Clipping
  20. vec4 clipPlane = vec4(0, 0, 0, 0);
  21. vec4 clipPlane2 = vec4(0, -1, 0, 0);
  22. //uniform vec4 clipPlane;
  23.  
  24. layout (location = 0) in vec3 aVertex;
  25. layout (location = 2) in vec3 aNormal;
  26. layout (location = 3) in vec2 aTexCoord;
  27. layout (location = 4) in vec3 aTangent;
  28. layout (location = 5) in vec3 aBiTangent;
  29.  
  30. out vec4 color;
  31. out vec4 position;
  32. out vec3 normal;
  33. out float fogStrength;
  34. out vec2 texCoord0;
  35. out mat3 matrixTangent;
  36. out float waterDepth;
  37. out vec4 worldposition;
  38.  
  39.  
  40.  
  41. // ambient light declaration
  42. struct AMBIENT
  43. {
  44. int on;
  45. vec3 color;
  46. };
  47. uniform AMBIENT lightAmbient;
  48.  
  49. // directional light declaration
  50. struct DIRECTIONAL
  51. {
  52. int on;
  53. vec3 direction;
  54. vec3 diffuse;
  55. };
  56. uniform DIRECTIONAL lightDir;
  57.  
  58.  
  59. // ambient light function
  60. vec4 AmbientLight(AMBIENT light)
  61. {
  62. return vec4(materialAmbient * light.color, 1);
  63. }
  64.  
  65. // directional light function
  66. vec4 DirectionalLight(DIRECTIONAL light)
  67. {
  68. vec4 color = vec4(0, 0, 0, 0);
  69. vec3 L = normalize(mat3(matrixView) * light.direction);
  70. float NdotL = dot(normal, L);
  71. if (NdotL > 0)
  72. {
  73. color += vec4 (materialDiffuse * light.diffuse, 1) * NdotL;
  74. }
  75. return color;
  76. }
  77.  
  78.  
  79.  
  80. void main(void)
  81. {
  82. // calculate position
  83. position = matrixModelView * vec4(aVertex, 1.0);
  84. gl_Position = matrixProjection * position;
  85. worldposition = position;
  86. worldposition = matrixViewInvert * worldposition;
  87.  
  88. // calculating color
  89. color = vec4 (0, 0, 0, 1);
  90.  
  91. // calculating normals
  92. normal = normalize(mat3(matrixModelView) * aNormal);
  93.  
  94. // calculating tangents for normal maps
  95. vec3 tangent = normalize(mat3(matrixModelView) * aTangent);
  96. vec3 biTangent = normalize(mat3(matrixModelView) * aBiTangent);
  97. matrixTangent = mat3(tangent, biTangent, normal);
  98.  
  99. // calculate the observer's altitude above the observed vertex
  100. float eyeAlt = dot(-position.xyz, mat3(matrixModelView) * vec3(0, 1, 0));
  101.  
  102. // calculating water depth at terrain vertex
  103. if (flipTextures == 0) waterDepth = waterLevel - aVertex.y;
  104. else waterDepth = waterLevel + aVertex.y;
  105.  
  106. // calculating underwater fog
  107. fogStrength = exp2(-fogThickness * length(position) * (max(waterDepth, 0) / eyeAlt));
  108.  
  109. // prepare texture vertexes to be sent to fragment shader
  110. texCoord0 = aTexCoord;
  111.  
  112. // clipping stuff
  113. gl_ClipDistance[0] = dot(worldposition, clipPlane);
  114. gl_ClipDistance[1] = dot(worldposition, clipPlane2);
  115.  
  116. if (lightAmbient.on == 1)
  117. {
  118. color += AmbientLight(lightAmbient);
  119. }
  120.  
  121. if (lightDir.on == 1)
  122. {
  123. color += DirectionalLight(lightDir);
  124. }
  125. }
RAW Paste Data