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.
 
 
 

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