[osg-users] Text colors in shaders

120 views
Skip to first unread message

Werner Modenbach

unread,
Feb 24, 2017, 6:58:12 AM2/24/17
to OpenSceneGraph Users
Dear all,

my project needs gl3 features and so I use the following methods:
 
camera->getGraphicsContext()->getState()->setUseModelViewAndProjectionUniforms(useGL3);
camera->getGraphicsContext()->getState()->setUseVertexAttributeAliasing(useGL3);

Of course I need my own shaders then.
But I also like having built-in functionality like StatsHandler.
To set a simple shader for the StatsHandlers camera I do like that:

osgViewer::StatsHandler * sthd = new osgViewer::StatsHandler;
view->addEventHandler(sthd);
if (useGL3) {
    stateSet = sthd->getCamera()->getOrCreateStateSet();
    stateSet->getOrCreateUniform(BASE_TEXTURE_UNIFORM, osg::Uniform::INT_SAMPLER_2D)->set(BASE_TEXTURE_UNIT);
    stateSet->setTextureAttributeAndModes(BASE_TEXTURE_UNIT, textur_weiss.get(), osg::StateAttribute::ON);
    installDefaultShader(stateSet);
}

    The shaders are really simple:
VERTEX
uniform mat4 osg_ModelViewProjectionMatrix;
uniform mat4 osg_ModelViewMatrix;
uniform mat3 osg_NormalMatrix;
uniform vec3 lightPosition0 = vec3(0.0f, 0.0f, 1.0f);

in vec4 osg_Vertex;
in vec4 osg_Normal;
in vec4 osg_Color;
in vec4 osg_MultiTexCoord0;
out vec3 normal;
out vec3 lightDir;
out vec4 vertexColor;
out vec2 textureCoord;
void main() {
    normal    = normalize(osg_NormalMatrix * osg_Normal.xyz);
    vec3 vertexPos = vec3(osg_ModelViewMatrix * osg_Vertex);
    lightDir = normalize(lightPosition0 - vertexPos);
    vertexColor = osg_Color;
    textureCoord = osg_MultiTexCoord0.xy;
    gl_Position = osg_ModelViewProjectionMatrix * osg_Vertex;
}

FRAGMENT
uniform sampler2D 	baseTexture;

in vec3 normal;
in vec3 lightDir;
in vec4 vertexColor;
in vec2 textureCoord;
out vec4 fragData;
void main() {
    vec4 textureColor = texture2D(baseTexture, textureCoord);
    fragData = vertexColor * textureColor;
}

That works fine for the graphics but all text is black. Obviously text does not set osg_Color .
Unfortunately I failed discovering the reason in the sources of osg.

Can anyone give me a hint on how to solve this?

Many thanks in advance

- Werner -

Sebastian Messerschmidt

unread,
Feb 24, 2017, 7:16:36 AM2/24/17
to OpenSceneGraph Users


