OpenScenegraph:纹理映射四元组

时间:2013-02-18 13:09:47

标签: openscenegraph

我正在尝试使用我从图像加载的纹理来创建由createTexturedQuadGeometry生成的四边形几何对象的纹理贴图。然后,我将此drawable添加到节点,将该节点添加到root并呈现层次结构。

下面的代码是我如何做到的。代码编译并运行但我只得到一个空白的黑屏而不是指定的图像。有人可以指出出了什么问题吗?

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();
} 

1 个答案:

答案 0 :(得分:5)

所以,我碰巧找出了错误。

createTexturedQuadGeometry需要规范化纹理坐标。

所以,

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);

解决了这个问题。