[SOLVED] Quad vanishes as the camera moves towards it?

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.
pandoragami
Posts: 226
Joined: Wed Jan 26, 2011 5:37 pm
Contact:

[SOLVED] Quad vanishes as the camera moves towards it?

Post by pandoragami »

Below is the image of the quad when the program starts but then if the camera moves towards it along the z axis (using up arrow) it vanishes for some reason? Below is the code.

Image

Code: Select all

#include <irrlicht.h>
using namespace irr;
 
class MyEventReceiver : public IEventReceiver
{
public:
    // This is the one method that we have to implement
    virtual bool OnEvent(const SEvent& event)
    {
        // Remember whether each key is down or up
        if (event.EventType == irr::EET_KEY_INPUT_EVENT)
            KeyIsDown[event.KeyInput.Key] = event.KeyInput.PressedDown;
 
        return false;
    }
 
    // This is used to check whether a key is being held down
    virtual bool IsKeyDown(EKEY_CODE keyCode) const
    {
        return KeyIsDown[keyCode];
    }
 
    MyEventReceiver()
    {
        for (u32 i=0; i<KEY_KEY_CODES_COUNT; ++i)
            KeyIsDown[i] = false;
    }
 
private:
    // We use this array to store the current state of each key
    bool KeyIsDown[KEY_KEY_CODES_COUNT];
};
 
MyEventReceiver receiver;
 
int main()
{
    IrrlichtDevice* device = 0;
    device = createDevice( video::EDT_OPENGL, core::dimension2d<u32>(640, 480), 16, false, false, false, &receiver);
 
    if (device == 0)
        return 1;
 
    video::IVideoDriver* driver = device->getVideoDriver();
    scene::ISceneManager* smgr = device->getSceneManager();
    driver->setTextureCreationFlag(video::ETCF_CREATE_MIP_MAPS, true);
 
    scene::ICameraSceneNode* cam = smgr->addCameraSceneNodeFPS( 0, 50.0f, 0.01f, -1, 0, 0, false, 0.0f, false, true);
    cam->setPosition(core::vector3df( 9, 12, 27));
    cam->setTarget(core::vector3df( 9, 11,5));
    device->getCursorControl()->setVisible(false);
 
    scene::SMesh *mesh = new scene::SMesh();
    scene::SMeshBuffer * meshbuf = new scene::SMeshBuffer();
 
    video::S3DVertex a(0,20, -50, 1,1,0, video::SColor(255,0,255,255), 0, 1);
    video::S3DVertex b(0,0, -50, 1,0,0, video::SColor(255,255,0,255), 1, 1);
    video::S3DVertex c(20,0,-50, 0,1,1, video::SColor(255,255,255,0), 1, 0);
    video::S3DVertex d(20,20, -50, 0,0,1, video::SColor(255,0,255,0), 0, 0);
 
    video::S3DVertex ver2[] = {a,b,c,d};
    u16 indices[] = {0, 1, 2, 2, 3, 0};
    mesh->addMeshBuffer(meshbuf);
    meshbuf->append((const void*)&ver2, 4, (u16*)&indices, 6);
 
    scene::IMeshSceneNode* Node = smgr->addMeshSceneNode(mesh);
    Node->setMaterialFlag(video::EMF_LIGHTING, false);
 
    int lastFPS = -1;
 
    while(device->run())
    {
        driver->beginScene(true, true, video::SColor(255,0,0,0));
        smgr->drawAll();
        driver->endScene();
        int fps = driver->getFPS();
 
        if (lastFPS != fps)
        {
            core::stringw str = L"[";
            str += " FPS:  ";
            str += fps;
            str += "]";
 
            device->setWindowCaption(str.c_str());
            lastFPS = fps;
        }
 
        device->sleep(50);
    }
 
    device->drop();
 
    return 0;
}
Last edited by pandoragami on Mon Mar 09, 2015 4:13 pm, edited 1 time in total.
mongoose7
Posts: 1227
Joined: Wed Apr 06, 2011 12:13 pm

Re: Quad vanishes as the camera moves towards it?

Post by mongoose7 »

Objects in front of the near plane are not displayed.
pandoragami
Posts: 226
Joined: Wed Jan 26, 2011 5:37 pm
Contact:

Re: Quad vanishes as the camera moves towards it?

Post by pandoragami »

mongoose7 wrote:Objects in front of the near plane are not displayed.
Sorry, I don't understand you, Irrlicht makes these camera problems dumbed down so folks like me can use it. Could you please elaborate?
pandoragami
Posts: 226
Joined: Wed Jan 26, 2011 5:37 pm
Contact:

Re: Quad vanishes as the camera moves towards it?

