OGRE Wiki
Support and community documentation for Ogre3D
Ogre Forums
ogre3d.org
Log in
Username:
Password:
CapsLock is on.
Remember me (for 1 year)
Log in
Home
Tutorials
Tutorials Home
Basic Tutorials
Intermediate Tutorials
Mad Marx Tutorials
In Depth Tutorials
Older Tutorials
External Tutorials
Cookbook
Cookbook Home
CodeBank
Snippets
Experiences
Ogre Articles
Libraries
Libraries Home
Alternative Languages
Assembling A Toolset
Development Tools
OGRE Libraries
List of Libraries
Tools
Tools Home
DCC Tools
DCC Tutorials
DCC Articles
DCC Resources
Assembling a production pipeline
Development
Development Home
Roadmap
Building Ogre
Installing the Ogre SDK
Setting Up An Application
Ogre Wiki Tutorial Framework
Frequently Asked Questions
Google Summer Of Code
Help Requested
Ogre Core Articles
Community
Community Home
Projects Using Ogre
Recommended Reading
Contractors
Wiki
Immediate Wiki Tasklist
Wiki Ideas
Wiki Guidelines
Article Writing Guidelines
Wiki Styles
Wiki Page Tracker
Ogre Wiki Help
Ogre Wiki Help Overview
Help - Basic Syntax
Help - Images
Help - Pages and Structures
Help - Wiki Plugins
Toolbox
Freetags
Categories
List Pages
Structures
Trackers
Statistics
Rankings
List Galleries
Ogre Lexicon
Comments
History: Intermediate Tutorial 7
View page
Source of version: 107
(current)
%tutorialhelp% {maketoc} !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 ((IntermediateTutorial7SourceCurrent|here)). __Note:__ There is also source available for the BaseApplication framework and Ogre 1.7 ((IntermediateTutorial7Source|here)). {img fileId="2260" rel="box[g]"} !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 ((IntermediateTutorialBaseSource|here)). !Setting Up the Scene First, as usual, we are going to set up a basic scene. Add the following variables to your project. {CODE(caption="BasicApp.h", wrap="1", colors="c++")} Ogre::MovablePlane* mPlane; Ogre::Entity* mPlaneEntity; Ogre::SceneNode* mPlaneNode; Ogre::Rectangle2D* mMiniScreen; {CODE} Remember to initalize them all in the constructor. {CODE(caption="BasicApp.cpp", wrap="1", colors="c++")} mPlane(0), mPlaneEntity(0), mPlaneNode(0), mMiniScreen(0) {CODE} Finally, we'll set up the basic scene elements we need. Add the following to {MONO()}createScene{MONO}: {CODE(wrap="1", colors="c++")} 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); {CODE} You might need to include these {CODE(wrap="1", colors="c++" caption="BasicApp.h")}#include <OgreMaterialManager.h> #include <OgreTechnique.h> #include <OgreMovablePlane.h> #include <OgreMeshManager.h>{CODE} If any of this is confusing, refer to previous tutorials. Compile and run your application. You should see a rotating grass-textured plane. {img fileId="2261" rel="box[g]"} !Creating a Texture The first step is to create a texture we can render to. Add the folowing to {MONO()}createScene{MONO}: {CODE(wrap="1", colors="c++")} 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); {CODE} 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. {CODE(wrap="1", colors="c++")} 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); {CODE} You might need to include these headers: {CODE(wrap="1", colors="c++", caption="BasicApp.h)} #include <OgreRenderTexture.h> #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. {CODE(wrap="1", colors="c++")} renderTexture->update(); renderTexture->writeContentsToFile("start.png"); {CODE} 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 {MONO()}setAutoUpdated{MONO} which will automatically update the RenderTexture if Ogre's rendering loop or {MONO()}Root::_updateAllRenderTargets{MONO} 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. {CODE(wrap="1", colors="c++")} mMiniScreen = new Ogre::Rectangle2D(true); mMiniScreen->setCorners(.5, 1.0, 1.0, .5); mMiniScreen->setBoundingBox(Ogre::AxisAlignedBox::BOX_INFINITE); {CODE} 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. {CODE(wrap="1", colors="c++")} Ogre::SceneNode* miniScreenNode = mSceneMgr->getRootSceneNode()->createChildSceneNode(); miniScreenNode->attachObject(mMiniScreen); {CODE} You might need to add this: {CODE(wrap="1", colors="c++", caption="BasicApp.h")}#include <OgreRectangle2D.h> {CODE} If you compile and run the application now, you'll see a white rectangle where our texture will soon be. {img fileId="2263" rel="box[g]"} !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. {CODE(wrap="1", colors="c++")} 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"); {CODE} 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. {CODE(wrap="1", colors="c++")} mMiniScreen->setMaterial("RttMat"); {CODE} 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: {MONO()}preRenderTargetUpdate{MONO} and {MONO()}postRenderTargetUpdate{MONO}. 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 {MONO()}OgreRenderTargetListener.h{MONO}. Add the following to your project: {CODE(caption="BasicApp.h", wrap="1", colors="c++")} #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); ... }; {CODE} Then we will add these methods and toggle the visibility off right before updating the RenderTexture and then right back on afterwards. {CODE(caption="BasicApp.cpp", wrap="1", colors="c++")} void BasicApp::preRenderTargetUpdate(const Ogre::RenderTargetEvent& rte) { mMiniScreen->setVisible(false); } void BasicApp::postRenderTargetUpdate(const Ogre::RenderTargetEvent& rte) { mMiniScreen->setVisible(true); } {CODE} 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 {MONO()}createScene{MONO}, right after we set the miniscreen material: {CODE(wrap="1", colors="c++")} renderTexture->addListener(this); {CODE} Compile and run your application. You should have a working RTT texture mapped to your miniscreen. {img fileId="2264" rel="box[g]"} !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 {MONO()}texture_unit{MONO} would look like this: {CODE(wrap="1", colors="c++")} texture_unit { texture RttTex } {CODE} If you need to change the shader's texture during runtime, then you would use the following two lines: {CODE(wrap="1", colors="c++")} Ogre::MaterialPtr material = Ogre::MaterialManager::getSingleton().getByName("Sepia"); material->getTechnique(0)->getPass(0)->getTextureUnitState(0)->setTextureName( "OtherRttTex"); {CODE} 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: {CODE(wrap="1", colors="c++")} uniform sampler2D SceneSampler : register(s0) {CODE} That's all there is to it. Download the files and try it out with your application. {ATTACH(id="151" showdesc="1")}{ATTACH} {img fileId="2260" rel="box[g]"} !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 # Add more objects to your scene and then hide some of them from the RenderTexture using the listener. !!Intermediate # Add a second miniscreen that displays the same scene from a different angle. !!Difficult # Set up and display a viewport in your miniscreen other than your main camera. !!Advanced # 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 ((IntermediateTutorial7SourceCurrent|here)). !Next This is the end of the Intermediate Tutorial series. You can start browsing ((Snippets)) for more information about Ogre's features. --- Alias: (alias(Intermediate_Tutorial_7))
Search by Tags
Search Wiki by Freetags
Latest Changes
IDE Eclipse
FMOD SoundManager
HDRlib
Building Ogre V2 with CMake
Ogre 2.1 FAQ
Minimal Ogre Collision
Artifex Terra
OpenMB
Advanced Mogre Framework
MogreSocks
...more
Search
Find
Advanced
Search Help
Online Users
307 online users