gtc_noise.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. #include <glm/gtc/noise.hpp>
  2. #include <gli/gli.hpp>
  3. #include <gli/gtx/loader.hpp>
  4. std::size_t const Size = 64;
  5. int test_simplex()
  6. {
  7. {
  8. std::vector<glm::byte> ImageData(Size * Size * 3);
  9. for(std::size_t y = 0; y < Size; ++y)
  10. for(std::size_t x = 0; x < Size; ++x)
  11. {
  12. ImageData[(x + y * Size) * 3 + 0] = glm::byte(glm::simplex(glm::vec2(x / 64.f, y / 64.f)) * 128.f + 127.f);
  13. ImageData[(x + y * Size) * 3 + 1] = ImageData[(x + y * Size) * 3 + 0];
  14. ImageData[(x + y * Size) * 3 + 2] = ImageData[(x + y * Size) * 3 + 0];
  15. }
  16. gli::texture2D Texture(1);
  17. Texture[0] = gli::image2D(glm::uvec2(Size), gli::RGB8U);
  18. memcpy(Texture[0].data(), &ImageData[0], ImageData.size());
  19. gli::saveDDS9(Texture, "texture_simplex2d_256.dds");
  20. }
  21. {
  22. std::vector<glm::byte> ImageData(Size * Size * 3);
  23. for(std::size_t y = 0; y < Size; ++y)
  24. for(std::size_t x = 0; x < Size; ++x)
  25. {
  26. ImageData[(x + y * Size) * 3 + 0] = glm::byte(glm::simplex(glm::vec3(x / 64.f, y / 64.f, 0.5f)) * 128.f + 127.f);
  27. ImageData[(x + y * Size) * 3 + 1] = ImageData[(x + y * Size) * 3 + 0];
  28. ImageData[(x + y * Size) * 3 + 2] = ImageData[(x + y * Size) * 3 + 0];
  29. }
  30. gli::texture2D Texture(1);
  31. Texture[0] = gli::image2D(glm::uvec2(Size), gli::RGB8U);
  32. memcpy(Texture[0].data(), &ImageData[0], ImageData.size());
  33. gli::saveDDS9(Texture, "texture_simplex3d_256.dds");
  34. }
  35. {
  36. std::vector<glm::byte> ImageData(Size * Size * 3);
  37. for(std::size_t y = 0; y < Size; ++y)
  38. for(std::size_t x = 0; x < Size; ++x)
  39. {
  40. ImageData[(x + y * Size) * 3 + 0] = glm::byte(glm::simplex(glm::vec4(x / 64.f, y / 64.f, 0.5f, 0.5f)) * 128.f + 127.f);
  41. ImageData[(x + y * Size) * 3 + 1] = ImageData[(x + y * Size) * 3 + 0];
  42. ImageData[(x + y * Size) * 3 + 2] = ImageData[(x + y * Size) * 3 + 0];
  43. }
  44. gli::texture2D Texture(1);
  45. Texture[0] = gli::image2D(glm::uvec2(Size), gli::RGB8U);
  46. memcpy(Texture[0].data(), &ImageData[0], ImageData.size());
  47. gli::saveDDS9(Texture, "texture_simplex4d_256.dds");
  48. }
  49. return 0;
  50. }
  51. int test_perlin()
  52. {
  53. {
  54. std::vector<glm::byte> ImageData(Size * Size * 3);
  55. for(std::size_t y = 0; y < Size; ++y)
  56. for(std::size_t x = 0; x < Size; ++x)
  57. {
  58. ImageData[(x + y * Size) * 3 + 0] = glm::byte(glm::perlin(glm::vec2(x / 64.f, y / 64.f)) * 128.f + 127.f);
  59. ImageData[(x + y * Size) * 3 + 1] = ImageData[(x + y * Size) * 3 + 0];
  60. ImageData[(x + y * Size) * 3 + 2] = ImageData[(x + y * Size) * 3 + 0];
  61. }
  62. gli::texture2D Texture(1);
  63. Texture[0] = gli::image2D(glm::uvec2(Size), gli::RGB8U);
  64. memcpy(Texture[0].data(), &ImageData[0], ImageData.size());
  65. gli::saveDDS9(Texture, "texture_perlin2d_256.dds");
  66. }
  67. {
  68. std::vector<glm::byte> ImageData(Size * Size * 3);
  69. for(std::size_t y = 0; y < Size; ++y)
  70. for(std::size_t x = 0; x < Size; ++x)
  71. {
  72. ImageData[(x + y * Size) * 3 + 0] = glm::byte(glm::perlin(glm::vec3(x / 64.f, y / 64.f, 0.5f)) * 128.f + 127.f);
  73. ImageData[(x + y * Size) * 3 + 1] = ImageData[(x + y * Size) * 3 + 0];
  74. ImageData[(x + y * Size) * 3 + 2] = ImageData[(x + y * Size) * 3 + 0];
  75. }
  76. gli::texture2D Texture(1);
  77. Texture[0] = gli::image2D(glm::uvec2(Size), gli::RGB8U);
  78. memcpy(Texture[0].data(), &ImageData[0], ImageData.size());
  79. gli::saveDDS9(Texture, "texture_perlin3d_256.dds");
  80. }
  81. {
  82. std::vector<glm::byte> ImageData(Size * Size * 3);
  83. for(std::size_t y = 0; y < Size; ++y)
  84. for(std::size_t x = 0; x < Size; ++x)
  85. {
  86. ImageData[(x + y * Size) * 3 + 0] = glm::byte(glm::perlin(glm::vec4(x / 64.f, y / 64.f, 0.5f, 0.5f)) * 128.f + 127.f);
  87. ImageData[(x + y * Size) * 3 + 1] = ImageData[(x + y * Size) * 3 + 0];
  88. ImageData[(x + y * Size) * 3 + 2] = ImageData[(x + y * Size) * 3 + 0];
  89. }
  90. gli::texture2D Texture(1);
  91. Texture[0] = gli::image2D(glm::uvec2(Size), gli::RGB8U);
  92. memcpy(Texture[0].data(), &ImageData[0], ImageData.size());
  93. gli::saveDDS9(Texture, "texture_perlin4d_256.dds");
  94. }
  95. return 0;
  96. }
  97. int test_perlin_pedioric()
  98. {
  99. {
  100. std::vector<glm::byte> ImageData(Size * Size * 3);
  101. for(std::size_t y = 0; y < Size; ++y)
  102. for(std::size_t x = 0; x < Size; ++x)
  103. {
  104. ImageData[(x + y * Size) * 3 + 0] = glm::byte(glm::perlin(glm::vec2(x / 64.f, y / 64.f), glm::vec2(2.0f)) * 128.f + 127.f);
  105. ImageData[(x + y * Size) * 3 + 1] = ImageData[(x + y * Size) * 3 + 0];
  106. ImageData[(x + y * Size) * 3 + 2] = ImageData[(x + y * Size) * 3 + 0];
  107. }
  108. gli::texture2D Texture(1);
  109. Texture[0] = gli::image2D(glm::uvec2(Size), gli::RGB8U);
  110. memcpy(Texture[0].data(), &ImageData[0], ImageData.size());
  111. gli::saveDDS9(Texture, "texture_perlin_pedioric_2d_256.dds");
  112. }
  113. {
  114. std::vector<glm::byte> ImageData(Size * Size * 3);
  115. for(std::size_t y = 0; y < Size; ++y)
  116. for(std::size_t x = 0; x < Size; ++x)
  117. {
  118. ImageData[(x + y * Size) * 3 + 0] = glm::byte(glm::perlin(glm::vec3(x / 64.f, y / 64.f, 0.5f), glm::vec3(2.0f)) * 128.f + 127.f);
  119. ImageData[(x + y * Size) * 3 + 1] = ImageData[(x + y * Size) * 3 + 0];
  120. ImageData[(x + y * Size) * 3 + 2] = ImageData[(x + y * Size) * 3 + 0];
  121. }
  122. gli::texture2D Texture(1);
  123. Texture[0] = gli::image2D(glm::uvec2(Size), gli::RGB8U);
  124. memcpy(Texture[0].data(), &ImageData[0], ImageData.size());
  125. gli::saveDDS9(Texture, "texture_perlin_pedioric_3d_256.dds");
  126. }
  127. {
  128. std::vector<glm::byte> ImageData(Size * Size * 3);
  129. for(std::size_t y = 0; y < Size; ++y)
  130. for(std::size_t x = 0; x < Size; ++x)
  131. {
  132. ImageData[(x + y * Size) * 3 + 0] = glm::byte(glm::perlin(glm::vec4(x / 64.f, y / 64.f, 0.5f, 0.5f), glm::vec4(2.0f)) * 128.f + 127.f);
  133. ImageData[(x + y * Size) * 3 + 1] = ImageData[(x + y * Size) * 3 + 0];
  134. ImageData[(x + y * Size) * 3 + 2] = ImageData[(x + y * Size) * 3 + 0];
  135. }
  136. gli::texture2D Texture(1);
  137. Texture[0] = gli::image2D(glm::uvec2(Size), gli::RGB8U);
  138. memcpy(Texture[0].data(), &ImageData[0], ImageData.size());
  139. gli::saveDDS9(Texture, "texture_perlin_pedioric_4d_256.dds");
  140. }
  141. return 0;
  142. }
  143. int main()
  144. {
  145. int Error = 0;
  146. Error += test_simplex();
  147. Error += test_perlin();
  148. Error += test_perlin_pedioric();
  149. return Error;
  150. }