Skip to content

Commit

Permalink
Renamed osgText::Font::SizePair to osgText::FontSizePair in prep for …
Browse files Browse the repository at this point in the history
…use this more

widely within osgText/freetype plugin.

Added support for inserting loading models into --mt multithreaded implementation.
  • Loading branch information
robertosfield committed Dec 23, 2007
1 parent 8c5a9ac commit dea0670
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 14 deletions.
28 changes: 22 additions & 6 deletions examples/osgtext/osgtext.cpp
Expand Up @@ -481,8 +481,10 @@ class UpdateTextOperation : public osg::Operation
{
public:

UpdateTextOperation(osg::Group* group):
UpdateTextOperation(const osg::Vec3& center, float diameter, osg::Group* group):
Operation("UpdateTextOperation", true),
_center(center),
_diameter(diameter),
_maxNumChildren(200),
_maxNumTextPerGeode(10),
_group(group)
Expand Down Expand Up @@ -544,7 +546,7 @@ class UpdateTextOperation : public osg::Operation

for(unsigned int i=0; i<_maxNumTextPerGeode; ++i)
{
osg::Vec3 position(float(rand()) / float(RAND_MAX), float(rand()) / float(RAND_MAX), float(i)/float(_maxNumTextPerGeode));
osg::Vec3 position(float(rand()) / float(RAND_MAX) - 0.5f, float(rand()) / float(RAND_MAX) - 0.5f, float(i)/float(_maxNumTextPerGeode) - 0.5f);

std::string str;
unsigned int _numCharacters = 5;
Expand All @@ -555,10 +557,10 @@ class UpdateTextOperation : public osg::Operation

osgText::Text* text = new osgText::Text;
text->setDataVariance(osg::Object::DYNAMIC);
text->setPosition(position);
text->setPosition(_center + position * _diameter);
text->setFont("times.ttf");
text->setText(str);
text->setCharacterSize(0.025f);
text->setCharacterSize(0.025f * _diameter);
text->setAxisAlignment(osgText::Text::SCREEN);

geode->addDrawable(text);
Expand All @@ -583,6 +585,8 @@ class UpdateTextOperation : public osg::Operation

typedef std::list< osg::ref_ptr<osg::Geode> > AvailableList;

osg::Vec3 _center;
float _diameter;
unsigned int _maxNumChildren;
unsigned int _maxNumTextPerGeode;

Expand Down Expand Up @@ -618,6 +622,18 @@ int main(int argc, char** argv)
// 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;
Expand All @@ -630,7 +646,7 @@ int main(int argc, char** argv)

// create the operation that will run in the background and
// sync once per frame with the main viewer loop.
updateOperation = new UpdateTextOperation(textGroup);
updateOperation = new UpdateTextOperation(center, diameter, textGroup);

// add the operation to the operation thread and start it.
operationThread->add(updateOperation.get());
Expand All @@ -643,7 +659,7 @@ int main(int argc, char** argv)
// add a unit cube for the text to appear within.
osg::Geode* geode = new osg::Geode;
geode->getOrCreateStateSet()->setAttribute(new osg::PolygonMode(osg::PolygonMode::FRONT_AND_BACK,osg::PolygonMode::LINE));
geode->addDrawable(new osg::ShapeDrawable(new osg::Box(osg::Vec3(0.5f,0.5f,0.5f),1.0)));
geode->addDrawable(new osg::ShapeDrawable(new osg::Box(center,diameter)));

mainGroup->addChild(geode);
}
Expand Down
7 changes: 3 additions & 4 deletions include/osgText/Font
Expand Up @@ -24,6 +24,7 @@
#include <osg/buffered_value>
#include <osg/TexEnv>
#include <osgDB/ReaderWriter>

#include <osgText/Export>
#include <osgText/KerningType>

Expand All @@ -34,7 +35,6 @@ namespace osgText {
class Font;
class Text;


/** Read a font from specified file. The filename may contain a path.
* It will search for the font file in the following places in this order:
* - In the current directory
Expand Down Expand Up @@ -176,12 +176,11 @@ protected:
typedef std::vector< osg::ref_ptr<osg::StateSet> > StateSetList;
typedef std::map< unsigned int, osg::ref_ptr<Glyph> > GlyphMap;

typedef std::pair< unsigned int, unsigned int > SizePair;
typedef std::map< SizePair, GlyphMap > SizeGlyphMap;
typedef std::map< FontSizePair, GlyphMap > FontSizeGlyphMap;

osg::ref_ptr<osg::TexEnv> _texenv;
osg::ref_ptr<osg::StateSet> _stateset;
SizeGlyphMap _sizeGlyphMap;
FontSizeGlyphMap _sizeGlyphMap;
GlyphTextureList _glyphTextureList;

// current active size of font
Expand Down
4 changes: 4 additions & 0 deletions include/osgText/KerningType
Expand Up @@ -16,11 +16,15 @@

namespace osgText
{

typedef std::pair< unsigned int, unsigned int > FontSizePair;

enum KerningType
{
KERNING_DEFAULT, //default locked to integer kerning values
KERNING_UNFITTED, //use floating point value for kerning
KERNING_NONE //no kerning
};

}
#endif /*OSGTEXT_KERNINGTYPE*/
4 changes: 2 additions & 2 deletions src/osgText/DefaultFont.cpp
Expand Up @@ -52,14 +52,14 @@ Font::Glyph* DefaultFont::getGlyph(unsigned int charcode)
{
if (_sizeGlyphMap.empty()) return 0;

SizeGlyphMap::iterator itr = _sizeGlyphMap.find(SizePair(_width,_height));
FontSizeGlyphMap::iterator itr = _sizeGlyphMap.find(FontSizePair(_width,_height));
if (itr==_sizeGlyphMap.end())
{
// no font found of correct size, will need to find the nearest.
itr = _sizeGlyphMap.begin();
int mindeviation = abs((int)_width-(int)itr->first.first)+
abs((int)_height-(int)itr->first.second);
SizeGlyphMap::iterator sitr=itr;
FontSizeGlyphMap::iterator sitr=itr;
++sitr;
for(;
sitr!=_sizeGlyphMap.end();
Expand Down
4 changes: 2 additions & 2 deletions src/osgText/Font.cpp
Expand Up @@ -347,7 +347,7 @@ osg::Texture::FilterMode Font::getMagFilterHint() const

Font::Glyph* Font::getGlyph(unsigned int charcode)
{
SizeGlyphMap::iterator itr = _sizeGlyphMap.find(SizePair(_width,_height));
FontSizeGlyphMap::iterator itr = _sizeGlyphMap.find(FontSizePair(_width,_height));
if (itr!=_sizeGlyphMap.end())
{
GlyphMap& glyphmap = itr->second;
Expand Down Expand Up @@ -417,7 +417,7 @@ bool Font::hasVertical() const

void Font::addGlyph(unsigned int width, unsigned int height, unsigned int charcode, Glyph* glyph)
{
_sizeGlyphMap[SizePair(width,height)][charcode]=glyph;
_sizeGlyphMap[FontSizePair(width,height)][charcode]=glyph;

int posX=0,posY=0;

Expand Down

0 comments on commit dea0670

Please sign in to comment.