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
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);
mMiniScreen->setMaterial("RttMat");