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.

terraingen.cpp 1.7KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. /*
  2. * =====================================================================================
  3. *
  4. * Filename: terraingen.cpp
  5. *
  6. * Description: Terrain generator source
  7. *
  8. * Version: 1.0
  9. * Created: 05/08/2014 06:45:02 PM
  10. * Revision: none
  11. * Compiler: gcc
  12. *
  13. * Author: YOUR NAME (),
  14. * Organization:
  15. *
  16. * =====================================================================================
  17. */
  18. #include "terraingen.h"
  19. #include "chunk.h"
  20. #include "mathplus.h"
  21. #include "terrain/noise.h"
  22. #include "terrain/ygradient.h"
  23. #include "terrain/yturbulence.h"
  24. #include <iostream>
  25. #include <time.h>
  26. #include <glm/glm.hpp>
  27. namespace vtk {
  28. TerrainGen::TerrainGen() {
  29. std::shared_ptr<noise::NoiseModule> noise = std::make_shared<noise::Noise>(6969696);
  30. std::shared_ptr<noise::NoiseModule> yGrad = std::make_shared<noise::YGradient>(0.0, 128.0);
  31. mNoise = std::make_shared<noise::YTurbulence>(yGrad, noise, 15.0);
  32. mTerrainScale = 32.0;
  33. }
  34. void TerrainGen::generateChunk(Chunk* chunk) {
  35. auto chunkPos = chunk->getPos();
  36. //first pass
  37. for (int i = 0; i < 16; i++) { //x
  38. for (int j = 0; j < 16; j++) { //y
  39. for (int k = 0; k < 16; k++) { //z
  40. double nVal = mNoise->get3D(
  41. (double)(chunkPos.x * 16 + i),
  42. (double)(chunkPos.y * 16 + j),
  43. (double)(chunkPos.z * 16 + k));
  44. if (nVal <= 0.0) {
  45. if (mNoise->get3D((double)(chunkPos.x * 16 + i),
  46. (double)(chunkPos.y * 16 + j + 1),
  47. (double)(chunkPos.z * 16 + k)) >= 0.0)
  48. {
  49. chunk->setVoxelType(i, j, k, 2);
  50. } else {// block above is air
  51. chunk->setVoxelType(i,j,k,1);
  52. }
  53. } else {
  54. chunk->setVoxelType(i,j,k,0);
  55. }
  56. }
  57. }
  58. }
  59. }
  60. }