HeightMap.cpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. #include "HeightMap.h"
  2. #include "stb_image.h"
  3. #include <GL/freeglut.h>
  4. #include <iostream>
  5. #include <string>
  6. HeightMap::HeightMap(const std::string &file)
  7. {
  8. int bpp;
  9. unsigned char* imgData = stbi_load(file.c_str(), &width, &height, &bpp, 4);
  10. for (int h = 0; h < height; h++)
  11. {
  12. for (int w = 0; w < width; w++)
  13. {
  14. int offsets[4][2] = { { 0, 0 },{ 1, 0 },{ 1, 1 },{ 0, 1 } };
  15. for (int i = 0; i < 4; i++)
  16. {
  17. float y = ((float)imgData[((h + offsets[i][0]) + (w + offsets[i][1]) * width) * 4]);
  18. y = (y / 256.0f) * 100.0f;
  19. vertices.push_back(Vertex{ (float)(h + offsets[i][0])*scale, y*scale, (float)(w + offsets[i][1])*scale,
  20. 0, 1, 0,
  21. (h + offsets[i][0]) / (float)height, (w + offsets[i][1]) / (float)width } );
  22. }
  23. }
  24. }
  25. glGenTextures(1, &imageIndex);
  26. glBindTexture(GL_TEXTURE_2D, imageIndex);
  27. glTexImage2D(GL_TEXTURE_2D,
  28. 0, //level
  29. GL_RGBA, //internal format
  30. width, //width
  31. height, //height
  32. 0, //border
  33. GL_RGBA, //data format
  34. GL_UNSIGNED_BYTE, //data type
  35. imgData); //data
  36. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  37. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  38. stbi_image_free(imgData);
  39. }
  40. HeightMap::~HeightMap()
  41. {
  42. }
  43. void HeightMap::Draw()
  44. {
  45. glEnable(GL_TEXTURE_2D);
  46. glBindTexture(GL_TEXTURE_2D, imageIndex);
  47. glEnableClientState(GL_VERTEX_ARRAY);
  48. glEnableClientState(GL_TEXTURE_COORD_ARRAY);
  49. //glEnableClientState(GL_COLOR_ARRAY);
  50. //glEnableClientState(GL_NORMAL_ARRAY);
  51. glVertexPointer(3, GL_FLOAT, sizeof(Vertex), ((float*)vertices.data()) + 0);
  52. glTexCoordPointer(2, GL_FLOAT, sizeof(Vertex), ((float*)vertices.data()) + 6);
  53. //glNormalPointer(GL_FLOAT, sizeof(Vertex), ((float*)cubeVertices.data()) + 3);
  54. glDrawArrays(GL_QUADS, 0, vertices.size());
  55. glDisableClientState(GL_VERTEX_ARRAY);
  56. glDisableClientState(GL_TEXTURE_COORD_ARRAY);
  57. //glDisableClientState(GL_COLOR_ARRAY);
  58. //glDisableClientState(GL_NORMAL_ARRAY);
  59. }
  60. void HeightMap::GetHeigth(float x, float z)
  61. {
  62. }
  63. void HeightMap::SetTexture(const std::string &file)
  64. {
  65. int bpp, width2, height2;
  66. unsigned char* imgData = stbi_load(file.c_str(), &width2, &height2, &bpp, 4);
  67. glBindTexture(GL_TEXTURE_2D, imageIndex);
  68. glTexImage2D(GL_TEXTURE_2D,
  69. 0, //level
  70. GL_RGBA, //internal format
  71. width2, //width
  72. height2, //height
  73. 0, //border
  74. GL_RGBA, //data format
  75. GL_UNSIGNED_BYTE, //data type
  76. imgData); //data
  77. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  78. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  79. stbi_image_free(imgData);
  80. }