123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216 |
- #include "EntityRenderer.h"
- #include "../../../engine/Wrapper.h"
- #include "../../../engine/Utils.h"
- EntityRenderer::EntityRenderer() : texture("resources/skin.png")
- {
- // head
- addCuboid(0.0f, 0.0f, 0.0f, 0.5f, 0.5f, 0.5f,
- 0.125f, 0.0f, 0.25f, 0.125f,
- 0.25f, 0.0f, 0.375f, 0.125f,
- 0.25f, 0.125f, 0.375f, 0.25f,
- 0.0f, 0.125f, 0.125f, 0.25f,
- 0.125f, 0.125f, 0.25f, 0.25f,
- 0.375f, 0.125f, 0.5f, 0.25f);
- // right arm
- addCuboid(0.0f, 0.0f, 0.0f, 0.25f, 0.75f, 0.25f,
- 0.6875f, 0.25f, 0.75f, 0.3125f,
- 0.75f, 0.25f, 0.8125f, 0.3125f,
- 0.75f, 0.3125f, 0.8125f, 0.5f,
- 0.625f, 0.3125f, 0.6875f, 0.5f,
- 0.6875f, 0.3125f, 0.75f, 0.5f,
- 0.8125f, 0.3125f, 0.875f, 0.5f);
- // left arm
- addCuboid(0.0f, 0.0f, 0.0f, 0.25f, 0.75f, 0.25f,
- 0.5625f, 0.75f, 0.625f, 0.8125f,
- 0.625f, 0.75f, 0.6875f, 0.8125f,
- 0.625f, 0.8125f, 0.6875f, 1.0f,
- 0.5f, 0.8125f, 0.5625f, 1.0f,
- 0.5625f, 0.8125f, 0.625f, 1.0f,
- 0.6875f, 0.8125f, 0.75f, 1.0f);
- // body
- addCuboid(0.0f, 0.0f, 0.0f, 0.5f, 0.75f, 0.25f,
- 0.3125f, 0.25f, 0.4375f, 0.3125f,
- 0.4375f, 0.25f, 0.5625f, 0.3125f,
- 0.4375f, 0.3125f, 0.5f, 0.5f,
- 0.25f, 0.3125f, 0.3125f, 0.5f,
- 0.3125f, 0.3125f, 0.4375f, 0.5f,
- 0.5f, 0.3125f, 0.625f, 0.5f);
- // right leg
- addCuboid(0.0f, 0.0f, 0.0f, 0.25f, 0.75f, 0.25f,
- 0.0625f, 0.25f, 0.125f, 0.3125f,
- 0.125f, 0.25f, 0.1875f, 0.3125f,
- 0.125f, 0.3125f, 0.1875f, 0.5f,
- 0.0f, 0.3125f, 0.0625f, 0.5f,
- 0.0625f, 0.3125f, 0.125f, 0.5f,
- 0.1875f, 0.3125f, 0.25f, 0.5f);
- // left leg
- addCuboid(0.0f, 0.0f, 0.0f, 0.25f, 0.75f, 0.25f,
- 0.3125f, 0.75f, 0.375f, 0.8125f,
- 0.375f, 0.75f, 0.4375f, 0.8125f,
- 0.375f, 0.8125f, 0.4375f, 1.0f,
- 0.25f, 0.8125f, 0.3125f, 1.0f,
- 0.3125f, 0.8125f, 0.375f, 1.0f,
- 0.4375f, 0.8125f, 0.5f, 1.0f);
- mesh.build();
- }
- EntityRenderer::~EntityRenderer()
- {
- }
- void EntityRenderer::tick()
- {
- lifetime += 15;
- }
- void EntityRenderer::renderTick(Shader& shader, Camera3D camera, DirectRenderer& dr, float lag)
- {
- const int d = 360;
-
- float f1 = lifetime % d;
- if(f1 >= d / 2)
- {
- f1 = d - f1;
- }
-
- float f2 = (lifetime + 15) % d;
- if(f2 >= d / 2)
- {
- f2 = d - f2;
- }
-
- float inter = interpolate(lag, f1, f2);
-
- texture.bind();
-
- shader.setToIdentity();
- shader.translateTo(0.0f, -10.0f, 0.0f);
-
- shader.push();
- shader.translate(0.25f, 1.5f, 0.0f);
- Engine::setWorldModelMatrix(shader.getModelMatrix());
- mesh.draw(0, 36);
- shader.pop();
-
- shader.push();
- shader.translate(0.0f, 1.375f, 0.25f);
- shader.rotateX(inter / 6 - 15);
- shader.translate(0.0f, -0.625f, -0.125f);
- Engine::setWorldModelMatrix(shader.getModelMatrix());
- mesh.draw(36, 36);
- shader.pop();
-
- shader.push();
- shader.translate(0.75f, 1.375f, 0.25f);
- shader.rotateX(- inter / 6 + 15);
- shader.translate(0.0f, -0.625f, -0.125f);
- Engine::setWorldModelMatrix(shader.getModelMatrix());
- mesh.draw(72, 36);
- shader.pop();
-
- shader.push();
- shader.translate(0.25f, 0.75f, 0.125f);
- Engine::setWorldModelMatrix(shader.getModelMatrix());
- mesh.draw(108, 36);
- shader.pop();
-
- shader.push();
- shader.translate(0.25f, 0.0f, 0.125f);
- Engine::setWorldModelMatrix(shader.getModelMatrix());
- mesh.draw(144, 36);
- shader.pop();
-
- shader.push();
- shader.translate(0.5f, 0.0f, 0.125f);
- Engine::setWorldModelMatrix(shader.getModelMatrix());
- mesh.draw(180, 36);
- shader.pop();
- }
- void EntityRenderer::addTriangle(float p1x, float p1y, float p1z, float p1nx, float p1ny, float p1nz, float p1texX, float p1texY,
- float p2x, float p2y, float p2z, float p2nx, float p2ny, float p2nz, float p2texX, float p2texY,
- float p3x, float p3y, float p3z, float p3nx, float p3ny, float p3nz, float p3texX, float p3texY)
- {
- mesh.addPosition(p1x, p1y, p1z);
- mesh.addPosition(p2x, p2y, p2z);
- mesh.addPosition(p3x, p3y, p3z);
-
- mesh.addNormal(p1nx, p1ny, p1nz);
- mesh.addNormal(p2nx, p2ny, p2nz);
- mesh.addNormal(p3nx, p3ny, p3nz);
-
- mesh.addTexture(p1texX, p1texY);
- mesh.addTexture(p2texX, p2texY);
- mesh.addTexture(p3texX, p3texY);
- }
- void EntityRenderer::addCuboid(float sx, float sy, float sz, float ex, float ey, float ez,
- float topTexStartX, float topTexStartY, float topTexEndX, float topTexEndY,
- float bottomTexStartX, float bottomTexStartY, float bottomTexEndX, float bottomTexEndY,
- float northTexStartX, float northTexStartY, float northTexEndX, float northTexEndY,
- float southTexStartX, float southTexStartY, float southTexEndX, float southTexEndY,
- float eastTexStartX, float eastTexStartY, float eastTexEndX, float eastTexEndY,
- float westTexStartX, float westTexStartY, float westTexEndX, float westTexEndY)
- {
- // bottom side
- addTriangle(
- sx, sy, sz, 0.0f, -1.0f, 0.0f, bottomTexStartX, bottomTexStartY,
- ex, sy, sz, 0.0f, -1.0f, 0.0f, bottomTexEndX, bottomTexStartY,
- sx, sy, ez, 0.0f, -1.0f, 0.0f, bottomTexStartX, bottomTexEndY);
- addTriangle(
- ex, sy, sz, 0.0f, -1.0f, 0.0f, bottomTexEndX, bottomTexStartY,
- ex, sy, ez, 0.0f, -1.0f, 0.0f, bottomTexEndX, bottomTexEndY,
- sx, sy, ez, 0.0f, -1.0f, 0.0f, bottomTexStartX, bottomTexEndY);
-
- // top side
- addTriangle(
- sx, ey, sz, 0.0f, 1.0f, 0.0f, topTexStartX, topTexStartY,
- sx, ey, ez, 0.0f, 1.0f, 0.0f, topTexStartX, topTexEndY,
- ex, ey, sz, 0.0f, 1.0f, 0.0f, topTexEndX, topTexStartY);
- addTriangle(
- ex, ey, sz, 0.0f, 1.0f, 0.0f, topTexEndX, topTexStartY,
- sx, ey, ez, 0.0f, 1.0f, 0.0f, topTexStartX, topTexEndY,
- ex, ey, ez, 0.0f, 1.0f, 0.0f, topTexEndX, topTexEndY);
-
- // north side
- addTriangle(
- ex, sy, sz, 1.0f, 0.0f, 0.0f, northTexStartX, northTexEndY,
- ex, ey, ez, 1.0f, 0.0f, 0.0f, northTexEndX, northTexStartY,
- ex, sy, ez, 1.0f, 0.0f, 0.0f, northTexEndX, northTexEndY);
- addTriangle(
- ex, sy, sz, 1.0f, 0.0f, 0.0f, northTexStartX, northTexEndY,
- ex, ey, sz, 1.0f, 0.0f, 0.0f, northTexStartX, northTexStartY,
- ex, ey, ez, 1.0f, 0.0f, 0.0f, northTexEndX, northTexStartY);
-
- // south side
- addTriangle(
- sx, sy, sz, -1.0f, 0.0f, 0.0f, southTexStartX, southTexEndY,
- sx, sy, ez, -1.0f, 0.0f, 0.0f, southTexEndX, southTexEndY,
- sx, ey, ez, -1.0f, 0.0f, 0.0f, southTexEndX, southTexStartY);
- addTriangle(
- sx, sy, sz, -1.0f, 0.0f, 0.0f, southTexStartX, southTexEndY,
- sx, ey, ez, -1.0f, 0.0f, 0.0f, southTexEndX, southTexStartY,
- sx, ey, sz, -1.0f, 0.0f, 0.0f, southTexStartX, southTexStartY);
-
- // east side
- addTriangle(
- sx, sy, ez, 0.0f, 0.0f, 1.0f, eastTexStartX, eastTexEndY,
- ex, sy, ez, 0.0f, 0.0f, 1.0f, eastTexEndX, eastTexEndY,
- ex, ey, ez, 0.0f, 0.0f, 1.0f, eastTexEndX, eastTexStartY);
- addTriangle(
- sx, sy, ez, 0.0f, 0.0f, 1.0f, eastTexStartX, eastTexEndY,
- ex, ey, ez, 0.0f, 0.0f, 1.0f, eastTexEndX, eastTexStartY,
- sx, ey, ez, 0.0f, 0.0f, 1.0f, eastTexStartX, eastTexStartY);
-
- // west side
- addTriangle(
- sx, sy, sz, 0.0f, 0.0f, -1.0f, westTexStartX, westTexEndY,
- ex, ey, sz, 0.0f, 0.0f, -1.0f, westTexEndX, westTexStartY,
- ex, sy, sz, 0.0f, 0.0f, -1.0f, westTexEndX, westTexEndY);
- addTriangle(
- sx, sy, sz, 0.0f, 0.0f, -1.0f, westTexStartX, westTexEndY,
- sx, ey, sz, 0.0f, 0.0f, -1.0f, westTexStartX, westTexStartY,
- ex, ey, sz, 0.0f, 0.0f, -1.0f, westTexEndX, westTexStartY);
- }
|