HeightMap.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. #include "HeightMap.h"
  2. #include "stb_image.h"
  3. #include "vector.h"
  4. #include "LevelObject.h"
  5. #include <GL/freeglut.h>
  6. #include <iostream>
  7. #include <string>
  8. #include "World.h"
  9. #define RED 0
  10. #define GREEN 1
  11. #define BLUE 2
  12. #define ALPHA 3
  13. HeightMap::HeightMap(const std::string &file, float scale, World* world)
  14. {
  15. this->scale = scale;
  16. int bpp;
  17. unsigned char* imgData = stbi_load(file.c_str(), &width, &height, &bpp, 4);
  18. auto heightAt = [&](int x, int y)
  19. {
  20. return (imgData[(x + y * width) * 4 ] / 256.0f) * 100.0f;
  21. };
  22. auto valueAt = [&](int x, int y, int offset = 0)
  23. {
  24. return imgData[(x + y * width) * 4 + offset];
  25. };
  26. for (int y = 0; y < height-1; y++)
  27. {
  28. for (int x = 0; x < width-1; x++)
  29. {
  30. int offsets[4][2] = { { 0, 0 },{ 1, 0 },{ 1, 1 },{ 0, 1 } };
  31. Vec3f ca(0, heightAt(x, y + 1) - heightAt(x, y), 1);
  32. Vec3f ba(1, heightAt(x + 1, y) - heightAt(x, y), 0);
  33. if ((valueAt(x, y, GREEN) >= 10) && (valueAt(x,y,GREEN) <= 100))
  34. {
  35. world->addLevelObject(new LevelObject(world->getObjectFromValue(valueAt(x, y, GREEN)), Vec3f(x*scale, heightAt(x, y), y*scale), Vec3f(0, rand() % 360, 0), 1, true));
  36. }
  37. if (valueAt(x, y, GREEN) == 180 || valueAt(x, y, GREEN) == 190 || valueAt(x, y, GREEN) == 170 || valueAt(x, y, GREEN) == 250)
  38. {
  39. world->addLevelObject(new LevelObject(world->getObjectFromValue(valueAt(x, y, GREEN)), Vec3f(x*scale, heightAt(x, y), y*scale), Vec3f(0, 0, 0), 1, true));
  40. }
  41. if ((valueAt(x, y, GREEN) >= 200) && (valueAt(x, y, GREEN) <= 240))
  42. {
  43. world->addLevelObject(new LevelObject(world->getObjectFromValue(valueAt(x, y, GREEN)), Vec3f(x*scale, heightAt(x, y), y*scale), Vec3f(0, 0, 0), 1, false));
  44. }
  45. Vec3f normal = ca.cross(ba);
  46. normal.Normalize();
  47. for (int i = 0; i < 4; i++)
  48. {
  49. float h = heightAt(x + offsets[i][0], y + offsets[i][1]);
  50. vertices.push_back(Vertex{ (float)(x + offsets[i][0])*scale, h*scale, (float)(y + offsets[i][1])*scale,
  51. normal.x, normal.y, normal.z,
  52. (x + offsets[i][0]) / (float)height, (y + offsets[i][1]) / (float)width } );
  53. }
  54. }
  55. }
  56. glGenTextures(1, &imageIndex);
  57. glBindTexture(GL_TEXTURE_2D, imageIndex);
  58. glTexImage2D(GL_TEXTURE_2D,
  59. 0, //level
  60. GL_RGBA, //internal format
  61. width, //width
  62. height, //height
  63. 0, //border
  64. GL_RGBA, //data format
  65. GL_UNSIGNED_BYTE, //data type
  66. imgData); //data
  67. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  68. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  69. stbi_image_free(imgData);
  70. }
  71. HeightMap::~HeightMap()
  72. {
  73. glDeleteTextures(1, &imageIndex);
  74. }
  75. void HeightMap::Draw()
  76. {
  77. glEnable(GL_LIGHTING);
  78. float color[] = { 0.7f, 0.7f, 0.7f, 1 };
  79. glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, color);
  80. glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, color);
  81. glEnable(GL_TEXTURE_2D);
  82. glBindTexture(GL_TEXTURE_2D, imageIndex);
  83. glEnableClientState(GL_VERTEX_ARRAY);
  84. glEnableClientState(GL_TEXTURE_COORD_ARRAY);
  85. //glEnableClientState(GL_COLOR_ARRAY);
  86. glEnableClientState(GL_NORMAL_ARRAY);
  87. glVertexPointer(3, GL_FLOAT, sizeof(Vertex), ((float*)vertices.data()) + 0);
  88. glTexCoordPointer(2, GL_FLOAT, sizeof(Vertex), ((float*)vertices.data()) + 6);
  89. glNormalPointer(GL_FLOAT, sizeof(Vertex), ((float*)vertices.data()) + 3);
  90. glDrawArrays(GL_QUADS, 0, vertices.size());
  91. glDisableClientState(GL_VERTEX_ARRAY);
  92. glDisableClientState(GL_TEXTURE_COORD_ARRAY);
  93. //glDisableClientState(GL_COLOR_ARRAY);
  94. glDisableClientState(GL_NORMAL_ARRAY);
  95. }
  96. float HeightMap::GetHeight(float x, float y)
  97. {
  98. x /= scale;
  99. y /= scale;
  100. int ix = x;
  101. int iy = y;
  102. int index = (ix + (width - 1) * iy) * 4;
  103. Vertex& a = vertices[index];
  104. Vertex& b = vertices[index+1];
  105. Vertex& c = vertices[index+3];
  106. float lowervalue = ((b.z - c.z)*(a.x - c.x) + (c.x - b.x)*(a.z - c.z));
  107. float labda1 = ((b.z - c.z)*(x - c.x) + (c.x - b.x)*(y - c.z)) / lowervalue;
  108. float labda2 = ((c.y - a.y)*(x - c.x) + (a.x - c.x)*(y - c.y)) / lowervalue;
  109. float labda3 = 1 - labda1 - labda2;
  110. Vertex z = a * labda1 + b * labda2 + c * labda3;
  111. return z.y;
  112. }
  113. void HeightMap::SetTexture(const std::string &file)
  114. {
  115. int bpp, width2, height2;
  116. unsigned char* imgData = stbi_load(file.c_str(), &width2, &height2, &bpp, 4);
  117. glBindTexture(GL_TEXTURE_2D, imageIndex);
  118. glTexImage2D(GL_TEXTURE_2D,
  119. 0, //level
  120. GL_RGBA, //internal format
  121. width2, //width
  122. height2, //height
  123. 0, //border
  124. GL_RGBA, //data format
  125. GL_UNSIGNED_BYTE, //data type
  126. imgData); //data
  127. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  128. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  129. stbi_image_free(imgData);
  130. }