Guest User

fragment shader

a guest
Aug 11th, 2024
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.04 KB | Source Code | 0 0
  1.  
  2. #version 440 core
  3. struct Material
  4. {
  5. vec3 ambientColor;
  6. float ambientStrength;
  7. vec3 diffuseColor;
  8. vec3 specularColor;
  9. float shininess;
  10. };
  11.  
  12. struct LightSource
  13. {
  14. vec3 position;
  15. vec3 ambientColor;
  16. vec3 diffuseColor;
  17. vec3 specularColor;
  18. float focalStrength;
  19. float specularIntensity;
  20. };
  21.  
  22. #define TOTAL_LIGHTS 4
  23.  
  24. in vec3 fragmentPosition;
  25. in vec3 fragmentVertexNormal;
  26. in vec2 fragmentTextureCoordinate;
  27.  
  28. out vec4 outFragmentColor;
  29.  
  30. uniform bool bUseTexture=false;
  31. uniform bool bUseLighting=false;
  32. uniform vec4 objectColor = vec4(1.0f);
  33. uniform sampler2D objectTexture;
  34. uniform vec3 viewPosition;
  35. uniform vec2 UVscale = vec2(1.0f, 1.0f);
  36. uniform LightSource lightSources[TOTAL_LIGHTS];
  37. uniform Material material;
  38.  
  39. // function prototypes
  40. vec3 CalcLightSource(LightSource light, vec3 lightNormal, vec3 vertexPosition, vec3 viewDirection);
  41.  
  42. void main()
  43. {
  44. if(bUseLighting == true)
  45. {
  46. // properties
  47. vec3 lightNormal = normalize(fragmentVertexNormal);
  48. vec3 viewDirection = normalize(viewPosition - fragmentPosition);
  49. vec3 phongResult = vec3(0.0f);
  50.  
  51. for(int i = 0; i < TOTAL_LIGHTS; i++)
  52. {
  53. phongResult += CalcLightSource(lightSources[i], lightNormal, fragmentPosition, viewDirection);
  54. }
  55.  
  56. if(bUseTexture == true)
  57. {
  58. vec4 textureColor = texture(objectTexture, fragmentTextureCoordinate * UVscale);
  59. outFragmentColor = vec4(phongResult * textureColor.xyz, 1.0);
  60. }
  61. else
  62. {
  63. outFragmentColor = vec4(phongResult * objectColor.xyz, objectColor.w);
  64. }
  65. }
  66. else
  67. {
  68. if(bUseTexture == true)
  69. {
  70. outFragmentColor = texture(objectTexture, fragmentTextureCoordinate * UVscale);
  71. }
  72. else
  73. {
  74. outFragmentColor = objectColor;
  75. }
  76. }
  77. }
  78.  
  79. // calculates the color when using a directional light.
  80. vec3 CalcLightSource(LightSource light, vec3 lightNormal, vec3 vertexPosition, vec3 viewDirection)
  81. {
  82. vec3 ambient;
  83. vec3 diffuse;
  84. vec3 specular;
  85.  
  86. //**Calculate Ambient lighting**
  87.  
  88. ambient = light.ambientColor + (material.ambientColor * material.ambientStrength);
  89.  
  90. //**Calculate Diffuse lighting**
  91.  
  92. // Calculate distance (light direction) between light source and fragments/pixels
  93. vec3 lightDirection = normalize(light.position - vertexPosition);
  94. // Calculate diffuse impact by generating dot product of normal and light
  95. float impact = max(dot(lightNormal, lightDirection), 0.0);
  96. // Generate diffuse material color
  97. diffuse = impact * material.diffuseColor;
  98.  
  99. //**Calculate Specular lighting**
  100.  
  101. // Calculate reflection vector
  102. vec3 reflectDir = reflect(-lightDirection, lightNormal);
  103. // Calculate specular component
  104. float specularComponent = pow(max(dot(viewDirection, reflectDir), 0.0), 32.0); //light.focalStrength);
  105. specular = (light.specularIntensity * material.shininess) * specularComponent * material.specularColor;
  106.  
  107. return(ambient + diffuse + specular);
  108. }
Advertisement
Add Comment
Please, Sign In to add comment