Antkeeper  0.0.1
culling-stage.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 
21 #include <engine/scene/camera.hpp>
23 #include <algorithm>
24 #include <execution>
25 #include <mutex>
26 
27 namespace render {
28 
30 {
31  // Get all objects in the collection
32  const auto& objects = ctx.collection->get_objects();
33 
34  // Get camera layer mask and view frustum
35  const auto camera_layer_mask = ctx.camera->get_layer_mask();
36  const auto& view_frustum = ctx.camera->get_view_frustum();
37 
38  // Construct mutex to guard set of visible objects
39  std::mutex mutex;
40 
41  // For each object in the scene collection
42  std::for_each
43  (
44  std::execution::par,
45  std::begin(objects),
46  std::end(objects),
47  [&](scene::object_base* object)
48  {
49  // Ignore cameras
51  {
52  return;
53  }
54 
55  // Cull object if it doesn't share any common layers with the camera
56  if (!(object->get_layer_mask() & camera_layer_mask))
57  {
58  return;
59  }
60 
61  // Cull object if it's outside of the camera view frustum
62  if (!view_frustum.intersects(object->get_bounds()))
63  {
64  return;
65  }
66 
67  // Insert object into set of visible objects
68  std::lock_guard<std::mutex> guard(mutex);
69  ctx.objects.push_back(object);
70  }
71  );
72 }
73 
74 } // namespace render
void execute(render::context &ctx) override
Executes the render stage.
constexpr const view_frustum_type & get_view_frustum() const noexcept
Returns the camera's view frustum.
Definition: camera.hpp:271
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
virtual const aabb_type & get_bounds() const noexcept=0
Returns the bounds of the object.
constexpr std::uint32_t get_layer_mask() const noexcept
Returns the layer mask of the object.
Definition: object.hpp:121
virtual const std::size_t get_object_type_id() const noexcept=0
Returns the type ID for this scene object type.
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.
Context of a renderer.
Definition: context.hpp:40
const scene::camera * camera
Pointer to the camera.
Definition: context.hpp:42
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