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.

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