Added osgText/shaders to support greyscale and Signed Distance Field based text
This commit is contained in:
@@ -28,84 +28,6 @@
|
||||
using namespace osg;
|
||||
using namespace osgText;
|
||||
|
||||
#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"
|
||||
};
|
||||
|
||||
|
||||
|
||||
|
||||
Text::Text():
|
||||
_enableDepthWrites(true),
|
||||
_backdropType(NONE),
|
||||
@@ -237,35 +159,47 @@ osg::StateSet* Text::createStateSet()
|
||||
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;
|
||||
OSG_NOTICE<<"Text::createStateSet() activeFont->getGlyphTextureFeatures()="<<activeFont->getGlyphTextureFeatures()<<std::endl;
|
||||
|
||||
stateset->setTextureMode(0, GL_TEXTURE_2D, osg::StateAttribute::ON);
|
||||
#endif
|
||||
|
||||
#if defined(OSG_GL_FIXED_FUNCTION_AVAILABLE)
|
||||
osg::DisplaySettings::ShaderHint shaderHint = osg::DisplaySettings::instance()->getShaderHint();
|
||||
if (shaderHint==osg::DisplaySettings::SHADER_GL3 || shaderHint==osg::DisplaySettings::SHADER_GLES3)
|
||||
if (activeFont->getGlyphTextureFeatures()==GlyphTexture::GREYSCALE && shaderHint==osg::DisplaySettings::SHADER_NONE)
|
||||
{
|
||||
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));
|
||||
OSG_INFO<<"Font::Font() Fixed function pipeline"<<std::endl;
|
||||
|
||||
stateset->setTextureMode(0, GL_TEXTURE_2D, osg::StateAttribute::ON);
|
||||
return stateset.release();
|
||||
}
|
||||
else if (shaderHint==osg::DisplaySettings::SHADER_GL2 || shaderHint==osg::DisplaySettings::SHADER_GLES2)
|
||||
#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());
|
||||
|
||||
{
|
||||
OSG_INFO<<"Font::Font() Setting up GL2 compatible shaders"<<std::endl;
|
||||
OSG_NOTICE<<"Using shaders/text.vert"<<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));
|
||||
#include "shaders/text_vert.cpp"
|
||||
program->addShader(osgDB::readRefShaderFileWithFallback(osg::Shader::VERTEX, "shaders/text.vert", text_vert));
|
||||
}
|
||||
|
||||
if (activeFont->getGlyphTextureFeatures()==GlyphTexture::GREYSCALE)
|
||||
{
|
||||
OSG_NOTICE<<"Using shaders/text_greyscale.frag"<<std::endl;
|
||||
|
||||
#include "shaders/text_greyscale_frag.cpp"
|
||||
program->addShader(osgDB::readRefShaderFileWithFallback(osg::Shader::VERTEX, "shaders/text_greyscale.frag", text_greyscale_frag));
|
||||
}
|
||||
else
|
||||
{
|
||||
OSG_NOTICE<<"Using shaders/text_sdf.frag"<<std::endl;
|
||||
|
||||
#include "shaders/text_sdf_frag.cpp"
|
||||
program->addShader(osgDB::readRefShaderFileWithFallback(osg::Shader::VERTEX, "shaders/text_sdf.frag", text_sdf_frag));
|
||||
}
|
||||
|
||||
return stateset.release();
|
||||
|
||||
Reference in New Issue
Block a user