c++irrlicht

Irrlicht a floor plane with createHillPlaneMesh


Hello I have a question with Irrlicht library

I want to do a map with a floor I use createHillPlaneMesh.

I'm actually learning Irrlicht, I actually have a camera and cubes.

here is when I launch it :

while (_device.get_device()->run()) {
        _device.get_driver()->beginScene(true, true, color);
        _scene.get_sceneManager()->drawAll();
        _device.get_driver()->endScene();
    }

I call addMapBlock and its work great :

void Eo::Scene::addMapBlock(irr::f32 x, irr::f32 y, irr::f32 z)
{
    irr::f32 unitSize = 10.0f;
    irr::core::vector3df pos;
    pos.X = x;
    pos.Y = y;
    pos.Z = z;
    irr::scene::IMeshSceneNode *cube = _sceneManager->addCubeSceneNode(
        unitSize, nullptr, -1, pos);
    cube->setMaterialFlag(irr::video::EMF_LIGHTING, false);
    cube->setMaterialTexture(0,
        _device.get_driver()->getTexture("../assets/img/texture.jpg"));
    _map.push(cube);
}

and here is when I create the floor but nothing happen :

void Eo::Scene::addMapFloor()
{
    irr::core::dimension2d<irr::f32> tileSize(100.0,100.0);
    irr::core::dimension2d<irr::u32> tileCount(50,50);
    auto material = new irr::video::SMaterial();
    irr::f32 hillHeight = 0;
    irr::core::dimension2d<irr::f32> countHills(20.0,20.0);
    irr::core::dimension2d<irr::f32> textureRepeatCount(1.0,1.0);
    irr::scene::IMesh *cube =
    _sceneManager->getGeometryCreator()->createHillPlaneMesh(
        tileSize,
        tileCount,
        material,
        hillHeight,
        countHills,
        textureRepeatCount);
    material->ColorMaterial = irr::video::E_COLOR_PLANE::ECP_BLUE;
    cube->setMaterialFlag(irr::video::EMF_WIREFRAME, true);
}

Might be I miss something but I try to read and replicate the documentation.


Solution

  • When you do:

    _sceneManager->getGeometryCreator()->createHillPlaneMesh(/* args */);
    

    You just create a mesh (the geometric description of an object). You then have to create a scene node to display your mesh somewhere in your scene:

    IMesh* mesh = _sceneManager->getGeometryCreator()->createHillPlaneMesh(/* args */);
    IMeshSceneNode* node = _sceneManager->addMeshSceneNode(mesh /*, optional args*/);
    // Once you don't need the mesh variable anymore, drop it.
    mesh->drop();
    

    Side note: It's extremely easy to forget to drop something (especially if parts of the code can throw exceptions), resulting in memory leaks. I advise that you wrap these pointers in some sort of smart pointer (here std::unique_ptr with a custom Deleter, for example).