2013-10-06 41 views
1

作爲渲染到紋理的先驅,我試圖簡單地將osgViewer的相機對齊到紋理映射平面。將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; 
} 

然而,結果顯示我認爲是完全傾斜。有人可以指出我的代碼中的錯誤嗎?

回答

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,這樣視圖是直接的。