Exemple #1
0
void createSphereInVolume(TerrainPager* volData, float fRadius, PolyVox::Vector3DInt32 _center, uint8_t material)
{
	PolyVox::Vector3DFloat center( _center.getX(), _center.getY(), _center.getZ() );

	//This vector hold the position of the center of the volume
	PolyVox::Region volRegion(volData->getEnclosingRegion());

	//This three-level for loop iterates over every voxel in the volume
	for (int z = max(center.getZ() - fRadius, (float)volRegion.getLowerCorner().getZ()); z < min(center.getZ() + fRadius, (float)volRegion.getUpperCorner().getZ()); z++)
	{
		for (int y = max(center.getY() - fRadius, (float)volRegion.getLowerCorner().getY()); y < min(center.getY() + fRadius, (float)volRegion.getUpperCorner().getY()); y++)
		{
			for (int x = max(center.getX() - fRadius, (float)volRegion.getLowerCorner().getX()); x < min(center.getX() + fRadius, (float)volRegion.getUpperCorner().getX()); x++)
			{
				//Store our current position as a vector...
				PolyVox::Vector3DFloat v3dCurrentPos(x,y,z);
				//And compute how far the current position is from the center of the volume
				float fDistToCenter = (v3dCurrentPos - center).length();

				//If the current voxel is less than 'radius' units from the center then we make it solid.
				if(fDistToCenter <= fRadius)
				{
					PolyVox::Vector3DInt32 point(x,y,z);

					if( volRegion.containsPoint(point) )
					{
						//Get the old voxel
						PolyVox::Material<uint8_t>  voxel = volData->getVoxelAt(point);

						voxel.setMaterial(material);

						//Wrte the voxel value into the volume
						volData->setVoxelAt( point, voxel);
					}
				}
			}
		}
	}
}
Exemple #2
0
void BlockGrid::setBlock(PolyVox::Vector3DInt32 location, CompositeBlock::blockDataType block, bool front)
{
	_blockmap->setVoxel(location, block);
	Coords chunk = chunkManager->blockToChunkCoords(Coords(location.getX(), location.getY(), location.getZ()));
	chunkManager->setDirty(chunk, front);

	//also update surrounding chunks if the block borders them

	// TODO: Better logic to check if the chunk actually needs rebuilding
	//X-
	if (location.getX() % Chunk::chunkWidth == 0)
	{
		Coords chunk = chunkManager->blockToChunkCoords(Coords(location.getX()-1, location.getY(), location.getZ()));
		chunkManager->setDirty(chunk, front);
	}

	//X+
	else if (location.getX() % Chunk::chunkWidth == Chunk::chunkWidth-1)
	{
		Coords chunk = chunkManager->blockToChunkCoords(Coords(location.getX() + 1, location.getY(), location.getZ()));
		chunkManager->setDirty(chunk, front);
	}

	//Y-
	if (location.getY() % Chunk::chunkWidth == 0)
	{
		Coords chunk = chunkManager->blockToChunkCoords(Coords(location.getX(), location.getY() - 1, location.getZ()));
		chunkManager->setDirty(chunk, front);
	}

	//Y+
	else if (location.getY() % Chunk::chunkWidth == Chunk::chunkWidth - 1)
	{
		Coords chunk = chunkManager->blockToChunkCoords(Coords(location.getX(), location.getY() + 1, location.getZ()));
		chunkManager->setDirty(chunk,front);
	}

	//Z-
	if (location.getZ() % Chunk::chunkHeight == 0)
	{
		Coords chunk = chunkManager->blockToChunkCoords(Coords(location.getX(), location.getY(), location.getZ() - 1));
		chunkManager->setDirty(chunk, front);
	}

	//Z+
	else if (location.getZ() % Chunk::chunkHeight == Chunk::chunkHeight - 1)
	{
		Coords chunk = chunkManager->blockToChunkCoords(Coords(location.getX(), location.getY(), location.getZ() + 1));
		chunkManager->setDirty(chunk, front);
	}
}