Am 2/24/2017 um 12:57 PM schrieb Werner Modenbach:
> Dear all,
>
> my project needs gl3 features and so I use the following methods:
>
>
> camera->getGraphicsContext()->getState()->setUseModelViewAndProjectionUniforms(useGL3);
>
> camera->getGraphicsContext()->getState()->setUseVertexAttributeAliasing(useGL3);
>
> Of course I need my own shaders then.
> But I also like having built-in functionality like StatsHandler.
> To set a simple shader for the StatsHandlers camera I do like that:
>
> osgViewer::StatsHandler*sthd=newosgViewer::StatsHandler;
>
> view->addEventHandler(sthd);
>
> if(useGL3){
> stateSet=sthd->getCamera()->getOrCreateStateSet();
>
> stateSet->getOrCreateUniform(BASE_TEXTURE_UNIFORM,osg::Uniform::INT_SAMPLER_2D)->set(BASE_TEXTURE_UNIT);
>
> stateSet->setTextureAttributeAndModes(BASE_TEXTURE_UNIT,textur_weiss.get(),osg::StateAttribute::ON);
osg_Color is an alias for the color-vertex attribute. Colors are set in
void Text::drawForegroundText ~line 1628. So there should be colors.

My fragment shader looks like this:

#version 440
#extension GL_ARB_enhanced_layouts : enable
#extension GL_ARB_separate_shader_objects : enable

layout (location=0) out vec4 FragmentData;

layout(location=1) in block
{
mediump vec2 tex_coord;
mediump vec4 color;
} In;


uniform sampler2D osg_Texture;

void main()
{

vec4 diffuse_color = In.color;

diffuse_color *= texture2D(osg_Texture, In.tex_coord).a;

FragmentData = diffuse_color;
}

which seems to work. As you can see I'm using the alpha channel only.

Cheers
Sebastian


> Unfortunately I failed discovering the reason in the sources of osg.
>
> Can anyone give me a hint on how to solve this?
>
> Many thanks in advance
>
> - Werner -
>
>
>
> _______________________________________________
> osg-users mailing list
> osg-...@lists.openscenegraph.org
> http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org
>
_______________________________________________
osg-users mailing list
osg-...@lists.openscenegraph.org
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org

Werner Modenbach

unread,
Feb 24, 2017, 9:30:35 AM2/24/17
to OpenSceneGraph Users
Hi Sebastian,

that's great. It works with the shader you suggested.
Strange is, that just using vertex color doesn't work. It seems alpha is set to transparent (0).
Also a texture is set but only alpha has a meaning. Texture color is black.
That looks odd to me.

But anyway, I'm happy now.

- Werner -
--
TEXION Software Solutions, Rotter Bruch 26a, D-52068 Aachen
Phone: +49 241 475757-0
Fax: +49 241 475757-29
Web: http://texion.eu
eMail: in...@texion.eu

Robert Osfield

unread,
Feb 24, 2017, 10:09:57 AM2/24/17
to OpenSceneGraph Users
On 24 February 2017 at 14:30, Werner Modenbach
<Werner.M...@texion.eu> wrote:
> that's great. It works with the shader you suggested.
> Strange is, that just using vertex color doesn't work. It seems alpha is set
> to transparent (0).
> Also a texture is set but only alpha has a meaning. Texture color is black.
> That looks odd to me.

If you build the OSG against the GL3 core profile it maps the texture
format to GL_RED rather than GL_ALPHA as the later is no longer
supported. The graphics hardware doesn't expand GL_RED in the same
way as GL_ALPHA so it can cause problems. It's a bit of pain having
GL pull the rug from GL_ALPHA as it has a particularly useful mapping.
Potentially one could use texture swizzle to get round this but it's
not support on all GL versions.

Robert.

Werner Modenbach

unread,
Feb 24, 2017, 10:16:35 AM2/24/17
to OpenSceneGraph Users
Hi Robert,

thanks for the good explanations - as we are used from your side :-)
I cannot switch completely to gl3 because I have customers whose hardware/driver doesn't support it
So at the start of the application I analyze it and use the

camera->getGraphicsContext()->getState()->setUseModelViewAndProjectionUniforms(useGL3);
camera->getGraphicsContext()->getState()->setUseVertexAttributeAliasing(useGL3);
accordingly. It's for sure a burden but what can I do?

Thanks for your excellent support!

- Werner -

Robert Osfield

unread,
Feb 24, 2017, 10:33:47 AM2/24/17
to OpenSceneGraph Users
On 24 February 2017 at 15:15, Werner Modenbach
<Werner.M...@texion.eu> wrote:
> Hi Robert,
>
> thanks for the good explanations - as we are used from your side :-)
> I cannot switch completely to gl3 because I have customers whose
> hardware/driver doesn't support it
> So at the start of the application I analyze it and use the
>
> camera->getGraphicsContext()->getState()->setUseModelViewAndProjectionUniforms(useGL3);
> camera->getGraphicsContext()->getState()->setUseVertexAttributeAliasing(useGL3);
>
> accordingly. It's for sure a burden but what can I do?

I don't know witthout testing your specific shader and scene graph setup.

For your own work, if you can't switching completely to GL3 why not
just use standard OSG build and not bother with the aliasing. For
decent GL drivers you should get access to modern GL features via the
OSG extension system, you don't need to go explicitly enabling GL3/GL4
features. the only exception is OSX and their CL3 core profile mess.
Reply all
Reply to author
Forward
0 new messages