透视投影与正视投影图:
正视投影正上方观察牛的代码:
#include #include#includeosg::Camera* createBirdsEye( const osg::BoundingSphere&bs )
{
osg::ref_ptr<:camera> camera = newosg::Camera;
camera->setClearMask( GL_COLOR_BUFFER_BIT |GL_DEPTH_BUFFER_BIT );
camera->setReferenceFrame( osg::Transform::ABSOLUTE_RF );double viewDistance = 2.0 *bs.radius();double znear = viewDistance -bs.radius();double zfar = viewDistance +bs.radius();float top =bs.radius();float right =bs.radius();float bottom =top;float left =right;//osg左手系坐标(z+向上,x+向右,y+向里)
camera->setProjectionMatrixAsOrtho( -left, right, -bottom, top, znear, zfar );//设置正投影//牛的身长变短了(在视口大小没有变化的情况下,加大left和right(原本是6.3),牛的左右相对于裁剪区域来说,就缩小了,我是这么理解。)//camera->setProjectionMatrixAsOrtho( -10, 10, -bottom, top, znear, zfar );//牛的身长变窄了(同上理)//camera->setProjectionMatrixAsOrtho( -left, right, -20, 20, znear, zfar );//改变了znear和zfar,好像没什么变化,为什么呢,待找答案。****//camera->setProjectionMatrixAsOrtho( -left, right, -bottom, top, 1, 1 );
/********* openGL解释***************************************************************************
setProjectionMatrixAsOrtho(left,right,bottom,top,zNear,zFar);
left和right表示最小和最大x值,top和bottom是最小和最大的y值,near和far是最小和最大的z值。
left:裁剪区域最左边的坐标。
right:裁剪区域最右边的坐标。
bottom:最底部的坐标。
top:最顶部的坐标。
near:从原点距离观察者的最小位置。
far:从原点距离观察者的最大位置。
**************************************************************************************************/osg::Vec3d upDirection(0.0,1.0,0.0);
osg::Vec3d viewDirection(0.0,0.0,1.0);
osg::Vec3d center=bs.center();
osg::Vec3d eyePoint= center + viewDirection *viewDistance;//从前面看牛//eyePoint.set(0.77,-10,0.5);//从牛头看牛//upDirection.set(0.0,0.0,1.0);//eyePoint.set(10,-0.4,0.5);//从牛下巴看牛//upDirection.set(1.0,0.0,-1.0);//eyePoint.set(100,-0.4,-100);
camera->setViewMatrixAsLookAt( eyePoint, center, upDirection );//相机位置(眼睛位置,眼睛看到的场景中心位置,视点向上的量)//eyePoint(0.77,-0.4,12.7) center(0.77,-0.4,0.0) upDirection(0,1,0) zfar = 19,redius = 6.3
returncamera.release();
}int main( int argc, char**argv )
{
osg::ArgumentParser arguments(&argc, argv );
osg::Node* model =osgDB::readNodeFiles( arguments );if ( !model ) model = osgDB::readNodeFile( "cow.osg");
osg::Camera* camera = createBirdsEye( model->getBound() );
camera->addChild( model );
osgViewer::Viewer viewer;
viewer.setSceneData( camera