HeightMap.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. #include "HeightMap.h"
  2. #include "stb_image.h"
  3. #include "vector.h"
  4. #include <GL/freeglut.h>
  5. #include <iostream>
  6. #include <string>
  7. HeightMap::HeightMap(const std::string &file)
  8. {
  9. int bpp;
  10. unsigned char* imgData = stbi_load(file.c_str(), &width, &height, &bpp, 4);
  11. auto heightAt = [&](int x, int y)
  12. {
  13. return (imgData[(x + y * width) * 4] / 256.0f) * 100.0f;
  14. };
  15. for (int y = 0; y < height-1; y++)
  16. {
  17. for (int x = 0; x < width-1; x++)
  18. {
  19. int offsets[4][2] = { { 0, 0 },{ 1, 0 },{ 1, 1 },{ 0, 1 } };
  20. Vec3f ca(0, heightAt(x, y + 1) - heightAt(x, y), 1);
  21. Vec3f ba(1, heightAt(x + 1, y) - heightAt(x, y), 0);
  22. Vec3f normal = ca.cross(ba);
  23. normal.Normalize();
  24. for (int i = 0; i < 4; i++)
  25. {
  26. float h = heightAt(x + offsets[i][0], y + offsets[i][1]);
  27. vertices.push_back(Vertex{ (float)(x + offsets[i][0])*scale, h*scale, (float)(y + offsets[i][1])*scale,
  28. normal.x, normal.y, normal.z,
  29. (x + offsets[i][0]) / (float)height, (y + offsets[i][1]) / (float)width } );
  30. }
  31. }
  32. }
  33. glGenTextures(1, &imageIndex);
  34. glBindTexture(GL_TEXTURE_2D, imageIndex);
  35. glTexImage2D(GL_TEXTURE_2D,
  36. 0, //level
  37. GL_RGBA, //internal format
  38. width, //width
  39. height, //height
  40. 0, //border
  41. GL_RGBA, //data format
  42. GL_UNSIGNED_BYTE, //data type
  43. imgData); //data
  44. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  45. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  46. stbi_image_free(imgData);
  47. }
  48. HeightMap::~HeightMap()
  49. {
  50. glDeleteTextures(1, &imageIndex);
  51. }
  52. void HeightMap::Draw()
  53. {
  54. glEnable(GL_LIGHTING);
  55. float color[] = { 0.7f, 0.7f, 0.7f, 1 };
  56. glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, color);
  57. glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, color);
  58. glEnable(GL_TEXTURE_2D);
  59. glBindTexture(GL_TEXTURE_2D, imageIndex);
  60. //glDisable(GL_TEXTURE_2D);
  61. glEnableClientState(GL_VERTEX_ARRAY);
  62. glEnableClientState(GL_TEXTURE_COORD_ARRAY);
  63. //glEnableClientState(GL_COLOR_ARRAY);
  64. glEnableClientState(GL_NORMAL_ARRAY);
  65. glVertexPointer(3, GL_FLOAT, sizeof(Vertex), ((float*)vertices.data()) + 0);
  66. glTexCoordPointer(2, GL_FLOAT, sizeof(Vertex), ((float*)vertices.data()) + 6);
  67. glNormalPointer(GL_FLOAT, sizeof(Vertex), ((float*)vertices.data()) + 3);
  68. glDrawArrays(GL_QUADS, 0, vertices.size());
  69. glDisableClientState(GL_VERTEX_ARRAY);
  70. glDisableClientState(GL_TEXTURE_COORD_ARRAY);
  71. //glDisableClientState(GL_COLOR_ARRAY);
  72. glDisableClientState(GL_NORMAL_ARRAY);
  73. }
  74. void HeightMap::GetHeigth(float x, float y)
  75. {
  76. x /= scale;
  77. y /= scale;
  78. int ix = x;
  79. int iy = y;
  80. int index = (ix + (width - 1) * iy) * 4;
  81. Vertex& a = vertices[index];
  82. Vertex& b = vertices[index+1];
  83. Vertex& c = vertices[index+3];
  84. //http://stackoverflow.com/questions/36090269/finding-height-of-point-on-height-map-triangles
  85. }
  86. void HeightMap::SetTexture(const std::string &file)
  87. {
  88. int bpp, width2, height2;
  89. unsigned char* imgData = stbi_load(file.c_str(), &width2, &height2, &bpp, 4);
  90. glBindTexture(GL_TEXTURE_2D, imageIndex);
  91. glTexImage2D(GL_TEXTURE_2D,
  92. 0, //level
  93. GL_RGBA, //internal format
  94. width2, //width
  95. height2, //height
  96. 0, //border
  97. GL_RGBA, //data format
  98. GL_UNSIGNED_BYTE, //data type
  99. imgData); //data
  100. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  101. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  102. stbi_image_free(imgData);
  103. }