TerrainGenerator C++ using diamond-square-algorithm

Diese Seite verwendet Cookies. Durch die Nutzung unserer Seite erklären Sie sich damit einverstanden, dass wir Cookies setzen. Weitere Informationen

  • TerrainGenerator C++ using diamond-square-algorithm

    Hallo,

    Ich bin momentan dabei, für ein Uni Projekt den Diamond-Square-Algorithmus zu implementieren. Es ist schon ziemlich viel vorgegeben gewesen. es geht darum, dass ich einen eindimensionalen vector erzeugen soll, der zu einer bestimmten resolution, die in der build command line angegeben wird, das height_field ausgeben soll. height_min ist 0 und height_max ist 1

    mein code wie er momentan ist liefert mir jedoch beim kompilieren ständig "vector subscript out of bounds". woran kann das liegen. da ich relativ neu zu c++ bin ist mein code sicher fehlerhaft.

    bitte um hilfe

    LG

    Spacemoose


    Quellcode

    1. #include "DiamondSquare.h"
    2. #include <stdlib.h>
    3. #include <time.h>
    4. #include <vector>
    5. using namespace std;
    6. #include <iostream>
    7. unsigned int i;
    8. DiamondSquare::DiamondSquare(int resolution, float height_min, float height_max)
    9. : m_resolution(resolution), m_height_min(height_min), m_height_max(height_max)
    10. {
    11. float default_height = m_height_min;
    12. m_heightfield.resize(resolution*resolution, default_height);
    13. }
    14. DiamondSquare::~DiamondSquare(void)
    15. {
    16. }
    17. float DiamondSquare::GetHeight(int x, int y)
    18. {
    19. return m_heightfield[y*m_resolution+x];
    20. }
    21. uint16_t DiamondSquare::GetHeightAsUint(int x, int y)
    22. {
    23. float height = GetHeight(x,y);
    24. //normalize height to [0;1]
    25. float height_normalized = (height - m_height_min) / (m_height_max - m_height_min);
    26. //convert to integer and return height
    27. return (uint16_t)(height_normalized * (float)UINT16_MAX);
    28. }
    29. // random number between -1 and 1
    30. float rnd() {
    31. float r = 2.0f * ((float)rand() / (float)RAND_MAX) - 1.0f;
    32. return r;
    33. }
    34. void DiamondSquare::GenerateHeightfield()
    35. {
    36. srand((unsigned)time(0));
    37. float hs = m_resolution;
    38. double roughness = 1.0;
    39. float size = m_resolution;
    40. float stepsize = m_resolution;
    41. float h = m_resolution;
    42. float w = m_resolution;
    43. while(size>0){
    44. int halfstep = stepsize / 2;
    45. for (int y = halfstep; y < h + halfstep; y += stepsize)
    46. {
    47. for (int x = halfstep; x < w + halfstep; x += stepsize)
    48. {
    49. SquareStep(x, y, stepsize, rnd() * roughness);
    50. }
    51. }
    52. for (int y = 0; y < h; y += stepsize)
    53. {
    54. for (int x = 0; x < w; x += stepsize)
    55. {
    56. DiamondStep(x + halfstep, y, stepsize, rnd() * roughness);
    57. DiamondStep(x, y + halfstep, stepsize, rnd() * roughness);
    58. }
    59. }
    60. size /= 2;
    61. }
    62. }
    63. void DiamondSquare::SquareStep(float x, float y, int size, double value){
    64. int hs = size / 2;
    65. //square
    66. // a b
    67. //
    68. // x
    69. //
    70. // c d
    71. float a = GetHeight(x - hs, y - hs);
    72. float b = GetHeight(x + hs, y - hs);
    73. float c = GetHeight(x - hs, y + hs);
    74. float d = GetHeight(x + hs, y + hs);
    75. SetHeight(x, y, ((a + b + c + d) / 4.0) + rnd());
    76. }
    77. void DiamondSquare::DiamondStep(float x, float y, int size, double value){
    78. int hs = size / 2;
    79. //diamond
    80. // c
    81. //
    82. //a x b
    83. //
    84. // d
    85. float a = GetHeight(x - hs, y);
    86. float b = GetHeight(x + hs, y);
    87. float c = GetHeight(x, y - hs);
    88. float d = GetHeight(x, y + hs);
    89. SetHeight(x, y, ((a + b + c + d) / 4.0) + rnd());
    90. }
    91. void DiamondSquare::SetHeight(int x, int y, float value)
    92. {
    93. m_heightfield[y*m_resolution+x] = value;
    94. }
    Alles anzeigen