123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171 |
- #include "client/rendering/block/BlockRenderer.h"
- #include "common/utils/Face.h"
- BlockRenderer::BlockRenderer()
- {
- }
- void BlockRenderer::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,
- int cullData)
- {
- // move texture coords a small amount to the middle of the triagngle
- // to prevent calculation errors getting out of the range of the texture
- float f = 1.0f / 256.0f;
-
- float mid1x = (((p2texX + p3texX) * 0.5) - p1texX) * f;
- float mid1y = (((p2texY + p3texY) * 0.5) - p1texY) * f;
- float mid2x = (((p1texX + p3texX) * 0.5) - p2texX) * f;
- float mid2y = (((p1texY + p3texY) * 0.5) - p2texY) * f;
- float mid3x = (((p2texX + p1texX) * 0.5) - p3texX) * f;
- float mid3y = (((p2texY + p1texY) * 0.5) - p3texY) * f;
-
- p1texX += mid1x;
- p1texY += mid1y;
- p2texX += mid2x;
- p2texY += mid2y;
- p3texX += mid3x;
- p3texY += mid3y;
-
- data.push_back(Triangle());
- Triangle& tri = data[data.size() - 1];
-
- tri.p1x = p1x;
- tri.p1y = p1y;
- tri.p1z = p1z;
-
- tri.p1nx = p1nx;
- tri.p1ny = p1ny;
- tri.p1nz = p1nz;
-
- tri.p1texX = p1texX;
- tri.p1texY = p1texY;
-
- tri.p2x = p2x;
- tri.p2y = p2y;
- tri.p2z = p2z;
-
- tri.p2nx = p2nx;
- tri.p2ny = p2ny;
- tri.p2nz = p2nz;
-
- tri.p2texX = p2texX;
- tri.p2texY = p2texY;
-
- tri.p3x = p3x;
- tri.p3y = p3y;
- tri.p3z = p3z;
-
- tri.p3nx = p3nx;
- tri.p3ny = p3ny;
- tri.p3nz = p3nz;
-
- tri.p3texX = p3texX;
- tri.p3texY = p3texY;
-
- tri.cullData = cullData;
- }
-
- void BlockRenderer::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,
- Face::DOWN.getCullData());
- 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,
- Face::DOWN.getCullData());
-
- // 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,
- Face::UP.getCullData());
- 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,
- Face::UP.getCullData());
-
- // 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,
- Face::NORTH.getCullData());
- 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,
- Face::NORTH.getCullData());
-
- // 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,
- Face::SOUTH.getCullData());
- 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,
- Face::SOUTH.getCullData());
-
- // 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,
- Face::EAST.getCullData());
- 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,
- Face::EAST.getCullData());
-
- // 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,
- Face::WEST.getCullData());
- 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,
- Face::WEST.getCullData());
- }
- void BlockRenderer::addToMesh(Mesh& m, int offsetX, int offsetY, int offsetZ, int cullData) const
- {
- for(const Triangle& tri : data)
- {
- // if any cull bits of a block are the same as of a triangle => do not render
- if((cullData & tri.cullData) == 0)
- {
- m.addPosition(offsetX + tri.p1x, offsetY + tri.p1y, offsetZ + tri.p1z);
- m.addPosition(offsetX + tri.p2x, offsetY + tri.p2y, offsetZ + tri.p2z);
- m.addPosition(offsetX + tri.p3x, offsetY + tri.p3y, offsetZ + tri.p3z);
-
- m.addNormal(tri.p1nx, tri.p1ny, tri.p1nz);
- m.addNormal(tri.p2nx, tri.p2ny, tri.p2nz);
- m.addNormal(tri.p3nx, tri.p3ny, tri.p3nz);
-
- m.addTexture(tri.p1texX, tri.p1texY);
- m.addTexture(tri.p2texX, tri.p2texY);
- m.addTexture(tri.p3texX, tri.p3texY);
- }
- }
- }
|