After some trial and error, osgText renders fine when explicitly
setting up shaders. Here's a bare minimum working example in case
anyone else runs into this.

Scene Graph:

    // text geometry
    std::string textStr("Hello World");
    osg::ref_ptr<osgText::Text> myText = new osgText::Text;
    myText->setFont("DroidSans-Bold.ttf");
    myText->setCharacterSize(20.0f);
    myText->setText(textStr);

    // text node
    osg::ref_ptr<osg::Geode> textGeode = new osg::Geode;
    textGeode->addDrawable(myText);

    // text shaders
    osg::ref_ptr<osg::Program> shProgram = new osg::Program;
    shProgram->setName("TextShader");

    std::string vShader = std::string("#version 120\n") +
            readFileAsString("vertexshader.glsl");
    shProgram->addShader(new osg::Shader(osg::Shader::VERTEX,vShader));

    std::string fShader = std::string("#version 120\n") +
            readFileAsString("fragmentshader.glsl");
    shProgram->addShader(new osg::Shader(osg::Shader::FRAGMENT,fShader));

    osg::ref_ptr<osg::Uniform> textColor = new
osg::Uniform("MaterialColor",osg::Vec4(0,1,1,1));
    osg::ref_ptr<osg::Uniform> textTexture = new osg::Uniform("GlyphTexture",0);

    osg::StateSet *ss = textGeode->getOrCreateStateSet();
    ss->addUniform(textColor);
    ss->addUniform(textTexture);
    ss->setAttributeAndModes(shProgram,osg::StateAttribute::ON);

    osg::ref_ptr<osg::Group> nodeRoot = new osg::Group;
    nodeRoot->addChild(textGeode);


Vertex Shader:
#version 120

varying mediump vec4 VertexColor;
varying mediump vec2 TexCoord0;

uniform vec4 MaterialColor;

void main()
{
    gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
    VertexColor = MaterialColor;
    TexCoord0 = gl_MultiTexCoord0.xy;
}


Fragment Shader:

varying vec4 VertexColor;
varying vec2 TexCoord0;

// set to zero by default
uniform sampler2D GlyphTexture;

void main()
{
    gl_FragColor = VertexColor * texture2D(GlyphTexture,TexCoord0).aaaa;
}


Preet
_______________________________________________
osg-users mailing list
[email protected]
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org

Reply via email to