I'm working on a 3D project which uses OpenSG, and I'd like to know if there is an efficient way of adding and removing materials (in fact I'm using SimpleTexturedMaterial) in a way such that a texture can be toggled on or off.
The 3D data is reconstructed from 2D images and I want to toggle a texture on a model so its possible to view the 3D data with the overlayed image or without so its possible to visualise the quality of the reconstruction. Currently I'm doing the following:
void OpenSGWidget::removeTexture(NodePtr &node)
{
GeometryPtr geo(GeometryPtr::dcast(node->getCore()));
if (geo == NullFC)
return;
MaterialPtr mat = geo->getMaterial();
if (mat != NullFC && mat->getType().isDerivedFrom(SimpleTexturedMaterial::getClassType()))
{
material_ = SimpleTexturedMaterialPtr::dcast(geo->getMaterial());
addRefCP(material_);
SimpleMaterialPtr simplemat = SimpleMaterial::create();
beginEditCP(simplemat);
simplemat->setDiffuse(Color3f(.9,.9,.9));
endEditCP(simplemat);
beginEditCP(geo, Geometry::MaterialFieldMask);
geo->setMaterial(simplemat);
endEditCP(geo, Geometry::MaterialFieldMask);
}
}
void OpenSGWidget::addTexture(NodePtr &node)
{
GeometryPtr geo(GeometryPtr::dcast(node->getCore()));
if (geo == NullFC)
return;
if (material_ != NullFC)
{
SimpleMaterialPtr simplemat = SimpleMaterialPtr::dcast(geo->getMaterial());
beginEditCP(geo, Geometry::MaterialFieldMask);
geo->setMaterial(material_);
endEditCP(geo, Geometry::MaterialFieldMask);
if (simplemat != NullFC)
subRefCP(simplemat);
subRefCP(material_);
material_ = NullFC;
}
}
OpenSGWidget is a QT widget, material_ is a local field that stores a pointer to the SimpleTexturedMaterial object.
The problem with this approach is that for large models, it is extremely slow and can take between 5 and 20 seconds to re-render. The delay occurs in the following function:
void OpenSGWidget::paintGL()
{
mgr_->redraw();
}
mgr_ here is a SimpleSceneManagerPtr.
Is there a way to efficiently toggle a texture in this way?
Thanks in advance,
Glyn Matthews
How much free photo storage do you get? Store your holiday snaps for FREE with Yahoo! Photos. Get Yahoo! Photos
