opengl_fragment.glsl 1.3 KB

123456789101112131415161718192021222324252627282930313233
  1. uniform sampler2D baseTexture;
  2. uniform sampler2D normalTexture;
  3. uniform vec3 yawVec;
  4. void main (void)
  5. {
  6. vec2 uv = gl_TexCoord[0].st;
  7. //texture sampling rate
  8. const float step = 1.0 / 256.0;
  9. float tl = texture2D(normalTexture, vec2(uv.x - step, uv.y + step)).r;
  10. float t = texture2D(normalTexture, vec2(uv.x - step, uv.y - step)).r;
  11. float tr = texture2D(normalTexture, vec2(uv.x + step, uv.y + step)).r;
  12. float r = texture2D(normalTexture, vec2(uv.x + step, uv.y)).r;
  13. float br = texture2D(normalTexture, vec2(uv.x + step, uv.y - step)).r;
  14. float b = texture2D(normalTexture, vec2(uv.x, uv.y - step)).r;
  15. float bl = texture2D(normalTexture, vec2(uv.x - step, uv.y - step)).r;
  16. float l = texture2D(normalTexture, vec2(uv.x - step, uv.y)).r;
  17. float dX = (tr + 2.0 * r + br) - (tl + 2.0 * l + bl);
  18. float dY = (bl + 2.0 * b + br) - (tl + 2.0 * t + tr);
  19. vec4 bump = vec4 (normalize(vec3 (dX, dY, 0.1)),1.0);
  20. float height = 2.0 * texture2D(normalTexture, vec2(uv.x, uv.y)).r - 1.0;
  21. vec4 base = texture2D(baseTexture, uv).rgba;
  22. vec3 L = normalize(vec3(0.0, 0.75, 1.0));
  23. float specular = pow(clamp(dot(reflect(L, bump.xyz), yawVec), 0.0, 1.0), 1.0);
  24. float diffuse = dot(yawVec, bump.xyz);
  25. vec3 color = (1.1 * diffuse + 0.05 * height + 0.5 * specular) * base.rgb;
  26. vec4 col = vec4(color.rgb, base.a);
  27. col *= gl_Color;
  28. gl_FragColor = vec4(col.rgb, base.a);
  29. }