我正在尝试使用我从图像加载的纹理对由 createTexturedQuadGeometry 生成的四边形几何对象进行纹理映射。然后我将这个drawable添加到一个节点,将该节点添加到根并渲染层次结构。
下面的代码是我的做法。代码编译并运行,但我只得到一个空白的黑屏而不是指定的图像。有人可以指出什么是错的吗?
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();
}