HeightMap.cpp 4.0 KB

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