Intermediate Tutorial 7         Render to Texture

%tutorialhelp%

Introduction

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.

rtt_visual1.png

Prerequisites

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.

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

Remember to initalize them all in the constructor.

BasicApp.cpp
mPlane(0),
mPlaneEntity(0),
mPlaneNode(0),
mMiniScreen(0)

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);
mCamera->lookAt(0,0,0);

Ogre::MaterialPtr mat =
  Ogre::MaterialManager::getSingleton().create(
    "PlaneMat", Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME);

Ogre::TextureUnitState* tuisTexture =
  mat->getTechnique(0)->getPass(0)->createTextureUnitState("grass_1024.jpg");

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

Ogre::MeshManager::getSingleton().createPlane(
  "PlaneMesh",
  Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME,
  *mPlane,
  120, 120, 1, 1,
  true,
  1, 1, 1,
  Ogre::Vector3::UNIT_Z);
mPlaneEntity = mSceneMgr->createEntity("PlaneMesh");
mPlaneEntity->setMaterialName("PlaneMat");

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


You might need to include these

BasicApp.h
#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.

rtt_visual2.png

Creating a Texture

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

Ogre::TexturePtr rttTexture =
  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);

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();
 
renderTexture->addViewport(mCamera);
renderTexture->getViewport(0)->setClearEveryFrame(true);
renderTexture->getViewport(0)->setBackgroundColour(Ogre::ColourValue::Black);
renderTexture->getViewport(0)->setOverlaysEnabled(false);


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.

renderTexture->update();
renderTexture->writeContentsToFile("start.png");

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);
mMiniScreen->setBoundingBox(Ogre::AxisAlignedBox::BOX_INFINITE);

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 =
  mSceneMgr->getRootSceneNode()->createChildSceneNode();

miniScreenNode->attachObject(mMiniScreen);


You might need to add this:

BasicApp.h
#include <OgreRectangle2D.h>


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

rtt_visual3.png

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 = 
  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");

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.

mMiniScreen->setMaterial("RttMat");

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:

BasicApp.h
#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.

BasicApp.cpp
void BasicApp::preRenderTargetUpdate(const Ogre::RenderTargetEvent& rte)
{
  mMiniScreen->setVisible(false);
}

void BasicApp::postRenderTargetUpdate(const Ogre::RenderTargetEvent& rte)
{
  mMiniScreen->setVisible(true);
}

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:

renderTexture->addListener(this);

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

rtt_visual4.png

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_unit
{
    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");
material->getTechnique(0)->getPass(0)->getTextureUnitState(0)->setTextureName(
  "OtherRttTex");

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.


rtt_visual1.png

Conclusion

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.

Exercises

Easy

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

Intermediate

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

Difficult

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

Advanced

  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.

Next

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