Moved Style and Bevel classes out into their own include/osgText/Style header.

Introduced GlyphGeometry class for handling the geometry data for rendering 3D text
This commit is contained in:
Robert Osfield 2010-09-24 12:57:55 +00:00
parent d9a133476a
commit c006c75615
8 changed files with 362 additions and 212 deletions

View File

@ -246,5 +246,4 @@ public:
}
#endif

View File

@ -1,4 +1,4 @@
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
/* -*-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
@ -25,6 +25,7 @@
#include <osgText/Export>
#include <osgText/KerningType>
#include <osgText/Style>
#include <OpenThreads/Mutex>
@ -33,6 +34,7 @@ namespace osgText {
class Font;
class Text;
class Glyph3D;
class GlyphGeometry;
class GlyphTexture;
class OSGTEXT_EXPORT Glyph : public osg::Image
@ -95,6 +97,50 @@ protected:
};
class OSGTEXT_EXPORT GlyphGeometry : public osg::Referenced
{
public:
GlyphGeometry();
void setup(const Glyph* glyph, const Style* style);
bool match(const Style* style) const;
osg::Geode* getGeode() const { return _geode.get(); }
osg::Geometry* getGeometry() const { return _geometry.get(); }
/** Set the VertexArray of the glyph. */
void setVertexArray(osg::Vec3Array * va) { _vertices = va; }
/** Get the VertexArray of the glyph. */
osg::Vec3Array * getVertexArray() const { return _vertices.get(); }
/** Set the VertexArray of the glyph. */
void setNormalArray(osg::Vec3Array* na) { _normals = na; }
/** Get the NormalArray for the wall face. */
osg::Vec3Array* getNormalArray() const { return _normals.get(); }
/** Get the PrimitiveSetList for the front face. */
osg::Geometry::PrimitiveSetList& getFrontPrimitiveSetList() { return _frontPrimitiveSetList; }
/** Get the PrimitiveSetList for the wall face. */
osg::Geometry::PrimitiveSetList& getWallPrimitiveSetList() { return _wallPrimitiveSetList; }
/** Get et the PrimitiveSetList for the back face. */
osg::Geometry::PrimitiveSetList& getBackPrimitiveSetList() { return _backPrimitiveSetList; }
protected:
osg::ref_ptr<Style> _style;
osg::ref_ptr<osg::Geode> _geode;
osg::ref_ptr<osg::Geometry> _geometry;
osg::ref_ptr<osg::Vec3Array> _vertices;
osg::ref_ptr<osg::Vec3Array> _normals;
osg::Geometry::PrimitiveSetList _frontPrimitiveSetList;
osg::Geometry::PrimitiveSetList _wallPrimitiveSetList;
osg::Geometry::PrimitiveSetList _backPrimitiveSetList;
};
class OSGTEXT_EXPORT Glyph3D : public osg::Referenced
{
public:
@ -150,17 +196,18 @@ public:
osg::Vec3Array * getNormalArray() { return _normalArray.get(); }
float getHorizontalWidth() { return (-_horizontalBearing.x() + _horizontalAdvance); }
float getHorizontalHeight() { return (-_horizontalBearing.y() + _bb.yMax()); }
float getVerticalWidth() { return (-_verticalBearing.x() + _bb.xMax()); }
float getVerticalHeight() { return (-_verticalBearing.y() + _verticalAdvance); }
float getHorizontalWidth() const { return (-_horizontalBearing.x() + _horizontalAdvance); }
float getHorizontalHeight() const { return (-_horizontalBearing.y() + _bb.yMax()); }
float getVerticalWidth() const { return (-_verticalBearing.x() + _bb.xMax()); }
float getVerticalHeight() const { return (-_verticalBearing.y() + _verticalAdvance); }
void setWidth(float width) { _width = width; }
float getWidth() { return _width; }
float getWidth() const { return _width; }
void setHeight(float height) { _height = height; }
float getHeight() { return _height; }
float getHeight() const { return _height; }
GlyphGeometry* getGlyphGeometry(Style* style);
protected:
@ -193,6 +240,9 @@ protected:
osg::ref_ptr<osg::Vec3Array> _rawVertexArray;
osg::Geometry::PrimitiveSetList _rawFacePrimitiveSetList;
typedef std::list< osg::ref_ptr<GlyphGeometry> > GlyphGeometries;
GlyphGeometries _glyphGeometries;
};

112
include/osgText/Style Normal file
View File

