c++3dirrlicht

Irrlicht - Creating 3D plane/cube mesh


I am fairly new to Irrlicht, but I am not new to C++. For the last couple of weeks I did alot of Googling, reading Irrlicht API documentations, etc. For some reason I can't seems to be able to create a 3D plane mesh.

Here is what I got so far.

irr::scene::ISceneNode* ground = sceneManager->addMeshSceneNode(plane);
ground->setPosition(irr::core::vector3df(0, 0, 10));

irr::scene::ICameraSceneNode* cam = sceneManager->addCameraSceneNode();
cam->setTarget(ground->getPosition());
sceneManager->addMeshSceneNode(plane);

I also try creating a 3D cube mesh using this method

irr::scene::IMesh* plane = geomentryCreator->createPlaneMesh(irr::core::dimension2d<irr::f32>(100, 100), irr::core::dimension2d<irr::u32>(100, 100));
irr::scene::ISceneNode* cube = sceneManager->addCubeSceneNode(20);
cube->render();

For some reason the screen remain black with nothing rendered. Nothing seems to work. Any suggestions?


Solution

  • Your problem is that the camera and the plane both have the same Y coordinate. You never specified any position for the camera, so it is at the point (0, 0, 0), so its Y coordinate is 0. You also specified the coordinate of the plane to be (0, 0, 10), so its Y coordinate is also 0. Since the Y coordinate is up in Irrlicht, this means that you are looking at the plane from the slice like in this drawing:

    enter image description here

    This is why you don't see anything. To see something, you have to place the camera higher up. The point (0, 50, 0) will work.

    Also, if you don't have any lights in the scene, the plane will be black just like the background, since it's by default sensitive to lighting. To change this, you need to make the plane insensitive to lighting with the following code:

    plane->setMaterialFlag(irr::video::EMF_LIGHTING, false);
    

    If the plane's color is black, which it is by default, you will have a black plane on a black background, so you won't see anything. So I suggest you make the background white instead by using this as the beginScene method in the main loop:

    driver->beginScene(true, true, irr::video::SColor(255, 255, 255, 255));
    

    Normally with this code, you should be able to see the following screenshot:

    irr::IrrlichtDevice *device = irr::createDevice(irr::video::EDT_OPENGL);
    irr::video::IVideoDriver *driver = device->getVideoDriver();
    irr::scene::ISceneManager *sceneManager = device->getSceneManager();
    const irr::scene::IGeometryCreator *geomentryCreator = sceneManager->getGeometryCreator();
    
    irr::scene::IMesh* plane = geomentryCreator->createPlaneMesh(irr::core::dimension2d<irr::f32>(100, 100), irr::core::dimension2d<irr::u32>(100, 100));
    irr::scene::ISceneNode* cube = sceneManager->addCubeSceneNode(20);
    cube->render();
    
    irr::scene::ISceneNode* ground = sceneManager->addMeshSceneNode(plane);
    ground->setPosition(irr::core::vector3df(0, 0, 10));
    plane->setMaterialFlag(irr::video::EMF_LIGHTING, false);    //This is important
    
    irr::scene::ICameraSceneNode* cam = sceneManager->addCameraSceneNode();
    cam->setPosition(irr::core::vector3df(0, 50, 0));    //This is also important
    cam->setTarget(ground->getPosition());
    sceneManager->addMeshSceneNode(plane);
    
    while(device->run()){
        driver->beginScene(true, true, irr::video::SColor(255, 255, 255, 255));    //Important for the background to be white
        sceneManager->drawAll();
        driver->endScene();
    }
    

    enter image description here