Cogs.Core
SwathBottomSystem.cpp
1#include <glm/glm.hpp>
2#include <glm/gtc/type_ptr.hpp>
3
4#include "Context.h"
5#include "EntityStore.h"
6#include "Resources/MeshManager.h"
7#include "Components/Core/MeshComponent.h"
8#include "Components/Core/MeshRenderComponent.h"
9#include "Components/Core/SceneComponent.h"
10#include "Components/Appearance/MaterialComponent.h"
11
12#include "../Components/BeamGroupComponent.h"
13#include "../Components/DataSetComponent.h"
14#include "../Components/WindowComponent.h"
15#include "../Components/DataRefComponent.h"
16#include "DataSetSystem.h"
17#include "SwathBottomSystem.h"
18
19#include "../Tasks/SwathSeabedBuildMeshTask.h"
20
22{
24}
25
27{
28 bool activateMeshes = false;
29 for (auto & surfComp : pool) {
30 auto & surfData = getData(&surfComp);
31 const auto * winComp = surfComp.getComponent<WindowComponent>();
32 const auto * groupComp = surfComp.getComponent<BeamGroupComponent>();
33
34 if (!winComp || !groupComp || !groupComp->sane || groupComp->majorCount != 1) continue;
35
36 // beamGroup->sane makes sure that we have data (and that it is sane).
37 const auto * dataRefComp = surfComp.getComponent<DataRefComponent>();
38 const auto * dataComp = dataRefComp->data->getComponent<DataSetComponent>();
39 const auto & dataData = dataSetSystem->getData(dataComp);
40
41 if ((surfData.beamGroupGen != groupComp->gen)
42 || (surfData.pathSpacing != winComp->pathSpacing)
43 || (surfData.beamSpacing != winComp->beamSpacing))
44 {
45 surfData.pathSpacing = winComp->pathSpacing;
46 surfData.beamSpacing = winComp->beamSpacing;
47 surfData.beamGroupGen = groupComp->gen;
48 surfData.resamplingPositions.clear(surfData.pathSpacing, context->variables->get("echo.maxPathUpsample")->getFloat());
49 surfData.chunks.clear();
50 surfData.meshManager.clear();
51 surfData.runMeshManager = true;
52 }
53
54 const auto * sceneComp = surfComp.getComponent<SceneComponent>();
55
56 bool resampleSitesChanged;
57 if (sceneComp->visible) {
58 resampleSitesChanged = surfData.resamplingPositions.update(winComp, dataComp, &dataData);
59 }
60 else {
61 resampleSitesChanged = surfData.resamplingPositions.clear(surfData.pathSpacing, context->variables->get("echo.maxPathUpsample")->getFloat());
62 }
63 if (resampleSitesChanged) {
64 if (surfData.chunks.update(surfData.resamplingPositions)) {
65 surfData.runMeshManager = true;
66 }
67 }
68
69 if (surfData.meshManager.ready() && surfData.runMeshManager) {
70 auto * containerPtr = surfComp.getContainer();
71
72
73 SwathPathMeshManager::CreateBuildFunc createBuildFunc = [context, groupComp, &surfComp, &surfData, &dataData](MeshHandle& mesh, uint32_t chunkIndex) -> TaskFunction
74 {
75 return SwathSeabedBuildMeshTask(context, mesh, chunkIndex, *groupComp, surfComp, surfData, dataData);
76 };
77
78 SwathPathMeshManager::CreateEntity createEntity = [context, containerPtr]() -> EntityPtr
79 {
80 auto e = context->store->createChildEntity("MeshPartWithMaterial", containerPtr);
81 auto m = e->getComponent<MaterialComponent>();
82 m->material = context->store->getEntity(containerPtr->getId());
83 return e;
84 };
85
86 SwathPathMeshManager::ActivateEntity activateEntity = [](EntityPtr /*e*/, uint32_t /*ix*/)
87 {
88 //auto mcomp = e->getComponent<MaterialComponent>();
89 //mcomp->diffuseColor = glm::vec4((ix >> 2) & 1, (ix >> 1) & 1, ix & 1, 1);
90 //mcomp->setChanged();
91 };
92
93 surfData.runMeshManager = surfData.meshManager.update(context,
94 surfData.chunks,
95 createBuildFunc,
96 createEntity,
97 activateEntity);
98 activateMeshes = true;
99 }
100
101 if (!surfData.meshManager.ready()) {
102 context->engine->triggerUpdate();
103 }
104 }
105
106
107 // Swap meshes so that they participate when creating building boxes.
108 if (activateMeshes) {
109 context->engine->swapResources();
110 }
111}
112
ComponentType * getComponent() const
Definition: Component.h:159
class Entity * getContainer() const
Get the container currently owning this component instance.
Definition: Component.h:151
Context * context
Pointer to the Context instance the system lives in.
virtual void initialize(Context *context)
Initialize the system.
void update()
Updates the system state to that of the current frame.
A Context instance contains all the services, systems and runtime components needed to use Cogs.
Definition: Context.h:83
class EntityStore * store
Entity store.
Definition: Context.h:231
std::unique_ptr< class Variables > variables
Variables service instance.
Definition: Context.h:180
std::unique_ptr< class Engine > engine
Engine instance.
Definition: Context.h:222
EntityPtr getEntity(const StringView &name, bool logIfNotFound=true) const
Retrieve a reference to the shared entity pointer to the Entity with the given name.
EntityPtr createChildEntity(const StringView &type, ComponentModel::Entity *parent, const StringView &name=StringView())
Create a new Entity, parenting it to the given parent.
Contains information on how the entity behaves in the scene.
std::function< void()> TaskFunction
Type of task function used by the task manager.
Definition: TaskManager.h:38
std::shared_ptr< ComponentModel::Entity > EntityPtr
Smart pointer for Entity access.
Definition: EntityPtr.h:12
void initialize(Context *context) override
Initialize the system.
Exposes material properties for legacy entities and code.