You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

chunk.h 2.5KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. #pragma once
  2. #include "voxelutils.h"
  3. #include "util/mobileatomic.h"
  4. #include <glm/glm.hpp>
  5. #include <queue>
  6. namespace vtk {
  7. class World;
  8. class HeightMap;
  9. typedef std::pair<short, unsigned short> LightIndexPair;
  10. class Chunk {
  11. friend class World;
  12. public:
  13. Chunk(World& world);
  14. bool isLoaded();
  15. // BREAK AND PLACE FUNCS
  16. // These handle all the mesh rebuilds and heightmap updates for you
  17. // Positions are chunk relative (0-15 on all axes)
  18. //break a voxel, returns the type of the voxel that was just broken
  19. unsigned breakVoxel(const glm::ivec3& pos);
  20. //place voxel, will not override previous voxels, returns true if successful
  21. bool placeVoxel(const glm::ivec3& pos, const unsigned& type);
  22. bool isVoxelSolid(const int& x, const int& y, const int& z); //Is the voxel not a transparent type?
  23. bool isVoxelSolid(const glm::ivec3& pos);
  24. void setVoxelType(const glm::ivec3& pos, const unsigned& type);
  25. void setVoxelType(const int& x, const int& y, const int& z, const unsigned& type, const bool& update = false);
  26. unsigned getVoxelType(const glm::ivec3& pos);
  27. unsigned getVoxelType(const unsigned& x, const unsigned& y, const unsigned& z);
  28. void rebuildLighting();
  29. void addLight(const glm::ivec3& pos, const unsigned short& light);
  30. void removeLight(const glm::ivec3& pos);
  31. glm::ivec3 getWorldCoords(const int& x, const int& y, const int& z);
  32. unsigned getLightLevel(const glm::ivec3& pos);
  33. unsigned short getLightPacked(const glm::ivec3& pos);
  34. void setLightPacked(const glm::ivec3& pos, const unsigned short& light);
  35. HeightMap* getHeightMap();
  36. void setPos(const glm::ivec3& pos);
  37. glm::ivec3 getPos();
  38. World& getWorld();
  39. //has no protections for out of bounds
  40. unsigned short fastGetLightPacked(const glm::ivec3& pos);
  41. protected:
  42. void propogateLight();
  43. void propogateLightTask();
  44. //conversion funcs for directly accessing data and lighting
  45. short vecToIndex(const glm::ivec3& pos);
  46. glm::ivec3 indexToVec(const short& index);
  47. //index, mask, Chunk* tuple
  48. typedef std::tuple<short, unsigned short, Chunk*> LightIndexTup;
  49. void setQueuedForMeshRebuild(const bool& rebuild = true);
  50. bool isQueuedForMeshRebuild();
  51. std::array<util::MobileAtomic<unsigned>, 4096> mData;
  52. std::array<util::MobileAtomic<unsigned short>, 4096> mLighting;
  53. World& mLinkedWorld;
  54. glm::ivec3 mPos;
  55. //queue for lighting voxels with BFS
  56. std::queue<LightIndexTup> mLightBFSQueue;
  57. //queue for removing lights with BFS
  58. std::queue<LightIndexTup> mDarkBFSQueue;
  59. std::atomic<bool> mLoaded;
  60. std::atomic<bool> mQueuedForMeshRebuild;
  61. std::atomic<bool> mPropogating;
  62. };
  63. }