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.

123 rivejä
3.1 KiB

  1. #pragma once
  2. #include "gp_config.hpp"
  3. #include "gp/algorithms/min_of.hpp"
  4. #include "gp/utils/allocators/buddy.hpp"
  5. #include "gp/functional/function.hpp"
  6. #include "gp/containers/indexed_array.hpp"
  7. #include "gp/math.hpp"
  8. #include "gp/math/rendering_math.hpp"
  9. // TODO: Namespace this correctly
  10. namespace gp {
  11. namespace math {
  12. namespace rendering {
  13. using vec2 = gp::math::vec2_g<>;
  14. using vec3 = gp::math::vec3_g<>;
  15. using vec4 = gp::math::vec4_g<>;
  16. struct camera{
  17. vec3 position;
  18. vec3 normal;
  19. };
  20. using index_t = size_t;
  21. using distance_t = gp_config::rendering::default_type;
  22. using color_t = GP_CONFIG__RENDERING__COLOR_T;
  23. struct render_point{
  24. distance_t distance;
  25. index_t material;
  26. bool operator<(const render_point& rhs) {
  27. return distance < rhs.distance;
  28. }
  29. };
  30. using sdf_t = gp::function<render_point(vec3&)>;
  31. using material_t = gp::function<color_t(vec3&)>;
  32. /**
  33. * @brief A pure ray-marching renderer. Prints pixels on order.
  34. */
  35. class renderer {
  36. using g_t = gp_config::rendering::default_type;
  37. constexpr static auto epsilon = gp_config::rendering::epsilon;
  38. public:
  39. gp::indexed_array<sdf_t, 4096> scene_elements;
  40. gp::indexed_array<material_t, 4096> materials;
  41. gp::buddy<> allocator;
  42. material_t sky_box;
  43. vec2 _resolution{128,64};
  44. camera _camera{{0, 0, -5}, {0, 0, 0}};
  45. vec2 _fov{90, 45};
  46. distance_t projection_start = 1;
  47. distance_t projection_end = 50;
  48. size_t passes = 12;
  49. renderer(gp::buffer<char> allocation_buffer)
  50. : allocator(allocation_buffer.begin().data, allocation_buffer.size())
  51. , sky_box{gp::reference_wrapper<gp::buddy<>>{allocator}}
  52. {}
  53. render_point sdf(vec3& render_target) {
  54. return gp::min_of(
  55. scene_elements.begin(),
  56. scene_elements.end(),
  57. [&](auto& p){
  58. return p(render_target);
  59. }
  60. );
  61. }
  62. auto& get_allocator() {
  63. return allocator;
  64. }
  65. color_t render(vec2 pixel) {
  66. g_t depth = projection_start;
  67. vec3 target = _camera.normal;
  68. auto half_res = _resolution/vec2{2.0, 2.0};
  69. pixel = pixel - half_res;
  70. pixel = pixel / half_res;
  71. pixel = pixel * _fov;
  72. pixel = pixel / vec2{180, 180} * vec2{gp::math::pi<g_t>,gp::math::pi<g_t>};
  73. // Y-rot (adjusts x)
  74. target = vec3{
  75. target.x*gp::math::cos(pixel.x) + target.z*gp::math::sin(pixel.x),
  76. target.y,
  77. -target.x*gp::math::sin(pixel.x)+target.z*gp::math::cos(pixel.x)
  78. };
  79. // X-rot (adjusts y)
  80. target = vec3{
  81. target.x,
  82. target.y*gp::math::cos(pixel.y) - target.z*gp::math::sin(pixel.y),
  83. target.y*gp::math::sin(pixel.y) + target.z*gp::math::cos(pixel.y)
  84. };
  85. vec3 render_target{_camera.position};
  86. for(int i = 0; i < passes; ++i) {
  87. render_point distance = sdf(render_target);
  88. if(distance.distance < epsilon) {
  89. return materials[distance.material](render_target);
  90. }
  91. depth += distance.distance;
  92. if(depth >= projection_end) {
  93. break;
  94. }
  95. render_target = _camera.position+depth*target;
  96. }
  97. return sky_box(render_target);
  98. }
  99. };
  100. }
  101. }
  102. }