Restructed the way that GlyphTexture is set up to better support control of osgText::ShaderTechnique from osgText::Text

This commit is contained in:
Robert Osfield 2017-10-23 14:50:35 +01:00
parent 817b6601d2
commit 4b295c46d1
8 changed files with 147 additions and 139 deletions

View File

@ -153,12 +153,12 @@ struct TextSettings
font->setMinFilterHint(minFilter);
font->setMagFilterHint(magFilter);
font->setMaxAnisotropy(maxAnisotropy);
font->setShaderTechnique(shaderTechnique);
text.setColor(textColor);
text.setBackdropType(backdropType);
text.setBackdropOffset(backdropOffset.x(), backdropOffset.y());
text.setBackdropColor(backdropColor);
text.setShaderTechnique(shaderTechnique);
text.setFont(font.get());

View File

@ -153,7 +153,7 @@ osg:: Node* createTextBelow(const osg::BoundingBox& bb, const std::string& label
if (s_useSDF)
{
text->getFont()->setShaderTechnique(osgText::ALL_FEATURES);
text->setShaderTechnique(osgText::ALL_FEATURES);
}
text->setAlignment(osgText::Text::CENTER_CENTER);
@ -186,7 +186,7 @@ osg:: Node* createTextLeft(const osg::BoundingBox& bb, const std::string& label,
if (s_useSDF)
{
text->getFont()->setShaderTechnique(osgText::ALL_FEATURES);
text->setShaderTechnique(osgText::ALL_FEATURES);
}
text->setAlignment(osgText::Text::RIGHT_CENTER);

View File

@ -107,11 +107,6 @@ public:
* return true on success, return false when not supported.*/
virtual bool getVerticalSize(float& ascender, float& descender) const { return _implementation ? _implementation->getVerticalSize(ascender, descender) : false; }
void setShaderTechnique(ShaderTechnique features) { _shaderTechnique = features; }
ShaderTechnique getShaderTechnique() const { return _shaderTechnique; }
/** Set the size of texture to create to store the glyph images when rendering.
* Note, this doesn't affect already created Texture Glhph's.*/
void setTextureSizeHint(unsigned int width,unsigned int height);
@ -162,6 +157,8 @@ public:
typedef std::vector< osg::ref_ptr<GlyphTexture> > GlyphTextureList;
GlyphTextureList& getGlyphTextureList() { return _glyphTextureList; }
void assignGlyphToGlyphTexture(Glyph* glyph, ShaderTechnique shaderTechnique);
protected:
virtual ~Font();
@ -186,8 +183,6 @@ protected:
// current active size of font
FontResolution _fontSize;
ShaderTechnique _shaderTechnique;
unsigned int _textureWidthHint;
unsigned int _textureHeightHint;
osg::Texture::FilterMode _minFilterHint;

View File

@ -77,22 +77,40 @@ public:
void setVerticalAdvance(float advance);
float getVerticalAdvance() const;
void setTexture(GlyphTexture* texture);
GlyphTexture* getTexture();
const GlyphTexture* getTexture() const;
struct TextureInfo : public osg::Referenced
{
TextureInfo():
texture(0),
texelMargin(0.0f) {}
void setTexturePosition(int posX,int posY);
int getTexturePositionX() const;
int getTexturePositionY() const;
TextureInfo(GlyphTexture* tex, int x, int y, const osg::Vec2& mintc, const osg::Vec2& maxtc, float margin):
texture(tex),
texturePositionX(x),
texturePositionY(y),
minTexCoord(mintc),
maxTexCoord(maxtc),
texelMargin(margin) {}
void setMinTexCoord(const osg::Vec2& coord);
const osg::Vec2& getMinTexCoord() const;
GlyphTexture* texture;
int texturePositionX;
int texturePositionY;
osg::Vec2 minTexCoord;
osg::Vec2 maxTexCoord;
float texelMargin;
};
void setMaxTexCoord(const osg::Vec2& coord);
const osg::Vec2& getMaxTexCoord() const;
void setTextureInfo(ShaderTechnique technique, TextureInfo* info)
{
if (technique>=_textureInfoList.size()) _textureInfoList.resize(technique+1);
_textureInfoList[technique] = info;
}
void setTexelMargin(float margin) { _texelMargin = margin; }
float getTexelMargin() const { return _texelMargin; }
const TextureInfo* getTextureInfo(ShaderTechnique technique) const
{
return (technique<_textureInfoList.size()) ? _textureInfoList[technique].get() : 0;
}
TextureInfo* getOrCreateTextureInfo(ShaderTechnique technique);
protected:
@ -112,16 +130,8 @@ protected:
osg::Vec2 _verticalBearing;
float _verticalAdvance;
GlyphTexture* _texture;
int _texturePosX;
int _texturePosY;
osg::Vec2 _minTexCoord;
osg::Vec2 _maxTexCoord;
float _texelMargin;
typedef osg::buffered_value<GLuint> GLObjectList;
mutable GLObjectList _globjList;
typedef std::vector< osg::ref_ptr<TextureInfo> > TextureInfoList;
TextureInfoList _textureInfoList;
};
class OSGTEXT_EXPORT GlyphGeometry : public osg::Referenced
@ -283,7 +293,7 @@ protected:
virtual ~GlyphTexture();
void copyGlyphImage(Glyph* glyph);
void copyGlyphImage(Glyph* glyph, Glyph::TextureInfo* info);
ShaderTechnique _shaderTechnique;

View File

@ -29,12 +29,6 @@ DefaultFont::DefaultFont()
_minFilterHint = osg::Texture::LINEAR_MIPMAP_LINEAR;
_magFilterHint = osg::Texture::LINEAR;
char *ptr;
if ((ptr = getenv("OSG_SDF_TEXT")) != 0)
{
_shaderTechnique = osgText::ALL_FEATURES;
}
constructGlyphs();
}

