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.9KB

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