From c7711fd2d4a93daea340573d98687b9f01df574a Mon Sep 17 00:00:00 2001 From: Robert Osfield Date: Sat, 18 Mar 2017 18:08:12 +0000 Subject: [PATCH] Added support for GL3/GLES3, GL2,GLE2 shaders to osgText::Font/Text. --- src/osgText/Font.cpp | 89 +++++++++++++++++++++++++++++++++++++++++- src/osgText/Text3D.cpp | 14 ++++++- 2 files changed, 100 insertions(+), 3 deletions(-) diff --git a/src/osgText/Font.cpp b/src/osgText/Font.cpp index b57d844b1..7620724d9 100644 --- a/src/osgText/Font.cpp +++ b/src/osgText/Font.cpp @@ -34,6 +34,67 @@ using namespace std; static osg::ApplicationUsageProxy Font_e0(osg::ApplicationUsage::ENVIRONMENTAL_VARIABLE,"OSG_TEXT_INCREMENTAL_SUBLOADING ","ON | OFF"); +#define FIXED_FUNCTION defined(OSG_GL_FIXED_FUNCTION_AVAILABLE) +#define SHADERS_GL3 (defined(OSG_GL3_AVAILABLE) || defined(OSG_GLES3_AVAILABLE)) +#define SHADERS_GL2 !FIXED_FUNCTION && !SHADERS_GL3 + + +#if SHADERS_GL3 +static const char* gl3_TextVertexShader = { + "#version 330 core\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 330 core\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).r);\n" + " else color = vertexColor;\n" + "}\n" +}; + +#endif + + +#if SHADERS_GL2 +static const char* gl2_TextVertexShader = { + "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 = { + "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" +}; +#endif osg::ref_ptr& Font::getDefaultFont() { @@ -240,14 +301,40 @@ Font::Font(FontImplementation* 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) +#if FIXED_FUNCTION + + OSG_NOTICE<<"Font::Font() Fixed function pipeline"<setTextureMode(0, GL_TEXTURE_2D, osg::StateAttribute::ON); #endif +#if SHADERS_GL3 + + OSG_NOTICE<<"Font::Font() Setting up GL3 compatible shaders"< 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)); + +#elif SHADERS_GL2 + + OSG_NOTICE<<"Font::Font() Setting up GL2 compatible shaders"< 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)); + +#endif + char *ptr; if( (ptr = getenv("OSG_MAX_TEXTURE_SIZE")) != 0) { diff --git a/src/osgText/Text3D.cpp b/src/osgText/Text3D.cpp index 17bbe4282..ce94840fb 100644 --- a/src/osgText/Text3D.cpp +++ b/src/osgText/Text3D.cpp @@ -491,6 +491,9 @@ void Text3D::drawImplementation(osg::RenderInfo& renderInfo) const // ** apply this new modelview matrix state.applyModelViewMatrix(modelview); + // workaround for GL3/GL2 + if (state.getUseModelViewAndProjectionUniforms()) state.applyModelViewAndProjectionUniformsIfRequired(); + // OSG_NOTICE<<"New state.applyModelViewMatrix() "<applyMode(GL_NORMALIZE, true); + osg::State::ApplyModeProxy applyNormalizeMode(state, GL_NORMALIZE, true); + osg::State::ApplyModeProxy applyLightingMode(state, GL_LIGHTING, true); #endif + const osg::StateSet* frontStateSet = getStateSet(); const osg::StateSet* wallStateSet = getWallStateSet(); const osg::StateSet* backStateSet = getBackStateSet(); @@ -574,6 +581,9 @@ void Text3D::drawImplementation(osg::RenderInfo& renderInfo) const { // restore the previous modelview matrix state.applyModelViewMatrix(previous_modelview); + + // workaround for GL3/GL2 + if (state.getUseModelViewAndProjectionUniforms()) state.applyModelViewAndProjectionUniformsIfRequired(); } }