Merge pull request #372 from openscenegraph/text_improvements

Text improvements, introducing implementation of Signed Distance Function texture generation and new shaders for outlines and shadows replacing old multi-pass approach
This commit is contained in:
OpenSceneGraph git repository
2017-10-26 14:26:01 +01:00
committed by GitHub
31 changed files with 1642 additions and 791 deletions

View File

@@ -118,7 +118,8 @@ void DisplaySettings::setDisplaySettings(const DisplaySettings& vs)
_swapMethod = vs._swapMethod;
_vertexBufferHint = vs._vertexBufferHint;
_shaderHint = vs._shaderHint;
setShaderHint(_shaderHint);
_keystoneHint = vs._keystoneHint;
_keystoneFileNames = vs._keystoneFileNames;
@@ -250,23 +251,17 @@ void DisplaySettings::setDefaults()
// _vertexBufferHint = VERTEX_ARRAY_OBJECT;
#if defined(OSG_GLES3_AVAILABLE)
_shaderHint = SHADER_GLES3;
OSG_INFO<<"DisplaySettings::SHADER_GLES3"<<std::endl;
setShaderHint(SHADER_GLES3);
#elif defined(OSG_GLES2_AVAILABLE)
_shaderHint = SHADER_GLES2;
OSG_INFO<<"DisplaySettings::SHADER_GLES2"<<std::endl;
setShaderHint(SHADER_GLES2);
#elif defined(OSG_GL3_AVAILABLE)
_shaderHint = SHADER_GL3;
OSG_INFO<<"DisplaySettings::SHADER_GL3"<<std::endl;
setShaderHint(SHADER_GL3);
#elif defined(OSG_GL_VERTEX_ARRAY_FUNCS_AVAILABLE)
OSG_INFO<<"DisplaySettings::SHADER_NONE"<<std::endl;
_shaderHint = SHADER_NONE;
setShaderHint(SHADER_NONE);
#else
OSG_INFO<<"DisplaySettings::SHADER_GL2"<<std::endl;
_shaderHint = SHADER_GL2;
setShaderHint(SHADER_GL2);
#endif
_keystoneHint = false;
_OSXMenubarBehavior = MENUBAR_AUTO_HIDE;
@@ -390,6 +385,9 @@ static ApplicationUsageProxy DisplaySetting_e31(ApplicationUsage::ENVIRONMENTAL_
static ApplicationUsageProxy DisplaySetting_e32(ApplicationUsage::ENVIRONMENTAL_VARIABLE,
"OSG_VERTEX_BUFFER_HINT <value>",
"Set the hint to what backend osg::Geometry implementation to use. NO_PREFERENCE | VERTEX_BUFFER_OBJECT | VERTEX_ARRAY_OBJECT");
static ApplicationUsageProxy DisplaySetting_e33(ApplicationUsage::ENVIRONMENTAL_VARIABLE,
"OSG_TEXT_SHADER_TECHNIQUE <value>",
"Set the defafult osgText::ShaderTechnique. ALL_FEATURES | ALL | GREYSCALE | SIGNED_DISTANCE_FIELD | SDF | NO_TEXT_SHADER | NONE");
void DisplaySettings::readEnvironmentalVariables()
{
@@ -725,26 +723,30 @@ void DisplaySettings::readEnvironmentalVariables()
{
if (strcmp(ptr,"GL2")==0)
{
_shaderHint = SHADER_GL2;
setShaderHint(SHADER_GL2);
}
else if (strcmp(ptr,"GL3")==0)
{
_shaderHint = SHADER_GL3;
setShaderHint(SHADER_GL3);
}
else if (strcmp(ptr,"GLES2")==0)
{
_shaderHint = SHADER_GLES2;
setShaderHint(SHADER_GLES2);
}
else if (strcmp(ptr,"GLES3")==0)
{
_shaderHint = SHADER_GLES3;
setShaderHint(SHADER_GLES3);
}
else if (strcmp(ptr,"NONE")==0)
{
_shaderHint = SHADER_NONE;
setShaderHint(SHADER_NONE);
}
}
if ((ptr = getenv("OSG_TEXT_SHADER_TECHNIQUE")) != 0)
{
setTextShaderTechnique(ptr);
}
if( (ptr = getenv("OSG_KEYSTONE")) != 0)
{
@@ -1098,3 +1100,84 @@ osg::Matrixd DisplaySettings::computeRightEyeViewImplementation(const osg::Matri
0.0,0.0,1.0,0.0,
-es,0.0,0.0,1.0);
}
void DisplaySettings::setShaderHint(ShaderHint hint, bool setShaderValues)
{
_shaderHint = hint;
if (setShaderValues)
{
switch(_shaderHint)
{
case(SHADER_GLES3) :
setValue("OSG_GLSL_VERSION", "#version 300 es");
setValue("OSG_PRECISION_FLOAT", "precision highp float;");
setValue("OSG_VARYING_IN", "in");
setValue("OSG_VARYING_OUT", "out");
OSG_NOTICE<<"DisplaySettings::SHADER_GLES3"<<std::endl;
break;
case(SHADER_GLES2) :
setValue("OSG_GLSL_VERSION", "");
setValue("OSG_PRECISION_FLOAT", "precision highp float;");
setValue("OSG_VARYING_IN", "varying");
setValue("OSG_VARYING_OUT", "varying");
OSG_NOTICE<<"DisplaySettings::SHADER_GLES2"<<std::endl;
break;
case(SHADER_GL3) :
setValue("OSG_GLSL_VERSION", "#version 330");
setValue("OSG_PRECISION_FLOAT", "");
setValue("OSG_VARYING_IN", "in");
setValue("OSG_VARYING_OUT", "out");
OSG_NOTICE<<"DisplaySettings::SHADER_GL3"<<std::endl;
break;
case(SHADER_GL2) :
setValue("OSG_GLSL_VERSION", "");
setValue("OSG_PRECISION_FLOAT", "");
setValue("OSG_VARYING_IN", "varying");
setValue("OSG_VARYING_OUT", "varying");
OSG_NOTICE<<"DisplaySettings::SHADER_GL2"<<std::endl;
break;
case(SHADER_NONE) :
setValue("OSG_GLSL_VERSION", "");
setValue("OSG_PRECISION_FLOAT", "");
setValue("OSG_VARYING_IN", "varying");
setValue("OSG_VARYING_OUT", "varying");
OSG_NOTICE<<"DisplaySettings::NONE"<<std::endl;
break;
}
}
}
void DisplaySettings::setValue(const std::string& name, const std::string& value)
{
OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_valueMapMutex);
_valueMap[name] = value;
}
bool DisplaySettings::getValue(const std::string& name, std::string& value, bool use_getenv_fallback) const
{
OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_valueMapMutex);
ValueMap::iterator itr = _valueMap.find(name);
if (itr!=_valueMap.end())
{
value = itr->second;
OSG_INFO<<"DisplaySettings::getValue("<<name<<") found existing value = ["<<value<<"]"<<std::endl;
return true;
}
if (!use_getenv_fallback) return false;
const char* str = getenv(name.c_str());
if (str)
{
OSG_INFO<<"DisplaySettings::getValue("<<name<<") found getenv value = ["<<value<<"]"<<std::endl;
_valueMap[name] = value = str;
return true;
}
else
{
return false;
}
}

View File

@@ -631,27 +631,27 @@ void Shader::PerContextShader::compileShader(osg::State& state)
#endif
std::string source = _shader->getShaderSource();
if (_shader->getType()==osg::Shader::VERTEX && (state.getUseVertexAttributeAliasing() || state.getUseModelViewAndProjectionUniforms()))
// if (_shader->getType()==osg::Shader::VERTEX && (state.getUseVertexAttributeAliasing() || state.getUseModelViewAndProjectionUniforms()))
{
state.convertVertexShaderSourceToOsgBuiltIns(source);
}
if (osg::getNotifyLevel()>=osg::INFO)
{
std::string sourceWithLineNumbers = insertLineNumbers(source);
OSG_INFO << "\nCompiling " << _shader->getTypename()
<< " source:\n" << sourceWithLineNumbers << std::endl;
}
GLint compiled = GL_FALSE;
// OSG_NOTICE<<"Compiling PerContextShader "<<this<<" ShaderDefine="<<getDefineString()<<std::endl;
// OSG_NOTICE<<"Compiling PerContextShader "<<this<<" DefineString="<<getDefineString()<<std::endl;
if (_defineStr.empty())
{
const GLchar* sourceText = reinterpret_cast<const GLchar*>(source.c_str());
_extensions->glShaderSource( _glShaderHandle, 1, &sourceText, NULL );
if (osg::getNotifyLevel()>=osg::INFO)
{
std::string sourceWithLineNumbers = insertLineNumbers(source);
OSG_INFO << "\nCompiling A :" << _shader->getTypename()
<< " source:\n" << sourceWithLineNumbers << std::endl;
}
}
else
{
@@ -690,21 +690,40 @@ void Shader::PerContextShader::compileShader(osg::State& state)
if (!versionLine.empty())
{
// OSG_NOTICE<<"Shader::PerContextShader::compileShader() : Found #version, lineNum = "<<lineNum<<" ["<<versionLine<<"] new source = ["<<source<<"]"<<std::endl;
const GLchar* sourceText[3];
//OSG_NOTICE<<"glShaderSource() ["<<versionLine<<"] "<<std::endl<<"["<<_defineStr<<"], ["<<sourceText<<"]"<<std::endl;
sourceText[0] = reinterpret_cast<const GLchar*>(versionLine.c_str());
sourceText[1] = reinterpret_cast<const GLchar*>(_defineStr.c_str());
sourceText[2] = reinterpret_cast<const GLchar*>(source.c_str());
_extensions->glShaderSource( _glShaderHandle, 3, sourceText, NULL );
if (osg::getNotifyLevel()>=osg::INFO)
{
std::string sourceWithLineNumbers = insertLineNumbers(versionLine+_defineStr+source);
OSG_INFO << "\nCompiling B: " << _shader->getTypename()
<< " source:\n" << sourceWithLineNumbers << std::endl;
}
// OSG_NOTICE<<" Version Line : ["<<std::endl<<versionLine<<"]"<<std::endl;
// OSG_NOTICE<<" DefineStr : ["<<std::endl<<_defineStr<<"]"<<std::endl;
// OSG_NOTICE<<" Source : ["<<std::endl<<source<<"]"<<std::endl;
}
else
{
const GLchar* sourceText[2];
//OSG_NOTICE<<"glShaderSource() ["<<_defineStr<<"], ["<<sourceText<<"]"<<std::endl;
sourceText[0] = reinterpret_cast<const GLchar*>(_defineStr.c_str());
sourceText[1] = reinterpret_cast<const GLchar*>(source.c_str());
_extensions->glShaderSource( _glShaderHandle, 2, sourceText, NULL );
if (osg::getNotifyLevel()>=osg::INFO)
{
std::string sourceWithLineNumbers = insertLineNumbers(_defineStr+source);
OSG_INFO << "\nCompiling C: " << _shader->getTypename()
<< " source:\n" << sourceWithLineNumbers << std::endl;
}
// OSG_NOTICE<<" DefineStr : ["<<std::endl<<_defineStr<<"]"<<std::endl;
// OSG_NOTICE<<" Source : ["<<std::endl<<source<<"]"<<std::endl;
}
}
_extensions->glCompileShader( _glShaderHandle );

