You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

chunkmesh.cpp 6.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. #include "graphics/chunkmesh.h"
  2. #include "world.h"
  3. #include "chunk.h"
  4. #include <vector>
  5. #include <GL/glew.h>
  6. #include <GL/gl.h>
  7. #include <iostream>
  8. namespace vtk {
  9. ChunkMesh::ChunkMesh(World& world, glm::ivec3 chunkPos) :
  10. mLinkedWorld(world),
  11. mLinkedChunkPos(chunkPos),
  12. mLocked(false)
  13. {
  14. glGenBuffers(1, &mGeometryTexVBO);
  15. glGenBuffers(1, &mLightVBO);
  16. glGenVertexArrays(1, &mVAO);
  17. glBindVertexArray(mVAO);
  18. glBindBuffer(GL_ARRAY_BUFFER, mGeometryTexVBO);
  19. //vertices
  20. glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6*sizeof(float), NULL);
  21. glEnableVertexAttribArray(0);
  22. //tex uv coords
  23. glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 6*sizeof(float), (void*)(3*sizeof(float)));
  24. glEnableVertexAttribArray(1);
  25. }
  26. void ChunkMesh::rebuildChunkGeometry() {
  27. if (mLocked) return;
  28. mLocked = true;
  29. //get the chunk from position
  30. auto chunk = mLinkedWorld.getChunk(mLinkedChunkPos);
  31. //temporary until I finish mesh providers
  32. float texUV [] = {
  33. 0.0f, 1.0f,
  34. 1.0f, 1.0f,
  35. 0.0f, 0.0f,
  36. 1.0f, 0.0f,
  37. 0.0f, 0.0f,
  38. 1.0f, 1.0f
  39. };
  40. float topModel [] = {
  41. //x y z
  42. 0.0f, 1.0f, 0.0f, //tl
  43. 1.0f, 1.0f, 0.0f, //tr
  44. 0.0f, 1.0f, 1.0f, //bl
  45. 1.0f, 1.0f, 1.0f, //br
  46. 0.0f, 1.0f, 1.0f, //bl
  47. 1.0f, 1.0f, 0.0f //tr
  48. };
  49. float bottomModel [] = {
  50. //x y z u v
  51. 0.0f, 0.0f, 1.0f, //tl
  52. 1.0f, 0.0f, 1.0f, //tr
  53. 0.0f, 0.0f, 0.0f, //bl
  54. 1.0f, 0.0f, 0.0f, //br
  55. 0.0f, 0.0f, 0.0f, //bl
  56. 1.0f, 0.0f, 1.0f //tr
  57. };
  58. float northModel [] = {
  59. //x y z u v
  60. 0.0f, 1.0f, 1.0f, //tl
  61. 1.0f, 1.0f, 1.0f, //tr
  62. 0.0f, 0.0f, 1.0f, //bl
  63. 1.0f, 0.0f, 1.0f, //br
  64. 0.0f, 0.0f, 1.0f, //bl
  65. 1.0f, 1.0f, 1.0f //tr
  66. };
  67. float southModel [] = {
  68. //x y z u v
  69. 1.0f, 1.0f, 0.0f, //tl
  70. 0.0f, 1.0f, 0.0f, //tr
  71. 1.0f, 0.0f, 0.0f, //bl
  72. 0.0f, 0.0f, 0.0f, //br
  73. 1.0f, 0.0f, 0.0f, //bl
  74. 0.0f, 1.0f, 0.0f //tr
  75. };
  76. float eastModel [] = {
  77. //x y z u v
  78. 1.0f, 1.0f, 1.0f, //tl
  79. 1.0f, 1.0f, 0.0f, //tr
  80. 1.0f, 0.0f, 1.0f, //bl
  81. 1.0f, 0.0f, 0.0f, //br
  82. 1.0f, 0.0f, 1.0f, //bl
  83. 1.0f, 1.0f, 0.0f //tr
  84. };
  85. float westModel [] = {
  86. //x y z u v
  87. 0.0f, 1.0f, 0.0f, //tl
  88. 0.0f, 1.0f, 1.0f, //tr
  89. 0.0f, 0.0f, 0.0f, //bl
  90. 0.0f, 0.0f, 1.0f, //br
  91. 0.0f, 0.0f, 0.0f, //bl
  92. 0.0f, 1.0f, 1.0f //tr
  93. };
  94. // geometry format: x,y,z,u,v,i
  95. geometry.clear();
  96. mFaceCount = 0;
  97. int chunkSize = 16; // TODO: make this not hardcoded
  98. for (int i = 0; i < chunkSize; ++i) {
  99. for (int j = 0; j < chunkSize; ++j) {
  100. for (int k = 0; k < chunkSize; ++k) {
  101. //for each voxel in the chunk...
  102. // TODO: move this code into a mesh provider system
  103. //top
  104. if (chunk->isVoxelSolid(i,j,k)) { //check that it's solid
  105. if (!chunk->isVoxelSolid(i, j + 1, k)) {
  106. ++mFaceCount;
  107. for (int l = 0; l < 6; ++l) {
  108. geometry.push_back(topModel[(l * 3)] + (float)i);
  109. geometry.push_back(topModel[(l * 3) + 1] + (float)j);
  110. geometry.push_back(topModel[(l * 3) + 2] + (float)k);
  111. geometry.push_back(texUV[l*2]);
  112. geometry.push_back(texUV[(l*2)+1]);
  113. geometry.push_back(mLinkedWorld.voxelInfo.getTexIndexFromID(2, Face3D::TOP));
  114. }
  115. }
  116. }
  117. //bottom
  118. if (chunk->isVoxelSolid(i,j,k)) { //check that it's solid
  119. if (!chunk->isVoxelSolid(i, j - 1, k)) {
  120. ++mFaceCount;
  121. for (int l = 0; l < 6; ++l) {
  122. geometry.push_back(bottomModel[(l * 3)] + (float)i);
  123. geometry.push_back(bottomModel[(l * 3) + 1] + (float)j);
  124. geometry.push_back(bottomModel[(l * 3) + 2] + (float)k);
  125. geometry.push_back(texUV[l*2]);
  126. geometry.push_back(texUV[(l*2)+1]);
  127. geometry.push_back(mLinkedWorld.voxelInfo.getTexIndexFromID(2, Face3D::TOP));
  128. }
  129. }
  130. }
  131. //north
  132. if (chunk->isVoxelSolid(i,j,k)) { //check that it's solid
  133. if (!chunk->isVoxelSolid(i, j, k + 1)) {
  134. ++mFaceCount;
  135. for (int l = 0; l < 6; ++l) {
  136. geometry.push_back(northModel[(l * 3)] + (float)i);
  137. geometry.push_back(northModel[(l * 3) + 1] + (float)j);
  138. geometry.push_back(northModel[(l * 3) + 2] + (float)k);
  139. geometry.push_back(texUV[l*2]);
  140. geometry.push_back(texUV[(l*2)+1]);
  141. geometry.push_back(mLinkedWorld.voxelInfo.getTexIndexFromID(2, Face3D::TOP));
  142. }
  143. }
  144. }
  145. //south
  146. if (chunk->isVoxelSolid(i,j,k)) { //check that it's solid
  147. if (!chunk->isVoxelSolid(i, j, k - 1)) {
  148. ++mFaceCount;
  149. for (int l = 0; l < 6; ++l) {
  150. geometry.push_back(southModel[(l * 3)] + (float)i);
  151. geometry.push_back(southModel[(l * 3) + 1] + (float)j);
  152. geometry.push_back(southModel[(l * 3) + 2] + (float)k);
  153. geometry.push_back(texUV[l*2]);
  154. geometry.push_back(texUV[(l*2)+1]);
  155. geometry.push_back(mLinkedWorld.voxelInfo.getTexIndexFromID(2, Face3D::TOP));
  156. }
  157. }
  158. }
  159. //east
  160. if (chunk->isVoxelSolid(i,j,k)) { //check that it's solid
  161. if (!chunk->isVoxelSolid(i + 1, j, k)) {
  162. ++mFaceCount;
  163. for (int l = 0; l < 6; ++l) {
  164. geometry.push_back(eastModel[(l * 3)] + (float)i);
  165. geometry.push_back(eastModel[(l * 3) + 1] + (float)j);
  166. geometry.push_back(eastModel[(l * 3) + 2] + (float)k);
  167. geometry.push_back(texUV[l*2]);
  168. geometry.push_back(texUV[(l*2)+1]);
  169. geometry.push_back(mLinkedWorld.voxelInfo.getTexIndexFromID(2, Face3D::TOP));
  170. }
  171. }
  172. }
  173. //west
  174. if (chunk->isVoxelSolid(i,j,k)) { //check that it's solid
  175. if (!chunk->isVoxelSolid(i - 1, j, k)) {
  176. ++mFaceCount;
  177. for (int l = 0; l < 6; ++l) {
  178. geometry.push_back(westModel[(l * 3)] + (float)i);
  179. geometry.push_back(westModel[(l * 3) + 1] + (float)j);
  180. geometry.push_back(westModel[(l * 3) + 2] + (float)k);
  181. geometry.push_back(texUV[l*2]);
  182. geometry.push_back(texUV[(l*2)+1]);
  183. geometry.push_back(mLinkedWorld.voxelInfo.getTexIndexFromID(2, Face3D::TOP));
  184. }
  185. }
  186. }
  187. }
  188. }
  189. }
  190. mUpdated = true;
  191. mLocked = false;
  192. }
  193. void ChunkMesh::rebuildChunkLighting() {
  194. //this doesn't do anything yet
  195. }
  196. void ChunkMesh::draw() {
  197. updateGeometry();
  198. glBindVertexArray(mVAO);
  199. glDrawArrays(GL_TRIANGLES, 0, mGeometryFaceCount * 6);
  200. }
  201. bool ChunkMesh::updateGeometry() {
  202. if (mUpdated) {
  203. mUpdated = false;
  204. glBindBuffer(GL_ARRAY_BUFFER, mGeometryTexVBO);
  205. glBufferData(GL_ARRAY_BUFFER, sizeof(float) * geometry.size(), geometry.data(), GL_STATIC_DRAW);
  206. mGeometryFaceCount = mFaceCount;
  207. return true;
  208. }
  209. return false;
  210. }
  211. } //namespace vtk