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