View File

@@ -1216,13 +1216,69 @@ namespace State_Utils
source.insert(declPos, qualifier + declarationPrefix + newStr + std::string(";\n"));
}
}
void replaceVar(const osg::State& state, std::string& str, std::string::size_type start_pos, std::string::size_type num_chars)
{
std::string var_str(str.substr(start_pos+1, num_chars-1));
std::string value;
if (state.getActiveDisplaySettings()->getValue(var_str, value))
{
str.replace(start_pos, num_chars, value);
}
else
{
str.erase(start_pos, num_chars);
}
}
void substitudeEnvVars(const osg::State& state, std::string& str)
{
std::string::size_type pos = 0;
while (pos<str.size() && ((pos=str.find_first_of("$'\"", pos)) != std::string::npos))
{
if (pos==str.size())
{
break;
}
if (str[pos]=='"' || str[pos]=='\'')
{
std::string::size_type start_quote = pos;
++pos;
pos = str.find(str[start_quote], pos);
}
else
{
std::string::size_type start_var = pos;
++pos;
pos = str.find_first_not_of("ABCDEFGHIJKLMNOPQRTSUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_", pos);
std::string var_str;
if (pos != std::string::npos)
{
replaceVar(state, str, start_var, pos-start_var);
pos = start_var;
}
else
{
replaceVar(state, str, start_var, str.size()-start_var);
pos = start_var;
}
}
}
}
}
bool State::convertVertexShaderSourceToOsgBuiltIns(std::string& source) const
{
OSG_INFO<<"State::convertShaderSourceToOsgBuiltIns()"<<std::endl;
OSG_DEBUG<<"State::convertShaderSourceToOsgBuiltIns()"<<std::endl;
OSG_DEBUG<<"++Before Converted source "<<std::endl<<source<<std::endl<<"++++++++"<<std::endl;
State_Utils::substitudeEnvVars(*this, source);
OSG_INFO<<"++Before Converted source "<<std::endl<<source<<std::endl<<"++++++++"<<std::endl;
std::string attributeQualifier("attribute ");
@@ -1279,7 +1335,7 @@ bool State::convertVertexShaderSourceToOsgBuiltIns(std::string& source) const
}
}
OSG_INFO<<"-------- Converted source "<<std::endl<<source<<std::endl<<"----------------"<<std::endl;
OSG_DEBUG<<"-------- Converted source "<<std::endl<<source<<std::endl<<"----------------"<<std::endl;
return true;
}

View File

