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.

world.cpp 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. #include "world.h"
  2. #include "chunk.h"
  3. #include <cmath>
  4. #include <iostream>
  5. #include <thread>
  6. #include <GL/glew.h>
  7. #include <GL/gl.h>
  8. #define GLM_FORCE_RADIANS
  9. #include <glm/glm.hpp>
  10. #include <glm/gtc/type_ptr.hpp>
  11. #include <glm/gtc/matrix_transform.hpp>
  12. namespace vtk {
  13. World::World() {
  14. chunkSize = 16;
  15. voxelSize = 1.0f;
  16. voxelInfo.linkedWorld = this;
  17. voxelMath.linkedWorld = this;
  18. rebuildThreadActive = false;
  19. }
  20. bool World::isVoxelSolid(const int& x, const int& y, const int& z) {
  21. auto chunkPos = glm::ivec3(floor((float)x / (float)chunkSize),
  22. floor((float)y / (float)chunkSize),
  23. floor((float)z / (float)chunkSize));
  24. if (mChunks.find(chunkPos) == mChunks.end()) { //block is in nonexistant chunk
  25. return true;
  26. }
  27. int relPosX = x - chunkPos.x * chunkSize;
  28. int relPosY = y - chunkPos.y * chunkSize;
  29. int relPosZ = z - chunkPos.z * chunkSize;
  30. return mChunks[chunkPos]->isVoxelSolid(relPosX, relPosY, relPosZ);
  31. }
  32. bool World::setVoxelType(const int& x, const int& y, const int& z, const unsigned& type, const bool& updateChunk) {
  33. auto chunkPos = glm::ivec3(floor((float)x / (float)chunkSize), floor((float)y / (float)chunkSize), floor((float)z / (float)chunkSize));
  34. if (mChunks.find(chunkPos) == mChunks.end()) { //block is in nonexistant chunk
  35. return false;
  36. }
  37. int relPosX = x - chunkPos.x * chunkSize;
  38. int relPosY = y - chunkPos.y * chunkSize;
  39. int relPosZ = z - chunkPos.z * chunkSize;
  40. mChunks[chunkPos]->setVoxelType(relPosX, relPosY, relPosZ, type);
  41. if (updateChunk) queueChunkUpdate(chunkPos, true);
  42. return true;
  43. }
  44. unsigned World::getVoxelType(const glm::ivec3& pos) {
  45. auto chunkPos = glm::ivec3(floor((float)pos.x / (float)chunkSize),
  46. floor((float)pos.y / (float)chunkSize),
  47. floor((float)pos.z / (float)chunkSize));
  48. if (mChunks.find(chunkPos) == mChunks.end()) {
  49. return 0;
  50. }
  51. int relPosX = pos.x - chunkPos.x * chunkSize;
  52. int relPosY = pos.y - chunkPos.y * chunkSize;
  53. int relPosZ = pos.z - chunkPos.z * chunkSize;
  54. return mChunks[chunkPos]->getVoxelType((unsigned)relPosX, (unsigned)relPosY, (unsigned)relPosZ);
  55. }
  56. bool World::makeChunk(const int& x, const int& y, const int& z) {
  57. auto pos = glm::ivec3(x, y, z);
  58. if (mChunks.find(pos) != mChunks.end()) { //chunk already exists
  59. return false;
  60. }
  61. auto newChunk = new Chunk(*this);
  62. mChunks[pos] = newChunk;
  63. newChunk->setPos(pos);
  64. mChunkMeshes.emplace(pos, ChunkMesh(*this, pos));
  65. return true;
  66. }
  67. bool World::generateChunk(const int& x, const int& y, const int& z) {
  68. bool chunkMade = makeChunk(x,y,z);
  69. if (chunkMade) {
  70. terrain.generateChunk(getChunk(glm::ivec3(x,y,z)));
  71. //queue this chunk for geometry update
  72. queueChunkUpdate(x,y,z);
  73. //queue the neighboring 6 chunks. the queue functions throws them out
  74. // if they don't exist
  75. queueChunkUpdate(x+1,y,z);
  76. queueChunkUpdate(x-1,y,z);
  77. queueChunkUpdate(x,y+1,z);
  78. queueChunkUpdate(x,y-1,z);
  79. queueChunkUpdate(x,y,z+1);
  80. queueChunkUpdate(x,y,z-1);
  81. }
  82. return chunkMade;
  83. }
  84. Chunk* World::getChunk(const glm::ivec3& pos) {
  85. auto chunk = mChunks.find(pos)->second;
  86. if (chunk) return chunk;
  87. else return nullptr;
  88. }
  89. void World::queueChunkUpdate(const int& x, const int& y, const int& z, const bool& back) {
  90. queueChunkUpdate(glm::ivec3(x,y,z));
  91. }
  92. void World::queueChunkUpdate(const glm::ivec3& pos, const bool& back) {
  93. if (mChunks.find(pos) == mChunks.end()) return; //chunk doesn't exist
  94. for (auto& i : mChunkUpdateQueue) {
  95. if (i == pos) return; //chunk is already in queue, we don't need to update multiple times
  96. }
  97. if (back) {
  98. mChunkUpdateQueue.push_back(pos);
  99. } else {
  100. mChunkUpdateQueue.push_front(pos);
  101. }
  102. }
  103. void World::draw() {
  104. for (auto& i : mChunkMeshes) {
  105. glm::mat4 modelMat = glm::translate(glm::mat4(), glm::vec3((float)i.first.x * 16,
  106. (float)i.first.y * 16,
  107. (float)i.first.z * 16
  108. ));
  109. glUniformMatrix4fv(modelMatUni, 1, GL_FALSE, glm::value_ptr(modelMat));
  110. i.second.draw();
  111. }
  112. }
  113. void World::update() {
  114. if (!rebuildThreadActive && !mChunkUpdateQueue.empty( )) {
  115. auto updatefunc = [&]() {
  116. while (!mChunkUpdateQueue.empty()) {
  117. auto& pos = mChunkUpdateQueue.back();
  118. auto& mesh = mChunkMeshes.find(pos)->second;
  119. mChunkUpdateQueue.pop_back();
  120. mesh.rebuildChunkGeometry();
  121. }
  122. rebuildThreadActive = false;
  123. };
  124. rebuildThreadActive = true;
  125. std::thread(updatefunc).detach();
  126. }
  127. }
  128. void World::forceGlobalGeometryUpdate() {
  129. int chunkCount = 1;
  130. chunkCount = 1;
  131. for (auto& i : mChunkMeshes) {
  132. std::cout << "\rUpdating chunk geometry, but better (" << chunkCount << "/" << mChunkMeshes.size() << ")" << std::flush;
  133. i.second.rebuildChunkGeometry();
  134. i.second.updateGeometry();
  135. ++chunkCount;
  136. }
  137. }
  138. }