1

作为渲染到纹理的先驱,我试图简单地将 osgViewer 的相机与纹理映射平面对齐。

这是我使用的相同代码:

int main()
{
    osgViewer::Viewer viewer;

    osg::ref_ptr<osg::Image> image = osgDB::readImageFile("path//to//file.png");
    if (!image.valid())
    {
    assert(false);
        return 1;
    }


    osg::ref_ptr<osg::Geometry> pictureQuad = osg::createTexturedQuadGeometry(osg::Vec3(0.f,0.f,0.f),
                                                                              osg::Vec3(image->s(),0.f,0.f),
                                                                              osg::Vec3(0.f,0.f,image->t()),
                                                                              0.f,
                                                                              0.f,
                                                                              image->s(),
                                                                              image->t());

    osg::ref_ptr<osg::TextureRectangle> textureRect = new osg::TextureRectangle(image);

    textureRect->setFilter(osg::Texture::MIN_FILTER, osg::Texture::LINEAR);
    textureRect->setFilter(osg::Texture::MAG_FILTER, osg::Texture::LINEAR);
    textureRect->setWrap(osg::Texture::WRAP_S, osg::Texture::CLAMP_TO_EDGE);
    textureRect->setWrap(osg::Texture::WRAP_T, osg::Texture::CLAMP_TO_EDGE);
    pictureQuad->getOrCreateStateSet()->setTextureAttributeAndModes(0, textureRect.get(), 
                                    osg::StateAttribute::ON);
    pictureQuad->getOrCreateStateSet()->setMode(GL_DEPTH_TEST, osg::StateAttribute::ON);
    osg::ref_ptr<osg::Geode> geode = new osg::Geode();
    geode->setDataVariance(osg::Object::DYNAMIC);
    geode->addDrawable(pictureQuad.get());
    osg::StateSet *state = geode->getOrCreateStateSet();
    state->setMode( GL_LIGHTING, osg::StateAttribute::PROTECTED | osg::StateAttribute::OFF );

    viewer.setSceneData(geode);

    osg::ref_ptr<osg::Camera> camera = viewer.getCamera();

    while( !viewer.done() )
    {
        camera->setReferenceFrame(osg::Transform::ABSOLUTE_RF);
        camera->setProjectionMatrix(osg::Matrix::ortho2D(0.f, image->s(), 0.f, image->t()));
        camera->setViewMatrixAsLookAt(osg::Vec3f(0.f, -100.f, 0.f),
                                      osg::Vec3f(image->s()*0.5, 0.f, image->t()*0.5f),
                                      osg::Vec3f(0.f, 0.f, 1.f));
        viewer.frame();
    }
    return 0;
}

然而,结果向我展示了一个完全歪曲的观点。有人可以指出我的代码中的错误吗?

4

1 回答 1

0

 camera->setViewMatrixAsLookAt(osg::Vec3f(0.f, -100.f, 0.f), // eye
                               osg::Vec3f(image->s()*0.5, 0.f, image->t()*0.5f), // center
                               osg::Vec3f(0.f, 0.f, 1.f)); // up vector

你的眼睛在地面上,看着你的图像的中心,从它上面,所以你自然会期待一个倾斜的图像。

试着把眼睛放在 image->t()*0.5 的高度,这样视图就可以直视了。

于 2013-10-07T21:41:13.143 回答