As a precursor to render to texture, I am trying to simply align the osgViewer's camera to a texture mapped plane.
This is the code I employ for the same:
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;
}
However, the results show me a view that is completely skewed. Can someone please point out the bug in my code?
In
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
Your eye is on the ground, looking at the center of your image, which is up from it, so you would naturally expect a slanted image.
Try putting the eye at height image->t()*0.5 so the view is straight-on.