MOS
renderer.hpp
1 #pragma once
2 #include <glad/glad.h>
3 #include <optional>
4 #include <initializer_list>
5 #include <unordered_map>
6 #include <array>
7 #include <future>
8 #include <glm/glm.hpp>
9 #include <mos/gfx/models.hpp>
10 #include <mos/gfx/scenes.hpp>
11 
12 #include <mos/gl/buffer.hpp>
13 #include <mos/gl/render_buffers.hpp>
14 #include <mos/gl/vertex_arrays.hpp>
15 #include <mos/gl/texture_buffers.hpp>
16 #include <mos/gl/frame_buffers.hpp>
17 #include <mos/gl/array_buffers.hpp>
18 #include <mos/gl/element_array_buffers.hpp>
19 
20 #include <mos/gl/light_uniforms.hpp> //TODO: remove
21 
22 #include <mos/gl/cloud_program.hpp>
23 #include <mos/gl/bloom_program.hpp>
24 #include <mos/gl/compositing_program.hpp>
25 #include <mos/gl/blur_program.hpp>
26 #include <mos/gl/depth_program.hpp>
27 #include <mos/gl/standard_program.hpp>
28 
29 #include <mos/gl/standard_target.hpp>
30 #include <mos/gl/blit_target.hpp>
31 #include <mos/gl/post_target.hpp>
32 #include <mos/gl/shadow_map_target.hpp>
33 #include <mos/gl/environment_map_target.hpp>
34 
35 #include <mos/gl/quad.hpp>
36 
37 namespace mos::gl {
38 
40 class Renderer final {
41 public:
43  explicit Renderer(const glm::ivec2 &resolution, const int samples = 1);
44  Renderer(const Renderer &renderer) = delete;
45  Renderer(const Renderer &&renderer) = delete;
46  Renderer & operator=(const Renderer & renderer) = delete;
47  Renderer & operator=(const Renderer && renderer) = delete;
48  ~Renderer() = default;
49 
51  auto load(const gfx::Model &model) -> gpu::Model;
52 
54  auto load(const gfx::Models & models) -> gpu::Models;
55 
57  auto load(const gfx::Mesh &mesh) -> gpu::Mesh;
58 
60  auto load(const gfx::Shared_mesh &mesh) -> void;
61 
63  auto unload(const gfx::Mesh &mesh) -> void;
64 
66  auto load(const gfx::Shared_texture_2D &texture) -> void;
67 
69  auto load_or_update(const gfx::Texture_2D &texture) -> void;
70 
72  auto unload(const gfx::Shared_texture_2D &texture) -> void;
73 
75  auto render(const gfx::Scenes &scenes,
76  const glm::vec4 &color = {0.0f, 0.0f, 0.0f, 1.0f},
77  const glm::ivec2 &resolution = glm::ivec2(128, 128)) -> void;
78 
80  auto clear_buffers() -> void;
81 
82  // TODO: MOVE
83  static GLuint generate(const std::function<void(GLsizei, GLuint*)> & f);
84  static GLuint wrap_convert(const gfx::Texture::Wrap& w);
85  static GLuint filter_convert(const gfx::Texture::Filter &f);
86  static GLuint filter_convert_mip(const gfx::Texture::Filter &f);
87 
88 private:
89  using Time_point = std::chrono::time_point<std::chrono::system_clock, std::chrono::nanoseconds>;
90 
91 
92 
93  struct FormatPair {
94  GLint internal_format;
95  GLenum format;
96  };
97 
98  auto render_texture_targets(const gfx::Scene &scene) -> void;
99 
100  auto render_scene(const gfx::Camera &camera,
101  const gfx::Scene &scene,
102  const glm::ivec2 &resolution) -> void;
103 
104  auto render_shadow_maps(const std::vector<gpu::Model> &models,
105  const gfx::Spot_lights &spot_lights) -> void;
106 
107  auto render_cascaded_shadow_maps(const std::vector<gpu::Model> &models,
108  const gfx::Directional_light &light,
109  const gfx::Camera &camera) -> void;
110 
111  auto render_environment(const gfx::Scene &scene,
112  const glm::vec4 &clear_color) -> void;
113 
114  auto render_sky(const gpu::Model &model,
115  const gfx::Camera &camera,
116  const gfx::Fog &fog,
117  const glm::vec2 &resolution,
118  const Standard_program& program) -> void;
119 
120  auto render_clouds(const gfx::Clouds &clouds,
121  const gfx::Directional_light &directional_light,
122  const gfx::Spot_lights &spot_lights,
123  const gfx::Environment_lights &environment_lights,
124  const mos::gfx::Camera &camera,
125  const glm::ivec2 &resolution,
126  const Cloud_program &program,
127  const GLenum &draw_mode) -> void;
128 
129  auto render_model(const gpu::Model &model,
130  const glm::mat4 &transform,
131  const gfx::Camera &camera,
132  const gfx::Spot_lights &spot_lights,
133  const gfx::Environment_lights &environment_lights,
134  const gfx::Fog &fog,
135  const glm::vec2 &resolution,
136  const Standard_program& program) -> void;
137 
138  auto render_model_depth(const gpu::Model &model,
139  const glm::mat4 &transform,
140  const gfx::Camera &camera,
141  const glm::vec2 &resolution,
142  const Depth_program& program) -> void;
143 
145  auto clear(const glm::vec4 &color) -> void;
146  auto clear_depth() -> void;
147  auto clear_color(const glm::vec4 &color) -> void;
148  auto blur(GLuint input_texture,
149  const Post_target &buffer_target,
150  const Post_target &output_target,
151  float iterations = 6) -> void;
152 
153  const bool context_;
154 
155  const Shader functions_shader_;
156  const Standard_program standard_program_;
157  const Cloud_program point_cloud_program_;
158  const Cloud_program line_cloud_program_;
159  const Depth_program depth_program_;
160  const Bloom_program bloom_program_;
161  const Compositing_program compositing_program_;
162  const Blur_program blur_program_;
163 
164  Frame_buffers frame_buffers_;
165  Render_buffers render_buffers_;
166  Texture_buffers textures_;
167  Array_buffers array_buffers_;
168  Element_array_buffers element_array_buffers_;
169  Vertex_arrays vertex_arrays_;
170 
171  const Standard_target standard_target_;
172  const Blit_target multisample_target_;
173  const Post_target post_target0_;
174  const Post_target post_target1_;
175 
176  const Quad quad_;
177 
178  const Texture_buffer_2D black_texture_;
179  const Texture_buffer_2D white_texture_;
180  const Texture_buffer_2D brdf_lut_texture_;
181 
182  std::array<int,2> cube_camera_index_;
183 
185  const Render_buffer shadow_maps_render_buffer_;
186  const std::array<Shadow_map_target, 4> shadow_maps_;
187  const Post_target shadow_map_blur_target_;
188  //TODO: blur directly into the shadowmaps, to save memory.
189  const std::array<Post_target, 4> shadow_map_blur_targets_;
190 
192  const Render_buffer environment_render_buffer_;
193  const std::array<Environment_map_target, 2> environment_maps_targets_;
194 
195  static constexpr const int cascade_count{4};
196  //TODO: return all theese from the render method
197  const std::array<Shadow_map_target, cascade_count> cascaded_shadow_maps_;
198  //TODO: Blur directly into the cascaded shadowmaps to save memory.
199  const std::array<Post_target, 4> cascaded_shadow_map_blur_targets_;
200 
201  glm::vec4 cascade_splits{}; //TODO: Generalize number of splits
202  std::array<glm::mat4, cascade_count> directional_light_ortho_matrices{};
203  std::array<glm::mat4, cascade_count> light_view_matrix{};
204 
205 };
206 }
Container.
Definition: container.hpp:12
Definition: shader.hpp:8
Definition: compositing_program.hpp:7
Fog with exponential falloff and near/far color blending.
Definition: fog.hpp:7
Definition: texture_buffer_2d.hpp:8
Definition: model.hpp:20
Definition: blur_program.hpp:7
Rendering camera view.
Definition: camera.hpp:12
Definition: depth_program.hpp:7
Definition: directional_light.hpp:8
Scene for rendering.
Definition: scene.hpp:21
auto load(const gfx::Model &model) -> gpu::Model
Loads a model into GPU memory.
Definition: post_target.hpp:8
Definition: bloom_program.hpp:7
auto unload(const gfx::Mesh &mesh) -> void
Unloads a mesh from GPU memory.
auto render(const gfx::Scenes &scenes, const glm::vec4 &color={0.0f, 0.0f, 0.0f, 1.0f}, const glm::ivec2 &resolution=glm::ivec2(128, 128)) -> void
Render multiple scenes.
Geometric data description, vertices and indices.
Definition: mesh.hpp:22
Definition: quad.hpp:7
auto clear_buffers() -> void
Clear all GPU buffers/memory.
Definition: render_buffer.hpp:7
Definition: array_buffers.hpp:5
Texture in two dimension.
Definition: texture_2d.hpp:17
Uniforms for the standard shader.
Definition: standard_program.hpp:10
Uniforms for the particle shader program.
Definition: cloud_program.hpp:10
Render geometry shapes with OpenGL.
Definition: renderer.hpp:40
auto load_or_update(const gfx::Texture_2D &texture) -> void
Loads a texture into GPU memory.
Definition: standard_target.hpp:7
Definition: blit_target.hpp:8
Renderer(const glm::ivec2 &resolution, const int samples=1)
Inits the renderer, creates an OpenGL context with GLAD.
Definition: mesh.hpp:12
Collection of properties for a renderable object.
Definition: model.hpp:24