General Purpose library for Freestanding C++ and POSIX systems
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

174 lines
4.1 KiB

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