HeightMap.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. float 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. float lowervalue = ((b.y - c.y)*(a.x - c.x) + (c.x - b.x)*(a.y - c.y));
  85. float labda1 = ((b.y - c.y)*(x - c.x) + (c.x - b.x)*(y - c.y))/ lowervalue;
  86. float labda2 = ((c.y - a.y)*(x - c.x) + (a.x - c.x)*(y - c.y)) / lowervalue;
  87. float labda3 = 1 - labda1 - labda2;
  88. Vec3f z = Vec3f(a.x, a.y, a.z) * labda1 + Vec3f(b.x, b.y, b.z) * labda2 + Vec3f(c.x, c.y, c.z) * labda3;
  89. return z.y;
  90. }
  91. void HeightMap::SetTexture(const std::string &file)
  92. {
  93. int bpp, width2, height2;
  94. unsigned char* imgData = stbi_load(file.c_str(), &width2, &height2, &bpp, 4);
  95. glBindTexture(GL_TEXTURE_2D, imageIndex);
  96. glTexImage2D(GL_TEXTURE_2D,
  97. 0, //level
  98. GL_RGBA, //internal format
  99. width2, //width
  100. height2, //height
  101. 0, //border
  102. GL_RGBA, //data format
  103. GL_UNSIGNED_BYTE, //data type
  104. imgData); //data
  105. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  106. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  107. stbi_image_free(imgData);
  108. }