voxelTerrain
 All Classes Functions Variables Typedefs Enumerations Pages
mesh.cpp

This is an example that converts a mesh, loaded by collada, to voxels and renders them.

voxel_mesh_wire.png
#include "blub/log/global.hpp"
#include "blub/log/system.hpp"
#include "blub/math/math.hpp"
#include "blub/math/quaternion.hpp"
#include "blub/math/sphere.hpp"
#include "blub/math/transform.hpp"
#include "blub/sync/identifier.hpp"
#include "blub/procedural/voxel/edit/mesh.hpp"
#include "blub/procedural/voxel/edit/sphere.hpp"
#include "blub/procedural/voxel/simple/container/inMemory.hpp"
#include "blub/procedural/voxel/terrain/accessor.hpp"
#include "blub/procedural/voxel/terrain/surface.hpp"
#include "blub/procedural/voxel/terrain/renderer.hpp"
#include "blub/procedural/voxel/tile/container.hpp"
#include "OgreTile.hpp"
#include "Handler.hpp"
using namespace blub::procedural;
using namespace blub;
typedef OgreTile<t_voxel> t_renderTile;
void createSphere(t_voxelContainer *container, const vector3 &position, const bool &cut);
int main(int /*argc*/, char* /*argv*/[])
{
blub::log::system::addConsole();
blub::log::system::addFile("voxelterrain.log");
Handler handler;
if (!handler.initialiseOgre())
{
return EXIT_FAILURE;
}
if (!handler.initialiseOIS())
{
return EXIT_FAILURE;
}
// initialise terrain
scopedPointer<t_voxelContainer> voxelContainer;
scopedPointer<t_voxelAccessor> voxelAccessor;
scopedPointer<t_voxelSurface> voxelSurface;
scopedPointer<t_voxelRenderer> voxelRenderer;
async::dispatcher terrainDispatcher(4, false, "terrain");
t_cameraIdentifier cameraIdentifier;
{
const int32 numLod(3); // no lod for mesh to voxel presentation
// voxel themself
voxelContainer.reset(new t_voxelContainer(terrainDispatcher));
// accessor
voxelAccessor.reset(new t_voxelAccessor(terrainDispatcher, *voxelContainer, numLod));
// surface
voxelSurface.reset(new t_voxelSurface(terrainDispatcher, *voxelAccessor));
// ogre3d render wrapper
const t_voxelRenderer::t_createTileCallback callbackCreate = boost::bind(t_renderTile::create, handler.renderScene, "none", &handler.graphicDispatcher);
// renderer
voxelRenderer.reset(new t_voxelRenderer(terrainDispatcher, *voxelSurface, lodRadien));
voxelRenderer->setCreateTileCallback(callbackCreate);
cameraIdentifier = sync::identifier::create();
voxelRenderer->addCamera(cameraIdentifier, handler.camera->getPosition());
handler.signalFrame()->connect(
[&] (real)
{
voxelRenderer->updateCamera(cameraIdentifier, handler.camera->getPosition());
}
);
// add/cut sphere when mouse-button gets pressed
handler.signalMouseGotPressed()->connect(
[&] (bool &left)
{
createSphere(voxelContainer.get(), handler.camera->getPosition()+handler.camera->getDirection()*10., !left);
}
);
}
// create voxel
{
mesh->load("/home/mlanner/Desktop/test.dae", transform(vector3(), quaternion(), 10.0));
voxelContainer->editVoxel(mesh);
}
// start rendering and begin terrain worker.
{
terrainDispatcher.start();
handler.renderSystem->startRendering(); // ends when render-window closes
terrainDispatcher.stop();
}
return EXIT_SUCCESS;
}
void createSphere(t_voxelContainer *container, const vector3 &position, const bool &cut)
{
BLUB_LOG_OUT() << "createSphere";
sphereEdit->setCut(cut);
container->editVoxel(sphereEdit);
}