Intermediate Tutorial 7         Render to Texture



This tutorial covers the basics of rendering a scene to a texture. This technique is used for a variety of effects. It is particularly useful in combination with shaders. Motion blur effects can be created in this way.

The basic idea is rather simple. Instead of just sending render information strictly to our render window, we will also send the information to be rendered directly to a texture in our scene. This texture will then be used like a texture loaded from the hard drive.

The full source for this tutorial is here.

Note: There is also source available for the BaseApplication framework and Ogre 1.7 here.



This tutorial assumes that you already know how to set up an Ogre project and compile it successfully. Knowledge of the topics from previous tutorials is also assumed.

The base code for this tutorial is here.

Setting Up the Scene

First, as usual, we are going to set up a basic scene. Add the following variables to your project.

Ogre::MovablePlane* mPlane;
Ogre::Entity* mPlaneEntity;
Ogre::SceneNode* mPlaneNode;
Ogre::Rectangle2D* mMiniScreen;

Remember to initalize them all in the constructor.


Finally, we'll set up the basic scene elements we need. Add the following to createScene:

mSceneMgr->setAmbientLight(Ogre::ColourValue(0.2, 0.2, 0.2));

Ogre::Light* light = mSceneMgr->createLight("MainLight");
light->setPosition(20, 80, 50);

mCamera->setPosition(60, 200, 70);

Ogre::MaterialPtr mat =
    "PlaneMat", Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME);

Ogre::TextureUnitState* tuisTexture =

mPlane = new Ogre::MovablePlane("Plane");
mPlane->d = 0;
mPlane->normal = Ogre::Vector3::UNIT_Y;

  120, 120, 1, 1,
  1, 1, 1,
mPlaneEntity = mSceneMgr->createEntity("PlaneMesh");

mPlaneNode = mSceneMgr->getRootSceneNode()->createChildSceneNode();

You might need to include these

#include <OgreMaterialManager.h>
#include <OgreTechnique.h>
#include <OgreMovablePlane.h>
#include <OgreMeshManager.h>

If any of this is confusing, refer to previous tutorials. Compile and run your application. You should see a rotating grass-textured plane.


Creating a Texture

The first step is to create a texture we can render to. Add the folowing to createScene:

Ogre::TexturePtr rttTexture =
    mWindow->getWidth(), mWindow->getHeight(), 

The first parameter of this function is the name to give the texture ("RttTex" is somewhat standard). The second specifies the default resource group. The third specifies the type of the texture to be 2D. The fourth and fifth specify the width and height of the texture. The sixth parameter is the number of mipmaps to be used. The seventh is a texture format, and the last is a usage flag.

There are many different texture formats, but the simplest one is PF_R8G8B8. This produces a 24-bit RGB texture. If you need an alpha channel, then PFR8G8B8A8 will work fine.

Next we need to get a pointer to the render target of this texture in order to set some parameters. It will also be used later to add a RenderTargetListener.

Ogre::RenderTarget* renderTexture = rttTexture->getBuffer()->getRenderTarget();

