gtc_noise.cpp 6.5 KB

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