White rectangle instead of Texture

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

White rectangle instead of Texture

Hi,
i'm trying to show a texture but there is only a white rectangle in the scene. I modified the annotation example:

#include <osgEarth/MapNode>
#include <osgEarthUtil/EarthManipulator>
#include <osgEarthUtil/ExampleResources>
#include <osgEarthSymbology/GeometryFactory>
#include <osgEarth/GLUtils>
#include <osgViewer/Viewer>

using namespace osgEarth;
using namespace osgEarth::Util;

int main(int argc, char** argv)
{
        osg::Group* root = new osg::Group();
        osg::ArgumentParser arguments(&argc, argv);
        osgViewer::Viewer viewer(arguments);
        viewer.setCameraManipulator(new EarthManipulator());
        osg::Node* node = MapNodeHelper().load(arguments, &viewer);

        if(!node)
                return -1;

        root->addChild(node);
        MapNode* mapNode = MapNode::findMapNode(node);

        if(!mapNode)
                return -1;

        osg::Group* annoGroup = new osg::Group();
        MapNode::get(node)->addChild(annoGroup);
        const SpatialReference* geoSRS = mapNode->getMapSRS()->getGeographicSRS();

        osgEarth::GeoPoint input;
        osg::Vec3d output;

        osg::Vec3Array* vertices = new osg::Vec3Array(4);
        input.set(geoSRS, -80.0, 25.0, 10000.0, osgEarth::ALTMODE_ABSOLUTE);
        input.toWorld(output);
        (*vertices)[0] = output;

        input.set(geoSRS, -80, 25, 90000.0, osgEarth::ALTMODE_ABSOLUTE);
        input.toWorld(output);
        (*vertices)[1] = output;

        input.set(geoSRS, -80.29, 25.8, 90000.0, osgEarth::ALTMODE_ABSOLUTE);
        input.toWorld(output);
        (*vertices)[2] = output;

        input.set(geoSRS, -80.29, 25.8, 10000.0, osgEarth::ALTMODE_ABSOLUTE);
        input.toWorld(output);
        (*vertices)[3] = output;

        osg::Geometry* geom = new osg::Geometry;
        geom->setVertexArray(vertices);

        osg::Image* image = osgDB::readImageFile("flag_us.png");
        if(image)
        {
                osg::Texture2D* texture = new osg::Texture2D;
                texture->setFilter(osg::Texture::MIN_FILTER, osg::Texture::LINEAR_MIPMAP_LINEAR);
                texture->setFilter(osg::Texture::MAG_FILTER, osg::Texture::LINEAR);
                texture->setResizeNonPowerOfTwoHint(false);
                texture->setImage(image);
                texture->setUnRefImageDataAfterApply(false);

                osg::StateSet* dstate = new osg::StateSet;
                dstate->setDataVariance(osg::Object::DYNAMIC);
                dstate->setMode(GL_CULL_FACE, osg::StateAttribute::OFF);
                GLUtils::setLighting(dstate, osg::StateAttribute::OFF);
                dstate->setTextureAttributeAndModes(0, texture, osg::StateAttribute::ON);
                geom->setUseVertexBufferObjects(true);
                geom->setStateSet(dstate);

                osg::Vec2Array* texcoords = new osg::Vec2Array(4);
                (*texcoords)[0].set(0.0f, 0.0f);
                (*texcoords)[1].set(1.0f, 0.0f);
                (*texcoords)[2].set(1.0f, 1.0f);
                (*texcoords)[3].set(0.0f, 1.0f);
                geom->setTexCoordArray(0, texcoords);
               
                osg::Vec4Array* colors = new osg::Vec4Array(osg::Array::BIND_OVERALL, 1);
                (*colors)[0].set(1.0f, 1.0f, 1.0f, 1.0f);
                geom->setColorArray(colors);
                GLushort indices[] = { 0,1,2,0,2,3 };
                geom->addPrimitiveSet(new osg::DrawElementsUShort(GL_TRIANGLES, 6, indices));

                osg::Geode* geode = new osg::Geode;
                geode->addDrawable(geom);
                annoGroup->addChild(geode);
        }

        // initialize the viewer:    
        viewer.setSceneData(root);
        viewer.getCamera()->setSmallFeatureCullingPixelSize(-1.0f);
        return viewer.run();
}

What is the problem here? I tried many different things, but without success...

Thanks for your help
ElRudi
gwaldron gwaldron
Reply | Threaded
Open this post in threaded view
|

Re: White rectangle instead of Texture

Hi ElRudi,

You probably need to run the shader generator on your new geometry.

osgEarth::Registry::shaderGenerator().run(node);

Hope that helps. As a side note, the osg::Geode class is deprecated - you can just use osg::Geometry directly as a node now.
Glenn Waldron / Pelican Mapping
ElRudi ElRudi
Reply | Threaded
Open this post in threaded view
|

Re: White rectangle instead of Texture

This helped. Thank you Glenn.