1#include "CubeMarkerSystem.h"
4#include "EntityStore.h"
6#include "Resources/Mesh.h"
7#include "Resources/MeshManager.h"
8#include "Resources/MaterialManager.h"
9#include "Resources/DefaultMaterial.h"
10#include "Resources/VertexFormats.h"
12#include "Components/Appearance/MaterialComponent.h"
13#include "Components/Core/SubMeshRenderComponent.h"
14#include "Components/Core/TransformComponent.h"
15#include "Components/Core/MeshComponent.h"
16#include "../Components/AxisCubeComponent.h"
17#include "Components/Core/SceneComponent.h"
19#include "Systems/Core/TransformSystem.h"
21#include "Utilities/Math.h"
27 void CubeMarkerSystem::createWallMarkers(CubeMarkerData & cmapData, CubeMarkerComponent & cmapComp)
31 if (cmapData.wallMarkerShapes[0].lock()) {
38 auto axisCubeComp = cube->getComponent<AxisCubeComponent>();
39 auto cubeTransformComp = cube->getComponent<TransformComponent>();
41 if (!axisCubeComp || !cubeTransformComp) {
47 for (
int axis = 0; axis < 3; axis++) {
50 cmapData.wallMarkerShapes[axis] = wallMarker;
53 auto meshComp = wallMarker->getComponent<MeshComponent>();
54 meshComp->meshHandle = createWallMarkerMesh(
context);
57 auto transformComponent = wallMarker->getComponent<TransformComponent>();
58 glm::vec3 a(axis == 0 ? 1.f : 0.f, axis == 1 ? 1.f : 0.f, axis == 2 ? 1.f : 0.f);
59 transformComponent->rotation = Cogs::Core::getRotation(glm::vec3(0.f, 0.f, 1.f), a);
60 transformComponent->scale = glm::vec3(cmapComp.markerSize, cmapComp.markerSize, 1.f);
61 transformComponent->parent = cubeTransformCompH;
65 updateWallMarkers(cmapData, cmapComp,
true);
68 void CubeMarkerSystem::updateWallMarkers(CubeMarkerData& cubeMarkerData, CubeMarkerComponent& cubeMarkerComp,
bool force)
71 glm::vec4 colors[3] = { cubeMarkerComp.xColor, cubeMarkerComp.yColor, cubeMarkerComp.zColor };
74 EntityPtr cube = cubeMarkerData.cube.lock();
75 if (cubeMarkerComp.showWallMarkers && cube) {
76 AxisCubeComponent* axisCubeComp = cube->getComponent<AxisCubeComponent>();
77 TransformComponent* cubeTransformComp = cube->getComponent<TransformComponent>();
78 TransformComponent* markerTransformComp = cubeMarkerComp.getComponent<TransformComponent>();
79 if (force || axisCubeComp->hasChanged() || cubeTransformComp->hasChanged() || markerTransformComp->hasChanged()) {
81 axisCubeComp->worldToLocal *
82 context->transformSystem->getLocalToWorld(markerTransformComp)*glm::vec4(0.f, 0.f, 0.f, 1.f);
84 glm::vec3 p = (1.f / ph.w)*glm::vec3(ph.x, ph.y, ph.z);
85 for (
int axis = 0; axis < 3; axis++) {
86 EntityPtr wallMarker = cubeMarkerData.wallMarkerShapes[axis].lock();
88 TransformComponent* wallMarkertransformComp = wallMarker->getComponent<TransformComponent>();
89 wallMarkertransformComp->position = p;
90 wallMarkertransformComp->scale = glm::vec3(cubeMarkerComp.markerSize,
91 cubeMarkerComp.markerSize,
92 glm::mix(axisCubeComp->adjustedMinCorner[axis],
93 axisCubeComp->adjustedMaxCorner[axis],
94 axisCubeComp->markerFaces[axis]) - p[axis]);
95 wallMarkertransformComp->setChanged();
97 uint32_t key = ((uint32_t(255.f*colors[axis].r) & 0xff) << 24)
98 | ((uint32_t(255.f*colors[axis].g) & 0xff) << 16)
99 | ((uint32_t(255.f*colors[axis].b) & 0xff) << 8)
100 | ((uint32_t(255.f*colors[axis].a) & 0xff));
102 if (force || (cubeMarkerData.colorKeys[axis] != key)) {
105 auto meshRenderComp = wallMarker->getComponent<SubMeshRenderComponent>();
106 auto it = wallMarkerMaterials.find(key);
107 auto jt = wallMarkerMaterials.find(cubeMarkerData.colorKeys[axis]);
108 if (it != wallMarkerMaterials.end()) {
110 meshRenderComp->materials.resize(3);
111 meshRenderComp->materials[0] = it->second.solid;
112 meshRenderComp->materials[1] = it->second.line;
113 meshRenderComp->materials[2] = it->second.line;
116 if (jt != wallMarkerMaterials.end()) {
117 if (jt->second.solid.resolve()->referenceCount() == 1) {
118 wallMarkerMaterials.erase(jt);
125 WallMarkerMaterials materials;
126 if ((jt != wallMarkerMaterials.end()) && (jt->second.solid.resolve()->referenceCount() == 2)) {
128 materials = jt->second;
129 wallMarkerMaterials.erase(jt);
134 materials.solid =
context->materialInstanceManager->createMaterialInstance(
context->materialManager->getDefaultMaterial());
135 materials.solid->setVariant(
"LightModel",
"BaseColor");
136 materials.solid->setVariant(
"EnableLighting",
false);
137 materials.solid->setBoolProperty(DefaultMaterial::EnableLighting,
false);
138 materials.solid->options.depthBiasEnabled =
true;
139 materials.solid->options.depthBias.constant = -1.f;
140 materials.solid->options.depthBias.slope = -1.f;
141 materials.solid->options.depthBias.clamp = 100.f;
144 materials.line =
context->materialInstanceManager->createMaterialInstance(
context->materialManager->getDefaultMaterial());
145 materials.line->setPermutation(
"Line");
146 materials.line->setBoolProperty(DefaultMaterial::EnableLighting,
false);
147 materials.line->options.depthBiasEnabled =
true;
148 materials.line->options.depthBias.constant = -1.f;
149 materials.line->options.depthBias.slope = -1.f;
150 materials.line->options.depthBias.clamp = 100.f;
154 meshRenderComp->materials.resize(3);
155 meshRenderComp->materials[0] = materials.solid;
156 meshRenderComp->materials[1] = materials.line;
157 meshRenderComp->materials[2] = materials.line;
158 materials.solid->setVec4Property(DefaultMaterial::DiffuseColor, 0.5f*colors[axis]);
159 materials.line->setVec4Property(DefaultMaterial::DiffuseColor, colors[axis]);
160 wallMarkerMaterials[key] = materials;
162 cubeMarkerData.colorKeys[axis] = key;
163 meshRenderComp->setChanged();
172 void CubeMarkerSystem::destroyWallMarkers(CubeMarkerData& cmapData, CubeMarkerComponent& cmapComp)
174 for (
int axis = 0; axis < 3; axis++) {
175 EntityPtr wallMarker = cmapData.wallMarkerShapes[axis].lock();
178 cmapData.wallMarkerShapes[axis].reset();
183 void CubeMarkerSystem::createCenterMarker(CubeMarkerData& cmapData, CubeMarkerComponent& cmapComp)
185 EntityPtr centerMarker = cmapData.centerMarkerShape.lock();
190 cmapData.centerMarkerShape = centerMarker;
192 auto * thisMatComp = cmapComp.getComponent<MaterialComponent>();
193 auto * thatMatComp = centerMarker->getComponent<MaterialComponent>();
194 thatMatComp->copyProperties(thisMatComp);
195 updateCenterMarker(cmapData, cmapComp,
true);
198 void CubeMarkerSystem::updateCenterMarker(CubeMarkerData& cubeMarkerData, CubeMarkerComponent& cubeMarkerComp,
bool force)
200 EntityPtr centerMarker = cubeMarkerData.centerMarkerShape.lock();
202 if (force || cubeMarkerComp.hasChanged()) {
203 TransformComponent* mtfComp = centerMarker->getComponent<TransformComponent>();
204 mtfComp->scale = glm::vec3(cubeMarkerComp.markerSize);
205 mtfComp->setChanged();
210 void CubeMarkerSystem::destroyCenterMarker(CubeMarkerData& cmapData, CubeMarkerComponent& cmapComp)
212 EntityPtr centerMarker = cmapData.centerMarkerShape.lock();
215 cmapData.centerMarkerShape.reset();
221 if (this->context !=
context) {
225 for (
auto& cubeMarkerComp :
pool) {
227 auto& cubeMarkerData = getData(&cubeMarkerComp);
231 if (matComp->hasChanged()) {
232 EntityPtr centerMarker = cubeMarkerData.centerMarkerShape.lock();
235 thatMatComp->copyProperties(matComp);
240 if (cubeMarkerComp.hasChanged()) {
241 EntityPtr compCube = cubeMarkerComp.cube.lock();
242 EntityPtr dataCube = cubeMarkerData.cube.lock();
245 if (compCube != dataCube) {
246 destroyWallMarkers(cubeMarkerData, cubeMarkerComp);
247 cubeMarkerData.cube = cubeMarkerComp.cube;
249 if (cubeMarkerComp.showWallMarkers) {
250 createWallMarkers(cubeMarkerData, cubeMarkerComp);
253 destroyWallMarkers(cubeMarkerData, cubeMarkerComp);
256 if (cubeMarkerComp.showCenterMarker) {
257 createCenterMarker(cubeMarkerData, cubeMarkerComp);
260 destroyCenterMarker(cubeMarkerData, cubeMarkerComp);
263 updateCenterMarker(cubeMarkerData, cubeMarkerComp,
false);
264 updateWallMarkers(cubeMarkerData, cubeMarkerComp,
false);
272 matI->setPermutation(
"Line");
275 matI->setVariant(
"LightModel",
"BaseColor");
276 matI->setVariant(
"EnableLighting",
false);
280 MeshHandle CubeMarkerSystem::createWallMarkerMesh(Context * context)
282 static const int N = 20;
284 std::vector<Cogs::Core::PositionVertex> V;
285 for (
int i = 0; i < N; i++) {
286 float theta = float(i) * float((2.0 * glm::pi<double>()) / N);
287 float c = std::cos(theta);
288 float s = std::sin(theta);
290 V.push_back({ glm::vec3(c, s, 1.f) });
291 V.push_back({ glm::vec3(0.5f*c, 0.5f*s, 1.f) });
293 V.push_back({ glm::vec3(0.f, 0.f, 1.f) });
294 V.push_back({ glm::vec3(0.f, 0.f, 0.f) });
296 std::vector<unsigned int> tristrip;
297 for (
int i = 0; i < N; i++) {
298 tristrip.push_back(2 * i + 0);
299 tristrip.push_back(2 * i + 1);
301 tristrip.push_back(0);
302 tristrip.push_back(1);
304 std::vector<unsigned int> linestrip;
305 for (
int i = 0; i < N; i++) {
306 linestrip.push_back(2 * i);
308 linestrip.push_back(0);
310 std::vector<unsigned int> line;
311 line.push_back(2 * N);
312 line.push_back(2 * N+1);
315 MeshHandle M =
context->meshManager->create();
316 Mesh* m =
context->meshManager->get(M);
318 m->setVertexData(V.data(), V.size());
325 M->setBounds(Geometry::BoundingBox{ glm::vec3(-1, -1, -1), glm::vec3(1, 1, 1) });
ComponentType * getComponent() const
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.
ComponentPool< ComponentType > pool
Pool of components managed by the system.
ComponentHandle getHandle(const ComponentType *component)
Get a handle to the given Component instance.
A Context instance contains all the services, systems and runtime components needed to use Cogs.
class EntityStore * store
Entity store.
EntityPtr createChildEntity(const StringView &type, ComponentModel::Entity *parent, const StringView &name=StringView())
Create a new Entity, parenting it to the given parent.
void removeChild(ComponentModel::Entity *parent, const ComponentModel::Entity *entity)
Remove the parent-child relationship between parent and entity.
std::shared_ptr< ComponentModel::Entity > EntityPtr
Smart pointer for Entity access.
@ None
No primitive culling performed.
Contains all Cogs related functionality.
Handle to a Component instance.
Exposes material properties for legacy entities and code.
Material instances represent a specialized Material combined with state for all its buffers and prope...
void setVec4Property(const VariableKey key, glm::vec4 value)
Set the vec4 property with the given key to value.
void setBoolProperty(const VariableKey key, bool value)
Set the bool property with the given key to value.
ResourceType * resolve() const
Resolve the handle, returning a pointer to the actual resource.
@ TriangleStrip
Triangle strip.