SHARE
TWEET

Untitled

PhoenixMee Apr 11th, 2019 75 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 matrixShadow;
  9.  
  10.  
  11. // Materials
  12. uniform vec3 materialAmbient;
  13. uniform vec3 materialDiffuse;
  14. uniform float fogThickness;
  15.  
  16.  
  17. layout (location = 0) in vec3 aVertex;
  18. layout (location = 2) in vec3 aNormal;
  19. layout (location = 3) in vec2 aTexCoord;
  20. layout (location = 4) in vec3 aTangent;
  21. layout (location = 5) in vec3 aBiTangent;
  22.  
  23.  
  24. out vec4 color;
  25. out vec4 position;
  26. out vec3 normal;
  27. out mat3 matrixTangent;
  28. out float fogStrength;
  29. out vec2 texCoord0;
  30. out vec3 texCoordCubeMap;
  31. out vec4 shadowCoord;
  32.  
  33.  
  34. // ambient light declaration
  35. struct AMBIENT
  36. {
  37.     int on;
  38.     vec3 color;
  39. };
  40. uniform AMBIENT lightAmbient;
  41.  
  42. // directional light declaration
  43. struct DIRECTIONAL
  44. {
  45.     int on;
  46.     vec3 direction;
  47.     vec3 diffuse;
  48. };
  49. uniform DIRECTIONAL lightDir;
  50.  
  51.  
  52. // ambient light function
  53. vec4 AmbientLight(AMBIENT light)
  54. {
  55.     return vec4(materialAmbient * light.color, 1);
  56. }
  57.  
  58. // directional light function
  59. vec4 DirectionalLight(DIRECTIONAL light)
  60. {
  61.     vec4 color = vec4(0, 0, 0, 0);
  62.     vec3 L = normalize(mat3(matrixView) * light.direction);
  63.     float NdotL = dot(normal, L);
  64.     if (NdotL > 0)
  65.     {
  66.         color += vec4 (materialDiffuse * light.diffuse, 1) * NdotL;
  67.     }
  68.     return color;
  69. }
  70.  
  71.  
  72.  
  73. void main(void)
  74. {
  75.     // calculate position
  76.     position = matrixModelView * vec4(aVertex, 1.0);
  77.     gl_Position = matrixProjection * position;
  78.  
  79.     // calculating color
  80.     color = vec4 (0, 0, 0, 1);
  81.  
  82.     // calculating normals
  83.     normal = normalize(mat3(matrixModelView) * aNormal);
  84.  
  85.     // calculating tangents for normal maps
  86.     vec3 tangent = normalize(mat3(matrixModelView) * aTangent);
  87.     vec3 biTangent = normalize(mat3(matrixModelView) * aBiTangent);
  88.     matrixTangent = mat3(tangent, biTangent, normal);
  89.  
  90.     // calculating fog
  91.     fogStrength = exp2(-fogThickness * length(position));
  92.  
  93.     // prepare texture vertexes to be sent to fragment shader
  94.     texCoord0 = aTexCoord;
  95.  
  96.     // calculating cubemap reflection
  97.     texCoordCubeMap = inverse(mat3(matrixView)) * mix(reflect(position.xyz, normal), normal.xyz, 0.5);
  98.  
  99.     // calculate shadow coordinate–using the Shadow Matrix
  100.     mat4 matrixModel = inverse(matrixView) * matrixModelView;
  101.     shadowCoord = matrixShadow * matrixModel * vec4(aVertex + aNormal * 0.1, 1);
  102.  
  103.     if (lightAmbient.on == 1) color += AmbientLight(lightAmbient);
  104.  
  105.     if (lightDir.on == 1) color += DirectionalLight(lightDir);
  106. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top