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.cpp 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /*
  2. * =====================================================================================
  3. *
  4. * Filename: chunk.cpp
  5. *
  6. * Description: Chunk
  7. *
  8. * Version: 1.0
  9. * Created: 04/04/2014 09:44:36 PM
  10. * Revision: none
  11. * Compiler: gcc
  12. *
  13. * Author: YOUR NAME (),
  14. * Organization:
  15. *
  16. * =====================================================================================
  17. */
  18. #include "chunk.h"
  19. #include "world.h"
  20. #include <iostream>
  21. namespace vtk {
  22. Chunk::Chunk(World& world) :
  23. mLinkedWorld(world),
  24. mLoaded(false),
  25. mQueuedForMeshRebuild(false)
  26. {
  27. mPos = glm::ivec3(0,0,0);
  28. renderer.linkedChunk = this;
  29. //fill voxels with 0
  30. for (unsigned i = 0; i < mData.size(); i++) {
  31. mData[i].store(0);
  32. }
  33. mLoaded.store(true);
  34. }
  35. bool Chunk::isLoaded() {
  36. return mLoaded.load();
  37. }
  38. bool Chunk::isVoxelSolid(const int& x, const int& y, const int& z) {
  39. if (x < 0 || x > 15 ||
  40. y < 0 || y > 15 ||
  41. z < 0 || z > 15 )
  42. { //position is outside of the chunk
  43. return mLinkedWorld.isVoxelSolid(mPos.x * 16 + x,
  44. mPos.y * 16 + y,
  45. mPos.z * 16 + z);
  46. }
  47. return !mLinkedWorld.voxelInfo.isTransparent(getVoxelType((unsigned)x, (unsigned)y, (unsigned)z));
  48. }
  49. void Chunk::setVoxelType(const int& x, const int& y, const int& z, const unsigned& type, const bool& update) {
  50. auto index = x + 16 * (y + 16 * z);
  51. if (index > 4095) {
  52. std::cout << "CHUNK ACCESS ERROR (set voxel): Out of range, doing nothing\n";
  53. return;
  54. }
  55. mData[index].store(type, std::memory_order_release);
  56. if (!update) return;
  57. mLinkedWorld.queueChunkUpdate(mPos);
  58. }
  59. unsigned Chunk::getVoxelType(const unsigned& x, const unsigned& y, const unsigned& z) {
  60. auto index = x + 16 * (y + 16 * z);
  61. if (index > 4095) {
  62. std::cout << "CHUNK ACCESS ERROR (get voxel): Out of range, returning type 0\n";
  63. return 0;
  64. }
  65. return mData[index].load(std::memory_order_consume);
  66. }
  67. glm::ivec3 Chunk::getWorldCoords(const int& x, const int& y, const int& z) {
  68. return glm::ivec3(mPos.x * 16 + x,
  69. mPos.y * 16 + y,
  70. mPos.z * 16 + z);
  71. }
  72. unsigned Chunk::getLightLevel(const glm::ivec3 &pos) {
  73. if (isVoxelSolid(pos.x, pos.y, pos.z)) {
  74. return 0;
  75. }
  76. return 15;
  77. }
  78. unsigned Chunk::getLightPacked(const glm::ivec3 &pos) {
  79. if (isVoxelSolid(pos.x, pos.y, pos.z)) {
  80. return 0x00000000;
  81. }
  82. auto wPos = chunkPosToWorldPos(mPos, pos);
  83. if (mLinkedWorld.getHeight(glm::ivec2(wPos.x, wPos.z)) > wPos.y) { //voxel is below ground
  84. //std::cout << mLinkedWorld.getHeight(glm::ivec2(pos.x, pos.z)) << ", ";
  85. return 0x000000AA;
  86. }
  87. return 0x000000FF;
  88. }
  89. HeightMap* Chunk::getHeightMap() {
  90. return mLinkedWorld.getHeightMap(glm::ivec2(mPos.x,mPos.z));
  91. }
  92. void Chunk::setPos(const glm::ivec3& pos) {
  93. mPos = pos;
  94. }
  95. glm::ivec3 Chunk::getPos() {
  96. return mPos;
  97. }
  98. World& Chunk::getWorld() {
  99. return mLinkedWorld;
  100. }
  101. void Chunk::setQueuedForMeshRebuild(const bool& rebuild) {
  102. mQueuedForMeshRebuild = rebuild;
  103. }
  104. bool Chunk::isQueuedForMeshRebuild() {
  105. return mQueuedForMeshRebuild.load();
  106. }
  107. }