75 wiersze
2.2 KiB

  1. //
  2. // Simplex Noise Test Program
  3. //
  4. // Copyright (c) 2010-2014 Sam Hocevar <sam@hocevar.net>
  5. // (c) 2013-2014 Guillaume Bittoun <guillaume.bittoun@gmail.com>
  6. // This program is free software; you can redistribute it and/or
  7. // modify it under the terms of the Do What The Fuck You Want To
  8. // Public License, Version 2, as published by Sam Hocevar. See
  9. // http://www.wtfpl.net/ for more details.
  10. //
  11. #if HAVE_CONFIG_H
  12. # include "config.h"
  13. #endif
  14. #include <lol/engine.h>
  15. using namespace lol;
  16. int main(int argc, char **argv)
  17. {
  18. UNUSED(argc, argv);
  19. int const period = 10;
  20. float const zoom = 0.02f;
  21. /* Create some gradients */
  22. array2d<vec2> gradients(vec_t<ptrdiff_t, 2>({period, period}));
  23. for (int i = 0 ; i < period ; ++i)
  24. for (int j = 0 ; j < period ; ++j)
  25. gradients[i][j] = normalize(vec2(rand(-1.f, 1.f), rand(-1.f, 1.f)));
  26. simplex_interpolator<2> s;
  27. s.SetGradients(gradients);
  28. /* Create an image */
  29. ivec2 const size(800, 600);
  30. Image img(size);
  31. array2d<vec4> &data = img.Lock2D<PixelFormat::RGBA_F32>();
  32. /* Fill image with simplex noise */
  33. for (int j = 0; j < size.y; ++j)
  34. for (int i = 0; i < size.x; ++i)
  35. {
  36. float p = 0.5f * s.Interp(zoom * vec2(i, j));
  37. #if 0
  38. for (int k = 2; k < 128; k *= 2)
  39. p += 0.5f / k * s.Interp(zoom * k * vec2(i, j));
  40. #endif
  41. data[i][j] = vec4(saturate(0.5f + p), 0.f, 0.f, 1.f);
  42. }
  43. /* Mark simplex vertices */
  44. vec2 diagonal = normalize(vec2(1.f));
  45. vec2 dx = mat2::rotate(60.f) * diagonal;
  46. vec2 dy = mat2::rotate(-60.f) * diagonal;
  47. for (int j = -100; j < 100; ++j)
  48. for (int i = -100; i < 100; ++i)
  49. {
  50. ivec2 coord = ivec2(i / zoom * dx + j / zoom * dy);
  51. if (coord.x >= 0 && coord.x < size.x - 1
  52. && coord.y >= 0 && coord.y < size.y - 1)
  53. {
  54. data[coord.x][coord.y] = vec4(1.f, 0.f, 1.f, 1.f);
  55. data[coord.x + 1][coord.y] = vec4(1.f, 0.f, 1.f, 1.f);
  56. data[coord.x][coord.y + 1] = vec4(1.f, 0.f, 1.f, 1.f);
  57. data[coord.x + 1][coord.y + 1] = vec4(1.f, 0.f, 1.f, 1.f);
  58. }
  59. }
  60. /* Save image */
  61. img.Unlock2D(data);
  62. img.Save("simplex.png");
  63. }