[SOLVED] When to reallocate SMeshBuffer?

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

[SOLVED] When to reallocate SMeshBuffer?

Post by pandoragami »

In the code below a single polygon is generated and then appended to the meshbuffer.

Code: Select all

scene::SMesh *mesh = new scene::SMesh();
    scene::SMeshBuffer * meshbuf = new scene::SMeshBuffer();
 
    video::S3DVertex a(0,0,10, 1,1,0, video::SColor(255,0,255,255), 0, 1);
    video::S3DVertex b(10,0,-10, 1,0,0, video::SColor(255,255,0,255), 1, 1);
    video::S3DVertex c(0,20,0, 0,1,1, video::SColor(255,255,255,0), 1, 0);
    video::S3DVertex d(-10,0,-10, 0,0,1, video::SColor(255,0,255,0), 0, 0);
 
    video::S3DVertex ver2[] = {a,b,c,d};
    u16 indices[] = { 0,2,3, 2,1,3, 1,0,3, 2,0,1 };
    mesh->addMeshBuffer(meshbuf);
    meshbuf->drop();
 
    meshbuf->append((const void*)&ver2, 4, (u16*)&indices, 12);//do these methods get called after every mesh->addMeshBuffer(meshbuf);  meshbuf->drop();
    meshbuf->Vertices.reallocate(4);
    meshbuf->Vertices.set_used(4);
    meshbuf->Indices.set_used(12);
    meshbuf->recalculateBoundingBox();
The question I have is when should the methods for the meshbuffer object get a call when a new polygon is added? And why is the meshbuf dropped?
Last edited by pandoragami on Sat Mar 07, 2015 7:33 pm, edited 1 time in total.
pandoragami
Posts: 226
Joined: Wed Jan 26, 2011 5:37 pm
Contact:

Re: When to reallocate SMeshBuffer?

Post by pandoragami »

The code below compiles but crashes in the loop where DrawAll is called. Basically I create two polygons separated by 10 units along x,y,z,

Here's the code

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( 13, 18, 11));
    cam->setTarget(core::vector3df( -3, 7,- 4));
    device->getCursorControl()->setVisible(false);
//////////////////////////////////////////////////////////////////////////////////////////////////////////
    scene::SMesh *mesh = new scene::SMesh();
    scene::SMeshBuffer * meshbuf = new scene::SMeshBuffer();
 
    video::S3DVertex a(0,0,10, 1,1,0, video::SColor(255,0,255,255), 0, 1);
    video::S3DVertex b(10,0,-10, 1,0,0, video::SColor(255,255,0,255), 1, 1);
    video::S3DVertex c(0,20,0, 0,1,1, video::SColor(255,255,255,0), 1, 0);
    video::S3DVertex d(-10,0,-10, 0,0,1, video::SColor(255,0,255,0), 0, 0);
 
    video::S3DVertex ver2[] = {a,b,c,d};
    u16 indices[] = { 0,2,3, 2,1,3, 1,0,3, 2,0,1 };
    mesh->addMeshBuffer(meshbuf);
 
    meshbuf->drop();
 
    meshbuf->append((const void*)&ver2, 4, (u16*)&indices, 12);
    meshbuf->Vertices.reallocate(4);
    meshbuf->Vertices.set_used(4);
    meshbuf->Indices.set_used(12);
    meshbuf->recalculateBoundingBox();
 
    scene::IMeshSceneNode* Node = smgr->addMeshSceneNode(mesh);
    Node->setMaterialFlag(video::EMF_LIGHTING, false);
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
 
    video::S3DVertex a1(0+10,0+10,10+10, 1,1,0, video::SColor(255,0,255,255), 0, 1);
    video::S3DVertex b1(10+10,0+10,-10+10, 1,0,0, video::SColor(255,255,0,255), 1, 1);
    video::S3DVertex c1(0+10,20+10,0+10, 0,1,1, video::SColor(255,255,255,0), 1, 0);
    video::S3DVertex d1(-10+10,0+10,-10+10, 0,0,1, video::SColor(255,0,255,0), 0, 0);
 
    ver2[0] = a1;
    ver2[1] = b1;
    ver2[2] = c1;
    ver2[3] = d1;
 
    mesh->addMeshBuffer(meshbuf);
 
    meshbuf->drop();
 
    meshbuf->append((const void*)&ver2, 4, (u16*)&indices, 12);
    meshbuf->Vertices.reallocate(8);
    meshbuf->Vertices.set_used(8);
    meshbuf->Indices.set_used(24);
    meshbuf->recalculateBoundingBox();
 
    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->drop();
 
    return 0;
}
Cube_
Posts: 1010
Joined: Mon Oct 24, 2011 10:03 pm
Location: 0x45 61 72 74 68 2c 20 69 6e 20 74 68 65 20 73 6f 6c 20 73 79 73 74 65 6d

Re: When to reallocate SMeshBuffer?

Post by Cube_ »

I would guess it's because you drop the mesh buffer and then try to append to it but I'm not an expert on this (evidently seeing my help topics on generating meshes).
From what I can tell you attach the mesh buffer to your mesh, then you drop it and then you try to append thing to the now nonexistent meshbuffer.
"this is not the bottleneck you are looking for"
pandoragami
Posts: 226
Joined: Wed Jan 26, 2011 5:37 pm
Contact:

Re: When to reallocate SMeshBuffer?

Post by pandoragami »

aaammmsterdddam wrote:I would guess it's because you drop the mesh buffer and then try to append to it but I'm not an expert on this (evidently seeing my help topics on generating meshes).
From what I can tell you attach the mesh buffer to your mesh, then you drop it and then you try to append thing to the now nonexistent meshbuffer.
I think it's because each SMesh needs it's own SMeshBuffer, that did solve it... unless there's a way to clear the SMeshBuffer after joining the SMesh to the IMeshSceneNode.
mongoose7
Posts: 1227
Joined: Wed Apr 06, 2011 12:13 pm

Re: When to reallocate SMeshBuffer?

Post by mongoose7 »

You added the same meshbuffer twice!!

Code: Select all

mesh->addMeshBuffer(meshbuf);
pandoragami
Posts: 226
Joined: Wed Jan 26, 2011 5:37 pm
Contact:

Re: When to reallocate SMeshBuffer?

Post by pandoragami »

mongoose7 wrote:You added the same meshbuffer twice!!

Code: Select all

mesh->addMeshBuffer(meshbuf);

Yup, I created a second one instead and it works now. I just wonder if the same one can be used for the same SMesh? I would like to create separate quads but all within the same scenenode.
mongoose7
Posts: 1227
Joined: Wed Apr 06, 2011 12:13 pm

Re: [SOLVED] When to reallocate SMeshBuffer?

Post by mongoose7 »

I don't know what you mean. You can add vertices to the same mesh buffer, or you can add a new mesh buffer and add the vertices to it. You split vertices into mesh buffers based on materials. Also, you do realise that these calls are redundant?:

Code: Select all

    meshbuf->Vertices.reallocate(4);
    meshbuf->Vertices.set_used(4);
    meshbuf->Indices.set_used(12);
and maybe also

Code: Select all

    meshbuf->recalculateBoundingBox();
pandoragami
Posts: 226
Joined: Wed Jan 26, 2011 5:37 pm
Contact:

Re: [SOLVED] When to reallocate SMeshBuffer?

Post by pandoragami »

mongoose7 wrote:I don't know what you mean. You can add vertices to the same mesh buffer, or you can add a new mesh buffer and add the vertices to it. You split vertices into mesh buffers based on materials. Also, you do realise that these calls are redundant?:

Code: Select all

    meshbuf->Vertices.reallocate(4);
    meshbuf->Vertices.set_used(4);
    meshbuf->Indices.set_used(12);
and maybe also

Code: Select all

    meshbuf->recalculateBoundingBox();
Right, that's what my main question was. The solution I found was to create a new meshbuf for each SMesh mesh object, that way I can create a single quad and then draw different textures/ materials to each quad. The thing is is that the above code has to be called for each new meshbuf since I can't see any other way to initialize a new SMesh object?
mongoose7
Posts: 1227
Joined: Wed Apr 06, 2011 12:13 pm

Re: [SOLVED] When to reallocate SMeshBuffer?

Post by mongoose7 »

You are calling them on a mesh buffer, not a mesh. In any case, doesn't having already called 'append' mean anything to you? You call append and then you initialise? What can be happening in your head while this is going on?
pandoragami
Posts: 226
Joined: Wed Jan 26, 2011 5:37 pm
Contact:

Re: [SOLVED] When to reallocate SMeshBuffer?

Post by pandoragami »

I got rid of those lines in the new code. The thread is solved. A new problem came up but was solved in this thread

http://irrlicht.sourceforge.net/forum/v ... 93#p292693
Post Reply