gtx_noise.cpp 6.5 KB

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