Advertisement
Guest User

Untitled

a guest
Jun 26th, 2020
195
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.06 KB | None | 0 0
  1. #version 150
  2.  
  3. #define MAX_LIGHTS_COUNT 16
  4.  
  5. struct Material {
  6.     int hasNormalMap;
  7.     vec4 ambient;
  8.     vec4 diffuse;
  9.     vec4 specular;
  10.     vec4 emission;
  11.     float reflectance;
  12. };
  13.  
  14. struct DirectionalLight {
  15.     vec4 direction;
  16.     vec4 ambient;
  17.     vec4 diffuse;
  18.     vec4 specular;
  19. };
  20.  
  21. struct Attenuation {
  22.     float constant;
  23.     float linear;
  24.     float exponent;
  25. };
  26.  
  27. struct PointLight {
  28.     vec4 position;
  29.     vec4 ambient;
  30.     vec4 diffuse;
  31.     vec4 specular;
  32.     Attenuation attenuation;
  33. };
  34.  
  35. struct SpotLight {
  36.     vec4 position;
  37.     vec4 ambient;
  38.     vec4 diffuse;
  39.     vec4 specular;
  40.     Attenuation attenuation;
  41.     vec3 direction;
  42.     float cutoff;
  43.     float exponent;
  44. };
  45.  
  46. in vec2 textureCoordinates;
  47. in vec3 vertexNormal;
  48. in vec4 modelWorldPosition;
  49. in vec4 lightViewPosition;
  50.  
  51. in mat4 f_modelViewMatrix;
  52.  
  53. out vec4 fragColor;
  54.  
  55. uniform sampler2D textureSampler;
  56. uniform sampler2D normalMap;
  57.  
  58. uniform int materialExists;
  59. uniform Material material;
  60.  
  61. uniform int directionalLightExists;
  62. uniform DirectionalLight directionalLight;
  63.  
  64. uniform int pointLightsCount;
  65. uniform PointLight pointLights[MAX_LIGHTS_COUNT];
  66.  
  67. uniform int spotLightsCount;
  68. uniform SpotLight spotLights[MAX_LIGHTS_COUNT];
  69.  
  70. uniform mat3 transposedModelMatrix;
  71. uniform vec3 cameraPosition;
  72.  
  73. vec3 normal;
  74. vec3 cameraDirection;
  75.  
  76. vec4 getDirectionalLightColor(DirectionalLight light) {
  77.     vec4 result;
  78.  
  79.     vec3 lightDirection = normalize(vec3(light.direction));
  80.  
  81.     result += material.emission;
  82.  
  83.     result += material.ambient * light.ambient;
  84.  
  85.     float nDotL = max(dot(normal, lightDirection), 0.0f);
  86.     result += material.diffuse * light.diffuse * nDotL;
  87.  
  88.     float rDotVPow = max(pow(dot(reflect(-lightDirection, normal), cameraDirection), material.reflectance), 0.0f);
  89.     result += material.specular * light.specular * rDotVPow;
  90.     return result;
  91. }
  92.  
  93. vec4 getPointLightColor() {
  94.     vec4 result;
  95.     for (int i = 0; i < pointLightsCount; i++) {
  96.         vec4 lightDir = pointLights[i].position - modelWorldPosition;
  97.  
  98.         float distance = length(lightDir);
  99.  
  100.         vec3 lightDirection = normalize(vec3(lightDir));
  101.  
  102.         float att = 1.0f / (pointLights[i].attenuation.constant + pointLights[i].attenuation.linear * distance +
  103.         pointLights[i].attenuation.exponent * distance * distance);
  104.  
  105.         result += material.emission;
  106.  
  107.         result += material.ambient * pointLights[i].ambient * att;
  108.  
  109.         float nDotL = max(dot(normal, lightDirection), 0.0f);
  110.         result += material.diffuse * pointLights[i].diffuse * nDotL * att;
  111.  
  112.         float rDotVPow = max(pow(dot(reflect(-lightDirection, normal), cameraDirection), material.reflectance), 0.0f);
  113.         result += material.specular * pointLights[i].specular * rDotVPow * att;
  114.     }
  115.     return result;
  116. }
  117.  
  118. vec4 getSpotLightColor() {
  119.     vec4 result;
  120.     for (int i = 0; i < spotLightsCount; i++) {
  121.         vec4 lightDir = spotLights[i].position - modelWorldPosition;
  122.  
  123.         float distance = length(lightDir);
  124.  
  125.         vec3 lightDirection = normalize(vec3(lightDir));
  126.  
  127.         float spotEffect = dot(normalize(spotLights[i].direction.xyz), -lightDirection);
  128.         float spot = float(spotEffect > spotLights[i].cutoff);
  129.         spotEffect = max(pow(spotEffect, spotLights[i].exponent), 0.0f);
  130.  
  131.         float att = spot * spotEffect / (spotLights[i].attenuation.constant + spotLights[i].attenuation.linear * distance +
  132.         spotLights[i].attenuation.exponent * distance * distance);
  133.  
  134.         result += material.emission;
  135.  
  136.         result += material.ambient * spotLights[i].ambient * att;
  137.  
  138.         float nDotL = max(dot(normal, lightDirection), 0.0f);
  139.         result += material.diffuse * spotLights[i].diffuse * nDotL * att;
  140.  
  141.         float rDotVPow = max(pow(dot(reflect(-lightDirection, normal), cameraDirection), material.reflectance), 0.0f);
  142.         result += material.specular * spotLights[i].specular * rDotVPow * att;
  143.     }
  144.     return result;
  145. }
  146.  
  147. vec3 calcNormal(Material material, vec3 normal, vec2 text_coord, mat4 modelViewMatrix)
  148. {
  149.     vec3 newNormal = normal;
  150.     if (material.hasNormalMap == 1) {
  151.         newNormal = texture(normalMap, text_coord).rgb;
  152.         newNormal = normalize(newNormal * 2 - 1);
  153.         newNormal = normalize(modelViewMatrix * vec4(newNormal, 0.0)).xyz;
  154.     }
  155.     return newNormal;
  156. }
  157.  
  158. void main() {
  159.     if (materialExists == 0) {
  160.         fragColor = texture(textureSampler, textureCoordinates);
  161.     } else if (materialExists == 1) {
  162.         normal = calcNormal(material, normalize(transposedModelMatrix * vertexNormal), textureCoordinates, f_modelViewMatrix);
  163.         cameraDirection = normalize(cameraPosition - vec3(modelWorldPosition));
  164.         if (directionalLightExists == 1) {
  165.             fragColor += getDirectionalLightColor(directionalLight);
  166.         }
  167.         if (pointLightsCount > 0) {
  168.             fragColor += getPointLightColor();
  169.         }
  170.         if (spotLightsCount > 0) {
  171.             fragColor += getSpotLightColor();
  172.         }
  173.         fragColor *= texture(textureSampler, textureCoordinates);
  174.     }
  175. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement