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.

127 lines
3.1 KiB

пре 4 година
пре 4 година
пре 4 година
пре 4 година
пре 4 година
пре 4 година
пре 4 година
пре 4 година
пре 4 година
  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{1000,500};
  39. a.sky_box = [](vec3) -> color_t {return {0,0,0,0};};
  40. auto v = a.materials.push(
  41. [&](vec3 p) -> color_t {
  42. //return color_t{0,0,1,1};
  43. const float EPSILON = 0.001;
  44. /*vec3(
  45. a.sdf(vec3(p.x + EPSILON, p.y, p.z)).distance - a.sdf(vec3(p.x - EPSILON, p.y, p.z)).distance,
  46. a.sdf(vec3(p.x, p.y + EPSILON, p.z)).distance - a.sdf(vec3(p.x, p.y - EPSILON, p.z)).distance,
  47. a.sdf(vec3(p.x, p.y, p.z + EPSILON)).distance - a.sdf(vec3(p.x, p.y, p.z - EPSILON)).distance
  48. )*/
  49. auto normals = p.normalize();
  50. auto light = vec3(1,1,1).normalize();
  51. auto tmp = light*p;
  52. auto color = tmp.x+tmp.y+tmp.z;
  53. return vec4(vec3(color, color, color), 1.0);
  54. //return {v.normalize(), 1.0};
  55. }
  56. );
  57. auto sphere = a.scene_elements.push(
  58. [=](vec3 pos) -> render_point {
  59. render_point ret;
  60. ret.distance = gp::fixed_sqrt<float, 6>(pos.x*pos.x + pos.y*pos.y + pos.z*pos.z) - 1.0;
  61. ret.material = v;
  62. return ret;
  63. }
  64. );
  65. a._camera.position = vec3{0, 0, -2};
  66. a._camera.normal = vec3{0, 0, 1};
  67. using pic_color = gp::vec4_g<uint8_t>;
  68. gp::bmp_viewport<true, pic_color> vp{
  69. {1000,500},
  70. [&](gp::vec2_g<int32_t> p) -> pic_color {
  71. auto orig = a.render({(float)p.x,(float)p.y});
  72. pic_color ret{};
  73. ret.x = (uint8_t)(orig.x*255);
  74. ret.y = (uint8_t)(orig.y*255);
  75. ret.z = (uint8_t)(orig.z*255);
  76. ret.w = (uint8_t)(orig.w*255);
  77. return ret;
  78. }
  79. };
  80. gp::array<char, 400000000>* buff = new gp::array<char, 400000000>();
  81. auto begin = std::chrono::steady_clock::now();
  82. auto r_end = vp.write(buff->as_buffer());
  83. auto end = std::chrono::steady_clock::now();
  84. std::cout << "render time: " << std::chrono::duration_cast<std::chrono::microseconds>(end - begin).count() << std::endl;
  85. auto myfile = std::fstream("render.bmp", std::ios::out | std::ios::binary);
  86. myfile.write(buff->begin().data, r_end - buff->begin());
  87. myfile.close();
  88. delete buff;
  89. //gp_config::assertion(a.render(vec2{64,32}).x == color_t{1.0,0,0,1.0}.x, "red sphere not perceived");
  90. //gp_config::assertion(a.render(vec2{0,0}).x == color_t{0.0,0,1.0,1.0}.x, "blue sky not perceived");
  91. return res;
  92. }
  93. };
  94. append_test dummy_ml8576f(new render_test{});