Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2019
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #version 430 core
  2.  
  3. #pragma optimize(on)
  4. #pragma debug(off)
  5.  
  6. const int MaxLights = 8;
  7.  
  8. // Structure for holding general light properties
  9. struct GeneralLight
  10. {
  11.  
  12.     vec4 ambientColor;      // ambient color of the light
  13.     vec4 diffuseColor;      // diffuse color of the light
  14.     vec4 specularColor;     // specular color of the light
  15.  
  16.                             // Either the position or direction
  17.                             // if w = 0 then the light is directional and direction specified
  18.                             // is the negative of the direction the light is shinning
  19.                             // if w = 1 then the light is positional
  20.     vec4 positionOrDirection;
  21.  
  22.     // Spotlight attributes
  23.     vec3 spotDirection;     // the direction the cone of light is shinning    
  24.     bool isSpot;                // 1 if the light is a spotlight  
  25.     float spotCutoffCos;    // Cosine of the spot cutoff angle
  26.     float spotExponent;     // For gradual falloff near cone edge
  27.  
  28.                             // Attenuation coefficients
  29.     float constant;
  30.     float linear;
  31.     float quadratic;
  32.  
  33.     bool enabled;           // true if light is "on"
  34.  
  35. };
  36.  
  37. layout(shared) uniform LightBlock
  38. {
  39.     GeneralLight lights[MaxLights];
  40. };
  41.  
  42.  
  43. struct Material
  44. {
  45.     vec4 ambientMat;
  46.     vec4 diffuseMat;
  47.     vec4 specularMat;
  48.     vec4 emmissiveMat;
  49.     float specularExp;
  50.     int textureMode;
  51.     bool diffuseTextureEnabled;
  52.     bool specularTextureEnabled;
  53.     bool normalMapEnabled;
  54.     bool bumpMapEnabled;
  55. };
  56.  
  57. layout(shared) uniform MaterialBlock
  58. {
  59.     Material object;
  60. };
  61.  
  62. layout(shared) uniform worldEyeBlock
  63. {
  64.     vec3 worldEyePosition;
  65. };
  66.  
  67. layout(location = 100) uniform sampler2D diffuseSampler;
  68. layout(location = 101) uniform sampler2D specularSampler;
  69.  
  70. in vec3 vertexWorldPosition;
  71. in vec3 vertexWorldNormal;
  72. in vec2 TexCoord;
  73.  
  74. out vec4 fragmentColor;
  75.  
  76. vec3 fragmentWorldNormal;
  77.  
  78. vec3 shadingCaculation(GeneralLight light, Material mat) {
  79.     vec3 totalLight;
  80.     vec3 lightVector = light.positionOrDirection.xyz;
  81.     vec3 eyeVector = worldEyePosition - vertexWorldPosition;
  82.     float falloffFactor = 1;
  83.     float attenuationCoffecient = 1 / (light.constant + light.linear * length(eyeVector) + light.quadratic * length(normalize(eyeVector * eyeVector)));
  84.  
  85.     totalLight = vec3(mat.emmissiveMat);
  86.     totalLight += light.ambientColor * mat.ambientMat;      //Ambient
  87.    
  88.     if (light.isSpot) {
  89.         falloffFactor = pow(light.spotCutoffCos, light.spotExponent);
  90.     }
  91.     totalLight += falloffFactor * (max(0, dot(fragmentWorldNormal, lightVector)) * light.diffuseColor * mat.diffuseMat +
  92.                 pow(max(0, dot(fragmentWorldNormal, (lightVector + eyeVector) / 2)), mat.specularExp) * light.specularColor * mat.specularMat); //Specular
  93.    
  94.  
  95.     return totalLight;
  96. }
  97.  
  98. void main()
  99. {
  100.     //fragmentColor = texture(diffuseSampler, TexCoord.st);
  101.    
  102.     Material material = object;
  103.     // Substitute diffuse texture for ambient and diffuse material properties
  104.     if (material.diffuseTextureEnabled == true && material.textureMode != 0) {
  105.  
  106.         material.diffuseMat = texture(diffuseSampler, TexCoord.st);
  107.         material.ambientMat = material.diffuseMat;
  108.     }
  109.  
  110.     // Substitute specular texture for specular material properties
  111.     if (material.specularTextureEnabled == true && material.textureMode != 0) {
  112.  
  113.         material.specularMat = texture(specularSampler, TexCoord.st);
  114.     }
  115.  
  116.     // Check if shading calculations should be performed
  117.     if (material.textureMode == 2 || material.textureMode == 0) {
  118.  
  119.         fragmentColor = material.emmissiveMat;
  120.  
  121.         for (int i = 0; i < MaxLights; i++) {
  122.  
  123.             fragmentColor += vec4(shadingCaculation(lights[i], material), 1.0);
  124.         }
  125.  
  126.     }
  127.     else if (material.textureMode == 1) { // No shading calculations
  128.  
  129.         fragmentColor = texture(diffuseSampler, TexCoord.st);
  130.     }
  131.  
  132. } // main
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement