General Purpose library for Freestanding C++ and POSIX systems
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

108 рядки
2.6 KiB

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