EntityRenderer.cpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. #include "EntityRenderer.h"
  2. #include "../../../engine/Wrapper.h"
  3. #include "../../../engine/Utils.h"
  4. EntityRenderer::EntityRenderer() : texture("resources/skin.png")
  5. {
  6. // head
  7. addCuboid(0.0f, 0.0f, 0.0f, 0.5f, 0.5f, 0.5f,
  8. 0.125f, 0.0f, 0.25f, 0.125f,
  9. 0.25f, 0.0f, 0.375f, 0.125f,
  10. 0.25f, 0.125f, 0.375f, 0.25f,
  11. 0.0f, 0.125f, 0.125f, 0.25f,
  12. 0.125f, 0.125f, 0.25f, 0.25f,
  13. 0.375f, 0.125f, 0.5f, 0.25f);
  14. // right arm
  15. addCuboid(0.0f, 0.0f, 0.0f, 0.25f, 0.75f, 0.25f,
  16. 0.6875f, 0.25f, 0.75f, 0.3125f,
  17. 0.75f, 0.25f, 0.8125f, 0.3125f,
  18. 0.75f, 0.3125f, 0.8125f, 0.5f,
  19. 0.625f, 0.3125f, 0.6875f, 0.5f,
  20. 0.6875f, 0.3125f, 0.75f, 0.5f,
  21. 0.8125f, 0.3125f, 0.875f, 0.5f);
  22. // left arm
  23. addCuboid(0.0f, 0.0f, 0.0f, 0.25f, 0.75f, 0.25f,
  24. 0.5625f, 0.75f, 0.625f, 0.8125f,
  25. 0.625f, 0.75f, 0.6875f, 0.8125f,
  26. 0.625f, 0.8125f, 0.6875f, 1.0f,
  27. 0.5f, 0.8125f, 0.5625f, 1.0f,
  28. 0.5625f, 0.8125f, 0.625f, 1.0f,
  29. 0.6875f, 0.8125f, 0.75f, 1.0f);
  30. // body
  31. addCuboid(0.0f, 0.0f, 0.0f, 0.5f, 0.75f, 0.25f,
  32. 0.3125f, 0.25f, 0.4375f, 0.3125f,
  33. 0.4375f, 0.25f, 0.5625f, 0.3125f,
  34. 0.4375f, 0.3125f, 0.5f, 0.5f,
  35. 0.25f, 0.3125f, 0.3125f, 0.5f,
  36. 0.3125f, 0.3125f, 0.4375f, 0.5f,
  37. 0.5f, 0.3125f, 0.625f, 0.5f);
  38. // right leg
  39. addCuboid(0.0f, 0.0f, 0.0f, 0.25f, 0.75f, 0.25f,
  40. 0.0625f, 0.25f, 0.125f, 0.3125f,
  41. 0.125f, 0.25f, 0.1875f, 0.3125f,
  42. 0.125f, 0.3125f, 0.1875f, 0.5f,
  43. 0.0f, 0.3125f, 0.0625f, 0.5f,
  44. 0.0625f, 0.3125f, 0.125f, 0.5f,
  45. 0.1875f, 0.3125f, 0.25f, 0.5f);
  46. // left leg
  47. addCuboid(0.0f, 0.0f, 0.0f, 0.25f, 0.75f, 0.25f,
  48. 0.3125f, 0.75f, 0.375f, 0.8125f,
  49. 0.375f, 0.75f, 0.4375f, 0.8125f,
  50. 0.375f, 0.8125f, 0.4375f, 1.0f,
  51. 0.25f, 0.8125f, 0.3125f, 1.0f,
  52. 0.3125f, 0.8125f, 0.375f, 1.0f,
  53. 0.4375f, 0.8125f, 0.5f, 1.0f);
  54. mesh.build();
  55. }
  56. EntityRenderer::~EntityRenderer()
  57. {
  58. }
  59. void EntityRenderer::tick()
  60. {
  61. lifetime += 15;
  62. }
  63. void EntityRenderer::renderTick(Shader& shader, Camera3D camera, DirectRenderer& dr, float lag)
  64. {
  65. const int d = 360;
  66. float f1 = lifetime % d;
  67. if(f1 >= d / 2)
  68. {
  69. f1 = d - f1;
  70. }
  71. float f2 = (lifetime + 15) % d;
  72. if(f2 >= d / 2)
  73. {
  74. f2 = d - f2;
  75. }
  76. float inter = interpolate(lag, f1, f2);
  77. texture.bind();
  78. shader.setToIdentity();
  79. shader.translateTo(0.0f, -10.0f, 0.0f);
  80. shader.push();
  81. shader.translate(0.25f, 1.5f, 0.0f);
  82. Engine::setWorldModelMatrix(shader.getModelMatrix());
  83. mesh.draw(0, 36);
  84. shader.pop();
  85. shader.push();
  86. shader.translate(0.0f, 1.375f, 0.25f);
  87. shader.rotateX(inter / 6 - 15);
  88. shader.translate(0.0f, -0.625f, -0.125f);
  89. Engine::setWorldModelMatrix(shader.getModelMatrix());
  90. mesh.draw(36, 36);
  91. shader.pop();
  92. shader.push();
  93. shader.translate(0.75f, 1.375f, 0.25f);
  94. shader.rotateX(- inter / 6 + 15);
  95. shader.translate(0.0f, -0.625f, -0.125f);
  96. Engine::setWorldModelMatrix(shader.getModelMatrix());
  97. mesh.draw(72, 36);
  98. shader.pop();
  99. shader.push();
  100. shader.translate(0.25f, 0.75f, 0.125f);
  101. Engine::setWorldModelMatrix(shader.getModelMatrix());
  102. mesh.draw(108, 36);
  103. shader.pop();
  104. shader.push();
  105. shader.translate(0.25f, 0.0f, 0.125f);
  106. Engine::setWorldModelMatrix(shader.getModelMatrix());
  107. mesh.draw(144, 36);
  108. shader.pop();
  109. shader.push();
  110. shader.translate(0.5f, 0.0f, 0.125f);
  111. Engine::setWorldModelMatrix(shader.getModelMatrix());
  112. mesh.draw(180, 36);
  113. shader.pop();
  114. }
  115. void EntityRenderer::addTriangle(float p1x, float p1y, float p1z, float p1nx, float p1ny, float p1nz, float p1texX, float p1texY,
  116. float p2x, float p2y, float p2z, float p2nx, float p2ny, float p2nz, float p2texX, float p2texY,
  117. float p3x, float p3y, float p3z, float p3nx, float p3ny, float p3nz, float p3texX, float p3texY)
  118. {
  119. mesh.addPosition(p1x, p1y, p1z);
  120. mesh.addPosition(p2x, p2y, p2z);
  121. mesh.addPosition(p3x, p3y, p3z);
  122. mesh.addNormal(p1nx, p1ny, p1nz);
  123. mesh.addNormal(p2nx, p2ny, p2nz);
  124. mesh.addNormal(p3nx, p3ny, p3nz);
  125. mesh.addTexture(p1texX, p1texY);
  126. mesh.addTexture(p2texX, p2texY);
  127. mesh.addTexture(p3texX, p3texY);
  128. }
  129. void EntityRenderer::addCuboid(float sx, float sy, float sz, float ex, float ey, float ez,
  130. float topTexStartX, float topTexStartY, float topTexEndX, float topTexEndY,
  131. float bottomTexStartX, float bottomTexStartY, float bottomTexEndX, float bottomTexEndY,
  132. float northTexStartX, float northTexStartY, float northTexEndX, float northTexEndY,
  133. float southTexStartX, float southTexStartY, float southTexEndX, float southTexEndY,
  134. float eastTexStartX, float eastTexStartY, float eastTexEndX, float eastTexEndY,
  135. float westTexStartX, float westTexStartY, float westTexEndX, float westTexEndY)
  136. {
  137. // bottom side
  138. addTriangle(
  139. sx, sy, sz, 0.0f, -1.0f, 0.0f, bottomTexStartX, bottomTexStartY,
  140. ex, sy, sz, 0.0f, -1.0f, 0.0f, bottomTexEndX, bottomTexStartY,
  141. sx, sy, ez, 0.0f, -1.0f, 0.0f, bottomTexStartX, bottomTexEndY);
  142. addTriangle(
  143. ex, sy, sz, 0.0f, -1.0f, 0.0f, bottomTexEndX, bottomTexStartY,
  144. ex, sy, ez, 0.0f, -1.0f, 0.0f, bottomTexEndX, bottomTexEndY,
  145. sx, sy, ez, 0.0f, -1.0f, 0.0f, bottomTexStartX, bottomTexEndY);
  146. // top side
  147. addTriangle(
  148. sx, ey, sz, 0.0f, 1.0f, 0.0f, topTexStartX, topTexStartY,
  149. sx, ey, ez, 0.0f, 1.0f, 0.0f, topTexStartX, topTexEndY,
  150. ex, ey, sz, 0.0f, 1.0f, 0.0f, topTexEndX, topTexStartY);
  151. addTriangle(
  152. ex, ey, sz, 0.0f, 1.0f, 0.0f, topTexEndX, topTexStartY,
  153. sx, ey, ez, 0.0f, 1.0f, 0.0f, topTexStartX, topTexEndY,
  154. ex, ey, ez, 0.0f, 1.0f, 0.0f, topTexEndX, topTexEndY);
  155. // north side
  156. addTriangle(
  157. ex, sy, sz, 1.0f, 0.0f, 0.0f, northTexStartX, northTexEndY,
  158. ex, ey, ez, 1.0f, 0.0f, 0.0f, northTexEndX, northTexStartY,
  159. ex, sy, ez, 1.0f, 0.0f, 0.0f, northTexEndX, northTexEndY);
  160. addTriangle(
  161. ex, sy, sz, 1.0f, 0.0f, 0.0f, northTexStartX, northTexEndY,
  162. ex, ey, sz, 1.0f, 0.0f, 0.0f, northTexStartX, northTexStartY,
  163. ex, ey, ez, 1.0f, 0.0f, 0.0f, northTexEndX, northTexStartY);
  164. // south side
  165. addTriangle(
  166. sx, sy, sz, -1.0f, 0.0f, 0.0f, southTexStartX, southTexEndY,
  167. sx, sy, ez, -1.0f, 0.0f, 0.0f, southTexEndX, southTexEndY,
  168. sx, ey, ez, -1.0f, 0.0f, 0.0f, southTexEndX, southTexStartY);
  169. addTriangle(
  170. sx, sy, sz, -1.0f, 0.0f, 0.0f, southTexStartX, southTexEndY,
  171. sx, ey, ez, -1.0f, 0.0f, 0.0f, southTexEndX, southTexStartY,
  172. sx, ey, sz, -1.0f, 0.0f, 0.0f, southTexStartX, southTexStartY);
  173. // east side
  174. addTriangle(
  175. sx, sy, ez, 0.0f, 0.0f, 1.0f, eastTexStartX, eastTexEndY,
  176. ex, sy, ez, 0.0f, 0.0f, 1.0f, eastTexEndX, eastTexEndY,
  177. ex, ey, ez, 0.0f, 0.0f, 1.0f, eastTexEndX, eastTexStartY);
  178. addTriangle(
  179. sx, sy, ez, 0.0f, 0.0f, 1.0f, eastTexStartX, eastTexEndY,
  180. ex, ey, ez, 0.0f, 0.0f, 1.0f, eastTexEndX, eastTexStartY,
  181. sx, ey, ez, 0.0f, 0.0f, 1.0f, eastTexStartX, eastTexStartY);
  182. // west side
  183. addTriangle(
  184. sx, sy, sz, 0.0f, 0.0f, -1.0f, westTexStartX, westTexEndY,
  185. ex, ey, sz, 0.0f, 0.0f, -1.0f, westTexEndX, westTexStartY,
  186. ex, sy, sz, 0.0f, 0.0f, -1.0f, westTexEndX, westTexEndY);
  187. addTriangle(
  188. sx, sy, sz, 0.0f, 0.0f, -1.0f, westTexStartX, westTexEndY,
  189. sx, ey, sz, 0.0f, 0.0f, -1.0f, westTexStartX, westTexStartY,
  190. ex, ey, sz, 0.0f, 0.0f, -1.0f, westTexEndX, westTexStartY);
  191. }