Mysteriously dissappearing mesh

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Post Reply
Ijon
Posts: 5
Joined: Mon Dec 19, 2011 11:41 pm

Mysteriously dissappearing mesh

Post by Ijon »

When looking at a mesh from certain positions and at certain angles it dissappears. Does anybody know why this happens? Here's a sample code with a square, that disappears when turning right (or setting the camera target to (4, 0, 1)):

Code: Select all

#include <irrlicht.h>
 
using namespace irr;
using namespace core;
using namespace scene;
using namespace video;
 
SColor const WHITE(255, 255, 255, 255);
 
int main() {
 
    IrrlichtDevice* irrDevice = createDevice(EDT_OPENGL, dimension2d<u32>(1600, 900), 32, false, false, false, 0); if (irrDevice == 0) return 1;
    ISceneManager* irrScene = irrDevice->getSceneManager();
    IVideoDriver* irrDriver = irrDevice->getVideoDriver();
    ICameraSceneNode* Camera = irrScene->addCameraSceneNodeFPS(0, 100.0f, 0.001f);
    Camera->setPosition(vector3df(1.1f, 1.0f, -0.6f));  /// When turning right from this position the mesh dissappears
    Camera->setTarget(vector3df(3, 0, 1));
 
    SMesh* Mesh = new SMesh;
    SMeshBuffer* TempBuff =  new SMeshBuffer();
    Mesh->addMeshBuffer(TempBuff);
    TempBuff->drop();
 
    TempBuff->Vertices.push_back(S3DVertex(vector3df(2, 0, 0), vector3df(0, 0, 0), WHITE, vector2df(0, 1)));
    TempBuff->Vertices.push_back(S3DVertex(vector3df(2, 1, 0), vector3df(0, 0, 0), WHITE, vector2df(0, 0)));
    TempBuff->Vertices.push_back(S3DVertex(vector3df(3, 0, 0), vector3df(0, 0, 0), WHITE, vector2df(1, 1)));
    TempBuff->Vertices.push_back(S3DVertex(vector3df(3, 1, 0), vector3df(0, 0, 0), WHITE, vector2df(1, 0)));
    TempBuff->Indices.push_back(0);  //┐
    TempBuff->Indices.push_back(1);  //├ 1st triangle
    TempBuff->Indices.push_back(2);  //┘                 1----3
    TempBuff->Indices.push_back(3);  //┐                 |    |
    TempBuff->Indices.push_back(2);  //├ 2nd triangle    |    |
    TempBuff->Indices.push_back(1);  //┘                 0----2
    TempBuff->setDirty(EBT_VERTEX_AND_INDEX);
 
    IMeshSceneNode* Meshnode = irrScene->addMeshSceneNode(Mesh);
 
    while(irrDevice->run()) {
        irrDriver->beginScene(true, true, SColor(255, 128, 128, 128));
        irrScene->drawAll();
        irrDriver->endScene();
    }
    Mesh->drop();
    irrDevice->drop();
    return 0;
}
CuteAlien
Admin
Posts: 9934
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: Mysteriously dissappearing mesh

Post by CuteAlien »

BackfaceCulling is by default enabled (meaning you don't see polygons from the backside).
Disable here with:

Code: Select all

 
TempBuff->getMaterial().BackfaceCulling = false;
 
Also the camera is very close to the polygon, so if you move a little closer it will vanish because of the near-clipping-plane of the camera frustum.
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
Ijon
Posts: 5
Joined: Mon Dec 19, 2011 11:41 pm

Re: Mysteriously dissappearing mesh

Post by Ijon »

It's neither a problem with backface culling nor with the near clipping plane. The whole mesh disappears instantly, this can also happen at greater distances, the coordinates in the code are just the only ones that I know exactly.
CuteAlien
Admin
Posts: 9934
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: Mysteriously dissappearing mesh

Post by CuteAlien »

Hm, yes - I see what you mean now. Although I haven't have it happen yet at greater distances. But something is strange indeed.

edit: It's the culling. With Meshnode->setAutomaticCulling(EAC_OFF) you could disable this. But the clean solution is recalculating the boundinboxes:

Code: Select all

 
TempBuff->recalculateBoundingBox();
 
Mesh->recalculateBoundingBox();
 
You have to do that whenever you change meshbuffer or mesh manually.
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
Ijon
Posts: 5
Joined: Mon Dec 19, 2011 11:41 pm

Re: Mysteriously dissappearing mesh

Post by Ijon »

CuteAlien wrote: It's the culling. With Meshnode->setAutomaticCulling(EAC_OFF) you could disable this. But the clean solution is recalculating the boundinboxes
I accept only clean solutions, of course. :wink:

In the original code I work with

Code: Select all

TempBuff->BoundingBox.addInternalPoint(...);
(Obviously I deleted a bit too much for the code sample.)

However, I didn't recalculate the bounding box of the mesh. I fixed this and now it works properly. Big thanks to you!
Post Reply