@ -0,0 +1,112 @@
/* -*-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_STYLE
#define OSGTEXT_STYLE 1
#include <osg/Object>
#include <osg/Vec2>
#include <osgText/Export>
#include <vector>
namespace osgText
{
class OSGTEXT_EXPORT Bevel : public osg::Object
{
public:
Bevel();
Bevel(const Bevel& bevel, const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY);
META_Object(osgText, Bevel)
bool operator == (const Bevel& rhs) const
{
if (_thickness != rhs._thickness) return false;
return _vertices==rhs._vertices;
}
void setBevelThickness(float thickness) { _thickness = thickness; }
float getBevelThickness() const { return _thickness; }
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);
typedef std::vector<osg::Vec2> Vertices;
void setVertices(const Vertices& vertices) { _vertices = vertices; }
Vertices& getVertices() { return _vertices; }
const Vertices& getVertices() const { return _vertices; }
void print(std::ostream& fout);
protected:
float _thickness;
Vertices _vertices;
};
class OSGTEXT_EXPORT Style : public osg::Object
{
public:
Style();
Style(const Style& style, const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY);
META_Object(osgText, Style)
/// default Layout implementation used if no other is specified on TextNode
static osg::ref_ptr<Style>& getDefaultStyle();
bool operator == (const Style& style) const;
/// NULL is no bevel
void setBevel(Bevel* bevel) { _bevel = bevel; }
const Bevel* getBevel() const { return _bevel.get(); }
/// 1 is the default width of the text
void setWidthRatio(float widthRatio) { _widthRatio = widthRatio; }
float getWidthRatio() const { return _widthRatio; }
/// 0 is 2D text
void setThicknessRatio(float thicknessRatio) { _thicknessRatio = thicknessRatio; }
float getThicknessRatio() const { return _thicknessRatio; }
/// 0 is off
void setOutlineRatio(float outlineRatio) { _outlineRatio = outlineRatio; }
float getOutlineRatio() const { return _outlineRatio; }
/// 1.0 is default number of samples
void setSampleDensity(float sd) { _sampleDensity = sd; }
float getSampleDensity() const { return _sampleDensity; }
protected:
osg::ref_ptr<Bevel> _bevel;
float _widthRatio;
float _thicknessRatio;
float _outlineRatio;
float _sampleDensity;
};
}
#endif

View File

@ -21,88 +21,13 @@
#include <osgText/Font>
#include <osgText/String>
#include <osgText/Glyph>
#include <osgText/Style>
namespace osgText {
// forward declare
class TextNode;
class Glyph;
class OSGTEXT_EXPORT Bevel : public osg::Object
{
public:
Bevel();
Bevel(const Bevel& bevel, const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY);
META_Object(osgText, Bevel)
void setBevelThickness(float thickness) { _thickness = thickness; }
float getBevelThickness() const { return _thickness; }
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);
typedef std::vector<osg::Vec2> Vertices;
void setVertices(const Vertices& vertices) { _vertices = vertices; }
Vertices& getVertices() { return _vertices; }
const Vertices& getVertices() const { return _vertices; }
void print(std::ostream& fout);
protected:
float _thickness;
Vertices _vertices;
};
class OSGTEXT_EXPORT Style : public osg::Object
{
public:
Style();
Style(const Style& style, const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY);
META_Object(osgText, Style)
/// default Layout implementation used if no other is specified on TextNode
static osg::ref_ptr<Style>& getDefaultStyle();
/// NULL is no bevel
void setBevel(Bevel* bevel) { _bevel = bevel; }
const Bevel* getBevel() const { return _bevel.get(); }
/// 1 is the default width of the text
void setWidthRatio(float widthRatio) { _widthRatio = widthRatio; }
float getWidthRatio() const { return _widthRatio; }
/// 0 is 2D text
void setThicknessRatio(float thicknessRatio) { _thicknessRatio = thicknessRatio; }
float getThicknessRatio() const { return _thicknessRatio; }
/// 0 is off
void setOutlineRatio(float outlineRatio) { _outlineRatio = outlineRatio; }
float getOutlineRatio() const { return _outlineRatio; }
/// 1.0 is default number of samples
void setSampleDensity(float sd) { _sampleDensity = sd; }
float getSampleDensity() const { return _sampleDensity; }
protected:
osg::ref_ptr<Bevel> _bevel;
float _widthRatio;
float _thicknessRatio;
float _outlineRatio;
float _sampleDensity;
};
class OSGTEXT_EXPORT Layout : public osg::Object
{

View File

@ -15,6 +15,7 @@ SET(LIB_PUBLIC_HEADERS
${HEADER_PATH}/Glyph
${HEADER_PATH}/KerningType
${HEADER_PATH}/String
${HEADER_PATH}/Style
${HEADER_PATH}/TextBase
${HEADER_PATH}/Text
${HEADER_PATH}/TextNode
@ -34,6 +35,7 @@ ADD_LIBRARY(${LIB_NAME}
FadeText.cpp
Glyph.cpp
String.cpp
Style.cpp
TextBase.cpp
Text.cpp
TextNode.cpp

View File

@ -524,3 +524,26 @@ void Glyph3D::computeText3DGeometryData()
else if (prim->getName()=="wall") _wallPrimitiveSetList.push_back(prim);
}
}
GlyphGeometry* Glyph3D::getGlyphGeometry(Style* style)
{
OSG_NOTICE<<"Glyph3D::getGlyphGeometry(Style* style) not implementated."<<std::endl;
return 0;
}
GlyphGeometry::GlyphGeometry()
{
OSG_NOTICE<<"GlyphGeometry::GlyphGeometry() not implementated."<<std::endl;
}
void GlyphGeometry::setup(const Glyph* glyph, const Style* style)
{
OSG_NOTICE<<"GlyphGeometry::setup(const Glyph* glyph, const Style* style) not implementated."<<std::endl;
}
bool GlyphGeometry::match(const Style* style) const
{
OSG_NOTICE<<"GlyphGeometry::match(const Style*) not implementated."<<std::endl;
return false;
}

166
src/osgText/Style.cpp Normal file
View File

@ -0,0 +1,166 @@
/* -*-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 <osgText/Style>
#include <osg/Notify>
#include <osg/io_utils>
using namespace osgText;
/////////////////////////////////////////////////////////////////////////////////////////
//
// Bevel
//
Bevel::Bevel()
{
_thickness = 0.02f;
flatBevel();
}
Bevel::Bevel(const Bevel& bevel, const osg::CopyOp&):
_thickness(bevel._thickness),
_vertices(bevel._vertices)
{
}
void Bevel::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 Bevel::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 Bevel::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 Bevel::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;
}
}
/////////////////////////////////////////////////////////////////////////////////////////
//
// Style
//
Style::Style():
_widthRatio(1.0f),
_thicknessRatio(0.0f),
_outlineRatio(0.0f),
_sampleDensity(1.0f)
{
}
Style::Style(const Style& style, const osg::CopyOp& copyop):
osg::Object(style,copyop),
_bevel(dynamic_cast<Bevel*>(copyop(style._bevel.get()))),
_widthRatio(style._widthRatio),
_thicknessRatio(style._thicknessRatio),
_outlineRatio(style._outlineRatio),
_sampleDensity(style._sampleDensity)
{
}
/// default Layout implementation used if no other is specified on TextNode
osg::ref_ptr<Style>& Style::getDefaultStyle()
{
static OpenThreads::Mutex s_DefaultStyleMutex;
OpenThreads::ScopedLock<OpenThreads::Mutex> lock(s_DefaultStyleMutex);
static osg::ref_ptr<Style> s_defaultStyle = new Style;
return s_defaultStyle;
}
bool Style::operator == (const Style& rhs) const
{
if (&rhs==this) return true;
if (_bevel.valid())
{
if (!rhs._bevel) return false;
if (!(*_bevel == *rhs._bevel)) return false;
}
if (rhs._bevel.valid()) return false;
if (_widthRatio != rhs._widthRatio) return false;
if (_thicknessRatio != rhs._thicknessRatio) return false;
if (_outlineRatio != rhs._outlineRatio) return false;
if (_sampleDensity != rhs._sampleDensity) return false;
return false;
}

View File

@ -22,133 +22,6 @@
using namespace osgText;
/////////////////////////////////////////////////////////////////////////////////////////
//
// Bevel
//
Bevel::Bevel()
{
_thickness = 0.02f;
flatBevel();
}
Bevel::Bevel(const Bevel& bevel, const osg::CopyOp&):
_thickness(bevel._thickness),
_vertices(bevel._vertices)
{
}
void Bevel::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 Bevel::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 Bevel::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 Bevel::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;
}
}
/////////////////////////////////////////////////////////////////////////////////////////
//
// Style
//
Style::Style():
_widthRatio(1.0f),
_thicknessRatio(0.0f),
_outlineRatio(0.0f),
_sampleDensity(1.0f)
{
}
Style::Style(const Style& style, const osg::CopyOp& copyop):
osg::Object(style,copyop),
_bevel(dynamic_cast<Bevel*>(copyop(style._bevel.get()))),
_widthRatio(style._widthRatio),
_thicknessRatio(style._thicknessRatio),
_outlineRatio(style._outlineRatio),
_sampleDensity(style._sampleDensity)
{
}
/// default Layout implementation used if no other is specified on TextNode
osg::ref_ptr<Style>& Style::getDefaultStyle()
{
static OpenThreads::Mutex s_DefaultStyleMutex;
OpenThreads::ScopedLock<OpenThreads::Mutex> lock(s_DefaultStyleMutex);
static osg::ref_ptr<Style> s_defaultStyle = new Style;
return s_defaultStyle;
}
/////////////////////////////////////////////////////////////////////////////////////////
//
// Layout