Intermediate Tutorial 7
From Ogre Wiki
Intermediate Tutorial 7: Render to texture
Any problems you encounter while working with this tutorial should be posted to the Help Forum.
Contents |
Introduction
This tutorial will teach you the basics of rendering to textures. This technique is necessary for a variety of effects especially in combination with shaders, e.g Motion Blur.
The idea behind rendering to textures ("RTT") is rather simple. Instead of sending the render output data of your scene to your render window, you just send it to a texture. This texture can then be used just as a normal texture from your harddrive.
You can find the code for this tutorial here.
The Initial Code
Create a cpp file in the IDE of your choice and add the following code to it:
#include "ExampleApplication.h"
class RTTListener : public ExampleFrameListener
{
public:
RTTListener(RenderWindow *win, Camera *cam, SceneNode *sn)
: ExampleFrameListener(win, cam), mPlaneNode(sn)
{
}
bool frameStarted(const FrameEvent& evt)
{
mPlaneNode->yaw(Radian(evt.timeSinceLastFrame));
return ExampleFrameListener::frameStarted(evt);
}
protected:
SceneNode *mPlaneNode;
};
class RTTApplication : public ExampleApplication
{
protected:
MovablePlane *mPlane;
Entity *mPlaneEnt;
SceneNode *mPlaneNode;
void createScene()
{
// Set ambient light
mSceneMgr->setAmbientLight(ColourValue(0.2f, 0.2f, 0.2f));
// Create a light
Light* l = mSceneMgr->createLight("MainLight");
l->setPosition(20, 80, 50);
// Position the camera
mCamera->setPosition(60, 200, 70);
mCamera->lookAt(0, 0, 0);
// Create a material for the plane (just a simple texture, here grass.jpg)
MaterialPtr mat = MaterialManager::getSingleton().create("PlaneMat", ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME);
TextureUnitState* t = mat->getTechnique(0)->getPass(0)->createTextureUnitState("grass_1024.jpg");
// Create a simple plane
mPlane = new MovablePlane("Plane");
mPlane->d = 0;
mPlane->normal = Vector3::UNIT_Y;
MeshManager::getSingleton().createPlane("PlaneMesh", ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME,
*mPlane, 120, 120, 1, 1, true, 1, 1, 1, Vector3::UNIT_Z);
mPlaneEnt = mSceneMgr->createEntity("PlaneEntity", "PlaneMesh");
mPlaneEnt->setMaterialName("PlaneMat");
// Attach the plane to a scene node
mPlaneNode = mSceneMgr->getRootSceneNode()->createChildSceneNode();
mPlaneNode->attachObject(mPlaneEnt);
}
// Create a new frame listener
void createFrameListener()
{
mFrameListener = new RTTListener(mWindow, mCamera, mPlaneNode);
mRoot->addFrameListener(mFrameListener);
}
};
#if OGRE_PLATFORM == OGRE_PLATFORM_WIN32
#define WIN32_LEAN_AND_MEAN
#include "windows.h"
#endif
#ifdef __cplusplus
extern "C" {
#endif
#if OGRE_PLATFORM == OGRE_PLATFORM_WIN32
INT WINAPI WinMain(HINSTANCE hInst, HINSTANCE, LPSTR strCmdLine, INT)
#else
int main(int argc, char **argv)
#endif
{
// Create application object
RTTApplication app;
try {
app.go();
} catch(Exception& e) {
#if OGRE_PLATFORM == OGRE_PLATFORM_WIN32
MessageBoxA(NULL, e.getFullDescription().c_str(),
"An exception has occurred!", MB_OK | MB_ICONERROR | MB_TASKMODAL);
#else
std::cerr << "An exception has occurred: " << e.getFullDescription();
#endif
}
return 0;
}
#ifdef __cplusplus
}
#endif
Compile and run this program before continuing. You should see a simple plane rotating around the Y-axis.
Render to texture
Creating the render textures
First of all, we need to create a texture. Find the createScene() method and add the following code:
Ogre::TexturePtr texture = Ogre::TextureManager::getSingleton().createManual("RttTex",
ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME, TEX_TYPE_2D, mWindow->getWidth(), mWindow->getHeight(), 0, PF_R8G8B8,
TU_RENDERTARGET);
The first parameter 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.
Now we need to get the render target of this texture in order to set some parameters and later also add a RenderTargetListener.
Ogre::RenderTexture *renderTexture = texture->getBuffer()->getRenderTarget(); renderTexture->addViewport(mCamera); renderTexture->getViewport(0)->setClearEveryFrame(true); renderTexture->getViewport(0)->setBackgroundColour(ColourValue::Black); renderTexture->getViewport(0)->setOverlaysEnabled(false);
After getting the render texture, we have to add a viewport to it. This is the viewport with whose content the RenderTexture will be filled. We also tell the viewport to clear itself every frame, set the background color to black and request to disable all the overlays for the texture as we don't want to have them on it.
Write texture to file
At this point we've got everything ready to do a first check: We just store the content of our created RenderTexture in a file. The handy thing is, that the RenderTextures are derived from RenderTarget and so have a ready function to store the content of the texture in a file (as well as we can do it with RenderWindows). But before saving the content to a file, you should update your RenderTexture. You can do this either manually via the update() function or request the application to automatically update the RenderTexture by once calling the setAutoUpdate() function.
// Either this way
renderTexture->setAutoUpdated(true);
// or this way
renderTexture->update();
renderTexture->writeContentsToFile("start.png");
After running the application, you will find now a .png-file in the directory your .exe is in, that should show the content of your screen (in our case the textured plane).
Implementing a mini screen
General
Now, we will implement some kind of mini screen in the lower right corner of our application window. Therefore we add a Rectangle2D to our scene which will get our just created RenderTexture as texture, so we actually see our scene twice: One time normally rendered to the RenderWindow and a second time as the texture on our Rectangle2D.
Via this method it is also possible to implement TV screens (or whatever) in a game on which you can see other parts of your level by just putting a camera there, creating a RenderTexture as described in the first section and displaying it on the TV screen.
Setting up the rectangle
Creating an Ogre::Rectangle2D is rather simple:
Ogre::Rectangle2D *miniScreen = new Ogre::Rectangle2D(true); miniScreen->setCorners(0.5, -0.5, 1.0, -1.0); miniScreen->setBoundingBox(AxisAlignedBox(-100000.0*Vector3::UNIT_SCALE, 100000.0*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(miniScreen);
If you run the application at this stage, you will see a white rectangle in the lower right corner of our application window.
Creating a material from scratch
The next step is now to show the RenderTexture we created in the first section on this rectangle. Therefore we have to create a material for it. We can do this either in a material script or directly in the code while runtime. We will do the last method in this tutorial to just have everything toghether in one file.
MaterialPtr material = MaterialManager::getSingleton().create("RttMat", ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME);
Ogre::Technique *technique = material->createTechnique();
technique->createPass();
material->getTechnique(0)->getPass(0)->setLightingEnabled(false);
material->getTechnique(0)->getPass(0)->createTextureUnitState("RttTex");
In the first line we create an empty material and add a technique and a pass in the next two lines. With the third line we disable the lightning to prevent our mini screen from beeing darker than the actual texture. In the last line we create a new TextureUnitState by passing our created RenderTexture from the first section. Now we can apply this material to our mini screen.
miniScreen->setMaterial("RttMat");
If you run the application now, you will see an undesired effect: The mini screen itself has a miniscreen! To solve this, Ogre introduces RenderTargetListener.
Usage of RenderTargetListener
General
In many cases you will need RTTs with only some scene objects on it. In our case we need a texture that only contains the output of the application window without the mini screen yet on it, as this texture is intended to be applied to our mini screen. So we have to hide the mini screen each time before the window output is stored in our RenderTexture. And this is where the RenderTargetListener comes in. This listener has two important functions: preRenderTargetUnpdate() and postRenderTargetUpdate(). As the names induces already, the first function is automatically called by the listener before the RenderTexture is filled (so we can hide our mini screen here) whereas the second function is automatically called after the RenderTexture has been filled (so we can show our mini screen again).
Implementing a RenderTargetListener
Implementig a RenderTargetListener is quite simple. First of all we let our application class derive from the RenderTargetListener to make it become its own listener. After that we just have to hide and show our mini screen in the pre- and postRenderTargetUpdate() functions. So your application class should basically look like this now:
class RTTApplication : public ExampleApplication, public RenderTargetListener
{
protected:
SceneNode *mPlaneNode;
Rectangle2D *mMiniScreen;
void createScene()
{
[...]
}
void createFrameListener()
{
[...]
}
void preRenderTargetUpdate(const RenderTargetEvent &evt)
{
mMiniScreen->setVisible(false);
}
void postRenderTargetUpdate(const RenderTargetEvent &evt)
{
mMiniScreen->setVisible(true);
}
};
Now, the last step we need to do is to add the listener to the RenderTexture. As the application class is derived from RenderTargetListener we can just pass the this pointer as paramter.
renderTexture->addListener(this);
That's it. Now you have a simple mini screen in your app. It's that simple.
RTTs and shaders
Passing a RTT to a shader
As RTTs are often used toghether with shaders, you have to know how to pass the RenderTexture to one. Gladly, this is really simple.
The most simple case is, that you will never change the texture for the shader during runtime. If don't need to, you just have to tell your material script the name of the texture you create while runtime, in our case RttTex. So your texture_unit in the material should look like this:
texture_unit
{
texture RttTex
}
If you will change the texture the shader should use during runtime, just add the following to lines:
Ogre::MaterialPtr material = Ogre::MaterialManager::getSingleton().getByName("Sepia");
material->getTechnique(0)->getPass(0)->getTextureUnitState(0)->setTextureName("OtherRttTex");
With the first line we get a pointer to the material (in this case here a sepia shader material) where we change the texture name in the second line to the desired one.
Now, as you have set the correct texture name in your material script with one of these two ways you can access the texture in your cg shader by the following line:
uniform sampler2D SceneSampler : register(s0)
Well, that's it. Your texture of the mini screen should now be passed through your shader and e.g. look like this (with a sepia shader):
Conclusion
That are all the basics you need to know for starting with RTT. Now play around a bit with this code and discover a new world of graphical effects.
| Ogre Tutorials |
|---|
|
Ogre Beginner Tutorials: 1. Basic Introduction - 2. Cameras, Lights and Shadows - 3. Terrain, Sky and Fog - 4. Frame Listeners and Unbuffered Input - 5. Buffered Input - 6. The Ogre Startup Sequence - 7. CEGUI and OGRE - 8. Multiple and Dual SceneManagers Intermediate Tutorials: 1. Animation, Interpolation and Quaternions - 2. RaySceneQueries and Basic Mouse Usage (1/2) - 3. Mouse Picking and SceneQuery Masks (2/2) - 4. Volume Selection and Manual Objects - 5. Static Geometry - 6. Projective Decals - 7. Render to Texture Advanced Tutorials: 1. Resources and ResourceManagers See also: Artist Tutorials - Ogre Articles - Cookbook |





