Further work on new 3D text support

This commit is contained in:
Robert Osfield 2010-09-06 15:43:59 +00:00
parent 32db4d6a98
commit a6abbb545e
10 changed files with 109 additions and 179 deletions

View File

@ -407,93 +407,6 @@ public:
};
/////////////////////////////////////////////////////////////////////////////////////////
//
// BevelProfile
//
BevelProfile::BevelProfile()
{
flatBevel();
}
void BevelProfile::flatBevel(float width)
{
_vertices.clear();
if (width>0.5f) width = 0.5f;
_vertices.push_back(osg::Vec2(0.0f,0.0f));
_vertices.push_back(osg::Vec2(width,1.0f));
if (width<0.5f) _vertices.push_back(osg::Vec2(1-width,1.0f));
_vertices.push_back(osg::Vec2(1.0f,0.0f));
}
void BevelProfile::roundedBevel(float width, unsigned int numSteps)
{
_vertices.clear();
if (width>0.5f) width = 0.5f;
unsigned int i = 0;
for(; i<=numSteps; ++i)
{
float angle = float(osg::PI)*0.5f*(float(i)/float(numSteps));
_vertices.push_back( osg::Vec2((1.0f-cosf(angle))*width, sinf(angle)) );
}
// start the second half one into the curve if the width is half way across
i = width<0.5f ? 0 : 1;
for(; i<=numSteps; ++i)
{
float angle = float(osg::PI)*0.5f*(float(numSteps-i)/float(numSteps));
_vertices.push_back( osg::Vec2(1.0-(1.0f-cosf(angle))*width, sin(angle)) );
}
}
void BevelProfile::roundedBevel2(float width, unsigned int numSteps)
{
_vertices.clear();
if (width>0.5f) width = 0.5f;
float h = 0.1f;
float r = 1.0f-h;
_vertices.push_back(osg::Vec2(0.0,0.0));
unsigned int i = 0;
for(; i<=numSteps; ++i)
{
float angle = float(osg::PI)*0.5f*(float(i)/float(numSteps));
_vertices.push_back( osg::Vec2((1.0f-cosf(angle))*width, h + sinf(angle)*r) );
}
// start the second half one into the curve if the width is half way across
i = width<0.5f ? 0 : 1;
for(; i<=numSteps; ++i)
{
float angle = float(osg::PI)*0.5f*(float(numSteps-i)/float(numSteps));
_vertices.push_back( osg::Vec2(1.0-(1.0f-cosf(angle))*width, h + sin(angle)*r) );
}
_vertices.push_back(osg::Vec2(1.0,0.0));
}
void BevelProfile::print(std::ostream& fout)
{
OSG_NOTICE<<"print bevel"<<std::endl;
for(Vertices::iterator itr = _vertices.begin();
itr != _vertices.end();
++itr)
{
OSG_NOTICE<<" "<<*itr<<std::endl;
}
}
/////////////////////////////////////////////////////////////////////////////////////////
//
// computeGlyphGeometry
@ -601,7 +514,7 @@ osg::Geometry* computeGlyphGeometry(osgText::Glyph3D* glyph, float bevelThicknes
//
// computeTextGeometry
//
osg::Geometry* computeTextGeometry(osg::Geometry* glyphGeometry, BevelProfile& profile, float width)
osg::Geometry* computeTextGeometry(osg::Geometry* glyphGeometry, const osgText::Bevel& profile, float width)
{
osg::Vec3Array* orig_vertices = dynamic_cast<osg::Vec3Array*>(glyphGeometry->getVertexArray());
if (!orig_vertices)
@ -698,7 +611,7 @@ osg::Geometry* computeTextGeometry(osg::Geometry* glyphGeometry, BevelProfile& p
unsigned int no_vertices_on_boundary = bevel->size()/2;
osgText::BevelProfile::Vertices& profileVertices = profile.getVertices();
const osgText::Bevel::Vertices& profileVertices = profile.getVertices();
unsigned int no_vertices_on_bevel = profileVertices.size();
Indices bevelIndices;
@ -781,7 +694,7 @@ osg::Geometry* computeTextGeometry(osg::Geometry* glyphGeometry, BevelProfile& p
//
// computeShellGeometry
//
osg::Geometry* computeShellGeometry(osg::Geometry* glyphGeometry, BevelProfile& profile, float width)
osg::Geometry* computeShellGeometry(osg::Geometry* glyphGeometry, const osgText::Bevel& profile, float width)
{
osg::Vec3Array* orig_vertices = dynamic_cast<osg::Vec3Array*>(glyphGeometry->getVertexArray());
if (!orig_vertices)
@ -924,7 +837,7 @@ osg::Geometry* computeShellGeometry(osg::Geometry* glyphGeometry, BevelProfile&
unsigned int no_vertices_on_boundary = bevel->size()/2;
osgText::BevelProfile::Vertices& profileVertices = profile.getVertices();
const osgText::Bevel::Vertices& profileVertices = profile.getVertices();
unsigned int no_vertices_on_bevel = profileVertices.size();
Indices bevelIndices;

View File

@ -15,38 +15,16 @@
#define OSGTEXT_GLYPHGEOMETRY 1
#include <osgText/Font3D>
#include "TextNode.h"
namespace osgText
{
class BevelProfile
{
public:
typedef std::vector<osg::Vec2> Vertices;
BevelProfile();
void flatBevel(float width=0.25f);
void roundedBevel(float width=0.5f, unsigned int numSteps=10);
void roundedBevel2(float width=0.5f, unsigned int numSteps=10);
void print(std::ostream& fout);
Vertices& getVertices() { return _vertices; }
protected:
Vertices _vertices;
};
extern osg::Geometry* computeGlyphGeometry(osgText::Glyph3D* glyph, float bevelThickness, float shellThickness);
extern osg::Geometry* computeTextGeometry(osg::Geometry* glyphGeometry, BevelProfile& profile, float width);
extern osg::Geometry* computeTextGeometry(osg::Geometry* glyphGeometry, const Bevel& profile, float width);
extern osg::Geometry* computeShellGeometry(osg::Geometry* glyphGeometry, BevelProfile& profile, float width);
extern osg::Geometry* computeShellGeometry(osg::Geometry* glyphGeometry, const Bevel& profile, float width);
}

View File

@ -28,7 +28,7 @@ using namespace osgText;
//
Bevel::Bevel()
{
_thickness = 0.1f;
_thickness = 0.02f;
flatBevel();
}
@ -191,8 +191,8 @@ void Layout::layout(TextNode& text) const
osg::Vec3 size(characterSize, characterSize, 0.0);
if (style)
{
size.y() = characterSize * style->getWidthRatio();
size.z() = characterSize * style->getThicknessRatio();
size.y() = characterSize;
size.z() = characterSize;
}
@ -242,8 +242,11 @@ void Layout::layout(TextNode& text) const
OSG_NOTICE<<"pos = "<<pos<<", charcode="<<charcode<<", glyph="<<glyph<< std::endl;
if (glyph)
{
technique->addCharacter(pos, size, glyph, style);
pos += osg::Vec3(size.x()*(glyph->getHorizontalAdvance()*characterWidthScale), 0.0f ,0.0f);
osg::Vec3 local_scale( size );
local_scale *= (1.0f/font->getScale());
technique->addCharacter(pos, local_scale, glyph, style);
pos += osg::Vec3(size.x()*(glyph->getHorizontalWidth()/font->getScale()), 0.0f ,0.0f);
}
}
@ -301,25 +304,26 @@ void TextTechnique::addCharacter(const osg::Vec3& position, const osg::Vec3& siz
{
OSG_NOTICE<<"TextTechnique::addCharacter 3D("<<position<<", "<<size<<", "<<glyph<<", "<<style<<")"<<std::endl;
double scale = size.x() / glyph->getVerticalHeight();
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(osg::Vec3d(scale, scale, scale));
transform->setScale(size);
osg::ref_ptr<osg::Geode> geode = new osg::Geode;
bool outline = false;
float thickness = 5;
float width = 10;
BevelProfile profile;
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(), profile, width);
osg::ref_ptr<osg::Geometry> shellGeometry = outline ? osgText::computeShellGeometry(glyphGeometry.get(), profile, width) : 0;
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());
@ -328,6 +332,10 @@ void TextTechnique::addCharacter(const osg::Vec3& position, const osg::Vec3& siz
{
osgUtil::SmoothingVisitor::smooth(*textGeometry, osg::DegreesToRadians(creaseAngle));
}
}
else
{
}
transform->addChild(geode.get());

View File

@ -74,7 +74,7 @@ int main_experimental(osg::ArgumentParser& arguments)
OSG_NOTICE<<"creaseAngle="<<creaseAngle<<std::endl;
osgText::BevelProfile profile;
osgText::Bevel profile;
float ratio = 0.5;
while(arguments.read("--rounded",ratio)) { profile.roundedBevel(ratio); }
while(arguments.read("--rounded2",ratio)) { profile.roundedBevel2(ratio); }
@ -169,10 +169,21 @@ int main(int argc, char** argv)
osg::ref_ptr<osgText::Style> style = new osgText::Style;
float thickness = 0.0f;
float thickness = 0.1f;
while(arguments.read("--thickness",thickness)) {}
style->setThicknessRatio(thickness);
// set up any bevel if required
float r;
osg::ref_ptr<osgText::Bevel> bevel;
while(arguments.read("--rounded",r)) { bevel = new osgText::Bevel; bevel->roundedBevel2(r); }
while(arguments.read("--rounded")) { bevel = new osgText::Bevel; bevel->roundedBevel2(0.25); }
while(arguments.read("--flat",r)) { bevel = new osgText::Bevel; bevel->flatBevel(r); }
while(arguments.read("--flat")) { bevel = new osgText::Bevel; bevel->flatBevel(0.25); }
while(arguments.read("--bevel-thickness",r)) { if (bevel.valid()) bevel->setBevelThickness(r); }
style->setBevel(bevel);
osgText::TextNode* text = new osgText::TextNode;
text->setText(word);
text->setFont(font.get());
@ -180,6 +191,8 @@ int main(int argc, char** argv)
text->setTextTechnique(new osgText::TextTechnique);
text->update();
viewer.addEventHandler( new osgGA::StateSetManipulator(viewer.getCamera()->getOrCreateStateSet()) );
viewer.addEventHandler(new osgViewer::StatsHandler);
viewer.setSceneData(text);
return viewer.run();

View File

@ -16,8 +16,7 @@
#include <osgText/TextBase>
#include <osgText/Font3D>
#include <osgText/Font>
namespace osgText {
@ -75,11 +74,7 @@ public:
/** Set the Font to use to render the text.
* setFont(0) sets the use of the default font.*/
inline void setFont(Font3D* font=0) { setFont(osg::ref_ptr<Font3D>(font)); };
/** Set the Font to use to render the text.*/
void setFont(osg::ref_ptr<Font3D> font);
void setFont(Font* font);
/** Set the font, loaded from the specified front file, to use to render the text,
* setFont("") sets the use of the default font.
@ -87,7 +82,7 @@ public:
void setFont(const std::string& fontfile);
/** Get the font. Return 0 if default is being used.*/
const Font3D* getFont() const { return _font.get(); }
const Font* getFont() const { return _font.get(); }
@ -156,7 +151,7 @@ protected:
TextRenderInfo _textRenderInfo;
osg::ref_ptr<Font3D> _font;
osg::ref_ptr<Font> _font;
float _characterDepth;

View File

@ -35,7 +35,8 @@ struct Char3DInfo
_maxY(-FLT_MAX),
_maxX(-FLT_MAX),
_minX(FLT_MAX),
_minY(FLT_MAX)
_minY(FLT_MAX),
_coord_scale(1.0/64.0)
{
}
~Char3DInfo()
@ -55,8 +56,12 @@ struct Char3DInfo
return _geometry.get();
}
void addVertex(const osg::Vec3& pos)
void addVertex(osg::Vec3 pos)
{
_previous = pos;
pos *= _coord_scale;
if (!_verts->empty() && _verts->back()==pos)
{
// OSG_NOTICE<<"addVertex("<<pos<<") duplicate, ignoring"<<std::endl;
@ -84,7 +89,7 @@ struct Char3DInfo
}
void conicTo(const osg::Vec2& control, const osg::Vec2& pos)
{
osg::Vec3 p0 = _verts->back();
osg::Vec3 p0 = _previous;
osg::Vec3 p1 = osg::Vec3(control.x(),control.y(),0);
osg::Vec3 p2 = osg::Vec3(pos.x(),pos.y(),0);
@ -103,7 +108,7 @@ struct Char3DInfo
void cubicTo(const osg::Vec2& control1, const osg::Vec2& control2, const osg::Vec2& pos)
{
osg::Vec3 p0 = _verts->back();
osg::Vec3 p0 = _previous;
osg::Vec3 p1 = osg::Vec3(control1.x(),control1.y(),0);
osg::Vec3 p2 = osg::Vec3(control2.x(),control2.y(),0);
osg::Vec3 p3 = osg::Vec3(pos.x(),pos.y(),0);
@ -136,44 +141,45 @@ struct Char3DInfo
osg::ref_ptr<osg::Vec3Array> _verts;
osg::ref_ptr<osg::Geometry> _geometry;
osg::Vec3 _previous;
int _idx;
int _numSteps;
double _maxY;
double _maxX;
double _minX;
double _minY;
double _coord_scale;
};
#define FT_NUM(x) (x/64.0)
int moveTo( const FT_Vector* to, void* user )
{
Char3DInfo* char3d = (Char3DInfo*)user;
char3d->moveTo( osg::Vec2(FT_NUM(to->x),FT_NUM(to->y)) );
char3d->moveTo( osg::Vec2(to->x,to->y) );
return 0;
}
int lineTo( const FT_Vector* to, void* user )
{
Char3DInfo* char3d = (Char3DInfo*)user;
char3d->lineTo( osg::Vec2(FT_NUM(to->x),FT_NUM(to->y)) );
char3d->lineTo( osg::Vec2(to->x,to->y) );
return 0;
}
int conicTo( const FT_Vector* control,const FT_Vector* to, void* user )
{
Char3DInfo* char3d = (Char3DInfo*)user;
char3d->conicTo( osg::Vec2(FT_NUM(control->x),FT_NUM(control->y)), osg::Vec2(FT_NUM(to->x),FT_NUM(to->y)) );
char3d->conicTo( osg::Vec2(control->x,control->y), osg::Vec2(to->x,to->y) );
return 0;
}
int cubicTo( const FT_Vector* control1,const FT_Vector* control2,const FT_Vector* to, void* user )
{
Char3DInfo* char3d = (Char3DInfo*)user;
char3d->cubicTo(
osg::Vec2(FT_NUM(control1->x),FT_NUM(control1->y)),
osg::Vec2(FT_NUM(control2->x),FT_NUM(control2->y)),
osg::Vec2(FT_NUM(to->x),FT_NUM(to->y)) );
osg::Vec2(control1->x,control1->y),
osg::Vec2(control2->x,control2->y),
osg::Vec2(to->x,to->y) );
return 0;
}
#undef FT_NUM
}
@ -183,7 +189,8 @@ FreeTypeFont::FreeTypeFont(const std::string& filename, FT_Face face, unsigned i
_buffer(0),
_face(face),
_flags(flags),
_scale(1.0f)
_scale(1.0f),
_freetype_scale(1.0f)
{
init();
}
@ -194,7 +201,8 @@ FreeTypeFont::FreeTypeFont(FT_Byte* buffer, FT_Face face, unsigned int flags):
_buffer(buffer),
_face(face),
_flags(flags),
_scale(1.0f)
_scale(1.0f),
_freetype_scale(1.0f)
{
init();
}
@ -279,7 +287,13 @@ void FreeTypeFont::init()
// long xmax = ft_ceiling( bb.xMax );
// double width = (xmax - xmin)/64.0;
_scale = 1.0/height;
#if 1
_freetype_scale = 1.0f/height;
_scale = 1.0f;
#else
_freetype_scale = 1.0f;
_scale = 1.0f/height;
#endif
}
}
@ -407,12 +421,14 @@ osgText::Glyph* FreeTypeFont::getGlyph(const osgText::FontResolution& fontRes, u
}
FT_Glyph_Metrics* metrics = &(glyphslot->metrics);
FT_Glyph_Metrics* metrics = &(_face->glyph->metrics);
glyph->setHorizontalBearing(osg::Vec2((float)metrics->horiBearingX/64.0f,(float)(metrics->horiBearingY-metrics->height)/64.0f)); // bottom left.
glyph->setHorizontalAdvance((float)metrics->horiAdvance/64.0f);
glyph->setVerticalBearing(osg::Vec2((float)metrics->vertBearingX/64.0f,(float)(metrics->vertBearingY-metrics->height)/64.0f)); // top middle.
glyph->setVerticalAdvance((float)metrics->vertAdvance/64.0f);
float coord_scale = _freetype_scale/64.0f;
glyph->setHorizontalBearing(osg::Vec2((float)metrics->horiBearingX * coord_scale,(float)(metrics->horiBearingY-metrics->height) * coord_scale)); // bottom left.
glyph->setHorizontalAdvance((float)metrics->horiAdvance * coord_scale);
glyph->setVerticalBearing(osg::Vec2((float)metrics->vertBearingX * coord_scale,(float)(metrics->vertBearingY-metrics->height) * coord_scale)); // top middle.
glyph->setVerticalAdvance((float)metrics->vertAdvance * coord_scale);
// cout << " in getGlyph() implementation="<<this<<" "<<_filename<<" facade="<<_facade<<endl;
@ -451,8 +467,11 @@ osgText::Glyph3D * FreeTypeFont::getGlyph3D(unsigned int charcode)
return 0;
}
float coord_scale = _freetype_scale/64.0f;
// ** init FreeType to describe the glyph
FreeType::Char3DInfo char3d(_facade->getNumberCurveSamples());
char3d._coord_scale = coord_scale;
FT_Outline outline = _face->glyph->outline;
FT_Outline_Funcs funcs;
@ -596,14 +615,13 @@ osgText::Glyph3D * FreeTypeFont::getGlyph3D(unsigned int charcode)
FT_Glyph_Metrics* metrics = &(_face->glyph->metrics);
glyph3D->setHorizontalBearing(osg::Vec2((float)metrics->horiBearingX/64.0f,(float)(metrics->horiBearingY-metrics->height)/64.0f)); // bottom left.
glyph3D->setHorizontalAdvance((float)metrics->horiAdvance/64.0f);
glyph3D->setVerticalBearing(osg::Vec2((float)metrics->vertBearingX/64.0f,(float)(metrics->vertBearingY-metrics->height)/64.0f)); // top middle.
glyph3D->setVerticalAdvance((float)metrics->vertAdvance/64.0f);
glyph3D->setWidth((float)metrics->width / 64.0f);
glyph3D->setHeight((float)metrics->height / 64.0f);
glyph3D->setHorizontalBearing(osg::Vec2((float)metrics->horiBearingX * coord_scale,(float)(metrics->horiBearingY-metrics->height) * coord_scale)); // bottom left.
glyph3D->setHorizontalAdvance((float)metrics->horiAdvance * coord_scale);
glyph3D->setVerticalBearing(osg::Vec2((float)metrics->vertBearingX * coord_scale,(float)(metrics->vertBearingY-metrics->height) * coord_scale)); // top middle.
glyph3D->setVerticalAdvance((float)metrics->vertAdvance * coord_scale);
glyph3D->setWidth((float)metrics->width * coord_scale);
glyph3D->setHeight((float)metrics->height * coord_scale);
FT_BBox ftbb;
FT_Outline_Get_BBox(&outline, &ftbb);
@ -613,7 +631,7 @@ osgText::Glyph3D * FreeTypeFont::getGlyph3D(unsigned int charcode)
long ymin = ft_floor( ftbb.yMin );
long ymax = ft_ceiling( ftbb.yMax );
osg::BoundingBox bb(xmin / 64.0f, ymin / 64.0f, 0.0f, xmax / 64.0f, ymax / 64.0f, 0.0f);
osg::BoundingBox bb(xmin * coord_scale, ymin * coord_scale, 0.0f, xmax * coord_scale, ymax * coord_scale, 0.0f);
glyph3D->setBoundingBox(bb);

View File

@ -58,6 +58,7 @@ protected:
FT_Face _face;
unsigned int _flags;
float _scale;
float _freetype_scale;
};
#endif

View File

@ -342,6 +342,8 @@ osg::Texture::FilterMode Font::getMagFilterHint() const
Glyph* Font::getGlyph(const FontResolution& fontRes, unsigned int charcode)
{
OSG_NOTICE<<"Font::getGlyph("<<charcode<<")"<<std::endl;
{
OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_glyphMapMutex);
FontSizeGlyphMap::iterator itr = _sizeGlyphMap.find(fontRes);
@ -364,6 +366,8 @@ Glyph* Font::getGlyph(const FontResolution& fontRes, unsigned int charcode)
Glyph3D* Font::getGlyph3D(unsigned int charcode)
{
OSG_NOTICE<<"Font::getGlyph3D("<<charcode<<")"<<std::endl;
{
OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_glyphMapMutex);
Glyph3DMap::iterator itr = _glyph3DMap.find(charcode);

View File

@ -102,7 +102,7 @@ void Text3D::accept(osg::PrimitiveFunctor& pf) const
}
}
void Text3D::setFont(osg::ref_ptr<Font3D> font)
void Text3D::setFont(Font* font)
{
_font = font;
@ -111,7 +111,7 @@ void Text3D::setFont(osg::ref_ptr<Font3D> font)
void Text3D::setFont(const std::string & fontfile)
{
setFont(readRefFont3DFile(fontfile));
setFont(readRefFontFile(fontfile));
}
String::iterator Text3D::computeLastCharacterOnLine(osg::Vec2& cursor, String::iterator first,String::iterator last)

View File

@ -11,7 +11,7 @@ static bool checkFont( const osgText::Text3D& text )
static bool readFont( osgDB::InputStream& is, osgText::Text3D& text )
{
std::string fontName; is.readWrappedString( fontName );
text.setFont( osgText::readFont3DFile(fontName) );
text.setFont( osgText::readFontFile(fontName) );
return true;
}