General Purpose library for Freestanding C++ and POSIX systems
Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

180 linhas
4.3 KiB

há 4 anos
há 4 anos
há 4 anos
há 4 anos
há 4 anos
há 4 anos
há 4 anos
há 4 anos
há 4 anos
há 4 anos
há 4 anos
há 4 anos
  1. #include "test_scaffold.h"
  2. #include "gp/array.hpp"
  3. #include "gp/math.hpp"
  4. #include "gp/rendering/renderer.hpp"
  5. #include "gp/rendering/bmp_viewport.hpp"
  6. #include <chrono>
  7. #include <cmath>
  8. #include <fstream>
  9. #include <iomanip>
  10. #include <iostream>
  11. struct sin_test : public test_scaffold {
  12. sin_test() {
  13. name = __FILE__ ":1";
  14. }
  15. virtual int run() {
  16. int res = 0;
  17. for(float i = 0; i < 100; i += 0.1) {
  18. float v = gp::sin(i);
  19. float ref = sin(i);
  20. res += 0.3 < gp::abs<float>(ref - v)*100.0/(gp::abs(ref+0.00000001));
  21. }
  22. for(float i = 0; i < 100; i += 0.1) {
  23. float v = gp::cos(i);
  24. float ref = cos(i);
  25. res += 0.3 < gp::abs<float>(ref - v)*100.0/(gp::abs(ref+0.00000001));
  26. }
  27. for(float i = 0; i < 100; i += 0.1) {
  28. float v = gp::cos(i)*gp::cos(i) + gp::sin(i)*gp::sin(i);
  29. res += 1 + gp_config::rendering::epsilon < v;
  30. res += 1 - gp_config::rendering::epsilon > v;
  31. }
  32. return res;
  33. }
  34. };
  35. append_test dummy_mldffh6f(new sin_test{});
  36. struct render_test : public test_scaffold {
  37. render_test() {
  38. name = __FILE__ ":2";
  39. }
  40. virtual int run() {
  41. int res = 0;
  42. gp::array<char, 2048> allocation_buffer;
  43. renderer a{allocation_buffer.as_buffer()};
  44. a._resolution = vec2{128,64};
  45. a.passes = 5;
  46. a.projection_end = 3;
  47. a.sky_box = material_t([](vec3) -> color_t {
  48. color_t ret;
  49. ret.r() = 0.5;
  50. ret.g() = 0.5;
  51. ret.b() = 1;
  52. ret.a() = 1;
  53. return ret;
  54. }, a.get_allocator());
  55. auto red = a.materials.push(
  56. material_t([&](vec3 p) -> color_t {
  57. color_t ret;
  58. ret.r() = 1;
  59. ret.g() = 0;
  60. ret.b() = 0;
  61. ret.a() = 1;
  62. return ret;
  63. }, a.get_allocator())
  64. );
  65. auto green = a.materials.push(
  66. material_t([&](vec3 p) -> color_t {
  67. color_t ret;
  68. ret.r() = 0;
  69. ret.g() = 1;
  70. ret.b() = 0;
  71. ret.a() = 1;
  72. return ret;
  73. }, a.get_allocator())
  74. );
  75. auto sphere = a.scene_elements.push(
  76. sdf_t([&](vec3 pos) -> render_point {
  77. auto l_sdf = gp::difference_sdf<float>(
  78. gp::sphere_sdf<float>({0.0,0.0,0.0}, 1.0),
  79. gp::sphere_sdf<float>({-0.75,0.0,0.0}, 1.0)
  80. );
  81. render_point ret;
  82. ret.distance = l_sdf(pos);
  83. ret.material = red;
  84. return ret;
  85. }, a.get_allocator())
  86. );
  87. auto sphere2 = a.scene_elements.push(
  88. sdf_t([&](vec3 pos) -> render_point {
  89. auto l_sdf_b = gp::sphere_sdf<float>({-0.75,0.0,0.0}, 1.0);
  90. render_point ret;
  91. ret.distance = l_sdf_b(pos);
  92. ret.material = green;
  93. return ret;
  94. }, a.get_allocator())
  95. );
  96. a._camera.position = vec3{0, 0, -2};
  97. a._camera.normal = vec3{0, 0, 1};
  98. using pic_color = gp::vec4_g<uint8_t>;
  99. using viewport = gp::bmp_viewport<true, pic_color, gp::buddy<>>;
  100. viewport vp{
  101. {128,64},
  102. viewport::src_t{[&](gp::vec2_g<int32_t> p) -> pic_color {
  103. auto orig = a.render({(float)p.x,(float)p.y});
  104. pic_color ret{};
  105. ret.x = (uint8_t)(orig.x*255);
  106. ret.y = (uint8_t)(orig.y*255);
  107. ret.z = (uint8_t)(orig.z*255);
  108. ret.w = (uint8_t)(orig.w*255);
  109. return ret;
  110. }, a.get_allocator()}
  111. };
  112. gp::array<char, 300000>* buff = new gp::array<char, 300000>();
  113. auto begin = std::chrono::steady_clock::now();
  114. auto r_end = vp.write(buff->as_buffer());
  115. auto end = std::chrono::steady_clock::now();
  116. std::cout << "render time: " << std::chrono::duration_cast<std::chrono::microseconds>(end - begin).count() << std::endl;
  117. auto myfile = std::fstream("render.bmp", std::ios::out | std::ios::binary);
  118. myfile.write(buff->begin().data, r_end - buff->begin());
  119. myfile.close();
  120. delete buff;
  121. //gp_config::assertion(a.render(vec2{64,32}).x == color_t{1.0,0,0,1.0}.x, "red sphere not perceived");
  122. //gp_config::assertion(a.render(vec2{0,0}).x == color_t{0.0,0,1.0,1.0}.x, "blue sky not perceived");
  123. return res;
  124. }
  125. };
  126. append_test dummy_pzj6f(new render_test{});
  127. struct function_test : public test_scaffold {
  128. function_test() {
  129. name = __FILE__ ":3";
  130. }
  131. virtual int run() {
  132. int res = 0;
  133. gp::array<char, 2048> allocation_buffer;
  134. gp::buddy<> allocator{allocation_buffer.begin().data, allocation_buffer.size()};
  135. gp::function<float(vec3), gp::buddy<>> l_sdf_b{gp::sphere_sdf<float>({0.0,0.0,0.0}, 1.0), allocator};
  136. {
  137. gp::function<float(vec3), gp::buddy<>> sdf{l_sdf_b};
  138. gp_config::assertion(l_sdf_b(vec3(0,0,0)) == -1 && sdf(vec3(0,0,0)) == -1, "Bad sdf");
  139. }
  140. return res;
  141. }
  142. };
  143. append_test dummy_ml8576f(new function_test{});