Exemple #1
0
void Chunk::createCube(int _x, int _y, int _z, std::vector<float> *_vertexes, std::vector<float> *_normals, std::vector<float> *_colours, std::vector<float> *_textureCoords, bool _allActive /*= false*/)
{
	if (!m_pBlocks[_x][_y][_z].isActive()) return;

	sf::Vector3f p1 = sf::Vector3f((_x + 0) * BLOCK_SIZE, (_y + 0) * BLOCK_SIZE, (_z + 1) * BLOCK_SIZE);
	sf::Vector3f p2 = sf::Vector3f((_x + 1) * BLOCK_SIZE, (_y + 0) * BLOCK_SIZE, (_z + 1) * BLOCK_SIZE);
	sf::Vector3f p3 = sf::Vector3f((_x + 1) * BLOCK_SIZE, (_y + 1) * BLOCK_SIZE, (_z + 1) * BLOCK_SIZE);
	sf::Vector3f p4 = sf::Vector3f((_x + 0) * BLOCK_SIZE, (_y + 1) * BLOCK_SIZE, (_z + 1) * BLOCK_SIZE);
	sf::Vector3f p5 = sf::Vector3f((_x + 1) * BLOCK_SIZE, (_y + 0) * BLOCK_SIZE, (_z + 0) * BLOCK_SIZE);
	sf::Vector3f p6 = sf::Vector3f((_x + 0) * BLOCK_SIZE, (_y + 0) * BLOCK_SIZE, (_z + 0) * BLOCK_SIZE);
	sf::Vector3f p7 = sf::Vector3f((_x + 0) * BLOCK_SIZE, (_y + 1) * BLOCK_SIZE, (_z + 0) * BLOCK_SIZE);
	sf::Vector3f p8 = sf::Vector3f((_x + 1) * BLOCK_SIZE, (_y + 1) * BLOCK_SIZE, (_z + 0) * BLOCK_SIZE);

	sf::Vector3f n;
	sf::Vector2f tex;

	DataManager *data = m_ChunkManager->getDataManager();

	Block::BlockType btype = m_pBlocks[_x][_y][_z].getType();

	bool front =	true;
	bool back =		true;
	bool left =		true;
	bool right =	true;
	bool top =		true;
	bool bottom =	true;

	if (!_allActive)
	{

		if (_x > 0)
		{
			if (m_pBlocks[_x - 1][_y][_z].isActive())
			{
				left = false;
			}
		}
		if (_x + 1 < CHUNK_SIZE)
		{
			if (m_pBlocks[_x + 1][_y][_z].isActive())
			{
				right = false;
			}
		}
		if (_y > 0)
		{
			if (m_pBlocks[_x][_y - 1][_z].isActive())
			{
				bottom = false;
			}
		}
		if (_y + 1 < CHUNK_SIZE)
		{
			if (m_pBlocks[_x][_y + 1][_z].isActive())
			{
				top = false;
			}
		}
		if (_z > 0)
		{
			if (m_pBlocks[_x][_y][_z - 1].isActive())
			{
				back = false;
			}
		}
		if (_z + 1 < CHUNK_SIZE)
		{
			if (m_pBlocks[_x][_y][_z + 1].isActive())
			{
				front = false;
			}
		}

	}


	if (front)
	{
		//~ Front
		n = sf::Vector3f(0.0f, 0.0f, 1.0f);
		//~ Triangle 1 {1, 2, 3}
		_vertexes->push_back(p1.x);			_vertexes->push_back(p1.y);			_vertexes->push_back(p1.z);
		_normals->push_back(n.x);			_normals->push_back(n.y);			_normals->push_back(n.z);
		_colours->push_back(0.0f);			_colours->push_back(1.0f);			_colours->push_back(0.0f);
		tex = Block::getTexCoords(data->getTileSheetCoords(btype, Block::FaceIndex::FRO), Block::VertIndex::BOT_LEF);
		_textureCoords->push_back(tex.x);	_textureCoords->push_back(tex.y);
		_vertexes->push_back(p2.x);			_vertexes->push_back(p2.y);			_vertexes->push_back(p2.z);
		_normals->push_back(n.x);			_normals->push_back(n.y);			_normals->push_back(n.z);
		_colours->push_back(0.0f);			_colours->push_back(1.0f);			_colours->push_back(0.0f);
		tex = Block::getTexCoords(data->getTileSheetCoords(btype, Block::FaceIndex::FRO), Block::VertIndex::BOT_RIG);
		_textureCoords->push_back(tex.x);	_textureCoords->push_back(tex.y);
		_vertexes->push_back(p3.x);			_vertexes->push_back(p3.y);			_vertexes->push_back(p3.z);
		_normals->push_back(n.x);			_normals->push_back(n.y);			_normals->push_back(n.z);
		_colours->push_back(0.0f);			_colours->push_back(1.0f);			_colours->push_back(0.0f);
		tex = Block::getTexCoords(data->getTileSheetCoords(btype, Block::FaceIndex::FRO), Block::VertIndex::TOP_RIG);
		_textureCoords->push_back(tex.x);	_textureCoords->push_back(tex.y);
		//~ Triangle 2 {1, 3, 4}
		_vertexes->push_back(p1.x);			_vertexes->push_back(p1.y);			_vertexes->push_back(p1.z);
		_normals->push_back(n.x);			_normals->push_back(n.y);			_normals->push_back(n.z);
		_colours->push_back(0.0f);			_colours->push_back(1.0f);			_colours->push_back(0.0f);
		tex = Block::getTexCoords(data->getTileSheetCoords(btype, Block::FaceIndex::FRO), Block::VertIndex::BOT_LEF);
		_textureCoords->push_back(tex.x);	_textureCoords->push_back(tex.y);
		_vertexes->push_back(p3.x);			_vertexes->push_back(p3.y);			_vertexes->push_back(p3.z);
		_normals->push_back(n.x);			_normals->push_back(n.y);			_normals->push_back(n.z);
		_colours->push_back(0.0f);			_colours->push_back(1.0f);			_colours->push_back(0.0f);
		tex = Block::getTexCoords(data->getTileSheetCoords(btype, Block::FaceIndex::FRO), Block::VertIndex::TOP_RIG);
		_textureCoords->push_back(tex.x);	_textureCoords->push_back(tex.y);
		_vertexes->push_back(p4.x);			_vertexes->push_back(p4.y);			_vertexes->push_back(p4.z);
		_normals->push_back(n.x);			_normals->push_back(n.y);			_normals->push_back(n.z);
		_colours->push_back(0.0f);			_colours->push_back(1.0f);			_colours->push_back(0.0f);
		tex = Block::getTexCoords(data->getTileSheetCoords(btype, Block::FaceIndex::FRO), Block::VertIndex::TOP_LEF);
		_textureCoords->push_back(tex.x);	_textureCoords->push_back(tex.y);
	}
	
	if (back)
	{
		//~ Back
		n = sf::Vector3f(0.0f, 0.0f, -1.0f);
		//~ Triangle 1 {5, 6, 7}
		_vertexes->push_back(p5.x);			_vertexes->push_back(p5.y);			_vertexes->push_back(p5.z);
		_normals->push_back(n.x);			_normals->push_back(n.y);			_normals->push_back(n.z);
		_colours->push_back(0.0f);			_colours->push_back(1.0f);			_colours->push_back(0.0f);
		tex = Block::getTexCoords(data->getTileSheetCoords(btype, Block::FaceIndex::BAC), Block::VertIndex::BOT_LEF);
		_textureCoords->push_back(tex.x);	_textureCoords->push_back(tex.y);
		_vertexes->push_back(p6.x);			_vertexes->push_back(p6.y);			_vertexes->push_back(p6.z);
		_normals->push_back(n.x);			_normals->push_back(n.y);			_normals->push_back(n.z);
		_colours->push_back(0.0f);			_colours->push_back(1.0f);			_colours->push_back(0.0f);
		tex = Block::getTexCoords(data->getTileSheetCoords(btype, Block::FaceIndex::BAC), Block::VertIndex::BOT_RIG);
		_textureCoords->push_back(tex.x);	_textureCoords->push_back(tex.y);
		_vertexes->push_back(p7.x);			_vertexes->push_back(p7.y);			_vertexes->push_back(p7.z);
		_normals->push_back(n.x);			_normals->push_back(n.y);			_normals->push_back(n.z);
		_colours->push_back(0.0f);			_colours->push_back(1.0f);			_colours->push_back(0.0f);
		tex = Block::getTexCoords(data->getTileSheetCoords(btype, Block::FaceIndex::BAC), Block::VertIndex::TOP_RIG);
		_textureCoords->push_back(tex.x);	_textureCoords->push_back(tex.y);
		//~ Triangle 2 {5, 7, 8}
		_vertexes->push_back(p5.x);			_vertexes->push_back(p5.y);			_vertexes->push_back(p5.z);
		_normals->push_back(n.x);			_normals->push_back(n.y);			_normals->push_back(n.z);
		_colours->push_back(0.0f);			_colours->push_back(1.0f);			_colours->push_back(0.0f);
		tex = Block::getTexCoords(data->getTileSheetCoords(btype, Block::FaceIndex::BAC), Block::VertIndex::BOT_LEF);
		_textureCoords->push_back(tex.x);	_textureCoords->push_back(tex.y);
		_vertexes->push_back(p7.x);			_vertexes->push_back(p7.y);			_vertexes->push_back(p7.z);
		_normals->push_back(n.x);			_normals->push_back(n.y);			_normals->push_back(n.z);
		_colours->push_back(0.0f);			_colours->push_back(1.0f);			_colours->push_back(0.0f);
		tex = Block::getTexCoords(data->getTileSheetCoords(btype, Block::FaceIndex::BAC), Block::VertIndex::TOP_RIG);
		_textureCoords->push_back(tex.x);	_textureCoords->push_back(tex.y);
		_vertexes->push_back(p8.x);			_vertexes->push_back(p8.y);			_vertexes->push_back(p8.z);
		_normals->push_back(n.x);			_normals->push_back(n.y);			_normals->push_back(n.z);
		_colours->push_back(0.0f);			_colours->push_back(1.0f);			_colours->push_back(0.0f);
		tex = Block::getTexCoords(data->getTileSheetCoords(btype, Block::FaceIndex::BAC), Block::VertIndex::TOP_LEF);
		_textureCoords->push_back(tex.x);	_textureCoords->push_back(tex.y);
	}

	if (right)
	{
		//~ Right
		n = sf::Vector3f(1.0f, 0.0f, 0.0f);
		//~ Triangle 1 {2, 5, 8}
		_vertexes->push_back(p2.x);			_vertexes->push_back(p2.y);			_vertexes->push_back(p2.z);
		_normals->push_back(n.x);			_normals->push_back(n.y);			_normals->push_back(n.z);
		_colours->push_back(0.0f);			_colours->push_back(1.0f);			_colours->push_back(0.0f);
		tex = Block::getTexCoords(data->getTileSheetCoords(btype, Block::FaceIndex::RIG), Block::VertIndex::BOT_LEF);
		_textureCoords->push_back(tex.x);	_textureCoords->push_back(tex.y);
		_vertexes->push_back(p5.x);			_vertexes->push_back(p5.y);			_vertexes->push_back(p5.z);
		_normals->push_back(n.x);			_normals->push_back(n.y);			_normals->push_back(n.z);
		_colours->push_back(0.0f);			_colours->push_back(1.0f);			_colours->push_back(0.0f);
		tex = Block::getTexCoords(data->getTileSheetCoords(btype, Block::FaceIndex::RIG), Block::VertIndex::BOT_RIG);
		_textureCoords->push_back(tex.x);	_textureCoords->push_back(tex.y);
		_vertexes->push_back(p8.x);			_vertexes->push_back(p8.y);			_vertexes->push_back(p8.z);
		_normals->push_back(n.x);			_normals->push_back(n.y);			_normals->push_back(n.z);
		_colours->push_back(0.0f);			_colours->push_back(1.0f);			_colours->push_back(0.0f);
		tex = Block::getTexCoords(data->getTileSheetCoords(btype, Block::FaceIndex::RIG), Block::VertIndex::TOP_RIG);
		_textureCoords->push_back(tex.x);	_textureCoords->push_back(tex.y);
		//~ Triangle 2 {2, 8, 3}
		_vertexes->push_back(p2.x);			_vertexes->push_back(p2.y);			_vertexes->push_back(p2.z);
		_normals->push_back(n.x);			_normals->push_back(n.y);			_normals->push_back(n.z);
		_colours->push_back(0.0f);			_colours->push_back(1.0f);			_colours->push_back(0.0f);
		tex = Block::getTexCoords(data->getTileSheetCoords(btype, Block::FaceIndex::RIG), Block::VertIndex::BOT_LEF);
		_textureCoords->push_back(tex.x);	_textureCoords->push_back(tex.y);
		_vertexes->push_back(p8.x);			_vertexes->push_back(p8.y);			_vertexes->push_back(p8.z);
		_normals->push_back(n.x);			_normals->push_back(n.y);			_normals->push_back(n.z);
		_colours->push_back(0.0f);			_colours->push_back(1.0f);			_colours->push_back(0.0f);
		tex = Block::getTexCoords(data->getTileSheetCoords(btype, Block::FaceIndex::RIG), Block::VertIndex::TOP_RIG);
		_textureCoords->push_back(tex.x);	_textureCoords->push_back(tex.y);
		_vertexes->push_back(p3.x);			_vertexes->push_back(p3.y);			_vertexes->push_back(p3.z);
		_normals->push_back(n.x);			_normals->push_back(n.y);			_normals->push_back(n.z);
		_colours->push_back(0.0f);			_colours->push_back(1.0f);			_colours->push_back(0.0f);
		tex = Block::getTexCoords(data->getTileSheetCoords(btype, Block::FaceIndex::RIG), Block::VertIndex::TOP_LEF);
		_textureCoords->push_back(tex.x);	_textureCoords->push_back(tex.y);
	}

	if (left)
	{
		//~ Left
		n = sf::Vector3f(-1.0f, 0.0f, 0.0f);
		//~ Triangle 1 {6, 1, 4}
		_vertexes->push_back(p6.x);			_vertexes->push_back(p6.y);			_vertexes->push_back(p6.z);
		_normals->push_back(n.x);			_normals->push_back(n.y);			_normals->push_back(n.z);
		_colours->push_back(0.0f);			_colours->push_back(1.0f);			_colours->push_back(0.0f);
		tex = Block::getTexCoords(data->getTileSheetCoords(btype, Block::FaceIndex::LEF), Block::VertIndex::BOT_LEF);
		_textureCoords->push_back(tex.x);	_textureCoords->push_back(tex.y);
		_vertexes->push_back(p1.x);			_vertexes->push_back(p1.y);			_vertexes->push_back(p1.z);
		_normals->push_back(n.x);			_normals->push_back(n.y);			_normals->push_back(n.z);
		_colours->push_back(0.0f);			_colours->push_back(1.0f);			_colours->push_back(0.0f);
		tex = Block::getTexCoords(data->getTileSheetCoords(btype, Block::FaceIndex::LEF), Block::VertIndex::BOT_RIG);
		_textureCoords->push_back(tex.x);	_textureCoords->push_back(tex.y);
		_vertexes->push_back(p4.x);			_vertexes->push_back(p4.y);			_vertexes->push_back(p4.z);
		_normals->push_back(n.x);			_normals->push_back(n.y);			_normals->push_back(n.z);
		_colours->push_back(0.0f);			_colours->push_back(1.0f);			_colours->push_back(0.0f);
		tex = Block::getTexCoords(data->getTileSheetCoords(btype, Block::FaceIndex::LEF), Block::VertIndex::TOP_RIG);
		_textureCoords->push_back(tex.x);	_textureCoords->push_back(tex.y);
		//~ Triangle 2 {6, 4, 7}
		_vertexes->push_back(p6.x);			_vertexes->push_back(p6.y);			_vertexes->push_back(p6.z);
		_normals->push_back(n.x);			_normals->push_back(n.y);			_normals->push_back(n.z);
		_colours->push_back(0.0f);			_colours->push_back(1.0f);			_colours->push_back(0.0f);
		tex = Block::getTexCoords(data->getTileSheetCoords(btype, Block::FaceIndex::LEF), Block::VertIndex::BOT_LEF);
		_textureCoords->push_back(tex.x);	_textureCoords->push_back(tex.y);
		_vertexes->push_back(p4.x);			_vertexes->push_back(p4.y);			_vertexes->push_back(p4.z);
		_normals->push_back(n.x);			_normals->push_back(n.y);			_normals->push_back(n.z);
		_colours->push_back(0.0f);			_colours->push_back(1.0f);			_colours->push_back(0.0f);
		tex = Block::getTexCoords(data->getTileSheetCoords(btype, Block::FaceIndex::LEF), Block::VertIndex::TOP_RIG);
		_textureCoords->push_back(tex.x);	_textureCoords->push_back(tex.y);
		_vertexes->push_back(p7.x);			_vertexes->push_back(p7.y);			_vertexes->push_back(p7.z);
		_normals->push_back(n.x);			_normals->push_back(n.y);			_normals->push_back(n.z);
		_colours->push_back(0.0f);			_colours->push_back(1.0f);			_colours->push_back(0.0f);
		tex = Block::getTexCoords(data->getTileSheetCoords(btype, Block::FaceIndex::LEF), Block::VertIndex::TOP_LEF);
		_textureCoords->push_back(tex.x);	_textureCoords->push_back(tex.y);
	}

	if (bottom)
	{
		//~ Bottom
		n = sf::Vector3f(0.0f, -1.0f, 0.0f);
		//~ Triangle 1 {2, 1, 6}
		_vertexes->push_back(p2.x);			_vertexes->push_back(p2.y);			_vertexes->push_back(p2.z);
		_normals->push_back(n.x);			_normals->push_back(n.y);			_normals->push_back(n.z);
		_colours->push_back(0.0f);			_colours->push_back(1.0f);			_colours->push_back(0.0f);
		tex = Block::getTexCoords(data->getTileSheetCoords(btype, Block::FaceIndex::BOT), Block::VertIndex::BOT_LEF);
		_textureCoords->push_back(tex.x);	_textureCoords->push_back(tex.y);
		_vertexes->push_back(p1.x);			_vertexes->push_back(p1.y);			_vertexes->push_back(p1.z);
		_normals->push_back(n.x);			_normals->push_back(n.y);			_normals->push_back(n.z);
		_colours->push_back(0.0f);			_colours->push_back(1.0f);			_colours->push_back(0.0f);
		tex = Block::getTexCoords(data->getTileSheetCoords(btype, Block::FaceIndex::BOT), Block::VertIndex::BOT_RIG);
		_textureCoords->push_back(tex.x);	_textureCoords->push_back(tex.y);
		_vertexes->push_back(p6.x);			_vertexes->push_back(p6.y);			_vertexes->push_back(p6.z);
		_normals->push_back(n.x);			_normals->push_back(n.y);			_normals->push_back(n.z);
		_colours->push_back(0.0f);			_colours->push_back(1.0f);			_colours->push_back(0.0f);
		tex = Block::getTexCoords(data->getTileSheetCoords(btype, Block::FaceIndex::BOT), Block::VertIndex::TOP_RIG);
		_textureCoords->push_back(tex.x);	_textureCoords->push_back(tex.y);
		//~ Triangle 2 {2, 6, 5}
		_vertexes->push_back(p2.x);			_vertexes->push_back(p2.y);			_vertexes->push_back(p2.z);
		_normals->push_back(n.x);			_normals->push_back(n.y);			_normals->push_back(n.z);
		_colours->push_back(0.0f);			_colours->push_back(1.0f);			_colours->push_back(0.0f);
		tex = Block::getTexCoords(data->getTileSheetCoords(btype, Block::FaceIndex::BOT), Block::VertIndex::BOT_LEF);
		_textureCoords->push_back(tex.x);	_textureCoords->push_back(tex.y);
		_vertexes->push_back(p6.x);			_vertexes->push_back(p6.y);			_vertexes->push_back(p6.z);
		_normals->push_back(n.x);			_normals->push_back(n.y);			_normals->push_back(n.z);
		_colours->push_back(0.0f);			_colours->push_back(1.0f);			_colours->push_back(0.0f);
		tex = Block::getTexCoords(data->getTileSheetCoords(btype, Block::FaceIndex::BOT), Block::VertIndex::TOP_RIG);
		_textureCoords->push_back(tex.x);	_textureCoords->push_back(tex.y);
		_vertexes->push_back(p5.x);			_vertexes->push_back(p5.y);			_vertexes->push_back(p5.z);
		_normals->push_back(n.x);			_normals->push_back(n.y);			_normals->push_back(n.z);
		_colours->push_back(0.0f);			_colours->push_back(1.0f);			_colours->push_back(0.0f);
		tex = Block::getTexCoords(data->getTileSheetCoords(btype, Block::FaceIndex::BOT), Block::VertIndex::TOP_LEF);
		_textureCoords->push_back(tex.x);	_textureCoords->push_back(tex.y);
	}

	if (top)
	{
		//~ Top
		n = sf::Vector3f(0.0f, 1.0f, 0.0f);
		//~ Triangle 1 {4, 3, 8}
		_vertexes->push_back(p4.x);			_vertexes->push_back(p4.y);			_vertexes->push_back(p4.z);
		_normals->push_back(n.x);			_normals->push_back(n.y);			_normals->push_back(n.z);
		_colours->push_back(0.0f);			_colours->push_back(1.0f);			_colours->push_back(0.0f);
		tex = Block::getTexCoords(data->getTileSheetCoords(btype, Block::FaceIndex::TOP), Block::VertIndex::BOT_LEF);
		_textureCoords->push_back(tex.x);	_textureCoords->push_back(tex.y);
		_vertexes->push_back(p3.x);			_vertexes->push_back(p3.y);			_vertexes->push_back(p3.z);
		_normals->push_back(n.x);			_normals->push_back(n.y);			_normals->push_back(n.z);
		_colours->push_back(0.0f);			_colours->push_back(1.0f);			_colours->push_back(0.0f);
		tex = Block::getTexCoords(data->getTileSheetCoords(btype, Block::FaceIndex::TOP), Block::VertIndex::BOT_RIG);
		_textureCoords->push_back(tex.x);	_textureCoords->push_back(tex.y);
		_vertexes->push_back(p8.x);			_vertexes->push_back(p8.y);			_vertexes->push_back(p8.z);
		_normals->push_back(n.x);			_normals->push_back(n.y);			_normals->push_back(n.z);
		_colours->push_back(0.0f);			_colours->push_back(1.0f);			_colours->push_back(0.0f);
		tex = Block::getTexCoords(data->getTileSheetCoords(btype, Block::FaceIndex::TOP), Block::VertIndex::TOP_RIG);
		_textureCoords->push_back(tex.x);	_textureCoords->push_back(tex.y);
		//~ Triangle 2 {4, 8, 7}
		_vertexes->push_back(p4.x);			_vertexes->push_back(p4.y);			_vertexes->push_back(p4.z);
		_normals->push_back(n.x);			_normals->push_back(n.y);			_normals->push_back(n.z);
		_colours->push_back(0.0f);			_colours->push_back(1.0f);			_colours->push_back(0.0f);
		tex = Block::getTexCoords(data->getTileSheetCoords(btype, Block::FaceIndex::TOP), Block::VertIndex::BOT_LEF);
		_textureCoords->push_back(tex.x);	_textureCoords->push_back(tex.y);
		_vertexes->push_back(p8.x);			_vertexes->push_back(p8.y);			_vertexes->push_back(p8.z);
		_normals->push_back(n.x);			_normals->push_back(n.y);			_normals->push_back(n.z);
		_colours->push_back(0.0f);			_colours->push_back(1.0f);			_colours->push_back(0.0f);
		tex = Block::getTexCoords(data->getTileSheetCoords(btype, Block::FaceIndex::TOP), Block::VertIndex::TOP_RIG);
		_textureCoords->push_back(tex.x);	_textureCoords->push_back(tex.y);
		_vertexes->push_back(p7.x);			_vertexes->push_back(p7.y);			_vertexes->push_back(p7.z);
		_normals->push_back(n.x);			_normals->push_back(n.y);			_normals->push_back(n.z);
		_colours->push_back(0.0f);			_colours->push_back(1.0f);			_colours->push_back(0.0f);
		tex = Block::getTexCoords(data->getTileSheetCoords(btype, Block::FaceIndex::TOP), Block::VertIndex::TOP_LEF);
		_textureCoords->push_back(tex.x);	_textureCoords->push_back(tex.y);
	}
}