BlockRenderer.cpp 6.7 KB

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