Cogs.Core
OctComponent.cpp
1#include "OctComponent.h"
2#include "Types.h"
3#include "../Systems/OctSystem.h"
4
5using namespace Cogs::Reflection;
6
7void Cogs::Core::Volumetric::OctComponent::registerType()
8{
9 Field fields[] = {
10 Field(Name("valueMin"), &OctComponent::valueMin),
11 Field(Name("valueMax"), &OctComponent::valueMax),
12 Field(Name("turbidity"), &OctComponent::turbidity),
13 Field(Name("ageScale"), &OctComponent::ageScale),
14 Field(Name("forceWipe"), &OctComponent::forceWipe),
15 Field(Name("drawDebug"), &OctComponent::drawDebug),
16 Field(Name("tolerance"), &OctComponent::tolerance),
17 Field(Name("blockShift"), &OctComponent::blockShift),
18 Field(Name("blockExtent"), &OctComponent::blockExtent),
19 Field(Name("tileSize"), &OctComponent::tileSize),
20 Field(Name("gpuCacheSize"), &OctComponent::gpuCacheSize),
21 Field(Name("transferTexture"), &OctComponent::transferTexture),
22 };
23
24 Method methods[] = {
25 Method(Name("initTileResponse"), &OctComponent::initTileResponse),
26 Method(Name("submitTileResponse"), &OctComponent::submitTileResponse),
27 };
28
29 Reflection::TypeDatabase::createType<OctComponent>()
30 .setFields(fields)
31 .setMethods(methods)
32 .setBase<Component>();
33}
34
35Cogs::Core::Volumetric::TileResponse* Cogs::Core::Volumetric::OctComponent::initTileResponse(const TileRequest* req)
36{
37 CpuInstrumentationScope(SCOPE_VOLUMETRIC, "OctComponent::initTileResponse");
38 auto & octData = system->getData(this);
39 if (octData.tileResponsesStash.empty()) return nullptr;
40
41 auto res = std::move(octData.tileResponsesStash.back());
42 octData.tileResponsesStash.pop_back();
43
44 auto i = int16_t(req->tileKey & 0xffffu);
45 auto j = int16_t((req->tileKey >> 16u) & 0xffffu);
46 auto k = int16_t((req->tileKey >> 32u) & 0xffffu);
47 auto w = int16_t((req->tileKey >> 48u) & 0xffffu);
48
49 //glm::ivec4 ix4(int(i) - octData.alignMinToZeroShift.x,
50 // int(j) - octData.alignMinToZeroShift.y,
51 // int(k) - octData.alignMinToZeroShift.z,
52 // int(w));
53
54 // FIXME: Tile requests for upper-level nodes should be split as these have lots of regions.
55
56 glm::ivec3 lll(i, j, k);
57 glm::ivec3 uuu = lll + glm::ivec3(1u << w);
58
59 // Find intersecting regions.
60 res->regionKeys.clear();
61 for (int z = lll.z; z < uuu.z; z++) {
62 for (int y = lll.y; y < uuu.y; y++) {
63 for (int x = lll.x; x < uuu.x; x++) {
64 const auto baseBlockKey = createBaseBlockKey(x, y, z);
65 auto it = octData.baseBlocks.find(baseBlockKey);
66 if (it != octData.baseBlocks.end()) {
67 res->regionKeys.insert(it->second->regionKeys.begin(), it->second->regionKeys.end());
68 }
69 }
70 }
71 }
72
73 const auto skirtExpand = (1u << w)*float(Volumetric::OctSystem::skirtSize) / float(tileSize - Volumetric::OctSystem::skirtSize);
74
75 res->tileKey = req->tileKey;
76 res->layoutHash = octData.layoutHash;
77 res->timestamp = octData.currentTimestamp;
78 res->min = blockExtent*(glm::vec3(lll) - glm::vec3(skirtExpand)) + blockShift;
79 res->max = blockExtent*(glm::vec3(uuu) + glm::vec3(skirtExpand)) + blockShift;
80 res->N = glm::ivec3(tileSize);
81
82 return res;
83}
84
85void Cogs::Core::Volumetric::OctComponent::submitTileResponse(TileResponse* res, bool success)
86{
87 if (res) {
88 auto & octData = system->getData(this);
89 if (success) {
90 octData.tileResponses.push_back(std::move(res));
91 }
92 else {
93 octData.tileResponsesStash.push_back(std::move(res));
94 }
95 }
96}
Base class for Component instances.
Definition: Component.h:143
Field definition describing a single data member of a data structure.
Definition: Field.h:68
Simple method definition.
Definition: Method.h:72
Contains reflection support.
Definition: Component.h:11
float tolerance
Requested refinement tolerance, may be somewhat ignored when atlas size is insufficient.
Definition: OctComponent.h:72
glm::vec3 blockShift
Object space grid origin, tweak if block boundaries happen at unfortunate places.
Definition: OctComponent.h:73
bool forceWipe
Discard all processed data, but regions persist.
Definition: OctComponent.h:71
Represents an unique name.
Definition: Name.h:70