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.

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