实现画中画
在Ogre里摄像机Camera和视口Viewport是一一对应的关系,普通情况下包含一个摄像机对应一个视口,我们只要添加摄像机和与之对应的视口就OK了!
bool
BaseApplication::setup(
void
)
{
mRoot = new Root();
// 设置资源
setupResources();
// 配置渲染窗口
bool carryOn = configure();
if ( ! carryOn)
return false ;
// 创建场景管理器
chooseSceneManager();
// 创建摄像机
createCamera();
// 创建视口
createViewports();
// 设置缺省 mipmap 等级
TextureManager::getSingleton().setDefaultNumMipmaps( 5 );
// 创建所有资源监听器(为了加载屏幕)
createResourceListener();
// 加载资源
loadResources();
// 创建场景
createScene();
// 创建帧监听器
createFrameListener();
return true ;
}
{
mRoot = new Root();
// 设置资源
setupResources();
// 配置渲染窗口
bool carryOn = configure();
if ( ! carryOn)
return false ;
// 创建场景管理器
chooseSceneManager();
// 创建摄像机
createCamera();
// 创建视口
createViewports();
// 设置缺省 mipmap 等级
TextureManager::getSingleton().setDefaultNumMipmaps( 5 );
// 创建所有资源监听器(为了加载屏幕)
createResourceListener();
// 加载资源
loadResources();
// 创建场景
createScene();
// 创建帧监听器
createFrameListener();
return true ;
}
其中,createCamera(); // 创建摄像机 和 createViewports(); // 创建视口 是我们需要关心的。
createCamera():
void
BaseApplication::createCamera(
void
)
{
// 主窗口摄像机
mCamera_1 = mSceneMgr -> createCamera( " Cam_1 " );
mCamera_1 -> setPosition(Vector3( 0 , 0 , 300 ));
mCamera_1 -> lookAt(Vector3( 0 , 0 , - 300 ));
mCamera_1 -> setNearClipDistance( 5 );
// 画中画摄像机
mCamera_2 = mSceneMgr -> createCamera( " Cam_2 " );
mCamera_2 -> setPosition(Vector3( 100 , 100 , 300 ));
mCamera_2 -> lookAt(Vector3( - 100 , - 100 , - 300 ));
mCamera_2 -> setNearClipDistance( 5 );
}
{
// 主窗口摄像机
mCamera_1 = mSceneMgr -> createCamera( " Cam_1 " );
mCamera_1 -> setPosition(Vector3( 0 , 0 , 300 ));
mCamera_1 -> lookAt(Vector3( 0 , 0 , - 300 ));
mCamera_1 -> setNearClipDistance( 5 );
// 画中画摄像机
mCamera_2 = mSceneMgr -> createCamera( " Cam_2 " );
mCamera_2 -> setPosition(Vector3( 100 , 100 , 300 ));
mCamera_2 -> lookAt(Vector3( - 100 , - 100 , - 300 ));
mCamera_2 -> setNearClipDistance( 5 );
}
createViewports():
void
BaseApplication::createViewports(
void
)
{
// 主窗口
Viewport * vp_1 = mWindow -> addViewport( mCamera_1 );
vp_1 -> setBackgroundColour( ColourValue( 0 , 0 , 0 ) );
mCamera_1 -> setAspectRatio(
Real( vp_1 -> getActualWidth() ) / Real( vp_1 -> getActualHeight() ) );
// 画中画
Viewport * vp_2 = mWindow -> addViewport( mCamera_2, 1 , 0.7 , 0.05 , 0.25 , 0.25 );
vp_2 -> setBackgroundColour( ColourValue( 0 , 0 , 0 ) );
vp_2 -> setOverlaysEnabled( false );
mCamera_2 -> setAspectRatio(
Real( vp_2 -> getActualWidth() ) / Real(vp_2 -> getActualHeight() ) );
}
{
// 主窗口
Viewport * vp_1 = mWindow -> addViewport( mCamera_1 );
vp_1 -> setBackgroundColour( ColourValue( 0 , 0 , 0 ) );
mCamera_1 -> setAspectRatio(
Real( vp_1 -> getActualWidth() ) / Real( vp_1 -> getActualHeight() ) );
// 画中画
Viewport * vp_2 = mWindow -> addViewport( mCamera_2, 1 , 0.7 , 0.05 , 0.25 , 0.25 );
vp_2 -> setBackgroundColour( ColourValue( 0 , 0 , 0 ) );
vp_2 -> setOverlaysEnabled( false );
mCamera_2 -> setAspectRatio(
Real( vp_2 -> getActualWidth() ) / Real(vp_2 -> getActualHeight() ) );
}
addViewport中我们控制画中画视口在主窗口中的位置和大小(注意这里是0~1的取值范围,类似于贴图坐标)
Viewport* addViewport(Camera* cam, int ZOrder = 0, float left = 0.0f, float top = 0.0f , float width = 1.0f, float height = 1.0f);
同一场景管理器内ZOrder的值不可以重复,否则报错.