Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

127 rader
3.6 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. /* Constants to tweak */
  17. float const zoom = 0.03f;
  18. int const octaves = 1;
  19. int main(int argc, char **argv)
  20. {
  21. UNUSED(argc, argv);
  22. /* Create an image */
  23. ivec2 const size(1280, 720);
  24. Image img(size);
  25. array2d<vec4> &data = img.Lock2D<PixelFormat::RGBA_F32>();
  26. /* Declare plenty of allocators */
  27. simplex_interpolator<2> s2;
  28. simplex_interpolator<3> s3;
  29. simplex_interpolator<4> s4;
  30. simplex_interpolator<5> s5;
  31. simplex_interpolator<6> s6;
  32. simplex_interpolator<7> s7;
  33. simplex_interpolator<8> s8;
  34. simplex_interpolator<9> s9;
  35. simplex_interpolator<10> s10;
  36. simplex_interpolator<11> s11;
  37. simplex_interpolator<12> s12;
  38. /* Fill image with simplex noise */
  39. for (int j = 0; j < size.y; ++j)
  40. for (int i = 0; i < size.x; ++i)
  41. {
  42. int cell = j / (size.y / 2) * 3 + i / (size.x / 3);
  43. float x = (float)i, y = (float)j;
  44. float sum = 0.f, coeff = 0.f;
  45. int const max_k = 1 << octaves;
  46. for (int k = 1; k < max_k; k *= 2)
  47. {
  48. float t = 0.f;
  49. switch (cell)
  50. {
  51. case 0:
  52. t = s2.Interp(zoom * k * vec2(x, y));
  53. break;
  54. case 1:
  55. t = s3.Interp(zoom * k * vec3(x, 1.f, y));
  56. break;
  57. case 2:
  58. t = s4.Interp(zoom * k * vec4(x, 1.f, y, 2.f));
  59. break;
  60. case 3:
  61. t = s6.Interp(zoom * k * vec6(x, 1.f, 2.f, y, 3.f, 4.f));
  62. break;
  63. case 4:
  64. t = s8.Interp(zoom * k * vec8(x, 1.f, 2.f, 3.f, y, 4.f,
  65. 5.f, 6.f));
  66. break;
  67. case 5:
  68. t = s12.Interp(zoom * k * vec12(x, 1.f, 2.f, 3.f, 4.f, 5.f,
  69. y, 6.f, 7.f, 8.f, 9.f, 10.f));
  70. break;
  71. default:
  72. break;
  73. }
  74. sum += 1.f / k * t;
  75. coeff += 1.f / k;
  76. }
  77. float c = saturate(0.5f + 0.5f * sum / coeff);
  78. data[i][j] = vec4(c, c, c, 1.f);
  79. //data[i][j] = Color::HSVToRGB(vec4(c, 1.0f, 0.5f, 1.f));
  80. }
  81. #if 0
  82. /* Mark simplex vertices */
  83. vec2 diagonal = normalize(vec2(1.f));
  84. vec2 dx = mat2::rotate(60.f) * diagonal;
  85. vec2 dy = mat2::rotate(-60.f) * diagonal;
  86. for (int j = -100; j < 100; ++j)
  87. for (int i = -100; i < 100; ++i)
  88. {
  89. auto putpixel = [&](ivec2 p, vec4 c = vec4(1.f, 0.f, 1.f, 1.f))
  90. {
  91. if (p.x >= 0 && p.x < size.x - 1 && p.y >= 0 && p.y < size.y - 1)
  92. data[p.x][p.y] = c;
  93. };
  94. ivec2 coord = ivec2(i / zoom * dx + j / zoom * dy);
  95. vec2 g = s2.GetGradient((i + 0.1f) * dx + (j + 0.1f) * dy);
  96. for (int t = 0; t < 40; ++t)
  97. putpixel(coord + (ivec2)(g * (t / 2.f)), vec4(0.f, 1.f, 0.f, 1.f));
  98. putpixel(coord);
  99. putpixel(coord + ivec2(1, 0));
  100. putpixel(coord + ivec2(0, 1));
  101. putpixel(coord + ivec2(1, 1));
  102. }
  103. #endif
  104. /* Save image */
  105. img.Unlock2D(data);
  106. img.Save("simplex.png");
  107. }