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 2.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. {
  26. mPos = glm::ivec3(0,0,0);
  27. renderer.linkedChunk = this;
  28. //fill voxels with 0
  29. for (unsigned i = 0; i < 4096; i++) {
  30. voxels[i] = 0;
  31. }
  32. mLoaded = true;
  33. }
  34. bool Chunk::isLoaded() {
  35. return mLoaded.load();
  36. }
  37. bool Chunk::isVoxelSolid(const int& x, const int& y, const int& z) {
  38. if (x < 0 || x > 15 ||
  39. y < 0 || y > 15 ||
  40. z < 0 || z > 15 )
  41. { //position is outside of the chunk
  42. return mLinkedWorld.isVoxelSolid(mPos.x * 16 + x,
  43. mPos.y * 16 + y,
  44. mPos.z * 16 + z);
  45. }
  46. return (getVoxelType((unsigned)x,(unsigned)y,(unsigned)z) != 0);
  47. }
  48. void Chunk::setVoxelType(const int& x, const int& y, const int& z, const unsigned& type, const bool& update) {
  49. auto index = x + 16 * (y + 16 * z);
  50. if (index > 4095) {
  51. std::cout << "CHUNK ACCESS ERROR (set voxel): Out of range, doing nothing\n";
  52. return;
  53. }
  54. voxels[index] = type;
  55. if (!update) return;
  56. mLinkedWorld.queueChunkUpdate(mPos);
  57. }
  58. unsigned Chunk::getVoxelType(const unsigned& x, const unsigned& y, const unsigned& z) {
  59. auto index = x + 16 * (y + 16 * z);
  60. if (index > 4095) {
  61. std::cout << "CHUNK ACCESS ERROR (get voxel): Out of range, returning type 0\n";
  62. return 0;
  63. }
  64. return voxels[index];
  65. }
  66. glm::ivec3 Chunk::getWorldCoords(const int& x, const int& y, const int& z) {
  67. return glm::ivec3(mPos.x * 16 + x,
  68. mPos.y * 16 + y,
  69. mPos.z * 16 + z);
  70. }
  71. void Chunk::setPos(const glm::ivec3& pos) {
  72. mPos = pos;
  73. }
  74. glm::ivec3 Chunk::getPos() {
  75. return mPos;
  76. }
  77. World& Chunk::getWorld() {
  78. return mLinkedWorld;
  79. }
  80. }