General Purpose library for Freestanding C++ and POSIX systems
Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.

148 righe
3.2 KiB

  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. renderer a;
  38. a._resolution = vec2{128,64};
  39. a.passes = 5;
  40. a.projection_end = 3;
  41. a.sky_box = [](vec3) -> color_t {
  42. color_t ret;
  43. ret.r() = 0.5;
  44. ret.g() = 0.5;
  45. ret.b() = 1;
  46. ret.a() = 1;
  47. return ret;
  48. };
  49. auto red = a.materials.push(
  50. [&](vec3 p) -> color_t {
  51. color_t ret;
  52. ret.r() = 1;
  53. ret.g() = 0;
  54. ret.b() = 0;
  55. ret.a() = 1;
  56. return ret;
  57. }
  58. );
  59. auto green = a.materials.push(
  60. [&](vec3 p) -> color_t {
  61. color_t ret;
  62. ret.r() = 0;
  63. ret.g() = 1;
  64. ret.b() = 0;
  65. ret.a() = 1;
  66. return ret;
  67. }
  68. );
  69. auto sphere = a.scene_elements.push(
  70. [&](vec3 pos) -> render_point {
  71. auto l_sdf = gp::difference_sdf<float>(
  72. gp::sphere_sdf<float>({0.0,0.0,0.0}, 1.0),
  73. gp::sphere_sdf<float>({-0.75,0.0,0.0}, 1.0)
  74. );
  75. render_point ret;
  76. ret.distance = l_sdf(pos);
  77. ret.material = red;
  78. return ret;
  79. }
  80. );
  81. auto sphere2 = a.scene_elements.push(
  82. [&](vec3 pos) -> render_point {
  83. auto l_sdf_b = gp::sphere_sdf<float>({-0.75,0.0,0.0}, 1.0);
  84. render_point ret;
  85. ret.distance = l_sdf_b(pos);
  86. ret.material = green;
  87. return ret;
  88. }
  89. );
  90. a._camera.position = vec3{0, 0, -2};
  91. a._camera.normal = vec3{0, 0, 1};
  92. using pic_color = gp::vec4_g<uint8_t>;
  93. gp::bmp_viewport<true, pic_color> vp{
  94. {128,64},
  95. [&](gp::vec2_g<int32_t> p) -> pic_color {
  96. auto orig = a.render({(float)p.x,(float)p.y});
  97. pic_color ret{};
  98. ret.x = (uint8_t)(orig.x*255);
  99. ret.y = (uint8_t)(orig.y*255);
  100. ret.z = (uint8_t)(orig.z*255);
  101. ret.w = (uint8_t)(orig.w*255);
  102. return ret;
  103. }
  104. };
  105. gp::array<char, 300000>* buff = new gp::array<char, 300000>();
  106. auto begin = std::chrono::steady_clock::now();
  107. auto r_end = vp.write(buff->as_buffer());
  108. auto end = std::chrono::steady_clock::now();
  109. std::cout << "render time: " << std::chrono::duration_cast<std::chrono::microseconds>(end - begin).count() << std::endl;
  110. auto myfile = std::fstream("render.bmp", std::ios::out | std::ios::binary);
  111. myfile.write(buff->begin().data, r_end - buff->begin());
  112. myfile.close();
  113. delete buff;
  114. //gp_config::assertion(a.render(vec2{64,32}).x == color_t{1.0,0,0,1.0}.x, "red sphere not perceived");
  115. //gp_config::assertion(a.render(vec2{0,0}).x == color_t{0.0,0,1.0,1.0}.x, "blue sky not perceived");
  116. return res;
  117. }
  118. };
  119. append_test dummy_ml8576f(new render_test{});