I have been using OSG for a number of years for a commercial product. Our product loads various models and earth views as the user requires them (for security monitoring – buildings and campuses mostly). I have a problem with 3.6.3/4 that wasn't there in 3.4.0.
I have an OpenFlight model (sample/demo model) that uses textures for terrain and on buildings. The first time I load it all is fine. However, if I load another model (or another graphics file – the application supports AutoCAD, raster, OSG Earth, and ArcGIS formats as well) then reload the model the terrain is black and the textures are corrupted if they are not black. This worked fine with 3.4.0 and 3.4.1 but does not work with 3.6.3 or 3.6.4.
I have debugged into the code and it appears that the model and textures are always reloaded from the cache and I cannot override this (setting Options::CACHE_NONE doesn’t affect it - in 3.4.0 the model always loaded from files with Options set to CACHE_NONE). If I look at the model graph in memory (after the first load), the textures appear to be erased or over-written during the traversals in one of the classes called in ViewerBase::runOperations() (though it’s a little confusing to me at that point, it appears to be when GlobalObjectVisitor::compile() calls node.accept(*this)). During the traversals where the textures appear to be over-written, I noticed the context ID is always 0, so the textures appear to be reloaded, over-writing the original data in the process (though I could be quite confused as to what I am seeing and it may all be irrelevant to the issue).
Below is a short snippet from my initialization and cleanup code between model loads. I load the model, add a clip node to peel down through the models vertically. I also add other nodes to hold embedded bitmaps (Billboards) representing security devices – cameras, doors, alarm points, etc. Those details are omitted from the code below. After the model is loaded I set up the graphics context and render the image to an offscreen window (which is then copied to a memory bitmap). The application is a Windows console app (with hidden window) that streams bitmap images to the client app via named pipe on the same machine (I know, it’s complicated, but it’s a complex environment, including live video windows).
As I said, it has worked fine with previous versions of OSG (back to 3.0.1, if not earlier). This may be specific to Open Flight models, as I can load FBX models, OSG earth files, or the cessnafire.osg model and the textures appear correct.
Any help would be appreciated. If the model would always be loaded from the file when Options::CACHE_NONE were set it would solve my problem.
OSGLoadResult OpenSceneGraphBitmap::LoadModel(std::string fileName, osgDB::Options* dbOptions)
{
CleanupModel();
if (m_Root == nullptr)
m_Root = new osg::Group; // Init the main Root Node/Group
// Load the Model from the model name
osg::Group* model = dynamic_cast<osg::Group*>(osgDB::readNodeFile(fileName, dbOptions));
if (model != nullptr)
{
// Optimize the model
osgUtil::Optimizer optimizer;
optimizer.optimize(model);
optimizer.reset();
// Create the clip node and add to scene
osg::ComputeBoundsVisitor cbbv;
model->accept(cbbv);
osg::BoundingBox bb = cbbv.getBoundingBox();
osg::ref_ptr<osg::ClipPlane> clipPlane = new osg::ClipPlane;
clipPlane->setClipPlane(0.0, 0.0, -1.0, bb.zMin() + (bb.zMax() - bb.zMin()));
clipPlane->setClipPlaneNum(0);
osg::ref_ptr<osg::ClipNode> clipNode = new osg::ClipNode;
clipNode->setName("CLIP_NODE");
clipNode->addClipPlane(clipPlane.get());
clipNode->setCullingActive(false);
model->setStateSet(clipNode->getStateSet());
m_Root->addChild(clipNode);
m_Root->addChild(model);
m_Root->setDataVariance(osg::Object::DYNAMIC);
return OSGLoadResult::Sucess;
}
// since load failed, reset the wait event so the render thread resumes
return OSGLoadResult::FileLoadError;
}
void OpenSceneGraphBitmap::CleanupModel()
{
RemoveViews();
if (m_Root != nullptr) // if root already exists (already loaded previous scene) remove children to clean up
{
m_Root->releaseGLObjects();
m_Root->removeChildren(0, m_Root->getNumChildren());
void* ptr = m_Root.release();
delete ptr;
m_Root = nullptr;
}
}
void OpenSceneGraphBitmap::RemoveViews()
{
if (m_nhiCompositeViewer != nullptr)
{
m_nhiCompositeViewer->setDone(true);
delete m_nhiCompositeViewer;
m_nhiCompositeViewer = nullptr;
}
}
osg::Group* model = dynamic_cast<osg::Group*>(osgDB::readNodeFile(fileName, dbOptions));
if (model != nullptr)
{...
This code will only assign the loaded object the m_Root if the loaded model root node is a Group, if isn't then it'll just be leaked, never to be deleted.
The best way to do a read to a particular type in robust way is to use ref_ptr<> and the readFile<T>(..) method i.e.
auto model = osgDB::readRefFile<osg::Group>(fileName, dbOptions)); // return a ref_ptr<osg::Group> that internally uses an dynamic_cast<osg::Group*>
The next odditiyr is that you have a CleanupModel method that removes the whole Viewer, but you call it a View:
void OpenSceneGraphBitmap::CleanupModel()
{
RemoveViews();
...
}
This seems like your application is conflating various different features together, which is a red flag by itself and makes me wonder if you have mis-understood the intent of the various osgViewer class available.
The new bit of related code is another sign of misuse of the how the OSG is intended to be used:
void OpenSceneGraphBitmap::RemoveViews()
{
if (m_nhiCompositeViewer != nullptr)
{
m_nhiCompositeViewer->setDone(true);
delete m_nhiCompositeViewer;
m_nhiCompositeViewer = nullptr;
}
The OSG has built in robust reference counting, it is almost never appropriate to directly delete a object, not in the scene graph, not in the viewer, not a whole viewer. I suspect your application at a higher level is not ideally organized so the following suggestion might just gloss over wider problems, any I say it here as understanding ref_ptr<> usage is important regardless...
So your m_nhiCompositeViewer pointer should *always* be a ref_ptr<> and *never* a straight C pointer. If you want to delete a viewer you just set the ref_ptr<> to nullptr and it'll be automatically deleted for you if no other references exist t it. The above method could safely be replaced with a single line : m_nhiCompositeViewer = nullptr;
However, this doesn't fix the other problems in the code, it'd just fix a bad practice.
Next problem will need to look at is back to the OpenSceneGraphBitmap::CleanupModel() method:
void OpenSceneGraphBitmap::CleanupModel()
{
RemoveViews();
if (m_Root != nullptr) // if root already exists (already loaded previous scene) remove children to clean up
{
m_Root->releaseGLObjects();
m_Root->removeChildren(0, m_Root->getNumChildren());
void* ptr = m_Root.release();
delete ptr;
m_Root = nullptr;
}
}
Here you call RemoveViews() which will delete the Viewer and all graphics contexts associated with it. The you try and do some manual clean up:
if (m_Root != nullptr) // if root already exists (already loaded previous scene) remove children to clean up
{
m_Root->releaseGLObjects();
m_Root->removeChildren(0, m_Root->getNumChildren());
This suggest to me that you are keeping m_Root around as some form of global container and then trying to manage it's contents. The code snippets don't say how the node and it's children. Deleting a Viewer will delete all it's GraphicsContext and clean up all the scene graphs that are directly attached to it, but it you have scene graph elements that are detached from the scene graph then it can't clean up these. If these detached elements contain GL objects will have already been deleted by the graphics context deletion, so the handles are orphaned but the OSG itself doesn't know about it, and calling releaseGLObjects() will release the handles into containers that the OSG uses to schedule deletion or reuse of the GL objects. If the graphics contexts already deleted then you have to discard any GL handles via calling discardGLObjects() rather than releaseGLObjects().
The osgViewer and scene graph are design to do all the automatic clean up and management of GL objects behind the scenes for you, for most applications there should never be a need to explicitly call
releaseGLObjects(); The OSG can't track what you detach from viewers and then manipulate, in these cases you really need to think whether what you are doing is necessary and sensible. My strong recommendation is that users avoid doing this.
Finally we have another instance of manually an object:
void* ptr = m_Root.release();
delete ptr;
m_Root = nullptr;
The code tells me that m_Root is likely a ref_ptr<Group> which rather than just do the sensible thing and call m_Root = nullptr and let the smart pointer do it's job in clean up you release the pointer and then manually delete it.
It's painful to see such a combination a misuse of the OSG. I don't know where you have picked up this coding style, but it's never been part of the OSG usage, none of the OSG examples, none of books, never in it's near 20 year history has abusing ref_ptr<> in this way been advocated.
I don't know the history of your application, it could be that you've inherited bad code and have been thrown in the deepened trying to learn and fix stuff at the same time. From this point, I am pretty sure that the regression you see in going from 3.6.0 to 3.6.4 is likely to mis-use of the OSG in your application code that make it's so fragile and dependent on accidental behaviors to function. Fixing bugs on the OSG then can easily break your application as it was relying on buggy behavior.
From the little snippets I've picked up a number of problems, fixing these might work around the problems, but my guess is that there are major problem throughout the code. The good news is that if you learn to use the OSG a bit more appropriately your codebase will become smaller, cleaner, easier to maintain, more robust, more fun to work with.
Spending a bit of time learning about how smart pointers work and how to use them will really help you. You have accesses to the full OSG source code so if you aren't sure about something just have a look at the code, put break points into the code, see what happens when smart pointers do there thing. Have a look through the examples, discussions online, have a look at the OSG books. This investment in learning more about the how thing work will make you much more productive.
Best of luck,
Robert.
}
My quick fix is to clear the cache on the first render (and call clear thereafter). OpenFlight files open and render fine now. Is this a safe fix?
void ViewerBase::frame(double simulationTime){....
osgDB::Registry::instance()->clearObjectCache(); // ADDED TO CLEAR CACHE AFTER RENDER SINCE IT BECOMES CORRUPTED}
I don't understand exactly what the cache does.
If it has an expiration time and objects are removed after a minute or so (which seems to happen) it would appear it is a short-term cache, perhaps to increase efficiency when the model is loading,
before it is rendered, such as re-using already loaded texture images and so forth. If it is for long-term caching (keeping models in memory even after another model is loaded) that would be counter productive in our application, since the user might load several different large model files in a minute in some situations, and keeping all those models in memory would be problematic. My preference would be to disable caching altogether, unless it is a short-term cache to make loading more efficient, in which case clearing the cache after the first render solves my problem.I have set the osgDB::Options to CACHE_NONE but it does not appear to have any effect on caching. The OpenFlight model and its textures are always loaded from the cache if the cache contains objects.osg::ref_ptr<osgDB::Options> dbOptions = new osgDB::Options();dbOptions->setObjectCacheHint(osgDB::Options::CACHE_NONE);osgDB::readNodeFile(fileName, dbOptions);Is this not the correct way to disable caching?
--
You received this message because you are subscribed to the Google Groups "OpenSceneGraph Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to osg-users+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/osg-users/93b6ffd8-b2e6-43c0-a6c8-3ef11949bbaf%40googlegroups.com.
_______________________________________________
osg-users mailing list
osg-...@lists.openscenegraph.org
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org
Do you have hangouts
To unsubscribe from this group and stop receiving emails from it, send an email to osg-...@googlegroups.com.