Cogs.Core
ClipShapeSystem.cpp
1#include "Scene/GetBounds.h"
2#include "Systems/Core/ClipShapeSystem.h"
3#include "Systems/Core/TransformSystem.h"
4
5
6namespace {
7 using namespace Cogs::Core;
8
9 void updateComponent(Context* context, ClipShapeSystem* clipSystem, const ClipShapeComponent& clipComp, const TransformComponent* trComp)
10 {
11 ClipShapeData& clipData = clipSystem->getData(&clipComp);
12
13 const glm::mat4& worldFromModel = context->transformSystem->getLocalToWorld(trComp);
14 const glm::mat4 modelFromWorld = glm::inverse(worldFromModel);
15
16 switch (clipComp.shape) {
17 case ClipShapeType::Cube:
18 case ClipShapeType::InvertedCube:
19 clipData.planes[0] = glm::vec4(1, 0, 0, -clipComp.min.x) * modelFromWorld;
20 clipData.planes[1] = glm::vec4(-1, 0, 0, clipComp.max.x) * modelFromWorld;
21 clipData.planes[2] = glm::vec4(0, 1, 0, -clipComp.min.y) * modelFromWorld;
22 clipData.planes[3] = glm::vec4(0, -1, 0, clipComp.max.y) * modelFromWorld;
23 clipData.planes[4] = glm::vec4(0, 0, 1, -clipComp.min.z) * modelFromWorld;
24 clipData.planes[5] = glm::vec4(0, 0, -1, clipComp.max.z) * modelFromWorld;
25 clipData.planeCount = 6;
26 clipData.shape = clipComp.shape;
27 break;
28 default:
29 clipData.planeCount = 0;
30 clipData.shape = ClipShapeType::None;
31 break;
32 }
33
34 switch (clipComp.shape) {
35 case ClipShapeType::Cube:
36 clipData.clipBounds = Bounds::getTransformedBounds(Cogs::Geometry::BoundingBox{ clipComp.min, clipComp.max }, worldFromModel);
37 break;
38 default:
39 clipData.clipBounds = Cogs::Geometry::BoundingBox();
40 break;
41 }
42 }
43
44}
45
46
48{
49 for (ClipShapeComponent& clipComp : pool) {
50 const TransformComponent* trComp = clipComp.getComponent<TransformComponent>();
51
52 if (trComp && (clipComp.hasChanged() || trComp->hasChanged())) {
53 context->transformSystem->updateTransformData(*trComp);
54 updateComponent(context, this, clipComp, trComp);
55 }
56 }
57}
58
59
60
62{
63
64 for (ClipShapeComponent& clipComp : pool) {
65 const TransformComponent* trComp = clipComp.getComponent<TransformComponent>();
66
67 if (trComp && (clipComp.hasChanged() || trComp->hasChanged())) {
68 updateComponent(context, this, clipComp, trComp);
69 }
70 }
71}
ComponentType * getComponent() const
Definition: Component.h:159
Sets up a clipping shape that can be used by multiple entities.
glm::vec3 max
The high-end corner of cube-shapes.
ClipShapeType shape
The basic shape of the clip region.
glm::vec3 min
The low-end corner of cube-shapes.
Context * context
Pointer to the Context instance the system lives in.
void update()
Updates the system state to that of the current frame.
void preUpdate()
Run the pre-update method of the system.
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
Defines a 4x4 transformation matrix for the entity and a global offset for root entities.
void updateTransformData(const TransformComponent &component)
Force an update of the transform data associated with the given component.
Contains the Engine, Renderer, resource managers and other systems needed to run Cogs....
Cogs::Geometry::BoundingBox clipBounds
Bounds on clip shape in world space, empty if clip shape is open.
glm::vec4 planes[6]
Clipping planes in world space.