Moved TextNode from osgText into example/osgtext3D in prep for 3.0
This commit is contained in:
@@ -1,9 +1,14 @@
|
||||
SET(TARGET_SRC
|
||||
TextNode.cpp
|
||||
osgtext3D_orig.cpp
|
||||
osgtext3D_test.cpp
|
||||
osgtext3D.cpp
|
||||
)
|
||||
|
||||
SET(TARGET_H
|
||||
TextNode.h
|
||||
)
|
||||
|
||||
SET(TARGET_ADDED_LIBRARIES osgText )
|
||||
#### end var setup ###
|
||||
SETUP_EXAMPLE(osgtext3D)
|
||||
|
||||
291
examples/osgtext3D/TextNode.cpp
Normal file
291
examples/osgtext3D/TextNode.cpp
Normal file
@@ -0,0 +1,291 @@
|
||||
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2010 Robert Osfield
|
||||
*
|
||||
* This library is open source and may be redistributed and/or modified under
|
||||
* the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
|
||||
* (at your option) any later version. The full license is in LICENSE file
|
||||
* included with this distribution, and on the openscenegraph.org website.
|
||||
*
|
||||
* This library is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* OpenSceneGraph Public License for more details.
|
||||
*/
|
||||
|
||||
#include "TextNode.h"
|
||||
#include "../../src/osgText/GlyphGeometry.h"
|
||||
|
||||
#include <osg/PositionAttitudeTransform>
|
||||
#include <osg/Geode>
|
||||
#include <osgUtil/SmoothingVisitor>
|
||||
|
||||
#include <osg/io_utils>
|
||||
|
||||
using namespace osgText;
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// Layout
|
||||
//
|
||||
Layout::Layout()
|
||||
{
|
||||
}
|
||||
|
||||
Layout::Layout(const Layout& layout, const osg::CopyOp& copyop):
|
||||
osg::Object(layout,copyop)
|
||||
{
|
||||
}
|
||||
|
||||
osg::ref_ptr<Layout>& Layout::getDefaultLayout()
|
||||
{
|
||||
static OpenThreads::Mutex s_DefaultLayoutMutex;
|
||||
OpenThreads::ScopedLock<OpenThreads::Mutex> lock(s_DefaultLayoutMutex);
|
||||
|
||||
static osg::ref_ptr<Layout> s_defaultLayout = new Layout;
|
||||
return s_defaultLayout;
|
||||
}
|
||||
|
||||
void Layout::layout(TextNode& text) const
|
||||
{
|
||||
OSG_NOTICE<<"Layout::layout"<<std::endl;
|
||||
|
||||
Font* font = text.getActiveFont();
|
||||
Style* style = text.getActiveStyle();
|
||||
TextTechnique* technique = text.getTextTechnique();
|
||||
const String& str = text.getText();
|
||||
|
||||
if (!text.getTextTechnique())
|
||||
{
|
||||
OSG_NOTICE<<"Warning: no TextTechnique assigned to Layout"<<std::endl;
|
||||
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;
|
||||
size.z() = characterSize;
|
||||
}
|
||||
|
||||
|
||||
osgText::FontResolution resolution(32,32);
|
||||
if (style)
|
||||
{
|
||||
resolution.first = static_cast<unsigned int>(static_cast<float>(resolution.first)*style->getSampleDensity());
|
||||
resolution.second = static_cast<unsigned int>(static_cast<float>(resolution.second)*style->getSampleDensity());
|
||||
}
|
||||
|
||||
float characterWidthScale = 1.0f;
|
||||
float characterHeightScale = 1.0f;
|
||||
|
||||
bool textIs3D = (style && style->getThicknessRatio()!=0.0);
|
||||
if (!textIs3D)
|
||||
{
|
||||
characterWidthScale = 1.0f/static_cast<float>(resolution.first);
|
||||
characterHeightScale = 1.0f/static_cast<float>(resolution.second);
|
||||
}
|
||||
|
||||
osgText::KerningType kerningType = osgText::KERNING_DEFAULT;
|
||||
|
||||
technique->start();
|
||||
|
||||
unsigned int previousCharcode = 0;
|
||||
for(unsigned int i=0; i<str.size(); ++i)
|
||||
{
|
||||
unsigned int charcode = str[i];
|
||||
|
||||
if (size.z()==0.0f)
|
||||
{
|
||||
osgText::Glyph* glyph = font->getGlyph(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 = "<<pos<<", charcode="<<charcode<<", glyph="<<glyph<< std::endl;
|
||||
if (glyph)
|
||||
{
|
||||
osg::Vec3 local_scale( size );
|
||||
technique->addCharacter(pos, local_scale, glyph, style);
|
||||
pos += osg::Vec3(size.x()*glyph->getHorizontalWidth(), 0.0f ,0.0f);
|
||||
}
|
||||
}
|
||||
|
||||
if (previousCharcode!=0 && charcode!=0)
|
||||
{
|
||||
osg::Vec2 offset = font->getKerning(previousCharcode, charcode, kerningType);
|
||||
OSG_NOTICE<<" offset = "<<offset<< std::endl;
|
||||
pos.x() += offset.x();
|
||||
pos.y() += offset.y();
|
||||
}
|
||||
|
||||
previousCharcode = charcode;
|
||||
}
|
||||
|
||||
technique->finish();
|
||||
}
|
||||
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// TextTechnique
|
||||
//
|
||||
TextTechnique::TextTechnique():
|
||||
_textNode(0)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
TextTechnique::TextTechnique(const TextTechnique& technique, const osg::CopyOp& copyop):
|
||||
osg::Object(technique, copyop),
|
||||
_textNode(0)
|
||||
{
|
||||
}
|
||||
|
||||
osg::ref_ptr<TextTechnique>& TextTechnique::getDefaultTextTechinque()
|
||||
{
|
||||
static OpenThreads::Mutex s_DefaultTextTechniqueMutex;
|
||||
OpenThreads::ScopedLock<OpenThreads::Mutex> lock(s_DefaultTextTechniqueMutex);
|
||||
|
||||
static osg::ref_ptr<TextTechnique> s_defaultTextTechnique = new TextTechnique;
|
||||
return s_defaultTextTechnique;
|
||||
}
|
||||
|
||||
void TextTechnique::start()
|
||||
{
|
||||
OSG_NOTICE<<"TextTechnique::start()"<<std::endl;
|
||||
}
|
||||
|
||||
void TextTechnique::addCharacter(const osg::Vec3& position, const osg::Vec3& size, Glyph* glyph, Style* style)
|
||||
{
|
||||
OSG_NOTICE<<"TextTechnique::addCharacter 2D("<<position<<", "<<size<<", "<<glyph<<", "<<style<<")"<<std::endl;
|
||||
}
|
||||
|
||||
void TextTechnique::addCharacter(const osg::Vec3& position, const osg::Vec3& size, Glyph3D* glyph, Style* style)
|
||||
{
|
||||
OSG_NOTICE<<"TextTechnique::addCharacter 3D("<<position<<", "<<size<<", "<<glyph<<", "<<style<<")"<<std::endl;
|
||||
|
||||
osg::ref_ptr<osg::PositionAttitudeTransform> 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(size);
|
||||
|
||||
osg::ref_ptr<osg::Geode> geode = new osg::Geode;
|
||||
|
||||
const Bevel* bevel = style ? style->getBevel() : 0;
|
||||
bool outline = style ? style->getOutlineRatio()>0.0f : false;
|
||||
float width = style->getThicknessRatio();
|
||||
float creaseAngle = 30.0f;
|
||||
bool smooth = true;
|
||||
|
||||
if (bevel)
|
||||
{
|
||||
float thickness = bevel->getBevelThickness();
|
||||
|
||||
osg::ref_ptr<osg::Geometry> glyphGeometry = osgText::computeGlyphGeometry(glyph, thickness, width);
|
||||
osg::ref_ptr<osg::Geometry> textGeometry = osgText::computeTextGeometry(glyphGeometry.get(), *bevel, width);
|
||||
osg::ref_ptr<osg::Geometry> shellGeometry = outline ? osgText::computeShellGeometry(glyphGeometry.get(), *bevel, 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));
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
osg::ref_ptr<osg::Geometry> textGeometry = osgText::computeTextGeometry(glyph, width);
|
||||
if (textGeometry.valid()) geode->addDrawable(textGeometry.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()
|
||||
{
|
||||
OSG_NOTICE<<"TextTechnique::finish()"<<std::endl;
|
||||
}
|
||||
|
||||
void TextTechnique::traverse(osg::NodeVisitor& nv)
|
||||
{
|
||||
// OSG_NOTICE<<"TextTechnique::traverse()"<<std::endl;
|
||||
if (_textNode) _textNode->Group::traverse(nv);
|
||||
}
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// TextNode
|
||||
//
|
||||
TextNode::TextNode():
|
||||
_characterSize(1.0f)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
TextNode::TextNode(const TextNode& text, const osg::CopyOp& copyop):
|
||||
osg::Group(text, copyop)
|
||||
{
|
||||
}
|
||||
|
||||
TextNode::~TextNode()
|
||||
{
|
||||
setTextTechnique(0);
|
||||
}
|
||||
|
||||
void TextNode::traverse(osg::NodeVisitor& nv)
|
||||
{
|
||||
if (_technique.valid())
|
||||
{
|
||||
_technique->traverse(nv);
|
||||
}
|
||||
else
|
||||
{
|
||||
Group::traverse(nv);
|
||||
}
|
||||
}
|
||||
|
||||
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."<<std::endl;
|
||||
technique = new TextTechnique(*TextTechnique::getDefaultTextTechinque());
|
||||
}
|
||||
|
||||
_technique = technique;
|
||||
|
||||
if (_technique.valid()) _technique->setTextNode(this);
|
||||
}
|
||||
|
||||
|
||||
void TextNode::update()
|
||||
{
|
||||
getActiveLayout()->layout(*this);
|
||||
}
|
||||
|
||||
void TextNode::setText(const std::string& str)
|
||||
{
|
||||
_string.set(str);
|
||||
}
|
||||
154
examples/osgtext3D/TextNode.h
Normal file
154
examples/osgtext3D/TextNode.h
Normal file
@@ -0,0 +1,154 @@
|
||||
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2010 Robert Osfield
|
||||
*
|
||||
* This library is open source and may be redistributed and/or modified under
|
||||
* the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
|
||||
* (at your option) any later version. The full license is in LICENSE file
|
||||
* included with this distribution, and on the openscenegraph.org website.
|
||||
*
|
||||
* This library is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* OpenSceneGraph Public License for more details.
|
||||
*/
|
||||
|
||||
#ifndef OSGTEXT_TEXTNODE
|
||||
#define OSGTEXT_TEXTNODE 1
|
||||
|
||||
|
||||
#include <osg/Group>
|
||||
#include <osg/Quat>
|
||||
#include <osgUtil/CullVisitor>
|
||||
|
||||
#include <osgText/Font>
|
||||
#include <osgText/String>
|
||||
#include <osgText/Glyph>
|
||||
#include <osgText/Style>
|
||||
|
||||
namespace osgText {
|
||||
|
||||
// forward declare
|
||||
class TextNode;
|
||||
|
||||
class OSGTEXT_EXPORT Layout : public osg::Object
|
||||
{
|
||||
public:
|
||||
|
||||
Layout();
|
||||
Layout(const Layout& layout, const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY);
|
||||
|
||||
META_Object(osgText,Layout)
|
||||
|
||||
/// default Layout implementation used if no other is specified on TextNode
|
||||
static osg::ref_ptr<Layout>& getDefaultLayout();
|
||||
|
||||
virtual void layout(TextNode& text) const;
|
||||
|
||||
protected:
|
||||
};
|
||||
|
||||
class OSGTEXT_EXPORT TextTechnique : public osg::Object
|
||||
{
|
||||
public:
|
||||
|
||||
TextTechnique();
|
||||
TextTechnique(const TextTechnique& technique, const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY);
|
||||
|
||||
META_Object(osgText, TextTechnique)
|
||||
|
||||
TextNode* getTextNode() { return _textNode; }
|
||||
const TextNode* getTextNode() const { return _textNode; }
|
||||
|
||||
/// default TextTechnique implementation used if no other is specified on TextNode
|
||||
static osg::ref_ptr<TextTechnique>& getDefaultTextTechinque();
|
||||
|
||||
/// start building a new charater layout
|
||||
virtual void start();
|
||||
|
||||
/// 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();
|
||||
|
||||
/// provide traversal control
|
||||
virtual void traverse(osg::NodeVisitor& nv);
|
||||
|
||||
protected:
|
||||
|
||||
friend class TextNode;
|
||||
|
||||
void setTextNode(TextNode* textNode) { _textNode = textNode; }
|
||||
|
||||
TextNode* _textNode;
|
||||
};
|
||||
|
||||
class OSGTEXT_EXPORT TextNode : public osg::Group
|
||||
{
|
||||
public:
|
||||
|
||||
TextNode();
|
||||
TextNode(const TextNode& text, const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY);
|
||||
|
||||
META_Node(osgText, TextNode)
|
||||
|
||||
virtual void traverse(osg::NodeVisitor& nv);
|
||||
|
||||
void setFont(Font* font) { _font = font; }
|
||||
Font* getFont() { return _font.get(); }
|
||||
const Font* getFont() const { return _font.get(); }
|
||||
Font* getActiveFont() { return _font.valid() ? _font.get() : Font::getDefaultFont().get(); }
|
||||
const Font* getActiveFont() const { return _font.valid() ? _font.get() : Font::getDefaultFont().get(); }
|
||||
|
||||
void setStyle(Style* style) { _style = style; }
|
||||
Style* getStyle() { return _style.get(); }
|
||||
const Style* getStyle() const { return _style.get(); }
|
||||
Style* getActiveStyle() { return _style.valid() ? _style.get() : Style::getDefaultStyle().get(); }
|
||||
const Style* getActiveStyle() const { return _style.valid() ? _style.get() : Style::getDefaultStyle().get(); }
|
||||
|
||||
void setLayout(Layout* layout) { _layout = layout; }
|
||||
Layout* getLayout() { return _layout.get(); }
|
||||
const Layout* getLayout() const { return _layout.get(); }
|
||||
const Layout* getActiveLayout() const { return _layout.valid() ? _layout.get() : Layout::getDefaultLayout().get(); }
|
||||
|
||||
void setTextTechnique(TextTechnique* technique);
|
||||
TextTechnique* getTextTechnique() { return _technique.get(); }
|
||||
const TextTechnique* getTextTechnique() const { return _technique.get(); }
|
||||
|
||||
void setText(const std::string& str);
|
||||
void setText(const String& str) { _string = str; }
|
||||
String& getText() { return _string; }
|
||||
const String& getText() const { return _string; }
|
||||
|
||||
void setPosition(const osg::Vec3d& position) { _position = position; }
|
||||
const osg::Vec3d& getPosition() const { return _position; }
|
||||
|
||||
void setRotation(const osg::Quat& rotation) { _rotation = rotation; }
|
||||
const osg::Quat& getRotation() const { return _rotation; }
|
||||
|
||||
void setCharacterSize(float characterSize) { _characterSize = characterSize; }
|
||||
float getCharacterSize() const { return _characterSize; }
|
||||
|
||||
/// force a regeneration of the rendering backend required to represent the text.
|
||||
virtual void update();
|
||||
|
||||
protected:
|
||||
|
||||
virtual ~TextNode();
|
||||
|
||||
osg::ref_ptr<Font> _font;
|
||||
osg::ref_ptr<Style> _style;
|
||||
osg::ref_ptr<Layout> _layout;
|
||||
osg::ref_ptr<TextTechnique> _technique;
|
||||
|
||||
String _string;
|
||||
osg::Vec3d _position;
|
||||
osg::Quat _rotation;
|
||||
float _characterSize;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -30,9 +30,10 @@
|
||||
#include <osgViewer/ViewerEventHandlers>
|
||||
#include <osg/io_utils>
|
||||
|
||||
#include <osgText/TextNode>
|
||||
#include <osgText/Text3D>
|
||||
|
||||
#include "TextNode.h"
|
||||
|
||||
extern int main_orig(int, char**);
|
||||
extern int main_test(int, char**);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user