[osg-users] osgText on Android

Akhtar Shamim shamimz at gmail.com
Fri Oct 23 22:18:16 PDT 2015


Dear All

I am trying to display a simple text on Android device using osgText. No
matter what I  do I can't get to display the text out on the screen.

I am using GLES2 build.

Attached is the code. I have debugged and verified that the font is loaded.
The text would just not appear.


In the following function i create some 2D text and add that to the
scenegraph. text <- _overlayGeode <- _overlayCamera <- _rootNode.


namespace
{

static const char gTextVertexShader[] =
"varying vec4 VertexColor; \n"
"varying mediump vec2 TexCoord0; \n"
"uniform vec4 MaterialColor; \n"
"void main() \n"
"{ \n"
"gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex; \n"
"VertexColor = MaterialColor; \n"
"TexCoord0 = gl_MultiTexCoord0.xy; \n"
"} \n";

static const char gTextFragmentShader[] =
"varying vec4 VertexColor; \n"
"varying mediump vec2 TexCoord0; \n"
"uniform sampler2D GlyphTexture; \n"
    "void main() {                             \n"
    " gl_FragColor = texture2D(GlyphTexture,TexCoord0); \n"
    "}                                         \n";
} // end of unnamed namespace


void TerrainApp::initTextOverlays()
{
    _overlayCamera = new osg::Camera;
    _rootNode->addChild(_overlayCamera.get());

    _overlayCamera->setReferenceFrame(osg::Transform::ABSOLUTE_RF);
    _overlayCamera->setProjectionMatrixAsOrtho2D(
    0, vfui::Global::instance()->getTrueWidth(),
    0, vfui::Global::instance()->getTrueHeight()
);

    _overlayCamera->setViewMatrix(osg::Matrix::identity());
    _overlayCamera->setClearMask(GL_DEPTH_BUFFER_BIT);

_overlayCamera->getOrCreateStateSet()->setMode(GL_LIGHTING,osg::StateAttribute::OFF);
    _overlayCamera->setCullingActive(false);
    _overlayCamera->getOrCreateStateSet()->setAttribute(new
osg::Depth(osg::Depth::LESS, 0.0f, .0001f));

    _overlayGeode = new osg::Geode;
    _overlayCamera->addChild(_overlayGeode.get());

    osg::Shader * vshader = new osg::Shader(osg::Shader::VERTEX,
gTextVertexShader );
    osg::Shader * fshader = new osg::Shader(osg::Shader::FRAGMENT,
gTextFragmentShader );

    osg::Program * prog = new osg::Program;
    prog->addShader ( vshader );
    prog->addShader ( 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);

    //set a sample text overlay.
osg::ref_ptr<osgText::Text> text =
TerrainApp::s_createText(osgText::String("Testing"), _datapath);

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

_overlayGeode->addDrawable(text.get());
}

osgText::Text* TerrainApp::s_createText(const osgText::String& str, const
std::string& datapath)
{
// text geometry
std::string textStr("Hello World"); // replace this with str.
osgText::Text* myText = new osgText::Text;

        std::string font_filename = datapath + "/fonts/androidnation.ttf";
        osgText::Font* font = osgText::readFontFile(font_filename);

myText->setFont(font);
myText->setCharacterSize(100.0f);
myText->setText(textStr);

myText->setPosition(osg::Vec3(500.0f, 500.0f, 0.0f));
return myText;

}

Any thing that I have done that is not correct? I am cracking my head. I
 found a couple of  examples / tutorials of osgText on Android using GLES2.
All of them dont seem to work. By right setting up text should be very
simple. Any help or advice will be greatly appreciated.

regards
Shamim
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.openscenegraph.org/pipermail/osg-users-openscenegraph.org/attachments/20151024/0bbc689d/attachment-0002.htm>


More information about the osg-users mailing list