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.

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