gtc_noise.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. ///////////////////////////////////////////////////////////////////////////////////
  2. /// OpenGL Mathematics (glm.g-truc.net)
  3. ///
  4. /// Copyright (c) 2005 - 2015 G-Truc Creation (www.g-truc.net)
  5. /// Permission is hereby granted, free of charge, to any person obtaining a copy
  6. /// of this software and associated documentation files (the "Software"), to deal
  7. /// in the Software without restriction, including without limitation the rights
  8. /// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. /// copies of the Software, and to permit persons to whom the Software is
  10. /// furnished to do so, subject to the following conditions:
  11. ///
  12. /// The above copyright notice and this permission notice shall be included in
  13. /// all copies or substantial portions of the Software.
  14. ///
  15. /// Restrictions:
  16. /// By making use of the Software for military purposes, you choose to make
  17. /// a Bunny unhappy.
  18. ///
  19. /// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. /// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. /// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. /// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. /// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. /// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. /// THE SOFTWARE.
  26. ///
  27. /// @file test/gtc/gtc_noise.cpp
  28. /// @date 2011-04-21 / 2014-11-25
  29. /// @author Christophe Riccio
  30. ///////////////////////////////////////////////////////////////////////////////////
  31. #include <glm/gtc/noise.hpp>
  32. #include <gli/gli.hpp>
  33. #include <gli/gtx/loader.hpp>
  34. int test_simplex()
  35. {
  36. std::size_t const Size = 256;
  37. {
  38. std::vector<glm::byte> ImageData(Size * Size * 3);
  39. for(std::size_t y = 0; y < Size; ++y)
  40. for(std::size_t x = 0; x < Size; ++x)
  41. {
  42. ImageData[(x + y * Size) * 3 + 0] = glm::byte(glm::simplex(glm::vec2(x / 64.f, y / 64.f)) * 128.f + 127.f);
  43. ImageData[(x + y * Size) * 3 + 1] = ImageData[(x + y * Size) * 3 + 0];
  44. ImageData[(x + y * Size) * 3 + 2] = ImageData[(x + y * Size) * 3 + 0];
  45. }
  46. gli::texture2D Texture(1);
  47. Texture[0] = gli::image2D(glm::uvec2(Size), gli::RGB8U);
  48. memcpy(Texture[0].data(), &ImageData[0], ImageData.size());
  49. gli::saveDDS9(Texture, "texture_simplex2d_256.dds");
  50. }
  51. {
  52. std::vector<glm::byte> ImageData(Size * Size * 3);
  53. for(std::size_t y = 0; y < Size; ++y)
  54. for(std::size_t x = 0; x < Size; ++x)
  55. {
  56. ImageData[(x + y * Size) * 3 + 0] = glm::byte(glm::simplex(glm::vec3(x / 64.f, y / 64.f, 0.5f)) * 128.f + 127.f);
  57. ImageData[(x + y * Size) * 3 + 1] = ImageData[(x + y * Size) * 3 + 0];
  58. ImageData[(x + y * Size) * 3 + 2] = ImageData[(x + y * Size) * 3 + 0];
  59. }
  60. gli::texture2D Texture(1);
  61. Texture[0] = gli::image2D(glm::uvec2(Size), gli::RGB8U);
  62. memcpy(Texture[0].data(), &ImageData[0], ImageData.size());
  63. gli::saveDDS9(Texture, "texture_simplex3d_256.dds");
  64. }
  65. {
  66. std::vector<glm::byte> ImageData(Size * Size * 3);
  67. for(std::size_t y = 0; y < Size; ++y)
  68. for(std::size_t x = 0; x < Size; ++x)
  69. {
  70. 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);
  71. ImageData[(x + y * Size) * 3 + 1] = ImageData[(x + y * Size) * 3 + 0];
  72. ImageData[(x + y * Size) * 3 + 2] = ImageData[(x + y * Size) * 3 + 0];
  73. }
  74. gli::texture2D Texture(1);
  75. Texture[0] = gli::image2D(glm::uvec2(Size), gli::RGB8U);
  76. memcpy(Texture[0].data(), &ImageData[0], ImageData.size());
  77. gli::saveDDS9(Texture, "texture_simplex4d_256.dds");
  78. }
  79. return 0;
  80. }
  81. int test_perlin()
  82. {
  83. std::size_t const Size = 256;
  84. {
  85. std::vector<glm::byte> ImageData(Size * Size * 3);
  86. for(std::size_t y = 0; y < Size; ++y)
  87. for(std::size_t x = 0; x < Size; ++x)
  88. {
  89. ImageData[(x + y * Size) * 3 + 0] = glm::byte(glm::perlin(glm::vec2(x / 64.f, y / 64.f)) * 128.f + 127.f);
  90. ImageData[(x + y * Size) * 3 + 1] = ImageData[(x + y * Size) * 3 + 0];
  91. ImageData[(x + y * Size) * 3 + 2] = ImageData[(x + y * Size) * 3 + 0];
  92. }
  93. gli::texture2D Texture(1);
  94. Texture[0] = gli::image2D(glm::uvec2(Size), gli::RGB8U);
  95. memcpy(Texture[0].data(), &ImageData[0], ImageData.size());
  96. gli::saveDDS9(Texture, "texture_perlin2d_256.dds");
  97. }
  98. {
  99. std::vector<glm::byte> ImageData(Size * Size * 3);
  100. for(std::size_t y = 0; y < Size; ++y)
  101. for(std::size_t x = 0; x < Size; ++x)
  102. {
  103. ImageData[(x + y * Size) * 3 + 0] = glm::byte(glm::perlin(glm::vec3(x / 64.f, y / 64.f, 0.5f)) * 128.f + 127.f);
  104. ImageData[(x + y * Size) * 3 + 1] = ImageData[(x + y * Size) * 3 + 0];
  105. ImageData[(x + y * Size) * 3 + 2] = ImageData[(x + y * Size) * 3 + 0];
  106. }
  107. gli::texture2D Texture(1);
  108. Texture[0] = gli::image2D(glm::uvec2(Size), gli::RGB8U);
  109. memcpy(Texture[0].data(), &ImageData[0], ImageData.size());
  110. gli::saveDDS9(Texture, "texture_perlin3d_256.dds");
  111. }
  112. {
  113. std::vector<glm::byte> ImageData(Size * Size * 3);
  114. for(std::size_t y = 0; y < Size; ++y)
  115. for(std::size_t x = 0; x < Size; ++x)
  116. {
  117. 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);
  118. ImageData[(x + y * Size) * 3 + 1] = ImageData[(x + y * Size) * 3 + 0];
  119. ImageData[(x + y * Size) * 3 + 2] = ImageData[(x + y * Size) * 3 + 0];
  120. }
  121. gli::texture2D Texture(1);
  122. Texture[0] = gli::image2D(glm::uvec2(Size), gli::RGB8U);
  123. memcpy(Texture[0].data(), &ImageData[0], ImageData.size());
  124. gli::saveDDS9(Texture, "texture_perlin4d_256.dds");
  125. }
  126. return 0;
  127. }
  128. int test_perlin_pedioric()
  129. {
  130. std::size_t const Size = 256;
  131. {
  132. std::vector<glm::byte> ImageData(Size * Size * 3);
  133. for(std::size_t y = 0; y < Size; ++y)
  134. for(std::size_t x = 0; x < Size; ++x)
  135. {
  136. 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);
  137. ImageData[(x + y * Size) * 3 + 1] = ImageData[(x + y * Size) * 3 + 0];
  138. ImageData[(x + y * Size) * 3 + 2] = ImageData[(x + y * Size) * 3 + 0];
  139. }
  140. gli::texture2D Texture(1);
  141. Texture[0] = gli::image2D(glm::uvec2(Size), gli::RGB8U);
  142. memcpy(Texture[0].data(), &ImageData[0], ImageData.size());
  143. gli::saveDDS9(Texture, "texture_perlin_pedioric_2d_256.dds");
  144. }
  145. {
  146. std::vector<glm::byte> ImageData(Size * Size * 3);
  147. for(std::size_t y = 0; y < Size; ++y)
  148. for(std::size_t x = 0; x < Size; ++x)
  149. {
  150. 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);
  151. ImageData[(x + y * Size) * 3 + 1] = ImageData[(x + y * Size) * 3 + 0];
  152. ImageData[(x + y * Size) * 3 + 2] = ImageData[(x + y * Size) * 3 + 0];
  153. }
  154. gli::texture2D Texture(1);
  155. Texture[0] = gli::image2D(glm::uvec2(Size), gli::RGB8U);
  156. memcpy(Texture[0].data(), &ImageData[0], ImageData.size());
  157. gli::saveDDS9(Texture, "texture_perlin_pedioric_3d_256.dds");
  158. }
  159. {
  160. std::vector<glm::byte> ImageData(Size * Size * 3);
  161. for(std::size_t y = 0; y < Size; ++y)
  162. for(std::size_t x = 0; x < Size; ++x)
  163. {
  164. 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);
  165. ImageData[(x + y * Size) * 3 + 1] = ImageData[(x + y * Size) * 3 + 0];
  166. ImageData[(x + y * Size) * 3 + 2] = ImageData[(x + y * Size) * 3 + 0];
  167. }
  168. gli::texture2D Texture(1);
  169. Texture[0] = gli::image2D(glm::uvec2(Size), gli::RGB8U);
  170. memcpy(Texture[0].data(), &ImageData[0], ImageData.size());
  171. gli::saveDDS9(Texture, "texture_perlin_pedioric_4d_256.dds");
  172. }
  173. return 0;
  174. }
  175. int main()
  176. {
  177. int Error = 0;
  178. Error += test_simplex();
  179. Error += test_perlin();
  180. Error += test_perlin_pedioric();
  181. return Error;
  182. }