Post by pandoragami »

I checked the nearvalue by calling

Code: Select all

 
std::cout<<cam->getNearValue()<<std::endl;
 
and I just get 1? Are these units in terms of distance from the camera or do they mean something else?
mongoose7
Posts: 1227
Joined: Wed Apr 06, 2011 12:13 pm

Re: Quad vanishes as the camera moves towards it?

Post by mongoose7 »

Yes, it is the distance from the camera to the near plane.
CuteAlien
Admin
Posts: 9689
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: Quad vanishes as the camera moves towards it?

Post by CuteAlien »

Near plane is a good guess, but it's not causing the problem in this case. The mesh vanishes way too soon - which is usually a sign that it's something about node culling. You can always test that by disabling the culling completely. In this case with:

Code: Select all

 
Node->setAutomaticCulling(scene::EAC_OFF);
 
That works, but you've only hidden the problem now (culling should work). I suspect the reason culling is wrong here is because the bounding box is not calculated. IMeshBuffer has some function to recalculate that - call that and then it should likely also work without disabling culling (I've not not tested that).
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
mongoose7
Posts: 1227
Joined: Wed Apr 06, 2011 12:13 pm

Re: Quad vanishes as the camera moves towards it?

Post by mongoose7 »

He calls

Code: Select all

meshbuf->append((const void*)&ver2, 4, (u16*)&indices, 6);
which, when I looked at it, calls a function to add each position to the bounding box. Surely there is no need to update the bounding box after this?
CuteAlien
Admin
Posts: 9689
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: Quad vanishes as the camera moves towards it?

Post by CuteAlien »

In theory - but taking a quick look at the function it doesn't check if the bounding-box is inialized so it always contains 0,0,0 when used like that. Might be considered a bug in append (or a bad optimization).
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
hendu
Posts: 2600
Joined: Sat Dec 18, 2010 12:53 pm

Re: Quad vanishes as the camera moves towards it?

Post by hendu »

Address of an array, and it doesn't blow up at runtime?
pandoragami
Posts: 226
Joined: Wed Jan 26, 2011 5:37 pm
Contact:

Re: Quad vanishes as the camera moves towards it?

Post by pandoragami »

Thanks for the replies so far.

@CuteAlien Yes that lines works, but without it I also tried putting back the bounding box call (lines 67-70) with same result so the bounding box doesn't work as expected.
I'm guessing that backface-culling won't work either.

Here's the code.

Code: Select all

 
#include <irrlicht.h>
#include <iostream>
 
using namespace irr;
 
class MyEventReceiver : public IEventReceiver
{
public:
    // This is the one method that we have to implement
    virtual bool OnEvent(const SEvent& event)
    {
        // Remember whether each key is down or up
        if (event.EventType == irr::EET_KEY_INPUT_EVENT)
            KeyIsDown[event.KeyInput.Key] = event.KeyInput.PressedDown;
 
        return false;
    }
 
    // This is used to check whether a key is being held down
    virtual bool IsKeyDown(EKEY_CODE keyCode) const
    {
        return KeyIsDown[keyCode];
    }
 
    MyEventReceiver()
    {
        for (u32 i=0; i<KEY_KEY_CODES_COUNT; ++i)
            KeyIsDown[i] = false;
    }
 
private:
    // We use this array to store the current state of each key
    bool KeyIsDown[KEY_KEY_CODES_COUNT];
};
 
MyEventReceiver receiver;
 
