Clone of Bael'Zharon's Respite @ https://github.com/boardwalk/bzr

LandRenderData.cpp 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. /*
  2. * Bael'Zharon's Respite
  3. * Copyright (C) 2014 Daniel Skorupski
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License along
  15. * with this program; if not, write to the Free Software Foundation, Inc.,
  16. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  17. */
  18. #include "graphics/LandRenderData.h"
  19. #include "graphics/Program.h"
  20. #include "Land.h"
  21. LandRenderData::LandRenderData(const Land& land)
  22. {
  23. initGeometry(land);
  24. initNormalTexture(land);
  25. }
  26. LandRenderData::~LandRenderData()
  27. {
  28. glDeleteVertexArrays(1, &vertexArray_);
  29. glDeleteBuffers(1, &vertexBuffer_);
  30. glDeleteTextures(1, &normalTexture_);
  31. }
  32. void LandRenderData::render()
  33. {
  34. glBindVertexArray(vertexArray_);
  35. glActiveTexture(GL_TEXTURE2);
  36. glBindTexture(GL_TEXTURE_2D, normalTexture_);
  37. glDrawArrays(GL_TRIANGLES, 0, vertexCount_);
  38. }
  39. static void pushRotatedCoord(vector<GLfloat>& vertexData, fp_t s, fp_t t, int rotations, int scale)
  40. {
  41. fp_t cosine = glm::cos(pi() / fp_t(180.0) * fp_t(90.0) * rotations);
  42. fp_t sine = glm::sin(pi() / fp_t(180.0) * fp_t(90.0) * rotations);
  43. fp_t ns = (s - fp_t(0.5)) * cosine - (t - fp_t(0.5)) * sine + fp_t(0.5);
  44. fp_t nt = (s - fp_t(0.5)) * sine + (t - fp_t(0.5)) * cosine + fp_t(0.5);
  45. vertexData.push_back(ns * scale);
  46. vertexData.push_back(nt * scale);
  47. }
  48. void LandRenderData::initGeometry(const Land& land)
  49. {
  50. vector<GLfloat> vertexData;
  51. for(uint8_t y = 0; y < Land::kGridSize - 1; y++)
  52. {
  53. for(uint8_t x = 0; x < Land::kGridSize - 1; x++)
  54. {
  55. #define T(dx, dy) land.getTerrain(x + (dx), y + (dy))
  56. uint8_t terrain[]
  57. {
  58. T(0, 0), T(1, 0), T(1, 1), T(0, 1)
  59. };
  60. #undef T
  61. #define R(dx, dy) land.getRoad(x + (dx), y + (dy))
  62. uint8_t road[]
  63. {
  64. R(0, 0), R(1, 0), R(1, 1), R(0, 1)
  65. };
  66. #undef R
  67. uint32_t terrainDone = 0;
  68. vector<uint8_t> textures;
  69. vector<uint8_t> blendTextures;
  70. vector<uint8_t> rotations;
  71. for(int i = 0; i < 4; i++)
  72. {
  73. if(terrainDone & (1 << terrain[i]))
  74. {
  75. continue;
  76. }
  77. terrainDone |= (1 << terrain[i]);
  78. uint8_t bitfield = 0;
  79. for(int j = 0; j < 4; j++)
  80. {
  81. if(terrain[j] == terrain[i])
  82. {
  83. bitfield |= (1 << j);
  84. }
  85. }
  86. // number of 90 degree ccw rotations
  87. uint8_t rotationCount = 0;
  88. uint8_t blendTex = 0xFF;
  89. for(;;)
  90. {
  91. switch(bitfield)
  92. {
  93. case 0x1: // 0001
  94. blendTex = 8;
  95. break;
  96. case 0x9: // 1001
  97. blendTex = 2;
  98. break;
  99. case 0x5: // 0101
  100. blendTex = 0; // TODO
  101. break;
  102. case 0xE: // 1110
  103. blendTex = 0x80 + 0x8;
  104. break;
  105. case 0xF: // 1111
  106. blendTex = 1;
  107. break;
  108. }
  109. if(blendTex != 0xFF)
  110. {
  111. break;
  112. }
  113. bitfield = ((bitfield << 1) | (bitfield >> 3)) & 0xF;
  114. rotationCount++;
  115. }
  116. textures.push_back(terrain[i]);
  117. blendTextures.push_back(blendTex);
  118. rotations.push_back(rotationCount);
  119. }
  120. while(textures.size() < 4)
  121. {
  122. textures.push_back(0);
  123. blendTextures.push_back(0);
  124. rotations.push_back(0);
  125. }
  126. uint8_t roadScale = 3;
  127. {
  128. uint8_t bitfield = 0;
  129. uint8_t roadType = 0;
  130. for(int j = 0; j < 4; j++)
  131. {
  132. if(road[j])
  133. {
  134. bitfield |= (1 << j);
  135. roadType = road[j];
  136. }
  137. }
  138. uint8_t rotationCount = 0;
  139. uint8_t blendTex = 0xFF;
  140. for(;;)
  141. {
  142. switch(bitfield)
  143. {
  144. case 0x0: // 0000
  145. blendTex = 0;
  146. break;
  147. case 0x1: // 0001
  148. blendTex = 5;
  149. break;
  150. case 0x9: // 1001
  151. blendTex = 2;
  152. break;
  153. case 0x5: // 0101
  154. blendTex = 0xA;
  155. roadScale = 1;
  156. break;
  157. case 0xE: // 1110
  158. blendTex = 0x80 + 5;
  159. break;
  160. case 0xF: // 1111
  161. blendTex = 1;
  162. break;
  163. }
  164. if(blendTex != 0xFF)
  165. {
  166. break;
  167. }
  168. bitfield = ((bitfield << 1) | (bitfield >> 3)) & 0xF;
  169. rotationCount++;
  170. }
  171. static const uint8_t kCommonRoad = 0x20;
  172. static const uint8_t kRareRoad = 0x1F;
  173. textures.push_back((roadType == 1) ? kCommonRoad : kRareRoad);
  174. blendTextures.push_back(blendTex);
  175. rotations.push_back(rotationCount);
  176. }
  177. // See LandVertexShader.glsl to see what these are
  178. // Terrain textures are tiled twice per quad (this is specified in region)
  179. // Hence the dx * 2 and dy *2
  180. #define V(dx, dy) \
  181. vertexData.push_back(static_cast<float>((x + (dx)) * 24)); \
  182. vertexData.push_back(static_cast<float>((y + (dy)) * 24)); \
  183. vertexData.push_back(land.getHeight(x + (dx), y + (dy))); \
  184. vertexData.push_back(static_cast<float>((dx) * 2)); \
  185. vertexData.push_back(static_cast<float>((dy) * 2)); \
  186. pushRotatedCoord(vertexData, dx, dy, rotations[0], 1); \
  187. vertexData.push_back(blendTextures[0]); \
  188. vertexData.push_back(textures[0]); \
  189. pushRotatedCoord(vertexData, dx, dy, rotations[1], 1); \
  190. vertexData.push_back(blendTextures[1]); \
  191. vertexData.push_back(textures[1]); \
  192. pushRotatedCoord(vertexData, dx, dy, rotations[2], 1); \
  193. vertexData.push_back(blendTextures[2]); \
  194. vertexData.push_back(textures[2]); \
  195. pushRotatedCoord(vertexData, dx, dy, rotations[3], 1); \
  196. vertexData.push_back(blendTextures[3]); \
  197. vertexData.push_back(textures[3]); \
  198. pushRotatedCoord(vertexData, dx, dy, rotations[4], roadScale); \
  199. vertexData.push_back(blendTextures[4]); \
  200. vertexData.push_back(textures[4]);
  201. if(land.isSplitNESW(x, y))
  202. {
  203. // lower right triangle
  204. V(0, 0) V(1, 0) V(1, 1)
  205. // top left triangle
  206. V(1, 1) V(0, 1) V(0, 0)
  207. }
  208. else
  209. {
  210. // lower left triangle
  211. V(0, 0) V(1, 0) V(0, 1)
  212. // top right triangle
  213. V(1, 0) V(1, 1) V(0, 1)
  214. }
  215. #undef V
  216. }
  217. }
  218. static const int kComponentsPerVertex = 25;
  219. vertexCount_ = static_cast<GLsizei>(vertexData.size()) / kComponentsPerVertex;
  220. glGenVertexArrays(1, &vertexArray_);
  221. glBindVertexArray(vertexArray_);
  222. glGenBuffers(1, &vertexBuffer_);
  223. glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer_);
  224. glBufferData(GL_ARRAY_BUFFER, vertexData.size() * sizeof(GLfloat), vertexData.data(), GL_STATIC_DRAW);
  225. glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, kComponentsPerVertex * sizeof(GLfloat), nullptr);
  226. glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, kComponentsPerVertex * sizeof(GLfloat), reinterpret_cast<GLvoid*>(sizeof(GLfloat) * 3));
  227. glVertexAttribPointer(2, 4, GL_FLOAT, GL_FALSE, kComponentsPerVertex * sizeof(GLfloat), reinterpret_cast<GLvoid*>(sizeof(GLfloat) * 5));
  228. glVertexAttribPointer(3, 4, GL_FLOAT, GL_FALSE, kComponentsPerVertex * sizeof(GLfloat), reinterpret_cast<GLvoid*>(sizeof(GLfloat) * 9));
  229. glVertexAttribPointer(4, 4, GL_FLOAT, GL_FALSE, kComponentsPerVertex * sizeof(GLfloat), reinterpret_cast<GLvoid*>(sizeof(GLfloat) * 13));
  230. glVertexAttribPointer(5, 4, GL_FLOAT, GL_FALSE, kComponentsPerVertex * sizeof(GLfloat), reinterpret_cast<GLvoid*>(sizeof(GLfloat) * 17));
  231. glVertexAttribPointer(6, 4, GL_FLOAT, GL_FALSE, kComponentsPerVertex * sizeof(GLfloat), reinterpret_cast<GLvoid*>(sizeof(GLfloat) * 21));
  232. glEnableVertexAttribArray(0);
  233. glEnableVertexAttribArray(1);
  234. glEnableVertexAttribArray(2);
  235. glEnableVertexAttribArray(3);
  236. glEnableVertexAttribArray(4);
  237. glEnableVertexAttribArray(5);
  238. glEnableVertexAttribArray(6);
  239. }
  240. void LandRenderData::initNormalTexture(const Land& land)
  241. {
  242. glGenTextures(1, &normalTexture_);
  243. glBindTexture(GL_TEXTURE_2D, normalTexture_);
  244. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB8, Land::kOffsetMapSize, Land::kOffsetMapSize, 0, GL_RGB, GL_UNSIGNED_BYTE, land.normalMap());
  245. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); // default is GL_NEAREST_MIPMAP_LINEAR
  246. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  247. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  248. }