Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

148 lignes
4.3 KiB

  1. //
  2. // Lol Engine — Simplex Noise tutorial
  3. //
  4. // Copyright © 2010—2017 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. using namespace lol;
  18. /* Constants to tweak */
  19. ivec2 const size(1280 * 1, 720 * 1);
  20. float const zoom = 0.03f / 1;
  21. int const octaves = 1;
  22. int main(int argc, char **argv)
  23. {
  24. UNUSED(argc, argv);
  25. srand(time(nullptr));
  26. /* Create an image */
  27. image img(size);
  28. array2d<vec4> &data = img.lock2d<PixelFormat::RGBA_F32>();
  29. /* Declare plenty of allocators */
  30. simplex_noise<2> s2;
  31. simplex_noise<3> s3;
  32. simplex_noise<4> s4;
  33. simplex_noise<5> s5;
  34. simplex_noise<6> s6;
  35. simplex_noise<7> s7;
  36. simplex_noise<8> s8;
  37. simplex_noise<9> s9;
  38. simplex_noise<10> s10;
  39. simplex_noise<11> s11;
  40. simplex_noise<12> s12;
  41. /* Fill image with simplex noise */
  42. for (int j = 0; j < size.y; ++j)
  43. for (int i = 0; i < size.x; ++i)
  44. {
  45. int cell = j / (size.y / 2) * 3 + i / (size.x / 3);
  46. float x = (float)i, y = (float)j;
  47. float sum = 0.f, coeff = 0.f;
  48. int const max_k = 1 << octaves;
  49. bool b_centre = false, b_sphere1 = false, b_sphere2 = false;
  50. for (int k = 1; k < max_k; k *= 2)
  51. {
  52. float t = 0.f;
  53. switch (cell)
  54. {
  55. case 0:
  56. t = s2.eval(zoom * k * vec2(x, y));
  57. break;
  58. case 1:
  59. t = s3.eval(zoom * k * vec3(x, 1.f, y));
  60. break;
  61. case 2:
  62. t = s4.eval(zoom * k * vec4(x, 1.f, y, 2.f));
  63. break;
  64. case 3:
  65. t = s6.eval(zoom * k * vec6(x, 1.f, 2.f, y, 3.f, 4.f));
  66. break;
  67. case 4:
  68. t = s8.eval(zoom * k * vec8(x, 1.f, 2.f, 3.f,
  69. y, 4.f, 5.f, 6.f));
  70. break;
  71. case 5:
  72. //t = s12.eval(zoom * k * vec12(x / 2, -x / 2, y / 2, -y / 2,
  73. // -x / 2, x / 2, -y / 2, y / 2,
  74. // 7.f, 8.f, 9.f, 10.f));
  75. t = s12.eval(zoom * k * vec12(x, 1.f, 2.f, 3.f, 4.f, 5.f,
  76. y, 6.f, 7.f, 8.f, 9.f, 10.f));
  77. break;
  78. default:
  79. break;
  80. }
  81. if (t == -2.f) b_centre = true;
  82. if (t == -3.f) b_sphere1 = true;
  83. if (t == -4.f) b_sphere2 = true;
  84. sum += 1.f / k * t;
  85. coeff += 1.f / k;
  86. }
  87. if (b_centre)
  88. data[i][j] = vec4(1.f, 0.f, 1.f, 1.f);
  89. else if (b_sphere1)
  90. data[i][j] = vec4(0.f, 1.f, 0.f, 1.f);
  91. else if (b_sphere2)
  92. data[i][j] = vec4(0.f, 0.f, 1.f, 1.f);
  93. else
  94. {
  95. float c = saturate(0.5f + 0.5f * sum / coeff);
  96. data[i][j] = vec4(c, c, c, 1.f);
  97. //data[i][j] = Color::HSVToRGB(vec4(c, 1.0f, 0.5f, 1.f));
  98. }
  99. }
  100. #if 0
  101. /* Mark simplex vertices */
  102. vec2 diagonal = normalize(vec2(1.f));
  103. vec2 dx = mat2::rotate(radians(60.f)) * diagonal;
  104. vec2 dy = mat2::rotate(radians(-60.f)) * diagonal;
  105. for (int j = -100; j < 100; ++j)
  106. for (int i = -100; i < 100; ++i)
  107. {
  108. auto putpixel = [&](ivec2 p, vec4 c = vec4(1.f, 0.f, 1.f, 1.f))
  109. {
  110. if (p.x >= 0 && p.x < size.x - 1 && p.y >= 0 && p.y < size.y - 1)
  111. data[p.x][p.y] = c;
  112. };
  113. ivec2 coord = ivec2(i / zoom * dx + j / zoom * dy);
  114. vec2 g = s2.gradient((i + 0.1f) * dx + (j + 0.1f) * dy);
  115. for (int t = 0; t < 40; ++t)
  116. putpixel(coord + (ivec2)(g * (t / 2.f)), vec4(0.f, 1.f, 0.f, 1.f));
  117. putpixel(coord);
  118. putpixel(coord + ivec2(1, 0));
  119. putpixel(coord + ivec2(0, 1));
  120. putpixel(coord + ivec2(1, 1));
  121. }
  122. #endif
  123. /* Save image */
  124. img.unlock2d(data);
  125. img.save("simplex.png");
  126. }