@@ -278,19 +278,6 @@ void FreeTypeFont::setFontResolution(const osgText::FontResolution& fontSize)
int width = fontSize.first;
int height = fontSize.second;
int maxAxis = std::max(width, height);
int margin = _facade->getGlyphImageMargin() + (int)((float)maxAxis * _facade->getGlyphImageMarginRatio());
if ((unsigned int)(width+2*margin) > _facade->getTextureWidthHint() ||
(unsigned int)(width+2*margin) > _facade->getTextureHeightHint())
{
OSG_WARN<<"Warning: FreeTypeFont::setSize("<<width<<","<<height<<") sizes too large,"<<std::endl;
width = _facade->getTextureWidthHint()-2*margin;
height = _facade->getTextureHeightHint()-2*margin;
OSG_WARN<<" sizes capped ("<<width<<","<<height<<") to fit int current glyph texture size."<<std::endl;
}
FT_Error error = FT_Set_Pixel_Sizes( _face, /* handle to face object */
width, /* pixel_width */
@@ -351,6 +338,8 @@ osgText::Glyph* FreeTypeFont::getGlyph(const osgText::FontResolution& fontRes, u
osg::ref_ptr<osgText::Glyph> glyph = new osgText::Glyph(_facade, charcode);
glyph->setFontResolution(fontRes);
unsigned int dataSize = width*height;
unsigned char* data = new unsigned char[dataSize];
@@ -359,14 +348,12 @@ osgText::Glyph* FreeTypeFont::getGlyph(const osgText::FontResolution& fontRes, u
for(unsigned char* p=data;p<data+dataSize;) { *p++ = 0; }
glyph->setImage(width,height,1,
OSGTEXT_GLYPH_INTERNALFORMAT,
OSGTEXT_GLYPH_FORMAT, GL_UNSIGNED_BYTE,
GL_ALPHA,
GL_ALPHA, GL_UNSIGNED_BYTE,
data,
osg::Image::USE_NEW_DELETE,
1);
glyph->setInternalTextureFormat(OSGTEXT_GLYPH_INTERNALFORMAT);
// copy image across to osgText::Glyph image.
switch(glyphslot->bitmap.pixel_mode)
{

View File

@@ -90,7 +90,7 @@ void Text::write(DataOutputStream* out){
out->writeFloat(getBackdropVerticalOffset());
out->writeVec4(getBackdropColor());
out->writeUInt(getBackdropImplementation());
out->writeUInt(4); // old DELAYED_DEPTH_WRITES
out->writeUInt(getColorGradientMode());
out->writeVec4(getColorGradientTopLeft());
@@ -213,7 +213,7 @@ void Text::read(DataInputStream* in){
setBackdropOffset(horizontalOffset,verticalOffset);
setBackdropColor(in->readVec4());
setBackdropImplementation((osgText::Text::BackdropImplementation) in->readUInt());
in->readUInt(); // read old BackdropImplementation value, no longer used
setColorGradientMode((osgText::Text::ColorGradientMode) in->readUInt());
osg::Vec4 colorGradientTopLeft,colorGradientBottomLeft,colorGradientBottomRight,colorGradientTopRight;

View File

@@ -24,8 +24,11 @@ using namespace osgText;
DefaultFont::DefaultFont()
{
_fontSize = FontResolution(8,12);
_minFilterHint = osg::Texture::LINEAR_MIPMAP_LINEAR;
_magFilterHint = osg::Texture::NEAREST;
_magFilterHint = osg::Texture::LINEAR;
constructGlyphs();
}
@@ -198,14 +201,12 @@ void DefaultFont::constructGlyphs()
for(unsigned char* p=data;p<data+dataSize;) { *p++ = 0; }
glyph->setImage(sourceWidth,sourceHeight,1,
OSGTEXT_GLYPH_INTERNALFORMAT,
OSGTEXT_GLYPH_FORMAT, GL_UNSIGNED_BYTE,
GL_ALPHA,
GL_ALPHA, GL_UNSIGNED_BYTE,
data,
osg::Image::USE_NEW_DELETE,
1);
glyph->setInternalTextureFormat(OSGTEXT_GLYPH_INTERNALFORMAT);
// now populate data array by converting bitmap into a luminance_alpha map.
unsigned char* ptr = rasters[i-32];
unsigned char value_on = 255;
@@ -233,6 +234,8 @@ void DefaultFont::constructGlyphs()
glyph->setVerticalBearing(osg::Vec2(0.5f,1.0f)); // top middle.
glyph->setVerticalAdvance(sourceHeight*coord_scale);
glyph->setFontResolution(fontRes);
addGlyph(fontRes,i,glyph.get());
}
}

View File

@@ -32,81 +32,6 @@
using namespace osgText;
using namespace std;
#if (!defined(OSG_GLES2_AVAILABLE) && !defined(OSG_GLES3_AVAILABLE))
#define GLSL_VERSION_STR "330 core"
#define GLYPH_CMP "r"
#else
#define GLSL_VERSION_STR "300 es"
#define GLYPH_CMP "a"
#endif
static const char* gl3_TextVertexShader = {
"#version " GLSL_VERSION_STR "\n"
"// gl3_TextVertexShader\n"
"#ifdef GL_ES\n"
" precision highp float;\n"
"#endif\n"
"in vec4 osg_Vertex;\n"
"in vec4 osg_Color;\n"
"in vec4 osg_MultiTexCoord0;\n"
"uniform mat4 osg_ModelViewProjectionMatrix;\n"
"out vec2 texCoord;\n"
"out vec4 vertexColor;\n"
"void main(void)\n"
"{\n"
" gl_Position = osg_ModelViewProjectionMatrix * osg_Vertex;\n"
" texCoord = osg_MultiTexCoord0.xy;\n"
" vertexColor = osg_Color; \n"
"}\n"
};
static const char* gl3_TextFragmentShader = {
"#version " GLSL_VERSION_STR "\n"
"// gl3_TextFragmentShader\n"
"#ifdef GL_ES\n"
" precision highp float;\n"
"#endif\n"
"uniform sampler2D glyphTexture;\n"
"in vec2 texCoord;\n"
"in vec4 vertexColor;\n"
"out vec4 color;\n"
"void main(void)\n"
"{\n"
" if (texCoord.x>=0.0) color = vertexColor * vec4(1.0, 1.0, 1.0, texture(glyphTexture, texCoord)." GLYPH_CMP ");\n"
" else color = vertexColor;\n"
"}\n"
};
static const char* gl2_TextVertexShader = {
"// gl2_TextVertexShader\n"
"#ifdef GL_ES\n"
" precision highp float;\n"
"#endif\n"
"varying vec2 texCoord;\n"
"varying vec4 vertexColor;\n"
"void main(void)\n"
"{\n"
" gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;\n"
" texCoord = gl_MultiTexCoord0.xy;\n"
" vertexColor = gl_Color; \n"
"}\n"
};
static const char* gl2_TextFragmentShader = {
"// gl2_TextFragmentShader\n"
"#ifdef GL_ES\n"
" precision highp float;\n"
"#endif\n"
"uniform sampler2D glyphTexture;\n"
"varying vec2 texCoord;\n"
"varying vec4 vertexColor;\n"
"void main(void)\n"
"{\n"
" if (texCoord.x>=0.0) gl_FragColor = vertexColor * vec4(1.0, 1.0, 1.0, texture2D(glyphTexture, texCoord).a);\n"
" else gl_FragColor = vertexColor;\n"
"}\n"
};
osg::ref_ptr<Font>& Font::getDefaultFont()
{
static OpenThreads::Mutex s_DefaultFontMutex;
@@ -299,67 +224,24 @@ osg::ref_ptr<Font> osgText::readRefFontStream(std::istream& stream, const osgDB:
Font::Font(FontImplementation* implementation):
osg::Object(true),
_margin(1),
_marginRatio(0.02),
_textureWidthHint(1024),
_textureHeightHint(1024),
_minFilterHint(osg::Texture::LINEAR_MIPMAP_LINEAR),
_magFilterHint(osg::Texture::LINEAR),
_maxAnisotropy(16),
_depth(1),
_numCurveSamples(10)
{
setImplementation(implementation);
_texenv = new osg::TexEnv;
_stateset = new osg::StateSet;
_stateset->setRenderingHint(osg::StateSet::TRANSPARENT_BIN);
_stateset->setMode(GL_LIGHTING, osg::StateAttribute::OFF);
_stateset->setMode(GL_BLEND, osg::StateAttribute::ON);
#if defined(OSG_GL_FIXED_FUNCTION_AVAILABLE)
OSG_INFO<<"Font::Font() Fixed function pipeline"<<std::endl;
_stateset->setTextureMode(0, GL_TEXTURE_2D, osg::StateAttribute::ON);
#endif
osg::DisplaySettings::ShaderHint shaderHint = osg::DisplaySettings::instance()->getShaderHint();
if (shaderHint==osg::DisplaySettings::SHADER_GL3 || shaderHint==osg::DisplaySettings::SHADER_GLES3)
{
OSG_INFO<<"Font::Font() Setting up GL3 compatible shaders"<<std::endl;
osg::ref_ptr<osg::Program> program = new osg::Program;
program->addShader(new osg::Shader(osg::Shader::VERTEX, gl3_TextVertexShader));
program->addShader(new osg::Shader(osg::Shader::FRAGMENT, gl3_TextFragmentShader));
_stateset->setAttributeAndModes(program.get());
_stateset->addUniform(new osg::Uniform("glyphTexture", 0));
}
else if (shaderHint==osg::DisplaySettings::SHADER_GL2 || shaderHint==osg::DisplaySettings::SHADER_GLES2)
{
OSG_INFO<<"Font::Font() Setting up GL2 compatible shaders"<<std::endl;
osg::ref_ptr<osg::Program> program = new osg::Program;
program->addShader(new osg::Shader(osg::Shader::VERTEX, gl2_TextVertexShader));
program->addShader(new osg::Shader(osg::Shader::FRAGMENT, gl2_TextFragmentShader));
_stateset->setAttributeAndModes(program.get());
_stateset->addUniform(new osg::Uniform("glyphTexture", 0));
}
char *ptr;
if( (ptr = getenv("OSG_MAX_TEXTURE_SIZE")) != 0)
if ((ptr = getenv("OSG_MAX_TEXTURE_SIZE")) != 0)
{
unsigned int osg_max_size = atoi(ptr);
if (osg_max_size<_textureWidthHint) _textureWidthHint = osg_max_size;
if (osg_max_size<_textureHeightHint) _textureHeightHint = osg_max_size;
}
}
Font::~Font()
@@ -390,26 +272,6 @@ std::string Font::getFileName() const
return std::string();
}
void Font::setGlyphImageMargin(unsigned int margin)
{
_margin = margin;
}
unsigned int Font::getGlyphImageMargin() const
{
return _margin;
}
void Font::setGlyphImageMarginRatio(float ratio)
{
_marginRatio = ratio;
}
float Font::getGlyphImageMarginRatio() const
{
return _marginRatio;
}
void Font::setTextureSizeHint(unsigned int width,unsigned int height)
{
_textureWidthHint = width;
@@ -518,9 +380,6 @@ void Font::setThreadSafeRefUnref(bool threadSafe)
{
osg::Object::setThreadSafeRefUnref(threadSafe);
if (_texenv.valid()) _texenv->setThreadSafeRefUnref(threadSafe);
if (_stateset.valid()) _stateset->setThreadSafeRefUnref(threadSafe);
for(GlyphTextureList::const_iterator itr=_glyphTextureList.begin();
itr!=_glyphTextureList.end();
++itr)
@@ -531,7 +390,12 @@ void Font::setThreadSafeRefUnref(bool threadSafe)
void Font::resizeGLObjectBuffers(unsigned int maxSize)
{
if (_stateset.valid()) _stateset->resizeGLObjectBuffers(maxSize);
for(StateSets::iterator itr = _statesets.begin();
itr != _statesets.end();
++itr)
{
(*itr)->resizeGLObjectBuffers(maxSize);
}
for(GlyphTextureList::const_iterator itr=_glyphTextureList.begin();
itr!=_glyphTextureList.end();
@@ -543,7 +407,12 @@ void Font::resizeGLObjectBuffers(unsigned int maxSize)
void Font::releaseGLObjects(osg::State* state) const
{
if (_stateset.valid()) _stateset->releaseGLObjects(state);
for(StateSets::const_iterator itr = _statesets.begin();
itr != _statesets.end();
++itr)
{
(*itr)->releaseGLObjects(state);
}
for(GlyphTextureList::const_iterator itr=_glyphTextureList.begin();
itr!=_glyphTextureList.end();
@@ -576,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;
@@ -583,12 +456,12 @@ void Font::addGlyph(const FontResolution& fontRes, unsigned int charcode, Glyph*
itr!=_glyphTextureList.end() && !glyphTexture;
++itr)
{
if ((*itr)->getSpaceForGlyph(glyph,posX,posY)) glyphTexture = itr->get();
if ((*itr)->getShaderTechnique()==shaderTechnique && (*itr)->getSpaceForGlyph(glyph,posX,posY)) glyphTexture = itr->get();
}
if (glyphTexture)
{
//cout << " found space for texture "<<glyphTexture<<" posX="<<posX<<" posY="<<posY<<endl;
//cout << " Font::assignGlyphToGlyphTexture() found space for texture "<<glyphTexture<<" posX="<<posX<<" posY="<<posY<<endl;
}
if (!glyphTexture)
@@ -602,12 +475,11 @@ 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->setGlyphImageMargin(_margin);
glyphTexture->setGlyphImageMarginRatio(_marginRatio);
glyphTexture->setShaderTechnique(shaderTechnique);
glyphTexture->setTextureSize(_textureWidthHint,_textureHeightHint);
glyphTexture->setFilter(osg::Texture::MIN_FILTER,_minFilterHint);
glyphTexture->setFilter(osg::Texture::MAG_FILTER,_magFilterHint);
glyphTexture->setMaxAnisotropy(8);
glyphTexture->setMaxAnisotropy(_maxAnisotropy);
_glyphTextureList.push_back(glyphTexture);
@@ -621,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

@@ -28,9 +28,34 @@
using namespace osgText;
using namespace std;
// GL_ALPHA and GL_LUMINANCE_ALPHA are deprecated in GL3/GL4 core profile, use GL_RED & GL_RB in this case.
#if defined(OSG_GL3_AVAILABLE) && !defined(OSG_GL2_AVAILABLE) && !defined(OSG_GL1_AVAILABLE)
#define OSGTEXT_GLYPH_ALPHA_FORMAT GL_RED
#define OSGTEXT_GLYPH_ALPHA_INTERNALFORMAT GL_R8
#define OSGTEXT_GLYPH_SDF_FORMAT GL_RG
#define OSGTEXT_GLYPH_SDF_INTERNALFORMAT GL_RG8
#else
#define OSGTEXT_GLYPH_ALPHA_FORMAT GL_ALPHA
#define OSGTEXT_GLYPH_ALPHA_INTERNALFORMAT GL_ALPHA
#define OSGTEXT_GLYPH_SDF_FORMAT GL_LUMINANCE_ALPHA
#define OSGTEXT_GLYPH_SDF_INTERNALFORMAT GL_LUMINANCE_ALPHA
#endif
#if 0
#define TEXTURE_IMAGE_NUM_CHANNELS 1
#define TEXTURE_IMAGE_FORMAT OSGTEXT_GLYPH_FORMAT
#else
#define TEXTURE_IMAGE_NUM_CHANNELS 2
#define TEXTURE_IMAGE_FORMAT GL_RGBA
#endif
//////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// GlyphTexture
//
GlyphTexture::GlyphTexture():
_margin(1),
_marginRatio(0.02f),
_usedY(0),
_partUsedX(0),
_partUsedY(0)
@@ -51,27 +76,52 @@ int GlyphTexture::compare(const osg::StateAttribute& rhs) const
return 0;
}
int GlyphTexture::getEffectMargin(const Glyph* glyph)
{
if (_shaderTechnique==GREYSCALE) return 0;
else return osg::maximum(glyph->getFontResolution().second/6, 2u);
}
int GlyphTexture::getTexelMargin(const Glyph* glyph)
{
int width = glyph->s();
int height = glyph->t();
int effect_margin = getEffectMargin(glyph);
int max_dimension = std::max(width, height) + 2 * effect_margin;
int margin = osg::maximum(max_dimension/4, 2) + effect_margin;
return margin;
}
bool GlyphTexture::getSpaceForGlyph(Glyph* glyph, int& posX, int& posY)
{
int maxAxis = osg::maximum(glyph->s(), glyph->t());
int margin = _margin + (int)((float)maxAxis * _marginRatio);
int width = glyph->s();
int height = glyph->t();
int width = glyph->s()+2*margin;
int height = glyph->t()+2*margin;
int margin = getTexelMargin(glyph);
// first check box (_partUsedX,_usedY) to (width,height)
if (width <= (getTextureWidth()-_partUsedX) &&
height <= (getTextureHeight()-_usedY))
width += 2*margin;
height += 2*margin;
int interval = 4;
int partUsedX = ((_partUsedX % interval) == 0) ? _partUsedX : (((_partUsedX/interval)+1)*interval);
int partUsedY = ((_partUsedY % interval) == 0) ? _partUsedY : (((_partUsedY/interval)+1)*interval);
int usedY = ((_usedY % interval) == 0) ? _usedY : (((_usedY/interval)+1)*interval);
// first check box (partUsedX, usedY) to (width,height)
if (width <= (getTextureWidth()-partUsedX) &&
height <= (getTextureHeight()-usedY))
{
// can fit in existing row.
// record the position in which the texture will be stored.
posX = _partUsedX+margin;
posY = _usedY+margin;
posX = partUsedX+margin;
posY = usedY+margin;
// move used markers on.
_partUsedX += width;
_partUsedX = posX+width;
if (_usedY+height>_partUsedY) _partUsedY = _usedY+height;
return true;
@@ -83,14 +133,14 @@ bool GlyphTexture::getSpaceForGlyph(Glyph* glyph, int& posX, int& posY)
{
// can fit next row.
_partUsedX = 0;
_usedY = _partUsedY;
_usedY = partUsedY;
posX = _partUsedX+margin;
posY = _usedY+margin;
// move used markers on.
_partUsedX += width;
if (_usedY+height>_partUsedY) _partUsedY = _usedY+height;
_partUsedX = posX+width;
_partUsedY = _usedY+height;
return true;
}
@@ -101,23 +151,237 @@ bool GlyphTexture::getSpaceForGlyph(Glyph* glyph, int& posX, int& posY)
void GlyphTexture::addGlyph(Glyph* glyph, int posX, int posY)
{
OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_mutex);
if (!_image.valid()) createImage();
_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());
_image->copySubImage(glyph->getTexturePositionX(), glyph->getTexturePositionY(), 0, glyph);
copyGlyphImage(glyph, info);
}
void GlyphTexture::copyGlyphImage(Glyph* glyph, Glyph::TextureInfo* info)
{
_image->dirty();
if (_shaderTechnique<=GREYSCALE)
{
// OSG_NOTICE<<"GlyphTexture::copyGlyphImage() greyscale copying. glyphTexture="<<this<<", glyph="<<glyph->getGlyphCode()<<std::endl;
// make sure the glyph image settings and the target image are consisent before copying.
glyph->setPixelFormat(_image->getPixelFormat());
glyph->setInternalTextureFormat(_image->getPixelFormat());
_image->copySubImage(info->texturePositionX, info->texturePositionY, 0, glyph);
return;
}
// OSG_NOTICE<<"GlyphTexture::copyGlyphImage() generating signed distance field. glyphTexture="<<this<<", glyph="<<glyph->getGlyphCode()<<std::endl;
int src_columns = glyph->s();
int src_rows = glyph->t();
unsigned char* src_data = glyph->data();
int dest_columns = _image->s();
int dest_rows = _image->t();
unsigned char* dest_data = _image->data(info->texturePositionX, info->texturePositionY);
int search_distance = getEffectMargin(glyph);
int left = -search_distance;
int right = glyph->s()+search_distance;
int lower = -search_distance;
int upper = glyph->t()+search_distance;
float multiplier = 1.0/255.0f;
float max_distance = sqrtf(float(search_distance)*float(search_distance)*2.0);
if ((left+info->texturePositionX)<0) left = -info->texturePositionX;
if ((right+info->texturePositionX)>=dest_columns) right = dest_columns-info->texturePositionX-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());
int bytes_per_pixel = osg::Image::computePixelSizeInBits(_image->getPixelFormat(),_image->getDataType())/8;
int alpha_offset = (_image->getPixelFormat()==GL_LUMINANCE_ALPHA) ? 1 : 0;
int sdf_offset = (_image->getPixelFormat()==GL_LUMINANCE_ALPHA) ? 0 : 1;
unsigned char full_on = 255;
unsigned char mid_point = full_on/2;
float mid_point_f = float(mid_point)*multiplier;
for(int dr=lower; dr<=upper; ++dr)
{
for(int dc=left; dc<=right; ++dc)
{
unsigned char value = 0;
unsigned char center_value = 0;
if (dr>=0 && dr<src_rows && dc>=0 && dc<src_columns) center_value = *(src_data + dr*src_columns + dc);
float center_value_f = center_value*multiplier;
float min_distance = max_distance;
if (center_value>0 && center_value<full_on)
{
if (center_value_f>=mid_point_f)
{
min_distance = center_value_f-mid_point_f;
value = 128+(min_distance/max_distance)*127;
}
else
{
min_distance = mid_point_f-center_value_f;
value = 127-(min_distance/max_distance)*127;
}
}
else
{
for(int radius=1; radius<search_distance; ++radius)
{
for(int span=-radius; span<=radius; ++span)
{
{
// left
int dx = -radius;
int dy = span;
int c = dc+dx;
int r = dr+dy;
unsigned char local_value = 0;
if (r>=0 && r<src_rows && c>=0 && c<src_columns) local_value = *(src_data + r*src_columns + c);
if (local_value!=center_value)
{
float local_value_f = float(local_value)*multiplier;
float D = sqrtf(float(dx*dx) + float(dy*dy));
float local_multiplier = (abs(dx)>abs(dy)) ? D/float(abs(dx)) : D/float(abs(dy));
float local_distance = sqrtf(float(radius*radius)+float(span*span));
if (center_value==0) local_distance += (mid_point_f-local_value_f)*local_multiplier;
else local_distance += (local_value_f - mid_point_f)*local_multiplier;
if (local_distance<min_distance) min_distance = local_distance;
}
}
{
// top
int dx = span;
int dy = radius;
int c = dc+dx;
int r = dr+dy;
unsigned char local_value = 0;
if (r>=0 && r<src_rows && c>=0 && c<src_columns) local_value = *(src_data + r*src_columns + c);
if (local_value!=center_value)
{
float local_value_f = float(local_value)*multiplier;
float D = sqrtf(float(dx*dx) + float(dy*dy));
float local_multiplier = (abs(dx)>abs(dy)) ? D/float(abs(dx)) : D/float(abs(dy));
float local_distance = sqrtf(float(radius*radius)+float(span*span));
if (center_value==0) local_distance += (mid_point_f-local_value_f)*local_multiplier;
else local_distance += (local_value_f - mid_point_f)*local_multiplier;
if (local_distance<min_distance) min_distance = local_distance;
}
}
{
// right
int dx = radius;
int dy = span;
int c = dc+dx;
int r = dr+dy;
unsigned char local_value = 0;
if (r>=0 && r<src_rows && c>=0 && c<src_columns) local_value = *(src_data + r*src_columns + c);
if (local_value!=center_value)
{
float local_value_f = float(local_value)*multiplier;
float D = sqrtf(float(dx*dx) + float(dy*dy));
float local_multiplier = (abs(dx)>abs(dy)) ? D/float(abs(dx)) : D/float(abs(dy));
float local_distance = sqrtf(float(radius*radius)+float(span*span));
if (center_value==0) local_distance += (mid_point_f-local_value_f)*local_multiplier;
else local_distance += (local_value_f - mid_point_f)*local_multiplier;
if (local_distance<min_distance) min_distance = local_distance;
}
}
{
// bottom
int dx = span;
int dy = -radius;
int c = dc+dx;
int r = dr+dy;
unsigned char local_value = 0;
if (r>=0 && r<src_rows && c>=0 && c<src_columns) local_value = *(src_data + r*src_columns + c);
if (local_value!=center_value)
{
float local_value_f = float(local_value)*multiplier;
float D = sqrtf(float(dx*dx) + float(dy*dy));
float local_multiplier = (abs(dx)>abs(dy)) ? D/float(abs(dx)) : D/float(abs(dy));
float local_distance = sqrtf(float(radius*radius)+float(span*span));
if (center_value==0) local_distance += (mid_point_f-local_value_f)*local_multiplier;
else local_distance += (local_value_f - mid_point_f)*local_multiplier;
if (local_distance<min_distance) min_distance = local_distance;
}
}
}
}
if (center_value_f>=0.5)
{
value = 128+(min_distance/max_distance)*127;
}
else
{
value = 127-(min_distance/max_distance)*127;
}
}
unsigned char* dest_ptr = dest_data + (dr*dest_columns + dc)*bytes_per_pixel;
if (num_components==2)
{
// signed distance field value
*(dest_ptr+sdf_offset) = value;
// original alpha value from glyph image
*(dest_ptr+alpha_offset) = center_value;
}
else
{
*(dest_ptr) = value;
}
}
}
}
void GlyphTexture::setThreadSafeRefUnref(bool threadSafe)
@@ -147,22 +411,26 @@ osg::Image* GlyphTexture::createImage()
{
if (!_image)
{
_image = new osg::Image;
_image->allocateImage(getTextureWidth(), getTextureHeight(), 1, OSGTEXT_GLYPH_FORMAT, GL_UNSIGNED_BYTE);
memset(_image->data(), 0, _image->getTotalSizeInBytes());
OSG_INFO<<"GlyphTexture::createImage() : Creating image 0x"<<std::hex<<TEXTURE_IMAGE_FORMAT<<std::dec<<std::endl;
for(GlyphRefList::iterator itr = _glyphs.begin();
itr != _glyphs.end();
++itr)
{
Glyph* glyph = itr->get();
_image->copySubImage(glyph->getTexturePositionX(), glyph->getTexturePositionY(), 0, glyph);
}
_image = new osg::Image;
GLenum imageFormat = (_shaderTechnique<=GREYSCALE) ? OSGTEXT_GLYPH_ALPHA_FORMAT : OSGTEXT_GLYPH_SDF_FORMAT;
GLenum internalFormat = (_shaderTechnique<=GREYSCALE) ? OSGTEXT_GLYPH_ALPHA_INTERNALFORMAT : OSGTEXT_GLYPH_SDF_INTERNALFORMAT;
_image->allocateImage(getTextureWidth(), getTextureHeight(), 1, imageFormat, GL_UNSIGNED_BYTE);
_image->setInternalTextureFormat(internalFormat);
memset(_image->data(), 0, _image->getTotalSizeInBytes());
}
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),
@@ -172,12 +440,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);
}
@@ -198,67 +461,37 @@ 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; }
void Glyph::subload() const
void Glyph::setTextureInfo(ShaderTechnique technique, TextureInfo* info)
{
GLenum errorNo = glGetError();
if (errorNo!=GL_NO_ERROR)
if (technique>=_textureInfoList.size())
{
const GLubyte* msg = osg::gluErrorString(errorNo);
if (msg) { OSG_WARN<<"before Glyph::subload(): detected OpenGL error: "<<msg<<std::endl; }
else { OSG_WARN<<"before Glyph::subload(): detected OpenGL error number: "<<errorNo<<std::endl; }
}
if(s() <= 0 || t() <= 0)
{
OSG_INFO<<"Glyph::subload(): texture sub-image width and/or height of 0, ignoring operation."<<std::endl;
return;
}
glPixelStorei(GL_UNPACK_ALIGNMENT,getPacking());
#if !defined(OSG_GLES1_AVAILABLE) && !defined(OSG_GLES2_AVAILABLE)
glPixelStorei(GL_UNPACK_ROW_LENGTH,getRowLength());
#endif
glTexSubImage2D(GL_TEXTURE_2D,0,
_texturePosX,_texturePosY,
s(),t(),
(GLenum)getPixelFormat(),
(GLenum)getDataType(),
data());
errorNo = glGetError();
if (errorNo!=GL_NO_ERROR)
{
const GLubyte* msg = osg::gluErrorString(errorNo);
if (msg) { OSG_WARN<<"after Glyph::subload() : detected OpenGL error: "<<msg<<std::endl; }
else { OSG_WARN<<"after Glyph::subload() : detected OpenGL error number: "<<errorNo<<std::endl; }
OSG_WARN<< "\tglTexSubImage2D(0x"<<hex<<GL_TEXTURE_2D<<dec<<" ,"<<0<<"\t"<<std::endl<<
"\t "<<_texturePosX<<" ,"<<_texturePosY<<std::endl<<
"\t "<<s()<<" ,"<<t()<<std::endl<<hex<<
"\t 0x"<<(GLenum)getPixelFormat()<<std::endl<<
"\t 0x"<<(GLenum)getDataType()<<std::endl<<
"\t "<<static_cast<const void*>(data())<<");"<<dec<<std::endl;
_textureInfoList.resize(technique+1);
}
_textureInfoList[technique] = info;
}
const Glyph::TextureInfo* Glyph::getTextureInfo(ShaderTechnique technique) const
{
return (technique<_textureInfoList.size()) ? _textureInfoList[technique].get() : 0;
}
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

@@ -25,13 +25,19 @@
#include <osgDB/ReadFile>
#include <sstream>
#include <iomanip>
#define DEBUG_MESSAGE_LEVEL osg::INFO
#define DEBUG_MESSAGE osg::notify(DEBUG_MESSAGE_LEVEL)
using namespace osg;
using namespace osgText;
Text::Text():
_shaderTechnique(GREYSCALE),
_enableDepthWrites(true),
_backdropType(NONE),
_backdropImplementation(DELAYED_DEPTH_WRITES),
_backdropHorizontalOffset(0.07f),
_backdropVerticalOffset(0.07f),
_backdropColor(0.0f, 0.0f, 0.0f, 1.0f),
@@ -42,13 +48,24 @@ Text::Text():
_colorGradientTopRight(1.0f, 1.0f, 1.0f, 1.0f)
{
_supportsVertexBufferObjects = true;
const std::string& str = osg::DisplaySettings::instance()->getTextShaderTechnique();
if (!str.empty())
{
if (str=="ALL_FEATURES" || str=="ALL") _shaderTechnique = ALL_FEATURES;
else if (str=="GREYSCALE") _shaderTechnique = GREYSCALE;
else if (str=="SIGNED_DISTANCE_FIELD" || str=="SDF") _shaderTechnique = SIGNED_DISTANCE_FIELD;
else if (str=="NO_TEXT_SHADER" || str=="NONE") _shaderTechnique = NO_TEXT_SHADER;
}
assignStateSet();
}
Text::Text(const Text& text,const osg::CopyOp& copyop):
osgText::TextBase(text,copyop),
_shaderTechnique(text._shaderTechnique),
_enableDepthWrites(text._enableDepthWrites),
_backdropType(text._backdropType),
_backdropImplementation(text._backdropImplementation),
_backdropHorizontalOffset(text._backdropHorizontalOffset),
_backdropVerticalOffset(text._backdropVerticalOffset),
_backdropColor(text._backdropColor),
@@ -65,21 +82,170 @@ Text::~Text()
{
}
void Text::setFont(osg::ref_ptr<Font> font)
void Text::setShaderTechnique(ShaderTechnique technique)
{
if (_font==font) return;
if (_shaderTechnique==technique) return;
osg::StateSet* previousFontStateSet = _font.valid() ? _font->getStateSet() : Font::getDefaultFont()->getStateSet();
osg::StateSet* newFontStateSet = font.valid() ? font->getStateSet() : Font::getDefaultFont()->getStateSet();
_shaderTechnique = technique;
if (getStateSet() == previousFontStateSet)
{
setStateSet( newFontStateSet );
}
assignStateSet();
TextBase::setFont(font);
computeGlyphRepresentation();
}
osg::StateSet* Text::createStateSet()
{
Font* activeFont = getActiveFont();
if (!activeFont) return 0;
Font::StateSets& statesets = activeFont->getCachedStateSets();
std::stringstream ss;
ss<<std::fixed<<std::setprecision(3);
osg::StateSet::DefineList defineList;
if (_backdropType!=NONE)
{
ss.str("");
ss << "vec4("<<_backdropColor.r()<<", "<<_backdropColor.g()<<", "<<_backdropColor.b()<<", "<<_backdropColor.a()<<")";
defineList["BACKDROP_COLOR"] = osg::StateSet::DefinePair(ss.str(), osg::StateAttribute::ON);
if (_backdropType==OUTLINE)
{
ss.str("");
ss <<_backdropHorizontalOffset;
defineList["OUTLINE"] = osg::StateSet::DefinePair(ss.str(), osg::StateAttribute::ON);
}
else
{
osg::Vec2 offset(_backdropHorizontalOffset, _backdropVerticalOffset);
switch(_backdropType)
{
case(DROP_SHADOW_BOTTOM_RIGHT) : offset.set(_backdropHorizontalOffset, -_backdropVerticalOffset); break;
case(DROP_SHADOW_CENTER_RIGHT) : offset.set(_backdropHorizontalOffset, 0.0f); break;
case(DROP_SHADOW_TOP_RIGHT) : offset.set(_backdropHorizontalOffset, _backdropVerticalOffset); break;
case(DROP_SHADOW_BOTTOM_CENTER) : offset.set(0.0f, -_backdropVerticalOffset); break;
case(DROP_SHADOW_TOP_CENTER) : offset.set(0.0f, _backdropVerticalOffset); break;
case(DROP_SHADOW_BOTTOM_LEFT) : offset.set(-_backdropHorizontalOffset, -_backdropVerticalOffset); break;
case(DROP_SHADOW_CENTER_LEFT) : offset.set(-_backdropHorizontalOffset, 0.0f); break;
case(DROP_SHADOW_TOP_LEFT) : offset.set(-_backdropHorizontalOffset, _backdropVerticalOffset); break;
default : break;
}
ss.str("");
ss << "vec2("<<offset.x()<<", "<<offset.y()<<")";
defineList["SHADOW"] = osg::StateSet::DefinePair(ss.str(), osg::StateAttribute::ON);
}
}
{
ss<<std::fixed<<std::setprecision(1);
ss.str("");
ss << float(_fontSize.second);
defineList["GLYPH_DIMENSION"] = osg::StateSet::DefinePair(ss.str(), osg::StateAttribute::ON);
ss.str("");
ss << float(activeFont->getTextureWidthHint());
defineList["TEXTURE_DIMENSION"] = osg::StateSet::DefinePair(ss.str(), osg::StateAttribute::ON);
}
if (_shaderTechnique>GREYSCALE)
{
defineList["SIGNED_DISTNACE_FIELD"] = osg::StateSet::DefinePair("1", osg::StateAttribute::ON);
}
#if 0
OSG_NOTICE<<"Text::createStateSet() defines:"<<defineList.size()<<std::endl;
for(osg::StateSet::DefineList::iterator itr = defineList.begin();
itr != defineList.end();
++itr)
{
OSG_NOTICE<<" define["<<itr->first<<"] = "<<itr->second.first<<std::endl;
}
#endif
if (!statesets.empty())
{
for(Font::StateSets::iterator itr = statesets.begin();
itr != statesets.end();
++itr)
{
if ((*itr)->getDefineList()==defineList)
{
// OSG_NOTICE<<"Text::createStateSet() : Matched DefineList, return StateSet "<<itr->get()<<std::endl;
return itr->get();
}
else
{
}
}
}
if (osg::isNotifyEnabled(DEBUG_MESSAGE_LEVEL))
{
DEBUG_MESSAGE<<"Text::createStateSet() ShaderTechnique ";
switch(_shaderTechnique)
{
case(NO_TEXT_SHADER) : DEBUG_MESSAGE<<"NO_TEXT_SHADER"<<std::endl; break;
case(GREYSCALE) : DEBUG_MESSAGE<<"GREYSCALE"<<std::endl; break;
case(SIGNED_DISTANCE_FIELD) : DEBUG_MESSAGE<<"SIGNED_DISTANCE_FIELD"<<std::endl; break;
case(ALL_FEATURES) : DEBUG_MESSAGE<<"ALL_FEATURES"<<std::endl; break;
}
}
DEBUG_MESSAGE<<"Text::createStateSet() : Not Matched DefineList, creating new StateSet"<<std::endl;
osg::ref_ptr<osg::StateSet> stateset = new osg::StateSet;
stateset->setDefineList(defineList);
statesets.push_back(stateset.get());
stateset->setRenderingHint(osg::StateSet::TRANSPARENT_BIN);
stateset->setMode(GL_LIGHTING, osg::StateAttribute::OFF);
stateset->setMode(GL_BLEND, osg::StateAttribute::ON);
#if defined(OSG_GL_FIXED_FUNCTION_AVAILABLE)
osg::DisplaySettings::ShaderHint shaderHint = osg::DisplaySettings::instance()->getShaderHint();
if (_shaderTechnique==NO_TEXT_SHADER && shaderHint==osg::DisplaySettings::SHADER_NONE)
{
DEBUG_MESSAGE<<"Font::Font() Fixed function pipeline"<<std::endl;
stateset->setTextureMode(0, GL_TEXTURE_2D, osg::StateAttribute::ON);
return stateset.release();
}
#endif
// set up the StateSet to use shaders
stateset->addUniform(new osg::Uniform("glyphTexture", 0));
osg::ref_ptr<osg::Program> program = new osg::Program;
stateset->setAttributeAndModes(program.get());
{
DEBUG_MESSAGE<<"Using shaders/text.vert"<<std::endl;
#include "shaders/text_vert.cpp"
program->addShader(osgDB::readRefShaderFileWithFallback(osg::Shader::VERTEX, "shaders/text.vert", text_vert));
}
{
DEBUG_MESSAGE<<"Using shaders/text.frag"<<std::endl;
#include "shaders/text_frag.cpp"
program->addShader(osgDB::readRefShaderFileWithFallback(osg::Shader::FRAGMENT, "shaders/text.frag", text_frag));
}
return stateset.release();
}
Font* Text::getActiveFont()
{
@@ -225,22 +391,20 @@ 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);
osg::DrawElements* primitives = 0;
if (glyphquad._primitives.empty())
osg::DrawElements* primitives = glyphquad._primitives.get();
if (!primitives)
{
unsigned int maxIndices = _text.size()*4;
if (maxIndices>=16384) primitives = new osg::DrawElementsUInt(GL_TRIANGLES);
else primitives = new osg::DrawElementsUShort(GL_TRIANGLES);
primitives->setBufferObject(_ebo.get());
glyphquad._primitives.push_back(primitives);
}
else
{
primitives = glyphquad._primitives[0].get();
glyphquad._primitives = primitives;
}
@@ -280,25 +444,18 @@ void Text::computeGlyphRepresentation()
if (!_texcoords) { _texcoords = new osg::Vec2Array(osg::Array::BIND_PER_VERTEX); _texcoords->setBufferObject(_vbo.get()); }
else _texcoords->clear();
#if 0
_textureGlyphQuadMap.clear();
#else
for(TextureGlyphQuadMap::iterator itr = _textureGlyphQuadMap.begin();
itr != _textureGlyphQuadMap.end();
++itr)
{
GlyphQuads& glyphquads = itr->second;
glyphquads._glyphs.clear();
for(Primitives::iterator pitr = glyphquads._primitives.begin();
pitr != glyphquads._primitives.end();
++pitr)
if (glyphquads._primitives.valid())
{
(*pitr)->resizeElements(0);
(*pitr)->dirty();
glyphquads._primitives->resizeElements(0);
glyphquads._primitives->dirty();
}
}
#endif
_lineCount = 0;
@@ -487,45 +644,53 @@ 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 fHorizTCMargin = 1.0f / glyph->getTexture()->getTextureWidth();
float fVertTCMargin = 1.0f / 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;
@@ -622,153 +787,10 @@ void Text::computePositionsImplementation()
{
TextBase::computePositionsImplementation();
computeBackdropPositions();
computeBackdropBoundingBox();
computeBoundingBoxMargin();
}
// Presumes the atc matrix is already up-to-date
void Text::computeBackdropPositions()
{
if(_backdropType == NONE)
{
return;
}
float avg_width = 0.0f;
float avg_height = 0.0f;
// FIXME: OPTIMIZE: This function produces the same value regardless of contextID.
// Since we tend to loop over contextID, we should cache this value some how
// instead of recomputing it each time.
bool is_valid_size = computeAverageGlyphWidthAndHeight(avg_width, avg_height);
if (!is_valid_size) return;
unsigned int backdrop_index;
unsigned int max_backdrop_index;
if(_backdropType == OUTLINE)
{
// For outline, we want to draw the in every direction
backdrop_index = 1;
max_backdrop_index = backdrop_index+8;
}
else
{
// Yes, this may seem a little strange,
// but since the code is using references,
// I would have to duplicate the following code twice
// for each part of the if/else because I can't
// declare a reference without setting it immediately
// and it wouldn't survive the scope.
// So it happens that the _backdropType value matches
// the index in the array I want to store the coordinates
// in. So I'll just setup the for-loop so it only does
// the one direction I'm interested in.
backdrop_index = _backdropType+1;
max_backdrop_index = backdrop_index+1;
}
for( ; backdrop_index < max_backdrop_index; backdrop_index++)
{
float horizontal_shift_direction;
float vertical_shift_direction;
switch(backdrop_index)
{
case DROP_SHADOW_BOTTOM_RIGHT:
{
horizontal_shift_direction = 1.0f;
vertical_shift_direction = -1.0f;
break;
}
case DROP_SHADOW_CENTER_RIGHT:
{
horizontal_shift_direction = 1.0f;
vertical_shift_direction = 0.0f;
break;
}
case DROP_SHADOW_TOP_RIGHT:
{
horizontal_shift_direction = 1.0f;
vertical_shift_direction = 1.0f;
break;
}
case DROP_SHADOW_BOTTOM_CENTER:
{
horizontal_shift_direction = 0.0f;
vertical_shift_direction = -1.0f;
break;
}
case DROP_SHADOW_TOP_CENTER:
{
horizontal_shift_direction = 0.0f;
vertical_shift_direction = 1.0f;
break;
}
case DROP_SHADOW_BOTTOM_LEFT:
{
horizontal_shift_direction = -1.0f;
vertical_shift_direction = -1.0f;
break;
}
case DROP_SHADOW_CENTER_LEFT:
{
horizontal_shift_direction = -1.0f;
vertical_shift_direction = 0.0f;
break;
}
case DROP_SHADOW_TOP_LEFT:
{
horizontal_shift_direction = -1.0f;
vertical_shift_direction = 1.0f;
break;
}
default: // error
{
horizontal_shift_direction = 1.0f;
vertical_shift_direction = -1.0f;
}
}
// now apply matrix to the glyphs.
for(TextureGlyphQuadMap::iterator titr=_textureGlyphQuadMap.begin();
titr!=_textureGlyphQuadMap.end();
++titr)
{
GlyphQuads& glyphquad = titr->second;
osg::DrawElements* src_primitives = glyphquad._primitives[0].get();
for(unsigned int i=glyphquad._primitives.size(); i<=backdrop_index; ++i)
{
osg::DrawElementsUShort* dst_primitives = new osg::DrawElementsUShort(GL_TRIANGLES);
dst_primitives->setBufferObject(src_primitives->getBufferObject());
glyphquad._primitives.push_back(dst_primitives);
}
osg::DrawElements* dst_primitives = glyphquad._primitives[backdrop_index].get();
dst_primitives->resizeElements(0);
unsigned int numCoords = src_primitives->getNumIndices();
Coords& src_coords = _coords;
TexCoords& src_texcoords = _texcoords;
Coords& dst_coords = _coords;
TexCoords& dst_texcoords = _texcoords;
for(unsigned int i=0;i<numCoords;++i)
{
unsigned int si = (*src_primitives).getElement(i);
osg::Vec3 v(horizontal_shift_direction * _backdropHorizontalOffset * avg_width + (*src_coords)[si].x(), vertical_shift_direction * _backdropVerticalOffset * avg_height + (*src_coords)[si].y(), 0.0f);
unsigned int di = dst_coords->size();
(*dst_primitives).addElement(di);
(*dst_coords).push_back(v);
(*dst_texcoords).push_back((*src_texcoords)[si]);
}
}
}
}
// This method adjusts the bounding box to account for the expanded area caused by the backdrop.
// This assumes that the bounding box has already been computed for the text without the backdrop.
void Text::computeBackdropBoundingBox()
@@ -1095,6 +1117,8 @@ void Text::drawImplementation(osg::RenderInfo& renderInfo) const
void Text::drawImplementationSinglePass(osg::State& state, const osg::Vec4& colorMultiplier) const
{
if (colorMultiplier.a()==0.0f || _color.a()==0.0f) return;
osg::VertexArrayState* vas = state.getCurrentVertexArrayState();
bool usingVertexBufferObjects = state.useVertexBufferObject(_supportsVertexBufferObjects && _useVertexBufferObjects);
bool usingVertexArrayObjects = usingVertexBufferObjects && state.useVertexArrayObject(_useVertexArrayObject);
@@ -1117,7 +1141,6 @@ void Text::drawImplementationSinglePass(osg::State& state, const osg::Vec4& colo
}
if (_drawMode & TEXT)
// if (false)
{
for(TextureGlyphQuadMap::const_iterator titr=_textureGlyphQuadMap.begin();
titr!=_textureGlyphQuadMap.end();
@@ -1128,33 +1151,6 @@ void Text::drawImplementationSinglePass(osg::State& state, const osg::Vec4& colo
const GlyphQuads& glyphquad = titr->second;
#if 1
if(_backdropType != NONE)
{
unsigned int backdrop_index;
unsigned int max_backdrop_index;
if(_backdropType == OUTLINE)
{
backdrop_index = 1;
max_backdrop_index = 8;
}
else
{
backdrop_index = _backdropType+1;
max_backdrop_index = backdrop_index+1;
}
if (max_backdrop_index>glyphquad._primitives.size()) max_backdrop_index=glyphquad._primitives.size();
state.disableColorPointer();
state.Color(_backdropColor.r(),_backdropColor.g(),_backdropColor.b(),_backdropColor.a());
for( ; backdrop_index < max_backdrop_index; backdrop_index++)
{
glyphquad._primitives[backdrop_index]->draw(state, usingVertexBufferObjects);
}
}
#endif
if(_colorGradientMode == SOLID)
{
vas->disableColorArray(state);
@@ -1168,7 +1164,7 @@ void Text::drawImplementationSinglePass(osg::State& state, const osg::Vec4& colo
}
}
glyphquad._primitives[0]->draw(state, usingVertexBufferObjects);
glyphquad._primitives->draw(state, usingVertexBufferObjects);
}
}
}
@@ -1216,9 +1212,6 @@ void Text::drawImplementation(osg::State& state, const osg::Vec4& colorMultiplie
vas->applyDisablingOfVertexAttributes(state);
}
#if 0
drawImplementationSinglePass(state, colorMultiplier);
#else
glDepthMask(GL_FALSE);
drawImplementationSinglePass(state, colorMultiplier);
@@ -1235,7 +1228,6 @@ void Text::drawImplementation(osg::State& state, const osg::Vec4& colorMultiplie
}
state.haveAppliedAttribute(osg::StateAttribute::DEPTH);
#endif
if (usingVertexBufferObjects && !usingVertexArrayObjects)
{
@@ -1273,16 +1265,16 @@ void Text::accept(osg::PrimitiveFunctor& pf) const
++titr)
{
const GlyphQuads& glyphquad = titr->second;
if (!glyphquad._primitives.empty())
if (glyphquad._primitives.valid())
{
const osg::DrawElementsUShort* drawElementsUShort = dynamic_cast<const osg::DrawElementsUShort*>(glyphquad._primitives[0].get());
const osg::DrawElementsUShort* drawElementsUShort = dynamic_cast<const osg::DrawElementsUShort*>(glyphquad._primitives.get());
if (drawElementsUShort)
{
pf.drawElements(GL_TRIANGLES, drawElementsUShort->size(), &(drawElementsUShort->front()));
}
else
{
const osg::DrawElementsUInt* drawElementsUInt = dynamic_cast<const osg::DrawElementsUInt*>(glyphquad._primitives[0].get());
const osg::DrawElementsUInt* drawElementsUInt = dynamic_cast<const osg::DrawElementsUInt*>(glyphquad._primitives.get());
if (drawElementsUInt)
{
pf.drawElements(GL_TRIANGLES, drawElementsUInt->size(), &(drawElementsUInt->front()));
@@ -1322,22 +1314,19 @@ void Text::setBackdropType(BackdropType type)
if (_backdropType==type) return;
_backdropType = type;
assignStateSet();
computeGlyphRepresentation();
}
void Text::setBackdropImplementation(BackdropImplementation implementation)
{
if (_backdropImplementation==implementation) return;
_backdropImplementation = implementation;
computeGlyphRepresentation();
}
void Text::setBackdropOffset(float offset)
{
_backdropHorizontalOffset = offset;
_backdropVerticalOffset = offset;
assignStateSet();
computeGlyphRepresentation();
}
@@ -1345,12 +1334,17 @@ void Text::setBackdropOffset(float horizontal, float vertical)
{
_backdropHorizontalOffset = horizontal;
_backdropVerticalOffset = vertical;
assignStateSet();
computeGlyphRepresentation();
}
void Text::setBackdropColor(const osg::Vec4& color)
{
_backdropColor = color;
assignStateSet();
}
void Text::setColorGradientMode(ColorGradientMode mode)
@@ -1391,20 +1385,10 @@ Text::GlyphQuads::GlyphQuads(const GlyphQuads&)
void Text::GlyphQuads::resizeGLObjectBuffers(unsigned int maxSize)
{
for(Primitives::iterator itr = _primitives.begin();
itr != _primitives.end();
++itr)
{
(*itr)->resizeGLObjectBuffers(maxSize);
}
if (_primitives.valid()) _primitives->resizeGLObjectBuffers(maxSize);
}
void Text::GlyphQuads::releaseGLObjects(osg::State* state) const
{
for(Primitives::const_iterator itr = _primitives.begin();
itr != _primitives.end();
++itr)
{
(*itr)->releaseGLObjects(state);
}
if (_primitives.valid()) _primitives->releaseGLObjects(state);
}

View File

@@ -50,7 +50,6 @@ TextBase::TextBase():
_lineCount(0),
_glyphNormalized(false)
{
setStateSet(Font::getDefaultFont()->getStateSet());
setUseDisplayList(false);
setSupportsDisplayList(false);
@@ -89,6 +88,11 @@ TextBase::~TextBase()
{
}
osg::StateSet* TextBase::createStateSet()
{
return 0;
}
void TextBase::initArraysAndBuffers()
{
_vbo = new osg::VertexBufferObject;
@@ -182,6 +186,10 @@ void TextBase::setColor(const osg::Vec4& color)
_color = color;
}
void TextBase::assignStateSet()
{
setStateSet(createStateSet());
}
void TextBase::setFont(osg::ref_ptr<Font> font)
{
@@ -189,6 +197,8 @@ void TextBase::setFont(osg::ref_ptr<Font> font)
_font = font;
assignStateSet();
computeGlyphRepresentation();
}
@@ -203,6 +213,9 @@ void TextBase::setFontResolution(unsigned int width, unsigned int height)
if (_fontSize==size) return;
_fontSize = size;
assignStateSet();
computeGlyphRepresentation();
}

View File

@@ -0,0 +1,257 @@
char text_frag[] = "$OSG_GLSL_VERSION\n"
"\n"
"#pragma import_defines( BACKDROP_COLOR, SHADOW, OUTLINE, SIGNED_DISTNACE_FIELD, TEXTURE_DIMENSION, GLYPH_DIMENSION)\n"
"\n"
"#ifdef GL_ES\n"
" #extension GL_OES_standard_derivatives : enable\n"
" #ifndef GL_OES_standard_derivatives\n"
" #undef SIGNED_DISTNACE_FIELD\n"
" #endif\n"
"#endif\n"
"\n"
"#if !defined(GL_ES)\n"
" #if __VERSION__>=400\n"
" #define osg_TextureQueryLOD textureQueryLod\n"
" #else\n"
" #extension GL_ARB_texture_query_lod : enable\n"
" #ifdef GL_ARB_texture_query_lod\n"
" #define osg_TextureQueryLOD textureQueryLOD\n"
" #endif\n"
" #endif\n"
"#endif\n"
"\n"
"$OSG_PRECISION_FLOAT\n"
"\n"
"#if __VERSION__>=130\n"
" #define TEXTURE texture\n"
" #define TEXTURELOD textureLod\n"
" out vec4 osg_FragColor;\n"
"#else\n"
" #define TEXTURE texture2D\n"
" #define TEXTURELOD texture2DLod\n"
" #define osg_FragColor gl_FragColor\n"
"#endif\n"
"\n"
"\n"
"#if !defined(GL_ES) && __VERSION__>=130\n"
" #define ALPHA r\n"
" #define SDF g\n"
"#else\n"
" #define ALPHA a\n"
" #define SDF r\n"
"#endif\n"
"\n"
"\n"
"uniform sampler2D glyphTexture;\n"
"\n"
"$OSG_VARYING_IN vec2 texCoord;\n"
"$OSG_VARYING_IN vec4 vertexColor;\n"
"\n"
"#ifndef TEXTURE_DIMENSION\n"
"const float TEXTURE_DIMENSION = 1024.0;\n"
"#endif\n"
"\n"
"#ifndef GLYPH_DIMENSION\n"
"const float GLYPH_DIMENSION = 32.0;\n"
"#endif\n"
"\n"
"#ifdef SIGNED_DISTNACE_FIELD\n"
"\n"
"float distanceFromEdge(vec2 tc)\n"
"{\n"
" float center_alpha = TEXTURELOD(glyphTexture, tc, 0.0).SDF;\n"
" if (center_alpha==0.0) return -1.0;\n"
"\n"
" //float distance_scale = (1.0/4.0)*1.41;\n"
" float distance_scale = (1.0/6.0)*1.41;\n"
" //float distance_scale = (1.0/8.0)*1.41;\n"
"\n"
" return (center_alpha-0.5)*distance_scale;\n"
"}\n"
"\n"
"vec4 distanceFieldColorSample(float edge_distance, float blend_width, float blend_half_width)\n"
"{\n"
"#ifdef OUTLINE\n"
" float outline_width = OUTLINE*0.5;\n"
" if (edge_distance>blend_half_width)\n"
" {\n"
" return vertexColor;\n"
" }\n"
" else if (edge_distance>-blend_half_width)\n"
" {\n"
" return mix(vertexColor, vec4(BACKDROP_COLOR.rgb, BACKDROP_COLOR.a*vertexColor.a), smoothstep(0.0, 1.0, (blend_half_width-edge_distance)/(blend_width)));\n"
" }\n"
" else if (edge_distance>(blend_half_width-outline_width))\n"
" {\n"
" return vec4(BACKDROP_COLOR.rgb, BACKDROP_COLOR.a*vertexColor.a);\n"
" }\n"
" else if (edge_distance>-(outline_width+blend_half_width))\n"
" {\n"
" return vec4(BACKDROP_COLOR.rgb, vertexColor.a * ((blend_half_width+outline_width+edge_distance)/blend_width));\n"
" }\n"
" else\n"
" {\n"
" return vec4(0.0, 0.0, 0.0, 0.0);\n"
" }\n"
"#else\n"
" if (edge_distance>blend_half_width)\n"
" {\n"
" return vertexColor;\n"
" }\n"
" else if (edge_distance>-blend_half_width)\n"
" {\n"
" return vec4(vertexColor.rgb, vertexColor.a * smoothstep(1.0, 0.0, (blend_half_width-edge_distance)/(blend_width)));\n"
" }\n"
" else\n"
" {\n"
" return vec4(0.0, 0.0, 0.0, 0.0);\n"
" }\n"
"#endif\n"
"}\n"
"\n"
"vec4 textColor(vec2 src_texCoord)\n"
"{\n"
" float sample_distance_scale = 0.75;\n"
" vec2 dx = dFdx(src_texCoord)*sample_distance_scale;\n"
" vec2 dy = dFdy(src_texCoord)*sample_distance_scale;\n"
"\n"
"\n"
" float distance_across_pixel = length(dx+dy)*(TEXTURE_DIMENSION/GLYPH_DIMENSION);\n"
"\n"
" // compute the appropriate number of samples required to avoid aliasing.\n"
" int maxNumSamplesAcrossSide = 4;\n"
"\n"
" int numSamplesX = int(TEXTURE_DIMENSION * length(dx));\n"
" int numSamplesY = int(TEXTURE_DIMENSION * length(dy));\n"
" if (numSamplesX<2) numSamplesX = 2;\n"
" if (numSamplesY<2) numSamplesY = 2;\n"
" if (numSamplesX>maxNumSamplesAcrossSide) numSamplesX = maxNumSamplesAcrossSide;\n"
" if (numSamplesY>maxNumSamplesAcrossSide) numSamplesY = maxNumSamplesAcrossSide;\n"
"\n"
"\n"
" vec2 delta_tx = dx/float(numSamplesX-1);\n"
" vec2 delta_ty = dy/float(numSamplesY-1);\n"
"\n"
" float numSamples = float(numSamplesX)*float(numSamplesY);\n"
" float scale = 1.0/numSamples;\n"
" vec4 total_color = vec4(0.0,0.0,0.0,0.0);\n"
"\n"
" float blend_width = 1.5*distance_across_pixel/numSamples;\n"
" float blend_half_width = blend_width*0.5;\n"
"\n"
" // check whether fragment is wholly within or outwith glyph body+outline\n"
" float cd = distanceFromEdge(src_texCoord); // central distance (distance from center to edge)\n"
" if (cd-blend_half_width>distance_across_pixel) return vertexColor; // pixel fully within glyph body\n"
"\n"
" #ifdef OUTLINE\n"
" float outline_width = OUTLINE*0.5;\n"
" if ((-cd-outline_width-blend_half_width)>distance_across_pixel) return vec4(0.0, 0.0, 0.0, 0.0); // pixel fully outside outline+glyph body\n"
" #else\n"
" if (-cd-blend_half_width>distance_across_pixel) return vec4(0.0, 0.0, 0.0, 0.0); // pixel fully outside glyph body\n"
" #endif\n"
"\n"
"\n"
" // use multi-sampling to provide high quality antialised fragments\n"
" vec2 origin = src_texCoord - dx*0.5 - dy*0.5;\n"
" for(;numSamplesY>0; --numSamplesY)\n"
" {\n"
" vec2 pos = origin;\n"
" int numX = numSamplesX;\n"
" for(;numX>0; --numX)\n"
" {\n"
" vec4 c = distanceFieldColorSample(distanceFromEdge(pos), blend_width, blend_half_width);\n"
" total_color = total_color + c * c.a;\n"
" pos += delta_tx;\n"
" }\n"
" origin += delta_ty;\n"
" }\n"
"\n"
" total_color.rgb /= total_color.a;\n"
" total_color.a *= scale;\n"
"\n"
" return total_color;\n"
"}\n"
"\n"
"#else\n"
"\n"
"vec4 textColor(vec2 src_texCoord)\n"
"{\n"
"\n"
"#ifdef OUTLINE\n"
"\n"
" float alpha = TEXTURE(glyphTexture, src_texCoord).ALPHA;\n"
" float delta_tc = 1.6*OUTLINE*GLYPH_DIMENSION/TEXTURE_DIMENSION;\n"
"\n"
" float outline_alpha = alpha;\n"
" vec2 origin = src_texCoord-vec2(delta_tc*0.5, delta_tc*0.5);\n"
"\n"
" float numSamples = 3.0;\n"
" delta_tc = delta_tc/(numSamples-1.0);\n"
"\n"
" float background_alpha = 1.0;\n"
"\n"
" for(float i=0.0; i<numSamples; ++i)\n"
" {\n"
" for(float j=0.0; j<numSamples; ++j)\n"
" {\n"
" float local_alpha = TEXTURE(glyphTexture, origin + vec2(i*delta_tc, j*delta_tc)).ALPHA;\n"
" outline_alpha = max(outline_alpha, local_alpha);\n"
" background_alpha = background_alpha * (1.0-local_alpha);\n"
" }\n"
" }\n"
"\n"
" #ifdef osg_TextureQueryLOD\n"
" float mipmapLevel = osg_TextureQueryLOD(glyphTexture, src_texCoord).x;\n"
" if (mipmapLevel<1.0)\n"
" {\n"
" outline_alpha = mix(1.0-background_alpha, outline_alpha, mipmapLevel/1.0);\n"
" }\n"
" #endif\n"
"\n"
" if (outline_alpha<alpha) outline_alpha = alpha;\n"
" if (outline_alpha>1.0) outline_alpha = 1.0;\n"
"\n"
" if (outline_alpha==0.0) return vec4(0.0, 0.0, 0.0, 0.0); // outside glyph and outline\n"
"\n"
" vec4 color = mix(BACKDROP_COLOR, vertexColor, smoothstep(0.0, 1.0, alpha));\n"
" color.a = vertexColor.a * smoothstep(0.0, 1.0, outline_alpha);\n"
"\n"
" return color;\n"
"\n"
"#else\n"
"\n"
" float alpha = TEXTURE(glyphTexture, src_texCoord).ALPHA;\n"
" if (alpha==0.0) vec4(0.0, 0.0, 0.0, 0.0);\n"
" return vec4(vertexColor.rgb, vertexColor.a * alpha);\n"
"\n"
"#endif\n"
"}\n"
"\n"
"#endif\n"
"\n"
"\n"
"void main(void)\n"
"{\n"
" if (texCoord.x<0.0 && texCoord.y<0.0)\n"
" {\n"
" osg_FragColor = vertexColor;\n"
" return;\n"
" }\n"
"\n"
"#ifdef SHADOW\n"
" float scale = -1.0*GLYPH_DIMENSION/TEXTURE_DIMENSION;\n"
" vec2 delta_tc = SHADOW*scale;\n"
" vec4 shadow_color = textColor(texCoord+delta_tc);\n"
" shadow_color.rgb = BACKDROP_COLOR.rgb;\n"
"\n"
" vec4 glyph_color = textColor(texCoord);\n"
" vec4 color = mix(shadow_color, glyph_color, glyph_color.a);\n"
"#else\n"
" vec4 color = textColor(texCoord);\n"
"#endif\n"
"\n"
" if (color.a==0.0) discard;\n"
"\n"
" osg_FragColor = color;\n"
"}\n"
"\n";

View File

@@ -0,0 +1,13 @@
char text_vert[] = "$OSG_GLSL_VERSION\n"
"$OSG_PRECISION_FLOAT\n"
"\n"
"$OSG_VARYING_OUT vec2 texCoord;\n"
"$OSG_VARYING_OUT vec4 vertexColor;\n"
"\n"
"void main(void)\n"
"{\n"
" gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;\n"
" texCoord = gl_MultiTexCoord0.xy;\n"
" vertexColor = gl_Color;\n"
"}\n"
"\n";

View File

@@ -153,6 +153,23 @@ void GLObjectsVisitor::apply(osg::StateSet& stateset)
}
}
void GLObjectsVisitor::compile(osg::Node& node)
{
if (_renderInfo.getState())
{
node.accept(*this);
if (_lastCompiledProgram.valid())
{
osg::State* state = _renderInfo.getState();
osg::GLExtensions* extensions = state->get<osg::GLExtensions>();
extensions->glUseProgram(0);
_renderInfo.getState()->setLastAppliedProgramObject(0);
}
}
}
/////////////////////////////////////////////////////////////////
//
// GLObjectsOperation

View File

@@ -571,7 +571,6 @@ void Renderer::compile()
{
DEBUG_MESSAGE<<"Renderer::compile()"<<std::endl;
_compileOnNextDraw = false;
osgUtil::SceneView* sceneView = _sceneView[0].get();
@@ -583,7 +582,7 @@ void Renderer::compile()
{
osgUtil::GLObjectsVisitor glov;
glov.setState(sceneView->getState());
sceneView->getSceneData()->accept(glov);
glov.compile(*(sceneView->getSceneData()));
}
sceneView->getState()->checkGLErrors("After Renderer::compile");

View File

@@ -134,7 +134,6 @@ void Label::setFontColor(const Color& c) {
void Label::setShadow(point_type offset) {
_text->setBackdropType(osgText::Text::DROP_SHADOW_BOTTOM_RIGHT);
_text->setBackdropImplementation(osgText::Text::NO_DEPTH_BUFFER);
_text->setBackdropOffset(offset);
_calculateSize(getTextSize());

View File

@@ -57,27 +57,6 @@ std::string convertBackdropTypeEnumToString(osgText::Text::BackdropType backdrop
}
}
osgText::Text::BackdropImplementation convertBackdropImplementationStringToEnum(std::string & str)
{
if (str=="POLYGON_OFFSET") return osgText::Text::POLYGON_OFFSET;
else if (str=="NO_DEPTH_BUFFER") return osgText::Text::NO_DEPTH_BUFFER;
else if (str=="DEPTH_RANGE") return osgText::Text::DEPTH_RANGE;
else if (str=="STENCIL_BUFFER") return osgText::Text::STENCIL_BUFFER;
else return static_cast<osgText::Text::BackdropImplementation>(-1);
}
std::string convertBackdropImplementationEnumToString(osgText::Text::BackdropImplementation backdropImplementation)
{
switch (backdropImplementation)
{
case osgText::Text::POLYGON_OFFSET: return "POLYGON_OFFSET";
case osgText::Text::NO_DEPTH_BUFFER: return "NO_DEPTH_BUFFER";
case osgText::Text::DEPTH_RANGE: return "DEPTH_RANGE";
case osgText::Text::STENCIL_BUFFER: return "STENCIL_BUFFER";
default : return "";
}
}
osgText::Text::ColorGradientMode convertColorGradientModeStringToEnum(std::string & str)
{
if (str=="SOLID") return osgText::Text::SOLID;
@@ -155,12 +134,6 @@ bool Text_readLocalData(osg::Object &obj, osgDB::Input &fr)
// backdropImplementation
if (fr[0].matchWord("backdropImplementation"))
{
std::string str = fr[1].getStr();
osgText::Text::BackdropImplementation backdropImplementation = convertBackdropImplementationStringToEnum(str);
if (backdropImplementation != static_cast<osgText::Text::BackdropImplementation>(-1))
text.setBackdropImplementation(backdropImplementation);
fr += 2;
itAdvanced = true;
}
@@ -254,9 +227,6 @@ bool Text_writeLocalData(const osg::Object &obj, osgDB::Output &fw)
osg::Vec4 c = text.getBackdropColor();
fw.indent() << "backdropColor " << c.x() << " " << c.y() << " " << c.z() << " " << c.w() << std::endl;
// backdropImplementation
fw.indent() << "backdropImplementation " << convertBackdropImplementationEnumToString(text.getBackdropImplementation()) << std::endl;
// colorGradientMode
fw.indent() << "colorGradientMode " << convertColorGradientModeEnumToString(text.getColorGradientMode()) << std::endl;