View File

@ -224,11 +224,6 @@ osg::ref_ptr<Font> osgText::readRefFontStream(std::istream& stream, const osgDB:
Font::Font(FontImplementation* implementation):
osg::Object(true),
#if 0
_shaderTechnique(ALL_FEATURES),
#else
_shaderTechnique(GREYSCALE),
#endif
_textureWidthHint(1024),
_textureHeightHint(1024),
_minFilterHint(osg::Texture::LINEAR_MIPMAP_LINEAR),
@ -247,15 +242,6 @@ Font::Font(FontImplementation* implementation):
if (osg_max_size<_textureWidthHint) _textureWidthHint = osg_max_size;
if (osg_max_size<_textureHeightHint) _textureHeightHint = osg_max_size;
}
if ((ptr = getenv("OSG_SDF_TEXT")) != 0)
{
_shaderTechnique = ALL_FEATURES;
}
else if ((ptr = getenv("OSG_GREYSCALE_TEXT")) != 0)
{
_shaderTechnique = GREYSCALE;
}
}
Font::~Font()
@ -459,6 +445,10 @@ void Font::addGlyph(const FontResolution& fontRes, unsigned int charcode, Glyph*
_sizeGlyphMap[fontRes][charcode]=glyph;
}
void Font::assignGlyphToGlyphTexture(Glyph* glyph, ShaderTechnique shaderTechnique)
{
int posX=0,posY=0;
GlyphTexture* glyphTexture = 0;
@ -485,7 +475,7 @@ void Font::addGlyph(const FontResolution& fontRes, unsigned int charcode, Glyph*
OSG_INFO<< " Font " << this<< ", numberOfTexturesAllocated "<<numberOfTexturesAllocated<<std::endl;
// reserve enough space for the glyphs.
glyphTexture->setShaderTechnique(_shaderTechnique);
glyphTexture->setShaderTechnique(shaderTechnique);
glyphTexture->setTextureSize(_textureWidthHint,_textureHeightHint);
glyphTexture->setFilter(osg::Texture::MIN_FILTER,_minFilterHint);
glyphTexture->setFilter(osg::Texture::MAG_FILTER,_magFilterHint);
@ -503,5 +493,4 @@ void Font::addGlyph(const FontResolution& fontRes, unsigned int charcode, Glyph*
// add the glyph into the texture.
glyphTexture->addGlyph(glyph,posX,posY);
}

View File

@ -42,8 +42,6 @@ using namespace std;
#endif
#if 0
#define TEXTURE_IMAGE_NUM_CHANNELS 1
#define TEXTURE_IMAGE_FORMAT OSGTEXT_GLYPH_FORMAT
@ -52,6 +50,11 @@ using namespace std;
#define TEXTURE_IMAGE_FORMAT GL_RGBA
#endif
//////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// GlyphTexture
//
GlyphTexture::GlyphTexture():
_usedY(0),
_partUsedX(0),
@ -154,22 +157,19 @@ void GlyphTexture::addGlyph(Glyph* glyph, int posX, int posY)
_glyphs.push_back(glyph);
// set up the details of where to place glyph's image in the texture.
glyph->setTexture(this);
glyph->setTexturePosition(posX,posY);
osg::ref_ptr<Glyph::TextureInfo> info = new Glyph::TextureInfo(
this,
posX, posY,
osg::Vec2( static_cast<float>(posX)/static_cast<float>(getTextureWidth()), static_cast<float>(posY)/static_cast<float>(getTextureHeight()) ), // minTexCoord
osg::Vec2( static_cast<float>(posX+glyph->s())/static_cast<float>(getTextureWidth()), static_cast<float>(posY+glyph->t())/static_cast<float>(getTextureHeight()) ), // maxTexCoord
float(getTexelMargin(glyph))); // margin
glyph->setMinTexCoord( osg::Vec2( static_cast<float>(posX)/static_cast<float>(getTextureWidth()),
static_cast<float>(posY)/static_cast<float>(getTextureHeight()) ) );
glyph->setMaxTexCoord( osg::Vec2( static_cast<float>(posX+glyph->s())/static_cast<float>(getTextureWidth()),
static_cast<float>(posY+glyph->t())/static_cast<float>(getTextureHeight()) ) );
glyph->setTextureInfo(_shaderTechnique, info.get());
glyph->setTexelMargin(float(getTexelMargin(glyph)));
copyGlyphImage(glyph);
copyGlyphImage(glyph, info);
}
void GlyphTexture::copyGlyphImage(Glyph* glyph)
void GlyphTexture::copyGlyphImage(Glyph* glyph, Glyph::TextureInfo* info)
{
_image->dirty();
@ -179,7 +179,7 @@ void GlyphTexture::copyGlyphImage(Glyph* glyph)
// make sure the glyph image settings and the target image are consisent before copying.
glyph->setPixelFormat(_image->getPixelFormat());
glyph->setInternalTextureFormat(_image->getPixelFormat());
_image->copySubImage(glyph->getTexturePositionX(), glyph->getTexturePositionY(), 0, glyph);
_image->copySubImage(info->texturePositionX, info->texturePositionY, 0, glyph);
return;
}
@ -191,7 +191,7 @@ void GlyphTexture::copyGlyphImage(Glyph* glyph)
int dest_columns = _image->s();
int dest_rows = _image->t();
unsigned char* dest_data = _image->data(glyph->getTexturePositionX(),glyph->getTexturePositionY());
unsigned char* dest_data = _image->data(info->texturePositionX, info->texturePositionY);
int search_distance = getEffectMargin(glyph);
@ -204,11 +204,11 @@ void GlyphTexture::copyGlyphImage(Glyph* glyph)
float max_distance = sqrtf(float(search_distance)*float(search_distance)*2.0);
if ((left+glyph->getTexturePositionX())<0) left = -glyph->getTexturePositionX();
if ((right+glyph->getTexturePositionX())>=dest_columns) right = dest_columns-glyph->getTexturePositionX()-1;
if ((left+info->texturePositionX)<0) left = -info->texturePositionX;
if ((right+info->texturePositionX)>=dest_columns) right = dest_columns-info->texturePositionX-1;
if ((lower+glyph->getTexturePositionY())<0) lower = -glyph->getTexturePositionY();
if ((upper+glyph->getTexturePositionY())>=dest_rows) upper = dest_rows-glyph->getTexturePositionY()-1;
if ((lower+info->texturePositionY)<0) lower = -info->texturePositionY;
if ((upper+info->texturePositionY)>=dest_rows) upper = dest_rows-info->texturePositionY-1;
int num_components = osg::Image::computeNumComponents(_image->getPixelFormat());
@ -427,13 +427,18 @@ osg::Image* GlyphTexture::createImage()
++itr)
{
Glyph* glyph = itr->get();
copyGlyphImage(glyph);
// copyGlyphImage(glyph); // TODO!!!!!
OSG_NOTICE<<"GlyphTexture::createImage() need to implement copy"<<std::endl;
}
}
return _image.get();
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Glyph
//
// all the methods in Font::Glyph have been made non inline because VisualStudio6.0 is STUPID, STUPID, STUPID PILE OF JUNK.
Glyph::Glyph(Font* font, unsigned int glyphCode):
_font(font),
@ -443,12 +448,7 @@ Glyph::Glyph(Font* font, unsigned int glyphCode):
_horizontalBearing(0.0f,0.f),
_horizontalAdvance(0.f),
_verticalBearing(0.0f,0.f),
_verticalAdvance(0.f),
_texture(0),
_texturePosX(0),
_texturePosY(0),
_minTexCoord(0.0f,0.0f),
_maxTexCoord(0.0f,0.0f)
_verticalAdvance(0.f)
{
setThreadSafeRefUnref(true);
}
@ -469,20 +469,20 @@ const osg::Vec2& Glyph::getVerticalBearing() const { return _verticalBearing; }
void Glyph::setVerticalAdvance(float advance) { _verticalAdvance=advance; }
float Glyph::getVerticalAdvance() const { return _verticalAdvance; }
void Glyph::setTexture(GlyphTexture* texture) { _texture = texture; }
GlyphTexture* Glyph::getTexture() { return _texture; }
const GlyphTexture* Glyph::getTexture() const { return _texture; }
void Glyph::setTexturePosition(int posX,int posY) { _texturePosX = posX; _texturePosY = posY; }
int Glyph::getTexturePositionX() const { return _texturePosX; }
int Glyph::getTexturePositionY() const { return _texturePosY; }
void Glyph::setMinTexCoord(const osg::Vec2& coord) { _minTexCoord=coord; }
const osg::Vec2& Glyph::getMinTexCoord() const { return _minTexCoord; }
void Glyph::setMaxTexCoord(const osg::Vec2& coord) { _maxTexCoord=coord; }
const osg::Vec2& Glyph::getMaxTexCoord() const { return _maxTexCoord; }
Glyph::TextureInfo* Glyph::getOrCreateTextureInfo(ShaderTechnique technique)
{
if (technique>=_textureInfoList.size()) _textureInfoList.resize(technique+1);
if (!_textureInfoList[technique])
{
_font->assignGlyphToGlyphTexture(this, technique);
}
return _textureInfoList[technique].get();
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Glyph3D
//
Glyph3D::Glyph3D(Font* font, unsigned int glyphCode):
osg::Referenced(true),
_font(font),

View File

@ -47,6 +47,16 @@ Text::Text():
{
_supportsVertexBufferObjects = true;
char* ptr = 0;
if ((ptr = getenv("OSG_SDF_TEXT")) != 0)
{
_shaderTechnique = ALL_FEATURES;
}
else if ((ptr = getenv("OSG_GREYSCALE_TEXT")) != 0)
{
_shaderTechnique = GREYSCALE;
}
assignStateSet();
}
@ -129,7 +139,7 @@ osg::StateSet* Text::createStateSet()
}
}
if (activeFont->getShaderTechnique()!=GREYSCALE)
if (_shaderTechnique!=GREYSCALE)
{
ss<<std::fixed<<std::setprecision(1);
@ -185,12 +195,12 @@ osg::StateSet* Text::createStateSet()
stateset->setMode(GL_BLEND, osg::StateAttribute::ON);
OSG_NOTICE<<"Text::createStateSet() activeFont->getShaderTechnique()="<<activeFont->getShaderTechnique()<<std::endl;
OSG_NOTICE<<"Text::createStateSet() ShaderTechnique="<<_shaderTechnique<<std::endl;
#if defined(OSG_GL_FIXED_FUNCTION_AVAILABLE)
osg::DisplaySettings::ShaderHint shaderHint = osg::DisplaySettings::instance()->getShaderHint();
if (activeFont->getShaderTechnique()==GREYSCALE && shaderHint==osg::DisplaySettings::SHADER_NONE)
if (_shaderTechnique==GREYSCALE && shaderHint==osg::DisplaySettings::SHADER_NONE)
{
OSG_NOTICE<<"Font::Font() Fixed function pipeline"<<std::endl;
@ -212,7 +222,7 @@ osg::StateSet* Text::createStateSet()
program->addShader(osgDB::readRefShaderFileWithFallback(osg::Shader::VERTEX, "shaders/text.vert", text_vert));
}
if (activeFont->getShaderTechnique()==GREYSCALE)
if (_shaderTechnique==GREYSCALE)
{
OSG_NOTICE<<"Using shaders/text_greyscale.frag"<<std::endl;
@ -374,7 +384,9 @@ String::iterator Text::computeLastCharacterOnLine(osg::Vec2& cursor, String::ite
void Text::addGlyphQuad(Glyph* glyph, const osg::Vec2& minc, const osg::Vec2& maxc, const osg::Vec2& mintc, const osg::Vec2& maxtc)
{
// set up the coords of the quad
GlyphQuads& glyphquad = _textureGlyphQuadMap[glyph->getTexture()];
const Glyph::TextureInfo* info = glyph->getOrCreateTextureInfo(_shaderTechnique);
GlyphTexture* glyphTexture = info ? info->texture : 0;
GlyphQuads& glyphquad = _textureGlyphQuadMap[glyphTexture];
glyphquad._glyphs.push_back(glyph);
@ -625,46 +637,54 @@ void Text::computeGlyphRepresentation()
local.x() += bearing.x() * wr;
local.y() += bearing.y() * hr;
// Adjust coordinates and texture coordinates to avoid
// clipping the edges of antialiased characters.
osg::Vec2 mintc = glyph->getMinTexCoord();
osg::Vec2 maxtc = glyph->getMaxTexCoord();
osg::Vec2 vDiff = maxtc - mintc;
float texelMargin = glyph->getTexelMargin();
float fHorizTCMargin = texelMargin / glyph->getTexture()->getTextureWidth();
float fVertTCMargin = texelMargin / glyph->getTexture()->getTextureHeight();
float fHorizQuadMargin = vDiff.x() == 0.0f ? 0.0f : width * fHorizTCMargin / vDiff.x();
float fVertQuadMargin = vDiff.y() == 0.0f ? 0.0f : height * fVertTCMargin / vDiff.y();
mintc.x() -= fHorizTCMargin;
mintc.y() -= fVertTCMargin;
maxtc.x() += fHorizTCMargin;
maxtc.y() += fVertTCMargin;
osg::Vec2 minc = local+osg::Vec2(0.0f-fHorizQuadMargin,0.0f-fVertQuadMargin);
osg::Vec2 maxc = local+osg::Vec2(width+fHorizQuadMargin,height+fVertQuadMargin);
addGlyphQuad(glyph, minc, maxc, mintc, maxtc);
// move the cursor onto the next character.
// also expand bounding box
switch(_layout)
const Glyph::TextureInfo* info = glyph->getOrCreateTextureInfo(_shaderTechnique);
if (info)
{
case LEFT_TO_RIGHT:
cursor.x() += glyph->getHorizontalAdvance() * wr;
_textBB.expandBy(osg::Vec3(minc.x(), minc.y(), 0.0f)); //lower left corner
_textBB.expandBy(osg::Vec3(maxc.x(), maxc.y(), 0.0f)); //upper right corner
break;
case VERTICAL:
cursor.y() -= glyph->getVerticalAdvance() * hr;
_textBB.expandBy(osg::Vec3(minc.x(),maxc.y(),0.0f)); //upper left corner
_textBB.expandBy(osg::Vec3(maxc.x(),minc.y(),0.0f)); //lower right corner
break;
case RIGHT_TO_LEFT:
_textBB.expandBy(osg::Vec3(maxc.x(),minc.y(),0.0f)); //lower right corner
_textBB.expandBy(osg::Vec3(minc.x(),maxc.y(),0.0f)); //upper left corner
break;
// Adjust coordinates and texture coordinates to avoid
// clipping the edges of antialiased characters.
osg::Vec2 mintc = info->minTexCoord;
osg::Vec2 maxtc = info->maxTexCoord;
osg::Vec2 vDiff = maxtc - mintc;
float texelMargin = info->texelMargin;
float fHorizTCMargin = texelMargin / info->texture->getTextureWidth();
float fVertTCMargin = texelMargin / info->texture->getTextureHeight();
float fHorizQuadMargin = vDiff.x() == 0.0f ? 0.0f : width * fHorizTCMargin / vDiff.x();
float fVertQuadMargin = vDiff.y() == 0.0f ? 0.0f : height * fVertTCMargin / vDiff.y();
mintc.x() -= fHorizTCMargin;
mintc.y() -= fVertTCMargin;
maxtc.x() += fHorizTCMargin;
maxtc.y() += fVertTCMargin;
osg::Vec2 minc = local+osg::Vec2(0.0f-fHorizQuadMargin,0.0f-fVertQuadMargin);
osg::Vec2 maxc = local+osg::Vec2(width+fHorizQuadMargin,height+fVertQuadMargin);
addGlyphQuad(glyph, minc, maxc, mintc, maxtc);
// move the cursor onto the next character.
// also expand bounding box
switch(_layout)
{
case LEFT_TO_RIGHT:
cursor.x() += glyph->getHorizontalAdvance() * wr;
_textBB.expandBy(osg::Vec3(minc.x(), minc.y(), 0.0f)); //lower left corner
_textBB.expandBy(osg::Vec3(maxc.x(), maxc.y(), 0.0f)); //upper right corner
break;
case VERTICAL:
cursor.y() -= glyph->getVerticalAdvance() * hr;
_textBB.expandBy(osg::Vec3(minc.x(),maxc.y(),0.0f)); //upper left corner
_textBB.expandBy(osg::Vec3(maxc.x(),minc.y(),0.0f)); //lower right corner
break;
case RIGHT_TO_LEFT:
_textBB.expandBy(osg::Vec3(maxc.x(),minc.y(),0.0f)); //lower right corner
_textBB.expandBy(osg::Vec3(minc.x(),maxc.y(),0.0f)); //upper left corner
break;
}
}
else
{
OSG_NOTICE<<"No TextureInfo for "<<charcode<<std::endl;
}
previous_charcode = charcode;