Pergunta

I am trying to do texture map a quad geometry object generated by createTexturedQuadGeometry with a texture that I load from an image. I then add this drawable to a node, add that node to root and render the hierarchy.

The code below is how I do it. The code compiles and runs but I only get a blank black screen instead of the specified image. Can someone please point out what is wrong?

int main(int argc, char** argv) 
{
   osg::ref_ptr<osg::Group> root = new osg::Group;

    osg::ref_ptr<osg::Texture2D> testTexture = new osg::Texture2D;
    osg::ref_ptr<osg::Image> testImage = osgDB::readImageFile("testImage.png");
    assert(testImage.valid());
    int viewWidth = testImage->s();
    int viewHeight = testImage->t();
    testTexture->setImage(testImage);

    osg::ref_ptr<osg::Geometry> pictureQuad = osg::createTexturedQuadGeometry(osg::Vec3(0.0f,0.0f,0.0f),
                                                                              osg::Vec3(viewWidth,0.0f,0.0f),
                                                                              osg::Vec3(0.0f,0.0f,viewHeight),
                                                                              0.0f,
                                                                              viewWidth,
                                                                              viewHeight,
                                                                              1.0f);

    pictureQuad->getOrCreateStateSet()->setTextureAttributeAndModes(0, testTexture.get());
    pictureQuad->getOrCreateStateSet()->setMode(GL_DEPTH_TEST, osg::StateAttribute::ON);
    osg::ref_ptr<osg::Geode> textureHolder = new osg::Geode();
    textureHolder->addDrawable(pictureQuad);
    root->addChild(textureHolder);

    osgViewer::Viewer viewer;
    viewer.setSceneData(root.get());
    viewer.run();
} 
Foi útil?

Solução

So, I happened to figure out the error.

createTexturedQuadGeometry expects normalised texture coordinates.

So,

osg::ref_ptr<osg::Geometry> pictureQuad = osg::createTexturedQuadGeometry(osg::Vec3(0.0f,0.0f,0.0f),
                                                                              osg::Vec3(viewWidth,0.0f,0.0f),
                                                                              osg::Vec3(0.0f,0.0f,viewHeight),
                                                                              0.0f,
                                                                              0.0f,
                                                                              1.0f,
                                                                              1.0f);

solves the problem.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top