Further work on new 3D text support
This commit is contained in:
@@ -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;
|
||||
|
||||
@@ -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);
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -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,32 +304,37 @@ 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;
|
||||
|
||||
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;
|
||||
if (textGeometry.valid()) geode->addDrawable(textGeometry.get());
|
||||
if (shellGeometry.valid()) geode->addDrawable(shellGeometry.get());
|
||||
|
||||
// create the normals
|
||||
if (smooth && textGeometry.valid())
|
||||
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
|
||||
{
|
||||
osgUtil::SmoothingVisitor::smooth(*textGeometry, osg::DegreesToRadians(creaseAngle));
|
||||
}
|
||||
|
||||
transform->addChild(geode.get());
|
||||
|
||||
@@ -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();
|
||||
|
||||
Reference in New Issue
Block a user