1 #ifndef PROCEDURAL_VOXEL_TILE_ACCESSOR_HPP
2 #define PROCEDURAL_VOXEL_TILE_ACCESSOR_HPP
4 #include "blub/core/array.hpp"
5 #include "blub/core/globals.hpp"
6 #include "blub/core/scopedPtr.hpp"
7 #include "blub/math/vector2int.hpp"
8 #include "blub/math/vector3int.hpp"
9 #include "blub/serialization/access.hpp"
10 #include "blub/serialization/nameValuePair.hpp"
28 template <
class configType>
29 class accessor :
public base<accessor<configType> >
33 typedef base<accessor<t_config> > t_base;
36 #if defined(BOOST_NO_CXX11_CONSTEXPR)
37 static const int32 voxelLength;
38 static const int32 voxelLengthWithNormalCorrection;
39 static const int32 voxelLengthLod;
40 static const int32 voxelCount;
41 static const int32 voxelCountLod;
42 static const int32 voxelCountLodAll;
43 static const int32 voxelLengthSurface;
44 static const int32 voxelCountSurface;
46 static constexpr int32 voxelLength = t_config::voxelsPerTile;
47 static constexpr int32 voxelLengthWithNormalCorrection = voxelLength+3;
48 static constexpr int32 voxelLengthLod = (voxelLength+1)*2;
49 static constexpr int32 voxelCount = voxelLengthWithNormalCorrection*voxelLengthWithNormalCorrection*voxelLengthWithNormalCorrection;
50 static constexpr int32 voxelCountLod = voxelLengthLod*voxelLengthLod;
51 static constexpr int32 voxelCountLodAll = 6*voxelCountLod;
52 static constexpr int32 voxelLengthSurface = t_config::voxelsPerTile+1;
53 static constexpr int32 voxelCountSurface = voxelLengthSurface*voxelLengthSurface*voxelLengthSurface;
56 typedef vector<t_voxel> t_voxelArray;
57 typedef vector<t_voxel> t_voxelArrayLod;
90 BASSERT(pos.x < voxelLengthWithNormalCorrection-1);
91 BASSERT(pos.y < voxelLengthWithNormalCorrection-1);
92 BASSERT(pos.z < voxelLengthWithNormalCorrection-1);
96 ++m_numVoxelLargerZero;
99 const int32 index((pos.x+1)*voxelLengthWithNormalCorrection*voxelLengthWithNormalCorrection + (pos.y+1)*voxelLengthWithNormalCorrection + pos.z+1);
100 const t_voxel oldValue(m_voxels[index]);
101 m_voxels[index] = toSet;
103 return oldValue != toSet;
126 BASSERT(pos.x >= -1);
127 BASSERT(pos.y >= -1);
128 BASSERT(pos.z >= -1);
129 BASSERT(pos.x < voxelLengthWithNormalCorrection-1);
130 BASSERT(pos.y < voxelLengthWithNormalCorrection-1);
131 BASSERT(pos.z < voxelLengthWithNormalCorrection-1);
133 const int32 index((pos.x+1)*voxelLengthWithNormalCorrection*voxelLengthWithNormalCorrection + (pos.y+1)*voxelLengthWithNormalCorrection + pos.z+1);
134 return m_voxels[index];
155 return m_numVoxelLargerZero == 0;
165 return m_numVoxelLargerZero == voxelCountSurface;
174 return m_calculateLod;
184 return m_numVoxelLargerZero;
193 return m_numVoxelLargerZeroLod;
219 return m_voxelsLod.get();
228 return m_voxelsLod.get();
237 if (m_calculateLod == lod)
243 m_calculateLod = lod;
246 BASSERT(m_voxelsLod ==
nullptr);
247 m_voxelsLod.reset(
new t_voxelArrayLod(6*voxelCountLod));
261 m_numVoxelLargerZero = toSet;
269 m_numVoxelLargerZeroLod = toSet;
277 : m_voxels(voxelCount)
278 , m_voxelsLod(nullptr)
279 , m_calculateLod(false)
280 , m_numVoxelLargerZero(0)
281 , m_numVoxelLargerZeroLod(0)
291 BASSERT(index < voxelCountLod);
292 BASSERT(m_calculateLod);
293 BASSERT(m_voxelsLod.get() !=
nullptr);
297 ++m_numVoxelLargerZeroLod;
299 const uint32 index_(lod*voxelCountLod + index.x*voxelLengthLod + index.y);
300 const t_voxel oldValue((*m_voxelsLod)[index_]);
301 (*m_voxelsLod)[index_] = toSet;
303 return oldValue != toSet;
311 BASSERT(index < voxelCountLod);
312 BASSERT(m_calculateLod);
313 return (*m_voxelsLod)[lod*voxelCountLod + index.x*voxelLengthLod + index.y];
330 BASSERT(pos.x < voxelLengthLod);
331 BASSERT(pos.y < voxelLengthLod);
332 BASSERT(pos.z < voxelLengthLod);
333 BASSERT(pos.x == 0 || pos.y == 0 || pos.z == 0);
356 BLUB_SERIALIZATION_ACCESS
358 template <
class formatType>
359 void save(formatType & readWrite,
const uint32& version)
const
361 using namespace serialization;
365 readWrite << nameValuePair::create(
"calculateLod", m_calculateLod);
367 template <
class formatType>
368 void load(formatType & readWrite,
const uint32& version)
370 using namespace serialization;
375 readWrite >> nameValuePair::create(
"calculateLod", calculateLod);
379 template <
class formatType>
380 void serialize(formatType & readWrite,
const uint32& version)
382 using namespace serialization;
386 readWrite & nameValuePair::create(
"numVoxelLargerZero", m_numVoxelLargerZero);
387 readWrite & nameValuePair::create(
"numVoxelLargerZeroLod", m_numVoxelLargerZeroLod);
388 saveLoad(readWrite, *
this, version);
389 readWrite & nameValuePair::create(
"voxels", m_voxels);
393 BASSERT(!m_voxelsLod.isNull());
394 readWrite & nameValuePair::create(
"voxelsLod", *m_voxelsLod);
401 t_voxelArray m_voxels;
402 blub::scopedPointer<t_voxelArrayLod> m_voxelsLod;
405 int32 m_numVoxelLargerZero;
406 int32 m_numVoxelLargerZeroLod;
412 #if defined(BOOST_NO_CXX11_CONSTEXPR)
413 template <
class voxelType>
const int32 accessor<voxelType>::voxelLength = t_config::voxelsPerTile;
414 template <
class voxelType>
const int32 accessor<voxelType>::voxelLengthWithNormalCorrection = voxelLength+3;
415 template <
class voxelType>
const int32 accessor<voxelType>::voxelLengthLod = (voxelLength+1)*2;
416 template <
class voxelType>
const int32 accessor<voxelType>::voxelCount = voxelLengthWithNormalCorrection*voxelLengthWithNormalCorrection*voxelLengthWithNormalCorrection;
417 template <
class voxelType>
const int32 accessor<voxelType>::voxelCountLod = voxelLengthLod*voxelLengthLod;
418 template <
class voxelType>
const int32 accessor<voxelType>::voxelCountLodAll = 6*voxelCountLod;
419 template <
class voxelType>
const int32 accessor<voxelType>::voxelLengthSurface = t_config::voxelsPerTile+1;
420 template <
class voxelType>
const int32 accessor<voxelType>::voxelCountSurface = voxelLengthSurface*voxelLengthSurface*voxelLengthSurface;
422 template <
class voxelType> constexpr int32 accessor<voxelType>::voxelLength;
423 template <
class voxelType> constexpr int32 accessor<voxelType>::voxelLengthWithNormalCorrection;
424 template <
class voxelType> constexpr int32 accessor<voxelType>::voxelLengthLod;
425 template <
class voxelType> constexpr int32 accessor<voxelType>::voxelCount;
426 template <
class voxelType> constexpr int32 accessor<voxelType>::voxelCountLod;
427 template <
class voxelType> constexpr int32 accessor<voxelType>::voxelCountLodAll;
428 template <
class voxelType> constexpr int32 accessor<voxelType>::voxelLengthSurface;
429 template <
class voxelType> constexpr int32 accessor<voxelType>::voxelCountSurface;
441 #endif // PROCEDURAL_VOXEL_TILE_ACCESSOR_HPP
const t_voxel & getVoxelLod(const vector2int32 &index, const int32 &lod) const
Definition: accessor.hpp:308
const t_voxelArray & getVoxelArray() const
getVoxelArray return voxel-array
Definition: accessor.hpp:200
Definition: customVertexInformation.cpp:193
const int32 & getNumVoxelLargerZeroLod() const
getNumVoxelLargerZeroLod returns number of voxel in lod not minimum.
Definition: accessor.hpp:191
bool setVoxelLod(const vector3int32 &pos, const t_voxel &toSet, const int32 &lod)
setVoxelLod sets a voxel to a lod array.
Definition: accessor.hpp:114
bool isFull() const
isFull returns true if all voxel are maximum.
Definition: accessor.hpp:163
bool isEmpty() const
isEmpty returns true if all voxel are minimum.
Definition: accessor.hpp:153
const int32 & getNumVoxelLargerZero() const
getNumVoxelLargerZero returns number of voxel not minimum.
Definition: accessor.hpp:182
Definition: sharedPointer.hpp:12
static t_base::pointer create()
create creates an instance.
Definition: accessor.hpp:63
virtual ~accessor()
~accessor destructor.
Definition: accessor.hpp:71
const t_voxel & getVoxelLod(const vector3int32 &pos, const int32 &lod) const
getVoxelLod returns ref to lod-voxel
Definition: accessor.hpp:143
bool setVoxelLod(const vector2int32 &index, const t_voxel &toSet, const int32 &lod)
Definition: accessor.hpp:288
const bool & getCalculateLod() const
getCalculateLod returns true if surface later shall calculate level-of-detail
Definition: accessor.hpp:172
void setCalculateLod(const bool &lod)
setCalculateLod enables or disables lod calculation and voxel buffering for it.
Definition: accessor.hpp:235
static vector2int32 calculateCoordsLod(const vector3int32 &pos, const int32 &lod)
calculateCoordsLod converts a 3d lod-coord to a 2d. Transvoxel needs per quader-side (6) only one 2d ...
Definition: accessor.hpp:323
void setNumVoxelLargerZero(const int32 &toSet)
setNumVoxelLargerZero internally used for extern sync. (optimisation)
Definition: accessor.hpp:259
Definition: predecl.hpp:25
accessor()
accessor constructor
Definition: accessor.hpp:276
The data class is the default voxel. Contains an 8-bit interpolation value. Replace/derive it and set...
Definition: data.hpp:27
void setNumVoxelLargerZeroLod(const int32 &toSet)
setNumVoxelLargerZero internally used for extern sync. (optimisation)
Definition: accessor.hpp:267
bool setVoxel(const vector3int32 &pos, const t_voxel &toSet)
setVoxel set ancalculateIndex convertes a 3d voxel-pos to a 1d array-index.
Definition: accessor.hpp:85
t_voxelArrayLod * getVoxelArrayLod() const
getVoxelArrayLod returns nullptr if no lod shall get calculated else 6-lod-arrays of voxels...
Definition: accessor.hpp:217
const t_voxel & getVoxel(const vector3int32 &pos) const
getVoxel return ref to voxel.
Definition: accessor.hpp:124
int8 & getInterpolation()
getInterpolation returns reference to interpolation.
Definition: data.hpp:70
Definition: deadlineTimer.hpp:10
t_voxelArrayLod * getVoxelArrayLod()
getVoxelArrayLod returns nullptr if no lod shall get calculated else 6-lod-arrays of voxels...
Definition: accessor.hpp:226
t_voxelArray & getVoxelArray()
getVoxelArray return voxel-array
Definition: accessor.hpp:208
Definition: customVertexInformation.cpp:177