diff --git a/examples/osgtext3D/TextNode.cpp b/examples/osgtext3D/TextNode.cpp index 4a716afe6..23ab043e3 100644 --- a/examples/osgtext3D/TextNode.cpp +++ b/examples/osgtext3D/TextNode.cpp @@ -12,6 +12,12 @@ */ #include "TextNode.h" +#include "GlyphGeometry.h" + +#include +#include +#include + #include using namespace osgText; @@ -172,6 +178,7 @@ void Layout::layout(TextNode& text) const Font* font = text.getActiveFont(); Style* style = text.getActiveStyle(); TextTechnique* technique = text.getTextTechnique(); + const String& str = text.getText(); if (!text.getTextTechnique()) { @@ -179,7 +186,78 @@ void Layout::layout(TextNode& text) const return; } + osg::Vec3 pos(0.0f,0.0f,0.0f); + float characterSize = text.getCharacterSize(); + osg::Vec3 size(characterSize, characterSize, 0.0); + if (style) + { + size.y() = characterSize * style->getWidthRatio(); + size.z() = characterSize * style->getThicknessRatio(); + } + + + osgText::FontResolution resolution(32,32); + if (style) + { + resolution.first = static_cast(static_cast(resolution.first)*style->getSampleDensity()); + resolution.second = static_cast(static_cast(resolution.second)*style->getSampleDensity()); + } + + float characterWidthScale = 1.0f; + float characterHeightScale = 1.0f; + + bool textIs3D = (style && style->getThicknessRatio()!=0.0); + if (textIs3D) + { + characterWidthScale = font->getScale(); + characterHeightScale = font->getScale(); + } + else + { + characterWidthScale = 1.0f/static_cast(resolution.first); + characterHeightScale = 1.0f/static_cast(resolution.second); + } + + osgText::KerningType kerningType = osgText::KERNING_DEFAULT; + technique->start(); + + unsigned int previousCharcode = 0; + for(unsigned int i=0; igetGlyph(resolution, charcode); + if (glyph) + { + technique->addCharacter(pos, size, glyph, style); + pos += osg::Vec3(size.x()*(glyph->getHorizontalAdvance()*characterWidthScale), 0.0f ,0.0f); + } + } + else + { + osgText::Glyph3D* glyph = font->getGlyph3D(charcode); + OSG_NOTICE<<"pos = "<getVerticalHeight(); + + osg::ref_ptr transform = new osg::PositionAttitudeTransform; + transform->setPosition(position); + transform->setAttitude(osg::Quat(osg::inDegrees(90.0),osg::Vec3d(1.0,0.0,0.0))); + transform->setScale(osg::Vec3d(scale, scale, scale)); + + osg::ref_ptr geode = new osg::Geode; + + bool outline = false; + float thickness = 5; + float width = 10; + BevelProfile profile; + float creaseAngle = 30.0f; + bool smooth = true; + + osg::ref_ptr glyphGeometry = osgText::computeGlyphGeometry(glyph, thickness, width); + osg::ref_ptr textGeometry = osgText::computeTextGeometry(glyphGeometry.get(), profile, width); + osg::ref_ptr shellGeometry = outline ? osgText::computeShellGeometry(glyphGeometry.get(), profile, width) : 0; + if (textGeometry.valid()) geode->addDrawable(textGeometry.get()); + if (shellGeometry.valid()) geode->addDrawable(shellGeometry.get()); + + // create the normals + if (smooth && textGeometry.valid()) + { + osgUtil::SmoothingVisitor::smooth(*textGeometry, osg::DegreesToRadians(creaseAngle)); + } + + transform->addChild(geode.get()); + + _textNode->addChild(transform.get()); + + transform->getOrCreateStateSet()->setMode(GL_NORMALIZE, osg::StateAttribute::ON); + } void TextTechnique::finish() @@ -226,14 +344,16 @@ void TextTechnique::finish() void TextTechnique::traverse(osg::NodeVisitor& nv) { - OSG_NOTICE<<"TextTechnique::traverse()"<Group::traverse(nv); } ///////////////////////////////////////////////////////////////////////////////////////// // // TextNode // -TextNode::TextNode() +TextNode::TextNode(): + _characterSize(1.0f) { } @@ -264,14 +384,14 @@ void TextNode::setTextTechnique(TextTechnique* technique) { if (_technique==technique) return; + if (_technique.valid()) _technique->setTextNode(0); + if (TextTechnique::getDefaultTextTechinque()==technique) { OSG_NOTICE<<"Warning: Attempt to assign DefaultTextTechnique() prototype to TextNode::setTextTechnique(..), assigning a clone() of it instead."<setTextNode(0); - _technique = technique; if (_technique.valid()) _technique->setTextNode(this); diff --git a/examples/osgtext3D/TextNode.h b/examples/osgtext3D/TextNode.h index dba503e78..736322800 100644 --- a/examples/osgtext3D/TextNode.h +++ b/examples/osgtext3D/TextNode.h @@ -142,6 +142,9 @@ class TextTechnique : public osg::Object /// called by Layout engine to place individual characters virtual void addCharacter(const osg::Vec3& position, const osg::Vec3& size, Glyph* glyph, Style* style); + /// called by Layout engine to place individual characters + virtual void addCharacter(const osg::Vec3& position, const osg::Vec3& size, Glyph3D* glyph, Style* style); + /// finish building new charater layout virtual void finish(); diff --git a/examples/osgtext3D/osgtext3D.cpp b/examples/osgtext3D/osgtext3D.cpp index 404150579..91dc16244 100644 --- a/examples/osgtext3D/osgtext3D.cpp +++ b/examples/osgtext3D/osgtext3D.cpp @@ -167,8 +167,16 @@ int main(int argc, char** argv) std::string word("This is a new test."); while (arguments.read("-w",word)) {} + osg::ref_ptr style = new osgText::Style; + + float thickness = 0.0f; + while(arguments.read("--thickness",thickness)) {} + style->setThicknessRatio(thickness); + osgText::TextNode* text = new osgText::TextNode; text->setText(word); + text->setFont(font.get()); + text->setStyle(style.get()); text->setTextTechnique(new osgText::TextTechnique); text->update();