int main()
{
    IrrlichtDevice* device = 0;
    device = createDevice( video::EDT_OPENGL, core::dimension2d<u32>(640, 480), 16, false, false, false, &receiver);
 
    if (device == 0)
        return 1;
 
    video::IVideoDriver* driver = device->getVideoDriver();
    scene::ISceneManager* smgr = device->getSceneManager();
    driver->setTextureCreationFlag(video::ETCF_CREATE_MIP_MAPS, true);
 
    scene::ICameraSceneNode* cam = smgr->addCameraSceneNodeFPS( 0, 50.0f, 0.01f, -1, 0, 0, false, 0.0f, false, true);
    cam->setPosition(core::vector3df( 9, 12, 27));
    cam->setTarget(core::vector3df( 9, 11,5));
    device->getCursorControl()->setVisible(false);
 
    scene::SMesh *mesh = new scene::SMesh();
    scene::SMeshBuffer * meshbuf = new scene::SMeshBuffer();
 
    video::S3DVertex a(0,20, -50, 1,1,0, video::SColor(255,0,255,255), 0, 1);
    video::S3DVertex b(0,0, -50, 1,0,0, video::SColor(255,255,0,255), 1, 1);
    video::S3DVertex c(20,0,-50, 0,1,1, video::SColor(255,255,255,0), 1, 0);
    video::S3DVertex d(20,20, -50, 0,0,1, video::SColor(255,0,255,0), 0, 0);
 
    video::S3DVertex ver2[] = {a,b,c,d};
    u16 indices[] = {0, 1, 2, 2, 3, 0};
    mesh->addMeshBuffer(meshbuf);
    meshbuf->append((const void*)&ver2, 4, (u16*)&indices, 6);
    meshbuf->Vertices.reallocate(4);//added these lines back
    meshbuf->Vertices.set_used(4);//added these lines back
    meshbuf->Indices.set_used(6);//added these lines back
    meshbuf->recalculateBoundingBox();//added these lines back
 
    scene::IMeshSceneNode* Node = smgr->addMeshSceneNode(mesh);
    Node->setMaterialFlag(video::EMF_LIGHTING, false);
    //Node->setAutomaticCulling(scene::EAC_OFF);
 
    int lastFPS = -1;
 
    while(device->run())
    {
        driver->beginScene(true, true, video::SColor(255,0,0,0));
        smgr->drawAll();
        driver->endScene();
        int fps = driver->getFPS();
 
        if (lastFPS != fps)
        {
            core::stringw str = L"[";
            str += " FPS:  ";
            str += fps;
            str += "]";
 
            device->setWindowCaption(str.c_str());
            lastFPS = fps;
        }
 
        device->sleep(50);
    }
 
    device->drop();
 
    return 0;
}
CuteAlien
Admin
Posts: 9689
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: Quad vanishes as the camera moves towards it?

Post by CuteAlien »

Backface culling is not related much to scenenode culling. Backface cullings is controlled by the material and done by the graphic-card per polygon. While scenenode culling is done per scenenode and checks if the whole node has to be rendered or not. It basically checks if any part of the node is inside the camera (for example using the boundingbox, but there are different options).
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
CuteAlien
Admin
Posts: 9689
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: Quad vanishes as the camera moves towards it?

Post by CuteAlien »

The problem with the boundingbox is - you have to recalculate that for the meshbuffer and the mesh. So add one more line:
mesh->recalculateBoundingBox();

You can see btw that backface culling works by moving the camera to the back of your node - it will vanish then (if you want it visible you have to disable backfaceculling in the material).
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
CuteAlien
Admin
Posts: 9689
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: Quad vanishes as the camera moves towards it?

Post by CuteAlien »

hendu wrote:Address of an array, and it doesn't blow up at runtime?
Ehm... that's a good question. It should be:

Code: Select all

 
meshbuf->append((const void*)ver2, 4, (u16*)indices, 6);
 
It's a little confusing it worked, but stackoverflow explains it all: http://stackoverflow.com/questions/2528 ... value-in-c
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
pandoragami
Posts: 226
Joined: Wed Jan 26, 2011 5:37 pm
Contact:

Re: Quad vanishes as the camera moves towards it?

Post by pandoragami »

CuteAlien wrote:Backface culling is not related much to scenenode culling. Backface cullings is controlled by the material and done by the graphic-card per polygon. While scenenode culling is done per scenenode and checks if the whole node has to be rendered or not. It basically checks if any part of the node is inside the camera (for example using the boundingbox, but there are different options).

Would I get a performance hit if I used Node->setAutomaticCulling(scene::EAC_OFF); ?
CuteAlien wrote:The problem with the boundingbox is - you have to recalculate that for the meshbuffer and the mesh. So add one more line:
mesh->recalculateBoundingBox();

You can see btw that backface culling works by moving the camera to the back of your node - it will vanish then (if you want it visible you have to disable backfaceculling in the material).
Would I need these lines

meshbuf->Vertices.reallocate(4);
meshbuf->Vertices.set_used(4);
meshbuf->Indices.set_used(6);
CuteAlien
Admin
Posts: 9689
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: Quad vanishes as the camera moves towards it?

Post by CuteAlien »

random_anonymouse wrote:Would I get a performance hit if I used Node->setAutomaticCulling(scene::EAC_OFF); ?
Depends on the situation - in this case no. In large scenes where many object can be hidden that way it can improve the speed. There is no general answer to this - it depends on the scene.
random_anonymouse wrote: Would I need these lines

meshbuf->Vertices.reallocate(4);
meshbuf->Vertices.set_used(4);
meshbuf->Indices.set_used(6);
Uhm - no! Especially the first one is rather bad and you're again lucky it even works (content is usually lost when a reallocation happens as the new memory is often in another place, you're just lucky in this case that it doesn't do the reallocation because it already had the right amount of memory)
And set_used just set's again the sizes it should already have from append.
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
Post Reply