BlockRenderer.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. #include "BlockRenderer.h"
  2. #include "../../../utils/Face.h"
  3. BlockRenderer::BlockRenderer()
  4. {
  5. }
  6. void BlockRenderer::addTriangle(float p1x, float p1y, float p1z, float p1nx, float p1ny, float p1nz, float p1texX, float p1texY,
  7. float p2x, float p2y, float p2z, float p2nx, float p2ny, float p2nz, float p2texX, float p2texY,
  8. float p3x, float p3y, float p3z, float p3nx, float p3ny, float p3nz, float p3texX, float p3texY,
  9. int cullData)
  10. {
  11. data.push_back(Triangle());
  12. Triangle& tri = data[data.size() - 1];
  13. tri.p1x = p1x;
  14. tri.p1y = p1y;
  15. tri.p1z = p1z;
  16. tri.p1nx = p1nx;
  17. tri.p1ny = p1ny;
  18. tri.p1nz = p1nz;
  19. tri.p1texX = p1texX;
  20. tri.p1texY = p1texY;
  21. tri.p2x = p2x;
  22. tri.p2y = p2y;
  23. tri.p2z = p2z;
  24. tri.p2nx = p2nx;
  25. tri.p2ny = p2ny;
  26. tri.p2nz = p2nz;
  27. tri.p2texX = p2texX;
  28. tri.p2texY = p2texY;
  29. tri.p3x = p3x;
  30. tri.p3y = p3y;
  31. tri.p3z = p3z;
  32. tri.p3nx = p3nx;
  33. tri.p3ny = p3ny;
  34. tri.p3nz = p3nz;
  35. tri.p3texX = p3texX;
  36. tri.p3texY = p3texY;
  37. tri.cullData = cullData;
  38. }
  39. void BlockRenderer::addCuboid(float sx, float sy, float sz, float ex, float ey, float ez,
  40. float topTexStartX, float topTexStartY, float topTexEndX, float topTexEndY,
  41. float bottomTexStartX, float bottomTexStartY, float bottomTexEndX, float bottomTexEndY,
  42. float northTexStartX, float northTexStartY, float northTexEndX, float northTexEndY,
  43. float southTexStartX, float southTexStartY, float southTexEndX, float southTexEndY,
  44. float eastTexStartX, float eastTexStartY, float eastTexEndX, float eastTexEndY,
  45. float westTexStartX, float westTexStartY, float westTexEndX, float westTexEndY)
  46. {
  47. // bottom side
  48. addTriangle(
  49. sx, sy, sz, 0.0f, -1.0f, 0.0f, bottomTexStartX, bottomTexStartY,
  50. ex, sy, sz, 0.0f, -1.0f, 0.0f, bottomTexEndX, bottomTexStartY,
  51. sx, sy, ez, 0.0f, -1.0f, 0.0f, bottomTexStartX, bottomTexEndY,
  52. Face::DOWN.getCullData());
  53. addTriangle(
  54. ex, sy, sz, 0.0f, -1.0f, 0.0f, bottomTexEndX, bottomTexStartY,
  55. ex, sy, ez, 0.0f, -1.0f, 0.0f, bottomTexEndX, bottomTexEndY,
  56. sx, sy, ez, 0.0f, -1.0f, 0.0f, bottomTexStartX, bottomTexEndY,
  57. Face::DOWN.getCullData());
  58. // top side
  59. addTriangle(
  60. sx, ey, sz, 0.0f, 1.0f, 0.0f, topTexStartX, topTexStartY,
  61. sx, ey, ez, 0.0f, 1.0f, 0.0f, topTexStartX, topTexEndY,
  62. ex, ey, sz, 0.0f, 1.0f, 0.0f, topTexEndX, topTexStartY,
  63. Face::UP.getCullData());
  64. addTriangle(
  65. ex, ey, sz, 0.0f, 1.0f, 0.0f, topTexEndX, topTexStartY,
  66. sx, ey, ez, 0.0f, 1.0f, 0.0f, topTexStartX, topTexEndY,
  67. ex, ey, ez, 0.0f, 1.0f, 0.0f, topTexEndX, topTexEndY,
  68. Face::UP.getCullData());
  69. // north side
  70. addTriangle(
  71. ex, sy, sz, 1.0f, 0.0f, 0.0f, northTexStartX, northTexEndY,
  72. ex, ey, ez, 1.0f, 0.0f, 0.0f, northTexEndX, northTexStartY,
  73. ex, sy, ez, 1.0f, 0.0f, 0.0f, northTexEndX, northTexEndY,
  74. Face::NORTH.getCullData());
  75. addTriangle(
  76. ex, sy, sz, 1.0f, 0.0f, 0.0f, northTexStartX, northTexEndY,
  77. ex, ey, sz, 1.0f, 0.0f, 0.0f, northTexStartX, northTexStartY,
  78. ex, ey, ez, 1.0f, 0.0f, 0.0f, northTexEndX, northTexStartY,
  79. Face::NORTH.getCullData());
  80. // south side
  81. addTriangle(
  82. sx, sy, sz, -1.0f, 0.0f, 0.0f, southTexStartX, southTexEndY,
  83. sx, sy, ez, -1.0f, 0.0f, 0.0f, southTexEndX, southTexEndY,
  84. sx, ey, ez, -1.0f, 0.0f, 0.0f, southTexEndX, southTexStartY,
  85. Face::SOUTH.getCullData());
  86. addTriangle(
  87. sx, sy, sz, -1.0f, 0.0f, 0.0f, southTexStartX, southTexEndY,
  88. sx, ey, ez, -1.0f, 0.0f, 0.0f, southTexEndX, southTexStartY,
  89. sx, ey, sz, -1.0f, 0.0f, 0.0f, southTexStartX, southTexStartY,
  90. Face::SOUTH.getCullData());
  91. // east side
  92. addTriangle(
  93. sx, sy, ez, 0.0f, 0.0f, 1.0f, eastTexStartX, eastTexEndY,
  94. ex, sy, ez, 0.0f, 0.0f, 1.0f, eastTexEndX, eastTexEndY,
  95. ex, ey, ez, 0.0f, 0.0f, 1.0f, eastTexEndX, eastTexStartY,
  96. Face::EAST.getCullData());
  97. addTriangle(
  98. sx, sy, ez, 0.0f, 0.0f, 1.0f, eastTexStartX, eastTexEndY,
  99. ex, ey, ez, 0.0f, 0.0f, 1.0f, eastTexEndX, eastTexStartY,
  100. sx, ey, ez, 0.0f, 0.0f, 1.0f, eastTexStartX, eastTexStartY,
  101. Face::EAST.getCullData());
  102. // west side
  103. addTriangle(
  104. sx, sy, sz, 0.0f, 0.0f, -1.0f, westTexStartX, westTexEndY,
  105. ex, ey, sz, 0.0f, 0.0f, -1.0f, westTexEndX, westTexStartY,
  106. ex, sy, sz, 0.0f, 0.0f, -1.0f, westTexEndX, westTexEndY,
  107. Face::WEST.getCullData());
  108. addTriangle(
  109. sx, sy, sz, 0.0f, 0.0f, -1.0f, westTexStartX, westTexEndY,
  110. sx, ey, sz, 0.0f, 0.0f, -1.0f, westTexStartX, westTexStartY,
  111. ex, ey, sz, 0.0f, 0.0f, -1.0f, westTexEndX, westTexStartY,
  112. Face::WEST.getCullData());
  113. }
  114. void BlockRenderer::addToMesh(Mesh& m, int offsetX, int offsetY, int offsetZ, int cullData) const
  115. {
  116. for(const Triangle& tri : data)
  117. {
  118. // if any cull bits of a block are the same as of a triangle => do not render
  119. if((cullData & tri.cullData) == 0)
  120. {
  121. m.addPosition(offsetX + tri.p1x, offsetY + tri.p1y, offsetZ + tri.p1z);
  122. m.addPosition(offsetX + tri.p2x, offsetY + tri.p2y, offsetZ + tri.p2z);
  123. m.addPosition(offsetX + tri.p3x, offsetY + tri.p3y, offsetZ + tri.p3z);
  124. m.addNormal(tri.p1nx, tri.p1ny, tri.p1nz);
  125. m.addNormal(tri.p2nx, tri.p2ny, tri.p2nz);
  126. m.addNormal(tri.p3nx, tri.p3ny, tri.p3nz);
  127. m.addTexture(tri.p1texX, tri.p1texY);
  128. m.addTexture(tri.p2texX, tri.p2texY);
  129. m.addTexture(tri.p3texX, tri.p3texY);
  130. }
  131. }
  132. }