Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "ChunkMeshHandler.h"
- ChunkMeshHandler::ChunkMeshHandler(std::vector<BlockType> &_blockTypes) : blockTypes(_blockTypes)
- {
- textureOffset = float(1) / World::maxBlockTypes;
- }
- ChunkMeshHandler::~ChunkMeshHandler()
- {
- }
- void ChunkMeshHandler::addChunk(Chunk &chunk)
- {
- std::vector<float> vertices = std::vector<float>();
- generateChunkVertices(vertices, chunk);
- ChunkMesh chunkMesh = ChunkMesh(chunk, vertices);
- chunkMeshes[chunk.getHash()] = chunkMesh;
- }
- void ChunkMeshHandler::removeChunk(Chunk &chunk)
- {
- chunkMeshes.erase(chunk.getHash());
- }
- void ChunkMeshHandler::generateChunkVertices(std::vector<float>& vertices, Chunk &chunk)
- {
- unsigned int index = 0;
- FaceCoords faceCoords;
- int startX = -1;
- int currentMeshId;
- bool meshing = false;
- std::vector<size_t> coords = { 0,0,0 };
- faceCoords.insert({ POSX,coords });
- faceCoords.insert({ NEGX,coords });
- faceCoords.insert({ POSY,coords });
- faceCoords.insert({ NEGY,coords });
- faceCoords.insert({ POSZ,coords });
- faceCoords.insert({ NEGZ,coords });
- for (size_t z = 0; z < chunk.chunkSize; z++)
- {
- for (size_t y = 0; y < chunk.chunkSize; y++)
- {
- for (size_t x = 0; x < chunk.chunkSize; x++)
- {
- int blockId = chunk.blocks[index];
- if (blockId != 0)
- {
- if(!blockId == currentMeshId || startX == -1){
- generateVertices(startX, x, y, z, blockId);
- startX = x;
- currentMeshId = blockId;
- }else if(blockId = currentMeshId){
- }
- TextureMap faceTextures = blockTypes[chunk.blocks[index]].faceTextures;
- float to = textureOffset;
- if (x < 31 && chunk.blocks[index + 1] == 0 || x == 31) {
- //generateBlockFace(vertices, POSX, x, y, z, faceTextures, to);
- std::vector<size_t> coords = { x,y,z };
- faceCoords = compareCoords(faceCoords, NEGZ, x, y, z);
- }
- if (x > 0 && chunk.blocks[index - 1] == 0 || x == 0) {
- //generateBlockFace(vertices, NEGX, x, y, z, faceTextures, to);
- std::vector<size_t> coords = { x,y,z };
- faceCoords = compareCoords(faceCoords, NEGZ, x, y, z);
- }
- if (y < 31 && chunk.blocks[index + chunk.chunkSize] == 0 || y == 31) {
- //generateBlockFace(vertices, POSY, x, y, z, faceTextures, to);
- std::vector<size_t> coords = { x,y,z };
- faceCoords = compareCoords(faceCoords, NEGZ, x, y, z);
- }
- if (y > 0 && chunk.blocks[index - chunk.chunkSize] == 0 || y == 0) {
- //generateBlockFace(vertices, NEGY, x, y, z, faceTextures, to);
- std::vector<size_t> coords = { x,y,z };
- faceCoords = compareCoords(faceCoords, NEGZ, x, y, z);
- }
- if (z < 31 && chunk.blocks[index + chunk.chunkSize * chunk.chunkSize] == 0 || z == 31) {
- //generateBlockFace(vertices, POSZ, x, y, z, faceTextures, to);
- std::vector<size_t> coords = { x,y,z };
- faceCoords = compareCoords(faceCoords, NEGZ, x, y, z);
- }
- if (z > 0 && chunk.blocks[index - chunk.chunkSize * chunk.chunkSize] == 0 || z == 0) {
- //generateBlockFace(vertices, NEGZ, x, y, z, faceTextures, to);
- std::vector<size_t> coords = { x,y,z };
- faceCoords = compareCoords(faceCoords, NEGZ, x, y, z);
- }
- //generateVertices(vertices, faceCoords, startCoords);
- }
- index++;
- }
- }
- }
- }
- std::vector<float>& ChunkMeshHandler::generateVertices(float startX, float x, float y, float z, int blockID) {
- std::vector<float> vertices;
- switch (face) {
- case NEGZ:
- vertices = {
- startX, y, z, 0, 0,
- startX + x, y, z, 1, 0,
- startX + x, y + 1, z, 1, 1,
- startX + x, y + 1, z, 1, 1,
- startX, y + 1 , z, 0, 1,
- startX, y, z, 0, 0,
- };
- break;
- case POSZ:
- vertices = {
- startX, y, z + 1, 0, 0,
- startX + x, y, z + 1, 1, 0,
- startX + x, y + 1, z + 1, 1, 1,
- startX + x, y + 1, z + 1, 1, 1,
- startX, y + 1, z + 1, 0, 1,
- startX, y, z + 1, 0, 0,
- };
- break;
- case NEGX:
- vertices = {
- startX, y + 1, z + 1, 1, 1,
- startX, y + 1, z, 0, 1,
- startX, y, z, 0, 0,
- startX, y, z, 0, 0,
- startX, y, z + 1, 1, 0,
- startX, y + 1, z + 1, 1, 1,
- };
- break;
- case POSX:
- vertices = {
- startX + x, y + 1, z + 1, 1, 1,
- startX + x, y + 1, z, 0, 1,
- startX + x, y, z, 0, 0,
- startX + x, y, z, 0, 0,
- startX + x, y, z + 1, 1, 0,
- startX + x, y + 1, z + 1, 1, 1,
- };
- break;
- case NEGY:
- vertices = {
- startX, y, z, 0, 0,
- startX + x, y, z, 1, 0,
- startX + x, y, z + 1, 1, 1,
- startX + x, y, z + 1, 1, 1,
- startX, y, z + 1, 0, 1,
- startX, y, z, 0, 0,
- };
- break;
- case POSY:
- vertices = {
- startX, y + 1, z, 0, 0,
- startX + x, y + 1, z, 1, 0,
- startX + x, y + 1, z + 1, 1, 1,
- startX + x, y + 1, z + 1, 1, 1,
- startX, y + 1, z + 1, 0, 1,
- startX, y + 1, z, 0, 0,
- };
- break;
- }
- mesh.insert(mesh.end(), vertices.begin(), vertices.end());
- }
- return mesh;
- }
- /*
- std::vector<float>& ChunkMeshHandler::generateVertices(std::vector<float>& mesh, FaceCoords faceCoords, std::vector<size_t> startCoords) {
- std::vector<float> vertices;
- for (auto& it : faceCoords) {
- BlockFace face = it.first;
- std::vector<size_t> coords = it.second;
- float x = coords.at(0);
- float y = coords.at(1);
- float z = coords.at(2);
- switch (face) {
- case NEGZ:
- vertices = {
- x, y, z, 0, 0,
- x + 1, y, z, 1, 0,
- x + 1, y + 1, z, 1, 1,
- x + 1, y + 1, z, 1, 1,
- x, y + 1 , z, 0, 1,
- x, y, z, 0, 0,
- };
- break;
- case POSZ:
- vertices = {
- x, y, z + 1, 0, 0,
- x + 1, y, z + 1, 1, 0,
- x + 1, y + 1, z + 1, 1, 1,
- x + 1, y + 1, z + 1, 1, 1,
- x, y + 1, z + 1, 0, 1,
- x, y, z + 1, 0, 0,
- };
- break;
- case NEGX:
- vertices = {
- x, y + 1, z + 1, 1, 1,
- x, y + 1, z, 0, 1,
- x, y, z, 0, 0,
- x, y, z, 0, 0,
- x, y, z + 1, 1, 0,
- x, y + 1, z + 1, 1, 1,
- };
- break;
- case POSX:
- vertices = {
- x + 1, y + 1, z + 1, 1, 1,
- x + 1, y + 1, z, 0, 1,
- x + 1, y, z, 0, 0,
- x + 1, y, z, 0, 0,
- x + 1, y, z + 1, 1, 0,
- x + 1, y + 1, z + 1, 1, 1,
- };
- break;
- case NEGY:
- vertices = {
- x, y, z, 0, 0,
- x + 1, y, z, 1, 0,
- x + 1, y, z + 1, 1, 1,
- x + 1, y, z + 1, 1, 1,
- x, y, z + 1, 0, 1,
- x, y, z, 0, 0,
- };
- break;
- case POSY:
- vertices = {
- x, y + 1, z, 0, 0,
- x + 1, y + 1, z, 1, 0,
- x + 1, y + 1, z + 1, 1, 1,
- x + 1, y + 1, z + 1, 1, 1,
- x, y + 1, z + 1, 0, 1,
- x, y + 1, z, 0, 0,
- };
- break;
- }
- mesh.insert(mesh.end(), vertices.begin(), vertices.end());
- }
- return mesh;
- }
- */
- FaceCoords ChunkMeshHandler::compareCoords(FaceCoords oldFaceCoords, BlockFace face, size_t x, size_t y, size_t z)
- {
- std::vector<size_t> currentCoords = oldFaceCoords.at(face);
- size_t oldX = currentCoords.at(0);
- size_t oldY = currentCoords.at(1);
- size_t oldZ = currentCoords.at(2);
- if (x > oldX) {
- currentCoords.at(0) = x;
- }
- if (y > oldY) {
- currentCoords.at(1) = y;
- }
- if (z > oldZ) {
- currentCoords.at(2) = z;
- }
- oldFaceCoords.insert({ face, currentCoords });
- return oldFaceCoords;
- }
- void ChunkMeshHandler::generateBlockFace(std::vector<float>& mesh, BlockFace face, float x, float y, float z, std::map<BlockFace, std::pair<float, float>> faceTextures, float textureOffset)
- {
- std::vector<float> vertices;
- float tx = faceTextures.at(face).first;
- float ty = faceTextures.at(face).second;
- float to = textureOffset;
- switch (face) {
- case NEGZ:
- vertices = {
- x, y, z, 0, 0,
- x + 1, y, z, 1, 0,
- x + 1, y + 1, z, 1, 1,
- x + 1, y + 1, z, 1, 1,
- x, y + 1 , z, 0, 1,
- x, y, z, 0, 0,
- };
- break;
- case POSZ:
- vertices = {
- x, y, z + 1, 0, 0,
- x + 1, y, z + 1, 1, 0,
- x + 1, y + 1, z + 1, 1, 1,
- x + 1, y + 1, z + 1, 1, 1,
- x, y + 1, z + 1, 0, 1,
- x, y, z + 1, 0, 0,
- };
- break;
- case NEGX:
- vertices = {
- x, y + 1, z + 1, 1, 1,
- x, y + 1, z, 0, 1,
- x, y, z, 0, 0,
- x, y, z, 0, 0,
- x, y, z + 1, 1, 0,
- x, y + 1, z + 1, 1, 1,
- };
- break;
- case POSX:
- vertices = {
- x + 1, y + 1, z + 1, 1, 1,
- x + 1, y + 1, z, 0, 1,
- x + 1, y, z, 0, 0,
- x + 1, y, z, 0, 0,
- x + 1, y, z + 1, 1, 0,
- x + 1, y + 1, z + 1, 1, 1,
- };
- break;
- case NEGY:
- vertices = {
- x, y, z, 0, 0,
- x + 1, y, z, 1, 0,
- x + 1, y, z + 1, 1, 1,
- x + 1, y, z + 1, 1, 1,
- x, y, z + 1, 0, 1,
- x, y, z, 0, 0,
- };
- break;
- case POSY:
- vertices = {
- x, y + 1, z, 0, 0,
- x + 1, y + 1, z, 1, 0,
- x + 1, y + 1, z + 1, 1, 1,
- x + 1, y + 1, z + 1, 1, 1,
- x, y + 1, z + 1, 0, 1,
- x, y + 1, z, 0, 0,
- };
- break;
- }
- mesh.insert(mesh.end(), vertices.begin(), vertices.end());
- }
- /*void ChunkMeshHandler::generateBlockFace(std::vector<float>& mesh, BlockFace face, float x, float y, float z, std::map<BlockFace, std::pair<float, float>> faceTextures, float textureOffset)
- {
- std::vector<float> vertices;
- float tx = faceTextures.at(face).first;
- float ty = faceTextures.at(face).second;
- float to = textureOffset;
- switch (face) {
- case NEGZ:
- vertices = {
- x, y, z, tx, ty,
- x + 1, y, z, tx + to, ty,
- x + 1, y + 1, z, tx + to, ty + to,
- x + 1, y + 1, z, tx + to, ty + to,
- x, y + 1 , z, tx, ty + to,
- x, y, z, tx, ty,
- };
- break;
- case POSZ:
- vertices = {
- x, y, z + 1, tx, ty,
- x + 1, y, z + 1, tx + to, ty,
- x + 1, y + 1, z + 1, tx + to, ty + to,
- x + 1, y + 1, z + 1, tx + to, ty + to,
- x, y + 1, z + 1, tx, ty + to,
- x, y, z + 1, tx, ty,
- };
- break;
- case NEGX:
- vertices = {
- x, y + 1, z + 1, tx + to, ty + to,
- x, y + 1, z, tx, ty + to,
- x, y, z, tx, ty,
- x, y, z, tx, ty,
- x, y, z + 1, tx + to, ty,
- x, y + 1, z + 1, tx + to, ty + to,
- };
- break;
- case POSX:
- vertices = {
- x + 1, y + 1, z + 1, tx + to, ty + to,
- x + 1, y + 1, z, tx, ty + to,
- x + 1, y, z, tx, ty,
- x + 1, y, z, tx, ty,
- x + 1, y, z + 1, tx + to, ty,
- x + 1, y + 1, z + 1, tx + to, ty + to,
- };
- break;
- case NEGY:
- vertices = {
- x, y, z, tx, ty,
- x + 1, y, z, tx + to, ty,
- x + 1, y, z + 1, tx + to, ty + to,
- x + 1, y, z + 1, tx + to, ty + to,
- x, y, z + 1, tx, ty + to,
- x, y, z, tx, ty,
- };
- break;
- case POSY:
- vertices = {
- x, y + 1, z, tx, ty,
- x + 1, y + 1, z, tx + to, ty,
- x + 1, y + 1, z + 1, tx + to, ty + to,
- x + 1, y + 1, z + 1, tx + to, ty + to,
- x, y + 1, z + 1, tx, ty + to,
- x, y + 1, z, tx, ty,
- };
- break;
- }
- mesh.insert(mesh.end(), vertices.begin(), vertices.end());
- }*/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement