The problem is this, after I create the MeshSceneNodefrom the intial Mesh, I can only edit/delete existing MeshBuffers but not add new buffers from new data, otherwise it crashes when smgr->drawAll(). I've tried set the Mesh to EHM_DYNAMIC and used setDirty() after changing buffers, but no good.
Below are the simplified proj to reproduce this problem
Code: Select all
class TMesh
{
public:
SMesh *Mesh;
TMesh()
{
Mesh = new SMesh();
}
~TMesh()
{
Mesh->drop();
}
void init(array<SColor> &colors)
{
int i;
for (i=0; i<colors.size(); i++)
addstrip(colors[i], i);
if(i < Mesh->getMeshBufferCount())
{
// clear the rest
for (int j=i; j < Mesh->getMeshBufferCount(); j++)
Mesh->getMeshBuffer(j)->drop();
Mesh->MeshBuffers.erase(i, Mesh->getMeshBufferCount()-i);
}
Mesh->recalculateBoundingBox();
}
void addstrip(SColor color, u32 bufNum)
{
SMeshBuffer *buf = 0;
if (bufNum < Mesh->getMeshBufferCount())
buf = (SMeshBuffer*)Mesh->getMeshBuffer(bufNum);
else
{
buf = new SMeshBuffer();
Mesh->addMeshBuffer(buf);
}
buf->Vertices.set_used(3);
buf->Vertices[0] = S3DVertex(vector3df(0, bufNum*100, 0), vector3df(0,1,0), color, vector2df(0,0));
buf->Vertices[1] = S3DVertex(vector3df(0, bufNum*100, 100), vector3df(0,1,0), color, vector2df(0,0));
buf->Vertices[2] = S3DVertex(vector3df(100, bufNum*100, 0), vector3df(0,1,0), color, vector2df(0,0));
buf->Indices.set_used(3);
buf->Indices[0] = 0;
buf->Indices[1] = 1;
buf->Indices[2] = 2;
buf->recalculateBoundingBox();
}
};
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];
};
int main()
{
MyEventReceiver rv;
IrrlichtDevice *device = createDevice(EDT_OPENGL, dimension2du(800, 600), 32, false, false, false, &rv);
if(!device)
return 1;
IVideoDriver *driver = device->getVideoDriver();
ISceneManager *smgr = device->getSceneManager();
device->setWindowCaption(L"Irrlicht Example for SMesh usage");
TMesh mesh;
array<SColor> colors;
colors.push_back(SColor(255, rand()%256, rand()%256, rand()%256));
colors.push_back(SColor(255, rand()%256, rand()%256, rand()%256));
mesh.init(colors);
// Add mesh to the scene graph
IMeshSceneNode *meshnode = smgr->addMeshSceneNode(mesh.Mesh);
meshnode->setMaterialFlag(EMF_LIGHTING, false);
ICameraSceneNode* camera = smgr->addCameraSceneNodeFPS();
camera->setPosition(vector3df(-20.f, 150.f, -20.f));
camera->setTarget(vector3df(0,0,0));
while(device->run())
{
if(rv.IsKeyDown(irr::KEY_KEY_1))
{
colors.push_back(SColor(255, rand()%256, rand()%256, rand()%256));
mesh.init(colors);
}
else if(rv.IsKeyDown(irr::KEY_KEY_2))
{
if (colors.size() != 0)
{
colors.erase(colors.size()-1);
mesh.init(colors);
}
}
driver->beginScene(true, true);
smgr->drawAll();
driver->endScene();
}
device->drop();
return 0;
}