(OGRE菜鸟之旅)二、OGRE中级教程七(RTT)随笔

Intermediate Tutorial 7         Render to texture (RTT)

整体上还是比较简单的。记录下自己的一点理解,方便日后翻阅。


第一部分:

关于渲染到纹理的过程


1、创建要渲染的纹理

【关键要指定TU_RENDERTARGET参数,在创建这个渲染目标纹理的过程中,Ogre会自动调用 Root::getSingleton().getRenderSystem()->attachRenderTarget把这个纹理添加到Root的渲染目标中,也就是说每帧都会渲染到这个纹理。】

First of all, we need to create a texture.

Ogre::TexturePtr rtt_texture = Ogre::TextureManager::getSingleton().createManual("RttTex", Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME, Ogre::TEX_TYPE_2D, mWindow->getWidth(), mWindow->getHeight(), 0, Ogre::PF_R8G8B8, Ogre::TU_RENDERTARGET);

("mWindow" is your Ogre::RenderWindow pointer)

The first parameter to createManual() is the name of the texture, which is commonly named RttTex. The second specifies the resource group, the third the texture type (in our case a 2D texture), the fourth and the fifth the width and height of the texture. You also have to pass the number of mip maps you want as well as the texture format and a usage flag. Concerning the texture format: There are many different ones, but the most simple is the PF_R8G8B8 which will create a 24-bit RGB-texture. If you are in the need of an alpha channel, PF_R8G8B8A8 should suit best for this. 



2、获得纹理的渲染目标

【有了渲染目标,才能指定视口和摄像机等参数】

Ogre::RenderTexture *renderTexture = rtt_texture->getBuffer()->getRenderTarget();

【给这个渲染目标指定摄像机,做了这步骤之后,每帧更新时就会把这个摄像机看到的东西渲染到renderTexture】

renderTexture->addViewport(mCamera);
renderTexture->getViewport(0)->setClearEveryFrame(true);
renderTexture->getViewport(0)->setBackgroundColour(Ogre::ColourValue::Black);
renderTexture->getViewport(0)->setOverlaysEnabled(false);

3、接下来就是显示我们创建的纹理

【因此,我们要为这个纹理创建一个材质】

Ogre::MaterialPtr renderMaterial = Ogre::MaterialManager::getSingleton().create("RttMat", Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME);
renderMaterial->getTechnique(0)->getPass(0)->setLightingEnabled(false);
renderMaterial->getTechnique(0)->getPass(0)->createTextureUnitState("RttTex");
【现在我们就可以应用这个材质了。注:mMiniScreen为教程中为了显示纹理而创建的,具体过程见教程。】

mMiniScreen->setMaterial("RttMat");

第二部分:

Implementing a mini screen

为了实现这个功能,我们需要在场景中添加一个Rectangle2D,它以上面提到的RenderTexture作为纹理。我们的场景被显示两次:一次是正常的显示在渲染窗口中;第二次是作为纹理渲染在Rectangle2D上面。
利用这个方法同样也可以实现TV screens(或者CCTV cameras等)。通过放置一个camera,你可以显示本层的其他部分,创建一个渲染纹理并显示在TV screen上面。
【就如果上面讲到的RTT一样】

1、Setting up the rectangle(注意添加一个包围盒)

Creating an Ogre::Rectangle2D is rather simple:

Ogre::Rectangle2D *mMiniScreen = new Ogre::Rectangle2D(true);
mMiniScreen->setCorners(0.5f, -0.5f, 1.0f, -1.0f);
mMiniScreen->setBoundingBox(Ogre::AxisAlignedBox(-100000.0f * Ogre::Vector3::UNIT_SCALE, 100000.0f * Ogre::Vector3::UNIT_SCALE));

In the first line, we set the parameter to true to generate texture coordinates, which we later need to map the texture on the rectangle. In the second line we have to specify the corners of the rectangle. Left is -1.0 and right +1.0, top is +1.0 and bottom -1.0. So our rectangle is just in the lower right corner of our application window.
We also give the rectangle a real huge bounding box to prevent it from being culled when we are not facing its scene node. You could also use AxisAlignedBox::setInfinite(), instead of manually setting the size of the box, but there have been some problems with this in the past, so manually setting a huge box should be the safest way.

Now that we have created the rectangle, we just need to attach it to a SceneNode which should not be anything new for you.

Ogre::SceneNode* miniScreenNode = mSceneMgr->getRootSceneNode()->createChildSceneNode("MiniScreenNode");
miniScreenNode->attachObject(mMiniScreen);
2、为我们的迷你窗口设置一个材质就可以了 
mMiniScreen->setMaterial("RttMat");




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值