mandelbrot.frag 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. //! [1]
  2. #version 440
  3. layout(location = 0) in vec2 vTexCoord;
  4. layout(location = 0) out vec4 fragColor;
  5. //! [2]
  6. // uniform block: 84 bytes
  7. layout(std140, binding = 0) uniform buf {
  8. mat4 qt_Matrix; // offset 0
  9. float qt_Opacity; // offset 64
  10. float zoom; // offset 68
  11. vec2 center; // offset 72
  12. int limit; // offset 80
  13. } ubuf;
  14. //! [2]
  15. void main()
  16. {
  17. vec4 color1 = vec4(1.0, 0.85, 0.55, 1);
  18. vec4 color2 = vec4(0.226, 0.0, 0.615, 1);
  19. float aspect_ratio = -ubuf.qt_Matrix[0][0]/ubuf.qt_Matrix[1][1];
  20. vec2 z, c;
  21. c.x = (vTexCoord.x - 0.5) / ubuf.zoom + ubuf.center.x;
  22. c.y = aspect_ratio * (vTexCoord.y - 0.5) / ubuf.zoom + ubuf.center.y;
  23. int i;
  24. z = c;
  25. for (i = 0; i < ubuf.limit; i++) {
  26. float x = (z.x * z.x - z.y * z.y) + c.x;
  27. float y = (z.y * z.x + z.x * z.y) + c.y;
  28. if ((x * x + y * y) > 4.0) break;
  29. z.x = x;
  30. z.y = y;
  31. }
  32. if (i == ubuf.limit) {
  33. fragColor = vec4(0.0, 0.0, 0.0, 1.0);
  34. } else {
  35. float f = (i * 1.0) / ubuf.limit;
  36. fragColor = mix(color1, color2, sqrt(f));
  37. }
  38. }
  39. //! [1]