General Purpose library for Freestanding C++ and POSIX systems
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

117 rader
2.6 KiB

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