PhongShading.fs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. // Created on: 2013-10-10
  2. // Created by: Denis BOGOLEPOV
  3. // Copyright (c) 2013-2014 OPEN CASCADE SAS
  4. //
  5. // This file is part of Open CASCADE Technology software library.
  6. //
  7. // This library is free software; you can redistribute it and/or modify it under
  8. // the terms of the GNU Lesser General Public License version 2.1 as published
  9. // by the Free Software Foundation, with special exception defined in the file
  10. // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
  11. // distribution for complete text of the license and disclaimer of any warranty.
  12. //
  13. // Alternatively, this file may be used under the terms of Open CASCADE
  14. // commercial license or contractual agreement.
  15. varying vec3 View; //!< Direction to the viewer
  16. varying vec3 Normal; //!< Vertex normal in view space
  17. varying vec4 Position; //!< Vertex position in view space.
  18. varying vec4 PositionWorld; //!< Vertex position in world space
  19. vec3 Ambient; //!< Ambient contribution of light sources
  20. vec3 Diffuse; //!< Diffuse contribution of light sources
  21. vec3 Specular; //!< Specular contribution of light sources
  22. //! Computes contribution of isotropic point light source
  23. void pointLight (in int theId,
  24. in vec3 theNormal,
  25. in vec3 theView,
  26. in vec3 thePoint)
  27. {
  28. vec3 aLight = occLight_Position (theId).xyz;
  29. if (!occLight_IsHeadlight (theId))
  30. {
  31. aLight = vec3 (occWorldViewMatrix * vec4 (aLight, 1.0));
  32. }
  33. aLight -= thePoint;
  34. float aDist = length (aLight);
  35. aLight = aLight * (1.0 / aDist);
  36. float anAtten = 1.0 / (occLight_ConstAttenuation (theId)
  37. + occLight_LinearAttenuation (theId) * aDist);
  38. vec3 aHalf = normalize (aLight + theView);
  39. vec3 aFaceSideNormal = gl_FrontFacing ? theNormal : -theNormal;
  40. float aNdotL = max (0.0, dot (aFaceSideNormal, aLight));
  41. float aNdotH = max (0.0, dot (aFaceSideNormal, aHalf ));
  42. float aSpecl = 0.0;
  43. if (aNdotL > 0.0)
  44. {
  45. aSpecl = pow (aNdotH, occMaterial_Shininess (gl_FrontFacing));
  46. }
  47. Diffuse += occLight_Diffuse (theId).rgb * aNdotL * anAtten;
  48. Specular += occLight_Specular (theId).rgb * aSpecl * anAtten;
  49. }
  50. //! Computes contribution of spotlight source
  51. void spotLight (in int theId,
  52. in vec3 theNormal,
  53. in vec3 theView,
  54. in vec3 thePoint)
  55. {
  56. vec3 aLight = occLight_Position (theId).xyz;
  57. vec3 aSpotDir = occLight_SpotDirection (theId).xyz;
  58. if (!occLight_IsHeadlight (theId))
  59. {
  60. aLight = vec3 (occWorldViewMatrix * vec4 (aLight, 1.0));
  61. aSpotDir = vec3 (occWorldViewMatrix * vec4 (aSpotDir, 0.0));
  62. }
  63. aLight -= thePoint;
  64. float aDist = length (aLight);
  65. aLight = aLight * (1.0 / aDist);
  66. aSpotDir = normalize (aSpotDir);
  67. // light cone
  68. float aCosA = dot (aSpotDir, -aLight);
  69. if (aCosA >= 1.0 || aCosA < cos (occLight_SpotCutOff (theId)))
  70. {
  71. return;
  72. }
  73. float anExponent = occLight_SpotExponent (theId);
  74. float anAtten = 1.0 / (occLight_ConstAttenuation (theId)
  75. + occLight_LinearAttenuation (theId) * aDist);
  76. if (anExponent > 0.0)
  77. {
  78. anAtten *= pow (aCosA, anExponent * 128.0);
  79. }
  80. vec3 aHalf = normalize (aLight + theView);
  81. vec3 aFaceSideNormal = gl_FrontFacing ? theNormal : -theNormal;
  82. float aNdotL = max (0.0, dot (aFaceSideNormal, aLight));
  83. float aNdotH = max (0.0, dot (aFaceSideNormal, aHalf ));
  84. float aSpecl = 0.0;
  85. if (aNdotL > 0.0)
  86. {
  87. aSpecl = pow (aNdotH, occMaterial_Shininess (gl_FrontFacing));
  88. }
  89. Diffuse += occLight_Diffuse (theId).rgb * aNdotL * anAtten;
  90. Specular += occLight_Specular (theId).rgb * aSpecl * anAtten;
  91. }
  92. //! Computes contribution of directional light source
  93. void directionalLight (in int theId,
  94. in vec3 theNormal,
  95. in vec3 theView)
  96. {
  97. vec3 aLight = normalize (occLight_Position (theId).xyz);
  98. if (!occLight_IsHeadlight (theId))
  99. {
  100. aLight = vec3 (occWorldViewMatrix * vec4 (aLight, 0.0));
  101. }
  102. vec3 aHalf = normalize (aLight + theView);
  103. vec3 aFaceSideNormal = gl_FrontFacing ? theNormal : -theNormal;
  104. float aNdotL = max (0.0, dot (aFaceSideNormal, aLight));
  105. float aNdotH = max (0.0, dot (aFaceSideNormal, aHalf ));
  106. float aSpecl = 0.0;
  107. if (aNdotL > 0.0)
  108. {
  109. aSpecl = pow (aNdotH, occMaterial_Shininess (gl_FrontFacing));
  110. }
  111. Diffuse += occLight_Diffuse (theId).rgb * aNdotL;
  112. Specular += occLight_Specular (theId).rgb * aSpecl;
  113. }
  114. //! Computes illumination from light sources
  115. vec4 computeLighting (in vec3 theNormal,
  116. in vec3 theView,
  117. in vec4 thePoint)
  118. {
  119. // Clear the light intensity accumulators
  120. Ambient = occLightAmbient.rgb;
  121. Diffuse = vec3 (0.0);
  122. Specular = vec3 (0.0);
  123. vec3 aPoint = thePoint.xyz / thePoint.w;
  124. for (int anIndex = 0; anIndex < occLightSourcesCount; ++anIndex)
  125. {
  126. int aType = occLight_Type (anIndex);
  127. if (aType == OccLightType_Direct)
  128. {
  129. directionalLight (anIndex, theNormal, theView);
  130. }
  131. else if (aType == OccLightType_Point)
  132. {
  133. pointLight (anIndex, theNormal, theView, aPoint);
  134. }
  135. else if (aType == OccLightType_Spot)
  136. {
  137. spotLight (anIndex, theNormal, theView, aPoint);
  138. }
  139. }
  140. vec3 aMatAmbient = occMaterial_Ambient (gl_FrontFacing);
  141. vec4 aMatDiffuse = occMaterial_Diffuse (gl_FrontFacing);
  142. vec3 aMatSpecular = occMaterial_Specular(gl_FrontFacing);
  143. vec3 aMatEmission = occMaterial_Emission(gl_FrontFacing);
  144. vec3 aColor = Ambient * aMatAmbient.rgb
  145. + Diffuse * aMatDiffuse.rgb
  146. + Specular * aMatSpecular.rgb
  147. + aMatEmission.rgb;
  148. return vec4 (aColor, aMatDiffuse.a);
  149. }
  150. //! Entry point to the Fragment Shader
  151. void main()
  152. {
  153. // process clipping planes
  154. for (int anIndex = 0; anIndex < occClipPlaneCount; ++anIndex)
  155. {
  156. vec4 aClipEquation = occClipPlaneEquations[anIndex];
  157. if (dot (aClipEquation.xyz, PositionWorld.xyz / PositionWorld.w) + aClipEquation.w < 0.0)
  158. {
  159. discard;
  160. }
  161. }
  162. vec4 aColor = computeLighting (normalize (Normal), normalize (View), Position);
  163. occSetFragColor (aColor);
  164. }