You might need to include these headers:
{CODE(wrap="1", colors="c++", caption="BasicApp.h)}

  1. include <OgreRenderTexture.h>
  2. include <OgreHardwarePixelBuffer.h>{CODE}

After retrieving the render texture, we add the camera's viewport to it. This is the viewport that will be rendered to our texture. We also tell the viewport to clear itself every frame. If we didn't do this, then we would get the infinite trails effect. Try it, it's fun. We also set the background color to black, and we disable overlays so that they won't be rendered on top of our texture if we were to use them.

Writing Our Texture to a File

We are now going to save our texture to a file. The RenderTexture class is derived from RenderTarget, so they already have a function to store their content to a file. Before saving the texture, be sure to update it once.


If you compile and run the application now, you will find 'start.png' in the directory that includes your executable. It should show the content of your screen.

Note there is also a function called setAutoUpdated which will automatically update the RenderTexture if Ogre's rendering loop or Root::_updateAllRenderTargets is being used. Keep in mind that it does not update the RenderTexture immediately when it is called.

Implementing the Miniscreen

Now we will add a miniscreen to the upper right corner of our application window. To do this, we'll use a Rectangle2D with our RenderTexture applied to it. Our scene will be shown twice. It will be rendered to our RenderWindow normally and rendered a second time on to our miniscreen.

You can use this method to create security camera monitors or computer screens with rendered graphics on them. Of course you don't need to simply replicate the camera's viewpoint. You can attach a different viewport to the RenderTexture, allowing you to do things like remote controlled robots with their own camera that feeds back to a miniscreen.

First, we'll set up the Rectangle2D.

mMiniScreen = new Ogre::Rectangle2D(true);
mMiniScreen->setCorners(.5, 1.0, 1.0, .5);

The first line tells the Rectangle2D to generate texture coordinates. These are what we'll use to apply our texture. In the second line we set the coordinates for our miniscreen. These are coordinates that run from the top-left (-1.0, 1.0) to the bottom-right (1.0, -1.0). This places our rectangle in the upper right corner of the screen with a width and height that are equal to 1/4 of the screen's dimensions. You'll just have to get used to the fact that a number of different coordinate systems are used in graphics programming. It's not a total loss because understanding simple coordinate transformations like this may help you understand the more complicated transformations you'll see in other areas.

Now that we have the rectangle, we need to attach it to a SceneNode as usual.

Ogre::SceneNode* miniScreenNode =


You might need to add this:

#include <OgreRectangle2D.h>

If you compile and run the application now, you'll see a white rectangle where our texture will soon be.


Creating a Material From Scratch

The next step is to apply our RenderTexture to the Rectangle2D we've just created. We have to create a material for it. This could be done in a material script or directly in code during runtime. We will use the second method to keep everything to one file for the tutorial.

Ogre::MaterialPtr renderMaterial = 


The first part creates an empty material called RttMat. Then we disable the lighting to prevent our miniscreen from being darker than the actual texture (if we didn't, the lighting would essentially be applied twice to our miniscreen rendering). The last line creates a new TextureUnitState using the RttTex we created earlier.

Now we simply set this material to be used by our miniscreen.


If you compile and run the application now, you'll see that something is wrong. The miniscreen...has a miniscreen. To solve this we'll make use of a RenderTargetListener.

Using a RenderTargetListener

There may be cases where you only want certain objects from your scene to be used by your RenderTexture. In our case, we don't want our miniscreen to be rendered again in our miniscreen. What we have to do is hide our miniscreen right before the window output is stored to our RenderTexture. This is what we can use the RenderTargetListener for.

The listener has two important functions for our problem: preRenderTargetUpdate and postRenderTargetUpdate. The names practically tell you exactly what they do. The first function is called automatically by the listener before the RenderTexture is filled, and the second function is called after the RenderTexture has been filled.

To implement a RenderTargetListener we will simply have our application inherit from RenderTargetListener. This class can be included with OgreRenderTargetListener.h. Add the following to your project:

#include <OgreRenderTargetListener.h>

class BasicApp
  : public Ogre::WindowEventListener,
    public Ogre::FrameListener,
    public Ogre::RenderTargetListener,
    public OIS::KeyListener,
    public OIS::MouseListener

  // Tutorial Section //
  virtual void preRenderTargetUpdate(const Ogre::RenderTargetEvent& rte);
  virtual void postRenderTargetUpdate(const Ogre::RenderTargetEvent& rte);



Then we will add these methods and toggle the visibility off right before updating the RenderTexture and then right back on afterwards.

void BasicApp::preRenderTargetUpdate(const Ogre::RenderTargetEvent& rte)

void BasicApp::postRenderTargetUpdate(const Ogre::RenderTargetEvent& rte)

All that is left is to add the listener to our RenderTexture so the methods are actually called automatically. Remember, our application has inherited from RenderTargetListener so it can be passed directly as the listener. Add the following to createScene, right after we set the miniscreen material:


Compile and run your application. You should have a working RTT texture mapped to your miniscreen.


Render to Texture and Shaders

This process is often used together with shaders to create a vast array of effects. So it will be useful to know how to pass a RenderTexture to a shader.

The simplest case is where the texture never changes for the shader during runtime. You just have to tell your material script the name of the texture you create during runtime, in our case "RttTex". So in your material script, the texture_unit would look like this:

    texture RttTex

If you need to change the shader's texture during runtime, then you would use the following two lines:

Ogre::MaterialPtr material = Ogre::MaterialManager::getSingleton().getByName("Sepia");

The first line gets a pointer to the material. The second line sets the desired texture name.

After setting the correct texture name, you can access the texture in your Cg shader with the following line:

uniform sampler2D SceneSampler : register(s0)

That's all there is to it. Download the files and try it out with your application.

 Plugin disabled
Plugin attach cannot be executed.



In this tutorial, we learned how to set up a RenderTexture that we can use to display the contents of a viewport. This involved creating a texture from scratch, then building a material from that texture, and finally applying that texture to a Rectangle2D object. We also introduced the RenderTargetListener. This allowed us to customize what would be rendered from our chosen viewport. The listener provides callback methods that allow us to manipulate the scene before and after the RenderTexture is applied to our miniscreen. Finally, we covered passing our RenderTexture through a Cg shader.

For further information on Cg shaders see Getting Started With Ogre CG Materials.



  1. Add more objects to your scene and then hide some of them from the RenderTexture using the listener.


  1. Add a second miniscreen that displays the same scene from a different angle.


  1. Set up and display a viewport in your miniscreen other than your main camera.


  1. Model a security camera monitor by setting up a viewport with an aerial view of the scene, and then rendering it to on the front of a cube sitting on a table. Remember you can manually create these meshes if you don't already know how to build an ogre mesh in a program like Blender.

Full Source

The full source for this tutorial is here.


This is the end of the Intermediate Tutorial series. You can start browsing Snippets for more information about Ogre's features.

Alias: Intermediate_Tutorial_7