[Solved] Problem with Texture2D on osg::Geometry

classic Classic list List threaded Threaded
3 messages Options
Nikkitta Nikkitta
Reply | Threaded
Open this post in threaded view

[Solved] Problem with Texture2D on osg::Geometry

This post was updated on .
I am trying to draw simple geometries above the Earth's surface and apply some textures to them.
I don't want to use osgEarth::Annotation::Feature because geometries will be updated frequently and, as far as I understood, osg::Geometry is much faster in this case.

Currently I've down-scaled my problem to the simplest quad (rectangle parallel to the surface) and the texture still doesn't apply to geometry. Here is the code which is not working:
// Setup GeoPoints and convert them to world coordinates
osgEarth::GeoPoint p1, p2, p3, p4;
osg::Vec3d _p1, _p2, _p3, _p4;
p1.set(_scene->getMapNode()->getMapSRS()->getGeographicSRS(), 2, -2, 60000, osgEarth::ALTMODE_ABSOLUTE);
p2.set(_scene->getMapNode()->getMapSRS()->getGeographicSRS(), 6, -2, 60000, osgEarth::ALTMODE_ABSOLUTE);
p3.set(_scene->getMapNode()->getMapSRS()->getGeographicSRS(), 6, 2, 60000, osgEarth::ALTMODE_ABSOLUTE);
p4.set(_scene->getMapNode()->getMapSRS()->getGeographicSRS(), 2, 2, 60000, osgEarth::ALTMODE_ABSOLUTE);

// Add vertices of geometry
osg::ref_ptr<osg::Vec3Array> vertices = new osg::Vec3Array;

osg::ref_ptr<osg::Vec3Array> normals = new osg::Vec3Array;
normals->push_back(osg::Vec3(1.0f, 0.0f, 0.0f));

// Add texture coordinates
osg::ref_ptr<osg::Vec2Array> texcoords = new osg::Vec2Array;
texcoords->push_back(osg::Vec2(0.0f, 0.0f));
texcoords->push_back(osg::Vec2(0.0f, 1.0f));
texcoords->push_back(osg::Vec2(1.0f, 1.0f));
texcoords->push_back(osg::Vec2(1.0f, 0.0f));

// Create osg::Geometry
osg::ref_ptr<osg::Geometry> quad = new osg::Geometry;
quad->setTexCoordArray(0, texcoords.get());
quad->addPrimitiveSet(new osg::DrawArrays(GL_QUADS, 0, 4));

// Create Texture2D
osg::ref_ptr<osg::Texture2D> texture = new osg::Texture2D;
// From OSG example: protect from being optimized away as static state.

// Load and set image of texture
osg::ref_ptr<osg::Image> image = osgDB::readImageFile(QCoreApplication::applicationDirPath().toStdString() + "/../../../../data/area/lz.rgb");

// Create osg::Geode, add geometry and set texture
osg::ref_ptr<osg::Geode> geodeQuad = new osg::Geode;
geodeQuad->getOrCreateStateSet()->setTextureAttributeAndModes(0, texture.get(), osg::StateAttribute::ON);

// Add to root group. Not to the MapNode(!)

As a result I see gray rectangle without texture.
I guess that this problem has a fairly simple solution, but I spent some time and I still haven't managed to fix it.
Best regards,
Nikita Petrov
gwaldron gwaldron
Reply | Threaded
Open this post in threaded view

Re: Problem with Texture2D on osg::Geometry

Depending on where your node is in the scene graph, you might need to generate shaders for it.
Please read this:


Hope this helps.
Glenn Waldron / Pelican Mapping
Nikkitta Nikkitta
Reply | Threaded
Open this post in threaded view

Re: Problem with Texture2D on osg::Geometry

Perfect, that helped. I've just added this line:
osgEarth::Registry::shaderGenerator().run( myNode );
and I have my texture now.
Thanks a lot!
Best regards,
Nikita Petrov