You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

149 lines
4.3 KiB

  1. //
  2. // Lol Engine — Simplex Noise tutorial
  3. //
  4. // Copyright © 2010—2020 Sam Hocevar <sam@hocevar.net>
  5. // © 2013—2014 Guillaume Bittoun <guillaume.bittoun@gmail.com>
  6. //
  7. // Lol Engine is free software. It comes without any warranty, to
  8. // the extent permitted by applicable law. You can redistribute it
  9. // and/or modify it under the terms of the Do What the Fuck You Want
  10. // to Public License, Version 2, as published by the WTFPL Task Force.
  11. // See http://www.wtfpl.net/ for more details.
  12. //
  13. #if HAVE_CONFIG_H
  14. # include "config.h"
  15. #endif
  16. #include <lol/engine.h>
  17. #include <lol/noise>
  18. using namespace lol;
  19. /* Constants to tweak */
  20. ivec2 const size(1280 * 1, 720 * 1);
  21. float const zoom = 0.03f / 1;
  22. int const octaves = 1;
  23. int main(int argc, char **argv)
  24. {
  25. UNUSED(argc, argv);
  26. srand(time(nullptr));
  27. /* Create an image */
  28. image img(size);
  29. array2d<vec4> &data = img.lock2d<PixelFormat::RGBA_F32>();
  30. /* Declare plenty of allocators */
  31. simplex_noise<2> s2;
  32. simplex_noise<3> s3;
  33. simplex_noise<4> s4;
  34. simplex_noise<5> s5;
  35. simplex_noise<6> s6;
  36. simplex_noise<7> s7;
  37. simplex_noise<8> s8;
  38. simplex_noise<9> s9;
  39. simplex_noise<10> s10;
  40. simplex_noise<11> s11;
  41. simplex_noise<12> s12;
  42. /* Fill image with simplex noise */
  43. for (int j = 0; j < size.y; ++j)
  44. for (int i = 0; i < size.x; ++i)
  45. {
  46. int cell = j / (size.y / 2) * 3 + i / (size.x / 3);
  47. float x = (float)i, y = (float)j;
  48. float sum = 0.f, coeff = 0.f;
  49. int const max_k = 1 << octaves;
  50. bool b_centre = false, b_sphere1 = false, b_sphere2 = false;
  51. for (int k = 1; k < max_k; k *= 2)
  52. {
  53. float t = 0.f;
  54. switch (cell)
  55. {
  56. case 0:
  57. t = s2.eval(zoom * k * vec2(x, y));
  58. break;
  59. case 1:
  60. t = s3.eval(zoom * k * vec3(x, 1.f, y));
  61. break;
  62. case 2:
  63. t = s4.eval(zoom * k * vec4(x, 1.f, y, 2.f));
  64. break;
  65. case 3:
  66. t = s6.eval(zoom * k * vec6(x, 1.f, 2.f, y, 3.f, 4.f));
  67. break;
  68. case 4:
  69. t = s8.eval(zoom * k * vec8(x, 1.f, 2.f, 3.f,
  70. y, 4.f, 5.f, 6.f));
  71. break;
  72. case 5:
  73. //t = s12.eval(zoom * k * vec12(x / 2, -x / 2, y / 2, -y / 2,
  74. // -x / 2, x / 2, -y / 2, y / 2,
  75. // 7.f, 8.f, 9.f, 10.f));
  76. t = s12.eval(zoom * k * vec12(x, 1.f, 2.f, 3.f, 4.f, 5.f,
  77. y, 6.f, 7.f, 8.f, 9.f, 10.f));
  78. break;
  79. default:
  80. break;
  81. }
  82. if (t == -2.f) b_centre = true;
  83. if (t == -3.f) b_sphere1 = true;
  84. if (t == -4.f) b_sphere2 = true;
  85. sum += 1.f / k * t;
  86. coeff += 1.f / k;
  87. }
  88. if (b_centre)
  89. data[i][j] = vec4(1.f, 0.f, 1.f, 1.f);
  90. else if (b_sphere1)
  91. data[i][j] = vec4(0.f, 1.f, 0.f, 1.f);
  92. else if (b_sphere2)
  93. data[i][j] = vec4(0.f, 0.f, 1.f, 1.f);
  94. else
  95. {
  96. float c = saturate(0.5f + 0.5f * sum / coeff);
  97. data[i][j] = vec4(c, c, c, 1.f);
  98. //data[i][j] = Color::HSVToRGB(vec4(c, 1.0f, 0.5f, 1.f));
  99. }
  100. }
  101. #if 0
  102. /* Mark simplex vertices */
  103. vec2 diagonal = normalize(vec2(1.f));
  104. vec2 dx = mat2::rotate(radians(60.f)) * diagonal;
  105. vec2 dy = mat2::rotate(radians(-60.f)) * diagonal;
  106. for (int j = -100; j < 100; ++j)
  107. for (int i = -100; i < 100; ++i)
  108. {
  109. auto putpixel = [&](ivec2 p, vec4 c = vec4(1.f, 0.f, 1.f, 1.f))
  110. {
  111. if (p.x >= 0 && p.x < size.x - 1 && p.y >= 0 && p.y < size.y - 1)
  112. data[p.x][p.y] = c;
  113. };
  114. ivec2 coord = ivec2(i / zoom * dx + j / zoom * dy);
  115. vec2 g = s2.gradient((i + 0.1f) * dx + (j + 0.1f) * dy);
  116. for (int t = 0; t < 40; ++t)
  117. putpixel(coord + (ivec2)(g * (t / 2.f)), vec4(0.f, 1.f, 0.f, 1.f));
  118. putpixel(coord);
  119. putpixel(coord + ivec2(1, 0));
  120. putpixel(coord + ivec2(0, 1));
  121. putpixel(coord + ivec2(1, 1));
  122. }
  123. #endif
  124. /* Save image */
  125. img.unlock2d(data);
  126. img.save("simplex.png");
  127. }