2
votes

I'm trying to make an animation with openscenegraph but I can't load the texture with this code :

osg::ref_ptr<osg::Node> createSceneGraph(){
    osg::ref_ptr<osg::Group> root = new osg::Group;
    osg::ref_ptr<osg::MatrixTransform> m1 = new osg::MatrixTransform();
    root->addChild(m1.get());

    // creer sphere
    osg::ref_ptr<osg::Geode> terre = new osg::Geode();
    terre->addDrawable( new osg::ShapeDrawable(new osg::Sphere(osg::Vec3(0, 0, 4), 2)));
    m1->addChild(terre);

    // ajouter texture
    osg::Image* im = new osgDB::readImageFile("land_shallow_topo_2048.jpg");
    osg::Texture2D* tex = new osg::setImage(im);

    return root;
}

int main( )
{
    osg::ref_ptr<osg::Node> root = createSceneGraph();
    osg::ref_ptr<osgViewer::Viewer> viewer = new osgViewer::Viewer;
    viewer->setUpViewInWindow( 32, 32, 1024, 768 );
    viewer->setSceneData(root.get());
    osg::ref_ptr<osg::Camera> camera = new osg::Camera;
    camera->setProjectionMatrix(osg::Matrix::perspective(60.0,1.333,0.01,100000.0));
    camera->setViewMatrix( osg::Matrix::identity() );
    viewer->setCamera(camera);
    return viewer->run();

}

This is the error :

 /home/yoyo/TP_avion_yoyo/main.cpp:39: erreur : cannot convert 'int*' to 'osg::Image*' in initialization

The image is correctly placed in my directory, and I have no idea why it doesn't work, since I have compiled another project that utilizes textures.

1
ReadImageFile is returning pointer to Image. You shouldn't call new on it - sajas
Yeah, that works! Merci, mec! - user3302089
@sajas If your advice worked, post it as a comment so the OP can accept it. - Adri C.S.
@AdriC.S. Thanks. I have added it. - sajas

1 Answers

2
votes

ReadImageFile method is returning a pointer to the image. So you can directly assign the returned pointer to your variable: im.

osg::Image* im = osgDB::readImageFile("land_shallow_topo_2048.jpg");