Further work on Bound class/Node::getBound() and Drawable::getBound() and usage in OSG codebase

This commit is contained in:
Robert Osfield
2014-05-14 16:01:40 +00:00
parent ff21af2b0d
commit 74f91037a7
30 changed files with 239 additions and 213 deletions

View File

@@ -53,13 +53,13 @@ osg::Group* createHUDText()
////////////////////////////////////////////////////////////////////////////////////////////////////////
//
//
// Examples of how to set up different text layout
//
osg::Vec4 layoutColor(1.0f,1.0f,0.0f,1.0f);
float layoutCharacterSize = 20.0f;
float layoutCharacterSize = 20.0f;
{
osgText::Text* text = new osgText::Text;
text->setFont(font);
@@ -103,32 +103,32 @@ osg::Group* createHUDText()
text->setText("text->setLayout(osgText::Text::VERTICAL);");
geode->addDrawable(text);
}
////////////////////////////////////////////////////////////////////////////////////////////////////////
//
//
// Examples of how to set up different font resolution
//
osg::Vec4 fontSizeColor(0.0f,1.0f,1.0f,1.0f);
float fontSizeCharacterSize = 30;
osg::Vec3 cursor = osg::Vec3(margin*2,windowHeight-margin*2,0.0f);
{
osgText::Text* text = new osgText::Text;
text->setFont(font);
text->setColor(fontSizeColor);
text->setCharacterSize(fontSizeCharacterSize);
text->setPosition(cursor);
// use text that uses 10 by 10 texels as a target resolution for fonts.
text->setFontResolution(10,10); // blocky but small texture memory usage
text->setText("text->setFontResolution(10,10); // blocky but small texture memory usage");
geode->addDrawable(text);
}
cursor.y() -= fontSizeCharacterSize;
{
osgText::Text* text = new osgText::Text;
@@ -136,14 +136,14 @@ osg::Group* createHUDText()
text->setColor(fontSizeColor);
text->setCharacterSize(fontSizeCharacterSize);
text->setPosition(cursor);
// use text that uses 20 by 20 texels as a target resolution for fonts.
text->setFontResolution(20,20); // smoother but higher texture memory usage (but still quite low).
text->setText("text->setFontResolution(20,20); // smoother but higher texture memory usage (but still quite low).");
geode->addDrawable(text);
}
cursor.y() -= fontSizeCharacterSize;
{
osgText::Text* text = new osgText::Text;
@@ -151,38 +151,38 @@ osg::Group* createHUDText()
text->setColor(fontSizeColor);
text->setCharacterSize(fontSizeCharacterSize);
text->setPosition(cursor);
// use text that uses 40 by 40 texels as a target resolution for fonts.
text->setFontResolution(40,40); // even smoother but again higher texture memory usage.
text->setText("text->setFontResolution(40,40); // even smoother but again higher texture memory usage.");
geode->addDrawable(text);
}
////////////////////////////////////////////////////////////////////////////////////////////////////////
//
//
// Examples of how to set up different sized text
//
osg::Vec4 characterSizeColor(1.0f,0.0f,1.0f,1.0f);
cursor.y() -= fontSizeCharacterSize*2.0f;
{
osgText::Text* text = new osgText::Text;
text->setFont(font);
text->setColor(characterSizeColor);
text->setFontResolution(20,20);
text->setPosition(cursor);
// use text that is 20 units high.
text->setCharacterSize(20); // small
text->setText("text->setCharacterSize(20.0f); // small");
geode->addDrawable(text);
}
cursor.y() -= 30.0f;
{
osgText::Text* text = new osgText::Text;
@@ -190,14 +190,14 @@ osg::Group* createHUDText()
text->setColor(characterSizeColor);
text->setFontResolution(30,30);
text->setPosition(cursor);
// use text that is 30 units high.
text->setCharacterSize(30.0f); // medium
text->setText("text->setCharacterSize(30.0f); // medium");
geode->addDrawable(text);
}
cursor.y() -= 50.0f;
{
osgText::Text* text = new osgText::Text;
@@ -205,17 +205,17 @@ osg::Group* createHUDText()
text->setColor(characterSizeColor);
text->setFontResolution(40,40);
text->setPosition(cursor);
// use text that is 60 units high.
text->setCharacterSize(60.0f); // large
text->setText("text->setCharacterSize(60.0f); // large");
geode->addDrawable(text);
}
////////////////////////////////////////////////////////////////////////////////////////////////////////
//
//
// Examples of how to set up different alignments
//
@@ -223,7 +223,7 @@ osg::Group* createHUDText()
float alignmentCharacterSize = 25.0f;
cursor.x() = 640;
cursor.y() = margin*4.0f;
typedef std::pair<osgText::Text::AlignmentType,std::string> AlignmentPair;
typedef std::vector<AlignmentPair> AlignmentList;
AlignmentList alignmentList;
@@ -260,49 +260,49 @@ osg::Group* createHUDText()
text->setCharacterSize(alignmentCharacterSize);
text->setPosition(cursor);
text->setDrawMode(osgText::Text::TEXT|osgText::Text::ALIGNMENT|osgText::Text::BOUNDINGBOX);
text->setAlignment(itr->first);
text->setText(itr->second);
alignmentGeode->addDrawable(text);
}
}
sequence->setMode(osg::Sequence::START);
sequence->setInterval(osg::Sequence::LOOP, 0, -1);
sequence->setDuration(1.0f, -1);
rootNode->addChild(sequence);
////////////////////////////////////////////////////////////////////////////////////////////////////////
//
//
// Examples of how to set up different fonts...
//
cursor.x() = margin*2.0f;
cursor.y() = margin*2.0f;
osg::Vec4 fontColor(1.0f,0.5f,0.0f,1.0f);
float fontCharacterSize = 20.0f;
float spacing = 40.0f;
{
osgText::Text* text = new osgText::Text;
text->setColor(fontColor);
text->setPosition(cursor);
text->setCharacterSize(fontCharacterSize);
text->setFont(0);
text->setText("text->setFont(0); // inbuilt font.");
geode->addDrawable(text);
cursor.x() = text->getBound().xMax() + spacing ;
cursor.x() = text->getBoundingBox().xMax() + spacing ;
}
{
osgText::Font* arial = osgText::readFontFile("fonts/arial.ttf");
@@ -310,16 +310,16 @@ osg::Group* createHUDText()
text->setColor(fontColor);
text->setPosition(cursor);
text->setCharacterSize(fontCharacterSize);
text->setFont(arial);
text->setText(arial!=0?
"text->setFont(\"fonts/arial.ttf\");":
"unable to load \"fonts/arial.ttf\"");
geode->addDrawable(text);
cursor.x() = text->getBound().xMax() + spacing ;
cursor.x() = text->getBoundingBox().xMax() + spacing ;
}
{
osgText::Font* times = osgText::readFontFile("fonts/times.ttf");
@@ -327,16 +327,16 @@ osg::Group* createHUDText()
text->setColor(fontColor);
text->setPosition(cursor);
text->setCharacterSize(fontCharacterSize);
geode->addDrawable(text);
text->setFont(times);
text->setText(times!=0?
"text->setFont(\"fonts/times.ttf\");":
"unable to load \"fonts/times.ttf\"");
cursor.x() = text->getBound().xMax() + spacing ;
cursor.x() = text->getBoundingBox().xMax() + spacing ;
}
cursor.x() = margin*2.0f;
cursor.y() = margin;
@@ -347,34 +347,34 @@ osg::Group* createHUDText()
text->setColor(fontColor);
text->setPosition(cursor);
text->setCharacterSize(fontCharacterSize);
text->setFont(dirtydoz);
text->setText(dirtydoz!=0?
"text->setFont(\"fonts/dirtydoz.ttf\");":
"unable to load \"fonts/dirtydoz.ttf\"");
geode->addDrawable(text);
cursor.x() = text->getBound().xMax() + spacing ;
cursor.x() = text->getBoundingBox().xMax() + spacing ;
}
{
osgText::Font* fudd = osgText::readFontFile("fonts/fudd.ttf");
osgText::Text* text = new osgText::Text;
text->setColor(fontColor);
text->setPosition(cursor);
text->setCharacterSize(fontCharacterSize);
text->setFont(fudd);
text->setText(fudd!=0?
"text->setFont(\"fonts/fudd.ttf\");":
"unable to load \"fonts/fudd.ttf\"");
geode->addDrawable(text);
cursor.x() = text->getBound().xMax() + spacing ;
cursor.x() = text->getBoundingBox().xMax() + spacing ;
}
return rootNode;
return rootNode;
}
@@ -386,9 +386,9 @@ osg::Group* create3DText(const osg::Vec3& center,float radius)
osg::Geode* geode = new osg::Geode;
////////////////////////////////////////////////////////////////////////////////////////////////////////
//
//
// Examples of how to set up axis/orientation alignments
//
@@ -477,14 +477,14 @@ osg::Group* create3DText(const osg::Vec3& center,float radius)
osg::Group* rootNode = new osg::Group;
rootNode->addChild(geode);
return rootNode;
return rootNode;
}
class UpdateTextOperation : public osg::Operation
{
public:
UpdateTextOperation(const osg::Vec3& center, float diameter, osg::Group* group):
UpdateTextOperation(const osg::Vec3& center, float diameter, osg::Group* group):
Operation("UpdateTextOperation", true),
_center(center),
_diameter(diameter),
@@ -502,13 +502,13 @@ public:
if (viewer) update();
else load();
}
void update()
{
// osg::notify(osg::NOTICE)<<"*** Doing update"<<std::endl;
OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_mutex);
if (_mergeSubgraph.valid())
{
_group->addChild(_mergeSubgraph.get());
@@ -525,14 +525,14 @@ public:
}
_group->removeChild(0,1);
}
_waitOnMergeBlock.release();
}
}
}
void load()
{
// osg::notify(osg::NOTICE)<<"Doing load"<<std::endl;
osg::ref_ptr<osg::Geode> geode;
@@ -544,7 +544,7 @@ public:
_availableSubgraph.pop_front();
}
}
if (!geode) geode = new osg::Geode;
for(unsigned int i=0; i<_maxNumTextPerGeode; ++i)
@@ -560,7 +560,7 @@ public:
{
str.push_back(char(32.0 + (float(rand())/float(RAND_MAX))*128.0f));
}
osgText::Text* text = new osgText::Text;
text->setDataVariance(osg::Object::DYNAMIC);
text->setPosition(_center + position * _diameter);
@@ -568,22 +568,22 @@ public:
text->setText(str);
text->setCharacterSize(0.025f * _diameter);
text->setAxisAlignment(osgText::Text::SCREEN);
geode->addDrawable(text);
}
{
{
OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_mutex);
_mergeSubgraph = geode;
}
// osg::notify(osg::NOTICE)<<"Waiting on merge"<<std::endl;
_waitOnMergeBlock.block();
}
virtual void release()
{
_waitOnMergeBlock.release();
@@ -595,13 +595,13 @@ public:
float _diameter;
unsigned int _maxNumChildren;
unsigned int _maxNumTextPerGeode;
OpenThreads::Mutex _mutex;
osg::ref_ptr<osg::Group> _group;
osg::ref_ptr<osg::Geode> _mergeSubgraph;
AvailableList _availableSubgraph;
OpenThreads::Block _waitOnMergeBlock;
unsigned int _counter;
};
@@ -613,7 +613,7 @@ int main(int argc, char** argv)
// construct the viewer.
osgViewer::Viewer viewer(arguments);
typedef std::list< osg::ref_ptr<osg::OperationThread> > Threads;
Threads operationThreads;
@@ -624,22 +624,22 @@ int main(int argc, char** argv)
{
// construct a multi-threaded text updating test.
if (numThreads==0) numThreads = 1;
// create a group to add everything into.
osg::Group* mainGroup = new osg::Group;
osg::Vec3 center(0.5f,0.5f,0.5f);
float diameter = 1.0f;
osg::ref_ptr<osg::Node> loadedModel = osgDB::readNodeFiles(arguments);
if (loadedModel.valid())
{
mainGroup->addChild(loadedModel.get());
center = loadedModel->getBound().center();
diameter = loadedModel->getBound().radius() * 2.0f;
}
for(unsigned int i=0; i<numThreads; ++i)
{
osg::Group* textGroup = new osg::Group;
@@ -647,7 +647,7 @@ int main(int argc, char** argv)
// create the background thread
osg::OperationThread* operationThread = new osg::OperationThread;
operationThreads.push_back(operationThread);
// create the operation that will run in the background and
@@ -669,15 +669,15 @@ int main(int argc, char** argv)
mainGroup->addChild(geode);
}
viewer.setSceneData(mainGroup);
viewer.setSceneData(mainGroup);
}
else
{
// prepare scene.
osg::Vec3 center(0.0f,0.0f,0.0f);
float radius = 1.0f;
// make sure the root node is group so we can add extra nodes to it.
osg::Group* group = new osg::Group;
@@ -715,7 +715,7 @@ int main(int argc, char** argv)
viewer.addEventHandler(new osgViewer::StatsHandler());
viewer.run();
if (!operationThreads.empty())
{
for(Threads::iterator itr = operationThreads.begin();