Antkeeper  0.0.1
renderer.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2023 Christopher J. Howard
3  *
4  * This file is part of Antkeeper source code.
5  *
6  * Antkeeper source code is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * Antkeeper source code is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with Antkeeper source code. If not, see <http://www.gnu.org/licenses/>.
18  */
19 
24 #include <engine/scene/camera.hpp>
28 #include <engine/scene/text.hpp>
29 #include <engine/render/model.hpp>
30 #include <engine/math/matrix.hpp>
32 #include <engine/config.hpp>
34 #include <engine/math/numbers.hpp>
35 #include <functional>
36 #include <set>
37 
38 namespace render {
39 
41 {
42  m_light_probe_stage = std::make_unique<render::light_probe_stage>(pipeline, resource_manager);
43  m_cascaded_shadow_map_stage = std::make_unique<render::cascaded_shadow_map_stage>(pipeline, resource_manager);
44  m_culling_stage = std::make_unique<render::culling_stage>();
45  m_queue_stage = std::make_unique<render::queue_stage>();
46 }
47 
48 void renderer::render(float t, float dt, float alpha, scene::collection& collection)
49 {
50  // Init render context
51  m_ctx.collection = &collection;
52  m_ctx.t = t;
53  m_ctx.dt = dt;
54  m_ctx.alpha = alpha;
55 
56  // Execute light probe stage
57  m_light_probe_stage->execute(m_ctx);
58 
59  // Get list of cameras to be sorted
60  const auto& cameras = collection.get_objects(scene::camera::object_type_id);
61 
62  // Process cameras in order
63  for (scene::object_base* camera_object: cameras)
64  {
65  scene::camera& camera = static_cast<scene::camera&>(*camera_object);
66 
67  // Skip cameras with no compositors
69  if (!compositor)
70  {
71  continue;
72  }
73 
74  // Update render context camera
75  m_ctx.camera = &camera;
76 
77  // Clear render queues
78  m_ctx.objects.clear();
79  m_ctx.operations.clear();
80 
81  // Execute cascaded shadow map stage
82  m_cascaded_shadow_map_stage->execute(m_ctx);
83 
84  // Execute culling stage
85  m_culling_stage->execute(m_ctx);
86 
87  // Execute queue stage
88  m_queue_stage->execute(m_ctx);
89 
90  // Pass render context to the camera's compositor
91  compositor->composite(m_ctx);
92  }
93 }
94 
95 } // namespace render
Graphics pipeline interface.
Definition: pipeline.hpp:48
void composite(render::context &ctx)
Definition: compositor.cpp:40
renderer(gl::pipeline &pipeline, ::resource_manager &resource_manager)
Constructs a renderer.
Definition: renderer.cpp:40
void render(float t, float dt, float alpha, scene::collection &collection)
Renders a collection of scene objects.
Definition: renderer.cpp:48
Manages the loading, caching, and saving of resources.
constexpr const render::compositor * get_compositor() const noexcept
Returns the camera's compositor.
Definition: camera.hpp:135
Collection of scene objects.
Definition: collection.hpp:33
const std::vector< object_base * > & get_objects() const noexcept
Returns all objects in the collection.
Definition: collection.hpp:56
Abstract base class for scene objects.
Definition: object.hpp:37
static const std::atomic< std::size_t > object_type_id
Unique type ID for this scene object type.
Definition: object.hpp:175
High-level rendering.
const scene::camera * camera
Pointer to the camera.
Definition: context.hpp:42
float alpha
Subframe interpolation factor.
Definition: context.hpp:54
scene::collection * collection
Collection of scene objects being rendered.
Definition: context.hpp:45
std::vector< scene::object_base * > objects
Objects visible to the active camera.
Definition: context.hpp:57
float dt
Timestep, in seconds.
Definition: context.hpp:51
std::vector< const operation * > operations
Render operations generated by visible objects.
Definition: context.hpp:60
float t
Current time, in seconds.
Definition: context.hpp:48