Cogs.Core
HighlightRegionBounds.cpp
1#include "HighlightRegionBounds.h"
2#include "HighlightRegionSystem.h"
3#include "Systems/Core/SceneSystem.h"
4#include "Systems/Core/TransformSystem.h"
5#include "Context.h"
6
7namespace {
8 using namespace Cogs::Core;
9
10 bool adjustBounds(Context* context, HighlightRegionSystem* hrSystem, Cogs::Geometry::BoundingBox& bounds, const HighlightRegionComponent& regionComp, bool ignoreVisibility)
11 {
12 const HighlightRegionData& data = hrSystem->getData(&regionComp);
13 if (data.boundsMax.x < data.boundsMin.x) return false;
14
15 if (!ignoreVisibility) {
16 if (const SceneComponent* sceneComp = regionComp.getComponent<SceneComponent>(); sceneComp) {
17 if (!sceneComp->visible) return false;
18 }
19 }
20
21 if (const TransformComponent* trComp = regionComp.getComponent<TransformComponent>(); trComp) {
22 glm::mat4 localToWorld = context->transformSystem->getLocalToWorld(trComp);
23
24 glm::vec3 corners[8] = {
25 glm::vec3(data.boundsMin.x, data.boundsMin.y, data.boundsMin.z),
26 glm::vec3(data.boundsMax.x, data.boundsMin.y, data.boundsMin.z),
27 glm::vec3(data.boundsMin.x, data.boundsMax.y, data.boundsMin.z),
28 glm::vec3(data.boundsMax.x, data.boundsMax.y, data.boundsMin.z),
29 glm::vec3(data.boundsMin.x, data.boundsMin.y, data.boundsMax.z),
30 glm::vec3(data.boundsMax.x, data.boundsMin.y, data.boundsMax.z),
31 glm::vec3(data.boundsMin.x, data.boundsMax.y, data.boundsMax.z),
32 glm::vec3(data.boundsMax.x, data.boundsMax.y, data.boundsMax.z),
33 };
34 for (const glm::vec3& corner : corners) {
35 glm::vec3 p = glm::vec3(localToWorld * glm::vec4(corner, 1));
36 bounds.min = glm::min(bounds.min, p);
37 bounds.max = glm::max(bounds.max, p);
38 }
39 return true;
40 }
41 return false;
42 }
43
44}
45
46void Cogs::Core::HighlightRegionBounds::getBounds(Context* context, Cogs::Geometry::BoundingBox& bounds)
47{
48 for (const HighlightRegionComponent& regionComp : hrSystem->pool) {
49 adjustBounds(context, hrSystem, bounds, regionComp, false);
50 }
51}
52
53bool Cogs::Core::HighlightRegionBounds::getBounds(Context* context, const ComponentModel::Entity* entity, Cogs::Geometry::BoundingBox& bounds, bool ignoreVisibility) const
54{
55 if (const HighlightRegionComponent* regionComp = entity->getComponent<HighlightRegionComponent>(); regionComp) {
56 return adjustBounds(context, hrSystem, bounds, *regionComp, ignoreVisibility);
57 }
58 return false;
59}
ComponentType * getComponent() const
Definition: Component.h:159
Container for components, providing composition of dynamic entities.
Definition: Entity.h:18
T * getComponent() const
Get a pointer to the first component implementing the given type in the entity.
Definition: Entity.h:35
ComponentPool< ComponentType > pool
Pool of components managed by the system.
A Context instance contains all the services, systems and runtime components needed to use Cogs.
Definition: Context.h:83
Contains information on how the entity behaves in the scene.
Defines a 4x4 transformation matrix for the entity and a global offset for root entities.
Contains the Engine, Renderer, resource managers and other systems needed to run Cogs....
void getBounds(Context *context, Cogs::Geometry::BoundingBox &bounds) override
Expand bounds including bounds of all entities in this system in world coordinates.