Terrain.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /*
  2. Copyright (c) 2013 Daniele Bartolini, Michele Rossi
  3. Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
  4. Permission is hereby granted, free of charge, to any person
  5. obtaining a copy of this software and associated documentation
  6. files (the "Software"), to deal in the Software without
  7. restriction, including without limitation the rights to use,
  8. copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. copies of the Software, and to permit persons to whom the
  10. Software is furnished to do so, subject to the following
  11. conditions:
  12. The above copyright notice and this permission notice shall be
  13. included in all copies or substantial portions of the Software.
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  15. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  16. OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  17. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  18. HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  19. WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  21. OTHER DEALINGS IN THE SOFTWARE.
  22. */
  23. #pragma once
  24. #include "Vec3.h"
  25. #include "Triangle.h"
  26. #include "Intersection.h"
  27. #include "Ray.h"
  28. #include "Renderer.h"
  29. #include "HeapAllocator.h"
  30. #define MAX_BRUSH_SIZE 256
  31. namespace crown
  32. {
  33. class Terrain
  34. {
  35. public:
  36. Terrain();
  37. ~Terrain();
  38. void CreateTerrain(uint32_t xSize, uint32_t zSize, uint32_t tilePerMeter, float initialHeight);
  39. float GetHeightAt(uint32_t x, uint32_t z) const;
  40. float GetHeightAt(const Vec3& xyz) const;
  41. void SetHeightAt(uint32_t x, uint32_t z, float height);
  42. void SetHeightAt(const Vec3& xyz, float height);
  43. void WorldToHeight(const Vec3& xyz, uint32_t& x, uint32_t& z) const;
  44. void UpdateVertexBuffer(bool recomputeNormals);
  45. bool TraceRay(const Ray& ray, Triangle& result, Triangle& tri2, float& dist);
  46. uint32_t SnapToGrid(const Vec3& vertex);
  47. void BuildBrush(uint32_t width, uint32_t height, float smooth);
  48. float GaussDist(float x, float y, float sigma);
  49. void ApplyBrush(uint32_t x, uint32_t z, float scale);
  50. void ApplyBrush(const Vec3& xyz, float scale);
  51. void PlotCircle(int32_t xx, int32_t yy, int32_t radius, int32_t i);
  52. void Render();
  53. private:
  54. HeapAllocator m_allocator;
  55. uint32_t mSizeX; // X in meters
  56. uint32_t mSizeZ; // Z in meters
  57. float mOffsetX;
  58. float mOffsetZ;
  59. uint32_t mTilePerMeter; // How many tiles per linear meter?
  60. uint32_t mTilesInSizeX;
  61. uint32_t mTilesInSizeZ;
  62. uint32_t mVerticesInSizeX;
  63. uint32_t mVerticesInSizeZ;
  64. float* mHeights; // Contains the heights
  65. float mMinHeight;
  66. float mMaxHeight;
  67. uint32_t mVertexCount;
  68. Vec3* mVertices;
  69. uint32_t mNormalCount;
  70. Vec3* mNormals;
  71. uint32_t mTexCoordCount;
  72. Vec2* mTexCoords;
  73. uint32_t mIndexCount;
  74. uint16_t* mIndices;
  75. VertexBufferId m_vertex_buffer;
  76. VertexBufferId m_normal_buffer;
  77. VertexBufferId m_tex_coord_buffer;
  78. IndexBufferId m_index_buffer;
  79. uint32_t mBrushWidth;
  80. uint32_t mBrushHeight;
  81. float mBrush[MAX_BRUSH_SIZE * MAX_BRUSH_SIZE];
  82. };
  83. } // namespace crown