1 #ifndef BLUB_PROCEDURAL_VOXEL_SIMPLE_RENDERER_HPP
2 #define BLUB_PROCEDURAL_VOXEL_SIMPLE_RENDERER_HPP
4 #include "blub/core/globals.hpp"
5 #include "blub/core/hashMap.hpp"
6 #include "blub/log/global.hpp"
7 #include "blub/math/octree/container.hpp"
8 #include "blub/math/vector3.hpp"
9 #include "blub/procedural/voxel/simple/base.hpp"
10 #include "blub/procedural/voxel/simple/surface.hpp"
11 #include "blub/procedural/voxel/tile/renderer.hpp"
12 #include "blub/procedural/voxel/tile/surface.hpp"
13 #include "blub/sync/predecl.hpp"
14 #include "blub/sync/sender.hpp"
36 template <
class configType>
37 class renderer :
public base<typename configType::t_renderer::t_tile>
41 typedef typename t_config::t_renderer::t_tile t_tile;
42 typedef sharedPointer<t_tile> t_tilePtr;
43 typedef base<t_tile> t_base;
46 typedef sharedPointer<sync::identifier> t_cameraPtr;
47 typedef sync::sender<t_tileId, t_cameraPtr> t_sync;
49 typedef hashMap<vector3int32, t_tilePtr> t_tileMap;
51 typedef typename t_config::t_surface::t_tile t_tileSurface;
52 typedef sharedPointer<t_tileSurface> t_tileDataPtr;
54 typedef std::function<bool (vector3, axisAlignedBox)> t_octreeSearchCallback;
56 typedef typename t_config::t_surface::t_simple t_rendererSurface;
68 t_rendererSurface *tiles,
70 const real &lodCutDistNear,
71 const real &lodCutDistFar)
74 , m_lodCutDistNear(lodCutDistNear)
75 , m_lodCutDistFar(lodCutDistFar)
76 , m_voxelSize(
math::pow(2., m_lod))
79 const int32 voxelsPerTile(t_config::voxelsPerTile);
80 m_sync =
new t_sync(worker,
vector3int32(voxelsPerTile));
104 m_sync->addReceiver(toAdd, position / m_voxelSize);
121 m_sync->removeReceiver(toRemove);
140 m_voxels->lockForRead();
150 auto& change(m_voxels->getTilesThatGotEdited());
151 for (
auto hasChanged : change)
153 const t_tileId id(hasChanged.first);
154 const t_tileDataPtr work(hasChanged.second);
166 m_voxels->unlockRead();
176 BASSERT(toSet->getIndices().size() > 0);
178 typename t_tileMap::const_iterator it = m_tileData.find(
id);
179 const bool found(it != m_tileData.cend());
181 const int32 voxelsPerTile(t_config::voxelsPerTile);
188 workTile = it->second;
195 workTile->setTileData(toSet, aabb);
199 m_tileData.insert(
id, workTile);
203 m_sync->addSyncMaster(
id,
vector3(pos));
213 typename t_tileMap::const_iterator it = m_tileData.find(
id);
214 BASSERT(it != m_tileData.cend());
216 m_sync->removeSyncMaster(
id);
218 m_tileData.erase(it);
225 const vector3 &posOfReceiverLeafCenter,
226 const typename t_sync::t_syncTree::t_nodePtr& octreeNode)
235 const vector3 &posOfSyncLeafCenter,
236 const typename t_sync::t_receiverTree::t_nodePtr& octreeNode)
248 const typename t_sync::t_sync sync)
251 typename t_tileMap::const_iterator it(m_tileData.find(sync));
252 BASSERT(it != m_tileData.cend());
254 it->second->setVisible(
true);
264 const typename t_sync::t_sync sync)
267 typename t_tileMap::const_iterator it(m_tileData.find(sync));
268 BASSERT(it != m_tileData.cend());
270 it->second->setVisible(
false);
293 const int32 sizeLeaf(t_config::voxelsPerTile);
303 const int32 toSetOnNeighbour[] = {1, 0, 3, 2, 5, 4};
305 if ((tileWork == 0) != toUpdate->getVisible())
307 BLUB_PROCEDURAL_LOG_WARNING() <<
"(tileWork == 0) != toUpdate->getVisible() id:" << id;
309 for (int32 lod = 0; lod < 6; ++lod)
312 const vector3int32 neighbourPosAbs(neighbourId*sizeLeaf);
315 if (toUpdate->getVisible())
317 toUpdate->setVisibleLod(lod, doLod == 1);
321 toUpdate->setVisibleLod(lod,
false);
323 typename t_tileMap::const_iterator it(m_tileData.find(neighbourId));
324 if (it == m_tileData.cend())
328 if (toUpdate->getVisible())
330 it->second->setVisibleLod(toSetOnNeighbour[lod],
false);
334 it->second->setVisibleLod(toSetOnNeighbour[lod], tileWork == 1);
347 const vector3 sizeLeaf(t_config::voxelsPerTile);
348 const vector3 sizeLeafDoubled(sizeLeaf*2);
349 const vector3 octreeNodeTwiceMinimum((octreeNode.
getMinimum()/sizeLeafDoubled).getFloor()*sizeLeafDoubled);
350 const axisAlignedBox octreeNodeTwice(octreeNodeTwiceMinimum, octreeNodeTwiceMinimum + octreeNode.
getSize()*2.);
352 const vector3 posLeafCenterScaled((posLeafCenter / sizeLeafDoubled).getFloor());
356 if (octreeNode.
getSize() == sizeLeaf)
358 const real radius(m_lodCutDistNear / 2.);
367 const real radius(m_lodCutDistFar);
368 blub::sphere coll(posLeafCenterScaled * sizeLeafDoubled + sizeLeaf, radius);
384 const vector3 camPosScaled(position / m_voxelSize);
385 const real tileContainerSize(t_config::voxelsPerTile);
386 m_cameraPositionInTreeLeaf = (camPosScaled/tileContainerSize).getFloor()*tileContainerSize +
vector3(tileContainerSize*0.5);
387 m_sync->updateReceiverMaster(toUpdate, camPosScaled);
392 const real m_lodCutDistNear;
393 const real m_lodCutDistFar;
394 vector3 m_cameraPositionInTreeLeaf;
396 t_rendererSurface* m_voxels;
398 t_tileMap m_tileData;
410 #endif // BLUB_PROCEDURAL_VOXEL_SIMPLE_RENDERER_HPP
void tileGotSetMaster(const t_tileId &id, const t_tileDataPtr toSet)
tileGotSetMaster sets the surface tiles to the octree.
Definition: renderer.hpp:174
Definition: customVertexInformation.cpp:193
vector3int32 t_tileId
Definition: base.hpp:44
vector3 getSize(void) const
Gets the size of the box.
Definition: axisAlignedBox.cpp:57
bool isInSyncRangeReceiver(const typename t_sync::t_receiver receiver, const vector3 &posOfReceiverLeafCenter, const typename t_sync::t_syncTree::t_nodePtr &octreeNode)
Definition: renderer.hpp:224
bool intersects(const sphere &s) const
Definition: sphere.cpp:9
void removeCamera(t_cameraPtr toRemove)
removeCamera removes a camera.
Definition: renderer.hpp:119
virtual t_tilePtr createTile() const
createTile creates a new Tile. Uses callback set by setCreateTileCallback()
Definition: base.hpp:249
void addCamera(t_cameraPtr toAdd, const blub::vector3 &position)
addCamera adds an camera.
Definition: renderer.hpp:102
void editDoneMaster()
editDoneMaster gets called when simple::surface changed.
Definition: renderer.hpp:148
void updateCameraMaster(t_cameraPtr toUpdate, const blub::vector3 &position)
Definition: renderer.hpp:382
Definition: axisAlignedBoxInt32.hpp:12
void tileGotRemovedMaster(const t_tileId &id)
tileGotRemovedMaster removes tile from octree.
Definition: renderer.hpp:211
renderer(blub::async::dispatcher &worker, t_rendererSurface *tiles, const int32 &lod, const real &lodCutDistNear, const real &lodCutDistFar)
renderer constructor.
Definition: renderer.hpp:67
Definition: dispatcher.hpp:29
bool isInSyncRangeSync(const typename t_sync::t_sync sync, const vector3 &posOfSyncLeafCenter, const typename t_sync::t_receiverTree::t_nodePtr &octreeNode)
Definition: renderer.hpp:234
void updateCamera(t_cameraPtr toUpdate, const blub::vector3 &position)
updateCamera updates the position of a camera you have to add before by using addCamera() ...
Definition: renderer.hpp:111
Definition: vector3.hpp:26
~renderer()
~renderer destructor.
Definition: renderer.hpp:91
void updateLod(const t_tileId &id, t_tilePtr toUpdate)
updateLod checks if a neighbour of the tile has a different lod. If so it enables the by the transvox...
Definition: renderer.hpp:281
Definition: axisAlignedBox.hpp:20
const t_tileMap & getTileMap() const
getTileMap Returns all surface-tiles holded by this class. Read-lock class before.
Definition: renderer.hpp:129
void addSyncReceiver(const typename t_sync::t_receiver receiver, const typename t_sync::t_sync sync)
addSyncReceiver gets called by octree if a tile should be visible.
Definition: renderer.hpp:247
const vector3 & getMinimum(void) const
Definition: axisAlignedBox.hpp:126
void removeSyncReceiver(const typename t_sync::t_receiver receiver, const typename t_sync::t_sync sync)
removeSyncReceiver gets called by octree if a tile shouldn't be visible.
Definition: renderer.hpp:263
blub::async::dispatcher & m_worker
m_worker use it to dispatch heavy work. Don't write to class member with it. Do not use any locks...
Definition: base.hpp:148
Definition: deadlineTimer.hpp:10
void editDone()
editDone gets called when simple::surface changed. Read-locks surface.
Definition: renderer.hpp:138
int32 isInRange(const vector3 &posLeafCenter, const axisAlignedBox &octreeNode)
isInRange checks the distance of a tile to the camera.
Definition: renderer.hpp:345
Definition: sphere.hpp:10