Advertisement
Guest User

Untitled

a guest
Jul 9th, 2017
219
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. [require]
  2. GLSL >= 1.20
  3.  
  4. [vertex shader]
  5. #version 120
  6. #define LOWPREC
  7. #define MATRIX_VIEW                    0                              
  8. #define MATRIX_PROJECTION              1                              
  9. #define MATRIX_WORLD                   2                              
  10. #define MATRIX_WORLD_VIEW              3                              
  11. #define MATRIX_WORLD_VIEW_PROJECTION   4                              
  12. #define MATRICES_MAX                   5                              
  13.  
  14. uniform mat4 gm_Matrices[MATRICES_MAX];                            
  15.  
  16. uniform bool gm_LightingEnabled;                                       
  17. uniform bool gm_VS_FogEnabled;                                         
  18. uniform float gm_FogStart;                                             
  19. uniform float gm_RcpFogRange;                                          
  20.  
  21. #ifndef MAX_VS_LIGHTS                                                  
  22. #define MAX_VS_LIGHTS   8                                              
  23. #endif                                                                
  24. #define MIRROR_WIN32_LIGHTING_EQUATION                                 
  25.  
  26. struct LightEnv                                                    
  27. {                                                                      
  28.     vec4 ambientcol;                                                   
  29.     vec4 m_dirLights_dir[MAX_VS_LIGHTS];                               
  30.     vec4 m_dirLights_diffusecol[MAX_VS_LIGHTS];                        
  31.     vec4 m_pointLights_posrange[MAX_VS_LIGHTS];                        
  32.     vec4 m_pointLights_diffusecol[MAX_VS_LIGHTS];                      
  33. };                                                                     
  34.  
  35. uniform LightEnv u_LightEnv;                                           
  36.  
  37. float CalcFogFactor(vec4 pos)                                          
  38. {                                                                      
  39. #ifdef USE_FOG                                                         
  40.     {                                                                  
  41.         vec4 viewpos = gm_Matrices[MATRIX_WORLD_VIEW] * pos;                           
  42.         float fogfactor = ((viewpos.z - gm_FogStart) * gm_RcpFogRange);
  43.         return fogfactor;                                              
  44.     }                                                                  
  45. #else // USE_FOG                                                       
  46.     {                                                                  
  47.         return 0.0;                                                    
  48.     }                                                                  
  49. #endif  // USE_FOG                                                     
  50. }                                                                      
  51.  
  52. vec4 DoDirLight(vec3 ws_normal, vec4 dir, vec4 diffusecol)             
  53. {                                                                      
  54.     float dotresult = dot(ws_normal, dir.xyz);                         
  55.     dotresult = max(0.0, dotresult);                                   
  56.  
  57.     return dotresult * diffusecol;                                     
  58. }                                                                      
  59.  
  60. vec4 DoPointLight(vec3 ws_pos, vec3 ws_normal, vec4 posrange, vec4 diffusecol)     
  61. {                                                                      
  62.     vec3 diffvec = ws_pos - posrange.xyz;                              
  63.     float veclen = length(diffvec);                                    
  64.     diffvec /= veclen;  // normalise                                   
  65. #ifdef MIRROR_WIN32_LIGHTING_EQUATION                                  
  66.     // This is based on the Win32 D3D and OpenGL falloff model, where: 
  67.     // Attenuation = 1.0f / (factor0 + (d * factor1) + (d*d * factor2))
  68.     // For some reason, factor0 is set to 0.0f while factor1 is set to 1.0f/lightrange (on both D3D and OpenGL)
  69.     // This'll result in no visible falloff as 1.0f / (d / lightrange) will always be larger than 1.0f (if the vertex is within range)
  70.     float atten = veclen < posrange.w ? 1.0 : 0.0;                     
  71. #else                                                                  
  72.     float atten = clamp( (1.0 - (veclen / posrange.w)), 0.0, 1.0);     
  73. #endif                                                                 
  74.     float dotresult = dot(ws_normal, diffvec);                         
  75.     dotresult = max(0.0, dotresult);                                   
  76.  
  77.     return dotresult * atten * diffusecol;                             
  78. }                                                                      
  79.  
  80. vec4 DoLighting(vec4 vertexcolour, vec4 objectspacepos, vec3 objectspacenormal)
  81. {                                                                      
  82.     {                                                                  
  83.         // Normally we'd have the light positions\directions back-transformed from world to object space
  84.         // But to keep things simple for the moment we'll just transform the normal to world space
  85.        // Also, depending on what world transforms have been set, the sphere of the light radius  
  86.        // may become a non-axis aligned irregularly scaled and sheared ellipsoid in object space
  87.        // which is obviously much harder to test against              
  88.         vec4 objectspacenormal4 = vec4(objectspacenormal, 0.0);        
  89.         vec3 ws_normal;                                                
  90.         ws_normal = (gm_Matrices[MATRIX_WORLD] * objectspacenormal4).xyz;              
  91.         ws_normal = -normalize(ws_normal);                             
  92.  
  93.         vec3 ws_pos;                                                   
  94.         ws_pos = (gm_Matrices[MATRIX_WORLD] * objectspacepos).xyz;                 
  95.  
  96.         // Accumulate lighting from different light types              
  97.         vec4 accumcol = vec4(0.0, 0.0, 0.0, 0.0);                      
  98.         for(int i = 0; i < MAX_VS_LIGHTS; i++)                             
  99.         {                                                              
  100.             accumcol += DoDirLight(ws_normal, u_LightEnv.m_dirLights_dir[i], u_LightEnv.m_dirLights_diffusecol[i]);
  101.         }                                                              
  102.  
  103.         for(int i = 0; i < MAX_VS_LIGHTS; i++)                             
  104.         {                                                              
  105.             accumcol += DoPointLight(ws_pos, ws_normal, u_LightEnv.m_pointLights_posrange[i], u_LightEnv.m_pointLights_diffusecol[i]);
  106.         }                                                              
  107.  
  108.         accumcol *= vertexcolour;                                      
  109.         accumcol += u_LightEnv.ambientcol;                             
  110.         accumcol = min(vec4(1.0, 1.0, 1.0, 1.0), accumcol);            
  111.         return accumcol;                                               
  112.     }                                                                  
  113. }                                                                      
  114. attribute vec3 in_Position;                            
  115. attribute vec2 in_TextureCoord;                            
  116. attribute LOWPREC vec4 in_Colour;                                  
  117. varying vec2 v_vTexcoord;                                  
  118. varying LOWPREC vec4 v_vColour;                                
  119. varying float v_fFog;                                      
  120. void main()                                            
  121. {                                                          
  122.     vec4 objectspacepos = vec4(in_Position, 1.0);          
  123.  
  124.     float fogfactor = CalcFogFactor(objectspacepos);       
  125.  
  126.     vec4 pos = gm_Matrices[MATRIX_WORLD_VIEW_PROJECTION] * objectspacepos;     
  127.     gl_Position = pos;                                     
  128.    gl_PointSize = 1.0;                                    
  129.  
  130.     v_vColour = in_Colour;                                 
  131.  
  132.     v_vTexcoord = in_TextureCoord;                             
  133.     v_fFog = fogfactor;                                    
  134. }                                                          
  135.  
  136. [fragment shader]
  137. #version 120
  138. #define LOWPREC
  139. uniform sampler2D gm_BaseTexture;              
  140.  
  141. uniform bool gm_PS_FogEnabled;                 
  142. uniform LOWPREC vec4 gm_FogColour;            
  143. uniform bool gm_AlphaTestEnabled;              
  144. uniform LOWPREC float gm_AlphaRefValue;    
  145.  
  146. void DoAlphaTest(LOWPREC vec4 SrcColour)       
  147. {                                              
  148. #ifdef USE_ALPHATEST                           
  149.     {                                          
  150.         if (SrcColour.a <= gm_AlphaRefValue)   
  151.         {                                      
  152.             discard;                           
  153.         }                                      
  154.     }                                          
  155. #endif // USE_ALPHATEST                    
  156. }                                              
  157.  
  158. LOWPREC vec4 DoFog(LOWPREC vec4 SrcColour, float fogval)
  159. {                                              
  160.     vec4 retCol;                               
  161. #ifdef USE_FOG                                 
  162.     {                                          
  163.         retCol.rgb = mix(SrcColour.rgb, gm_FogColour.rgb, clamp(fogval, 0.0, 1.0));
  164.         retCol.a = SrcColour.a;                
  165.     }                                          
  166. #else   // USE_FOG                             
  167.     retCol = SrcColour;                        
  168. #endif  // USE_FOG                             
  169.     return retCol;                             
  170. }                                              
  171.  
  172. varying vec2 v_vTexcoord;                                      
  173. varying LOWPREC vec4 v_vColour;                                    
  174. varying float v_fFog;                                          
  175.  
  176. void main()                                                
  177. {                                                              
  178.     LOWPREC vec4 vertColour = v_vColour;                               
  179.     LOWPREC vec4 texelColour = texture2D( gm_BaseTexture, v_vTexcoord );   
  180.     LOWPREC vec4 combinedColour = vertColour * texelColour;            
  181.  
  182.     DoAlphaTest(combinedColour);                               
  183.     combinedColour = DoFog(combinedColour, v_fFog);                            
  184.  
  185.     gl_FragColor = combinedColour;                             
  186. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement