将osgViewer相机与图像对齐

时间:2013-10-06 10:50:19

标签: openscenegraph

作为渲染纹理的先驱,我试图简单地将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;
}

然而,结果显示我的观点完全偏斜。有人可以指出我的代码中的错误吗?

1 个答案:

答案 0 :(得分: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

你的眼睛在地上,看着你的图像的中心,这是你的眼睛,所以你自然会想到一个倾斜的图像。

尝试将眼睛置于高度图像 - > t()* 0.5,以便视图直接显示。