Want more features on Pastebin? Sign Up, it's FREE!
Guest

Untitled

By: a guest on Oct 10th, 2012  |  syntax: OpenGL Shading  |  size: 1.41 KB  |  views: 132  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. ------------
  2. VERTEX PROGRAM:
  3. ------------
  4.  
  5. attribute vec4 a_position;
  6. attribute vec4 a_color;
  7. attribute vec2 a_texCoord0;
  8. uniform mat4 u_proj;
  9. uniform mat4 u_trans;
  10. uniform mat4 u_projTrans;
  11. varying vec4 v_color;
  12. varying vec2 v_texCoords;
  13.  
  14. void main()
  15. {
  16.    v_color = a_color;
  17.    v_texCoords = a_texCoord0;
  18.    gl_Position =  u_projTrans * a_position;
  19. }
  20.  
  21.  
  22.  
  23. ------------
  24. FRAGMENT PROGRAM:
  25. ------------
  26.  
  27. #ifdef GL_ES
  28. precision mediump float;
  29. #endif
  30. varying vec4 v_color;
  31. varying vec2 v_texCoords;
  32. uniform sampler2D u_texture;
  33. uniform sampler2D u_normals;
  34. uniform vec3 light;
  35. uniform vec3 ambientColor;
  36. uniform float ambientIntensity;
  37. uniform vec2 resolution;
  38. uniform vec3 lightColor;
  39. uniform bool useNormals;
  40. uniform bool useShadow;
  41. uniform vec3 attenuation;
  42.  
  43. void main() {
  44.         vec4 color = texture2D(u_texture, v_texCoords.st);
  45.         vec3 normal = normalize(texture2D(u_normals, v_texCoords.st).rgb * 2.0 - 1.0);
  46.         vec3 light_pos = normalize(light);
  47.         float lambert = useNormals ? max(dot(normal, light_pos), 0.0) : 1.0;
  48.        
  49.         //now let's get a nice little falloff
  50.         float d = distance(gl_FragCoord.xy, light.xy * resolution);
  51.         d *= light.z;
  52.        
  53.         float att = useShadow ? 1.0 / ( attenuation.x + (attenuation.y*d) + (attenuation.z*d*d) ) : 1.0;
  54.        
  55.         vec3 result = (ambientColor * ambientIntensity) + (lightColor.rgb * lambert) * att;
  56.         result *= color.rgb;
  57.        
  58.         gl_FragColor = v_color * vec4(result, color.a);
  59. }
clone this paste RAW Paste Data