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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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. Chunk* World::getChunk(const glm::ivec3& pos) {
  94. return chunks[std::make_tuple(pos.x, pos.y, pos.z)];
  95. }
  96. void World::queueChunkUpdate(const int& x, const int& y, const int& z) {
  97. queueChunkUpdate(std::make_tuple(x,y,z));
  98. }
  99. void World::queueChunkUpdate(const iPos& pos) {
  100. if (chunks.find(pos) == chunks.end()) return; //chunk doesn't exist
  101. for (auto& i : chunkUpdateQueue) {
  102. if (i == pos) return; //chunk is already in queue, we don't need to update multiple times
  103. }
  104. chunkUpdateQueue.push_back(pos);
  105. }
  106. void World::draw() {
  107. for (auto& i : chunks) {
  108. glm::mat4 modelMat = glm::translate(glm::mat4(), glm::vec3(
  109. (float)i.second->getPos().x * 16,
  110. (float)i.second->getPos().y * 16,
  111. (float)i.second->getPos().z * 16
  112. ));
  113. glUniformMatrix4fv(modelMatUni, 1, GL_FALSE, glm::value_ptr(modelMat));
  114. i.second->renderer.drawChunk();
  115. }
  116. }
  117. void World::update() {
  118. while (!chunkUpdateQueue.empty()) {
  119. auto chunk = chunks[chunkUpdateQueue.back()];
  120. chunkUpdateQueue.pop_back();
  121. std::thread(&ChunkRenderer::updateGeometry, &chunk->renderer).detach();
  122. }
  123. for (auto& i : chunks) {
  124. i.second->renderer.updateVertexData();
  125. }
  126. }
  127. void World::forceGlobalGeometryUpdate() {
  128. int chunkCount = 1;
  129. for (auto& i : chunks) {
  130. std::cout << "\rUpdating chunk geometry (" << chunkCount << "/" << chunks.size() << ")" << std::flush;
  131. i.second->renderer.updateGeometry();
  132. chunkCount++;
  133. //i.second->renderer.updateVertexData();
  134. }
  135. }
  136. }