Difference between revisions of "Rendering to texture (RTT) in CEGUI"

From CEGUI Wiki - Crazy Eddie's GUI System (Open Source)
Jump to: navigation, search
Line 1: Line 1:
{{VersionBadge|0.8}}
+
<p>{{VersionBadge|0.8}}
 +
</p>
  
 
+
== Rendering to a texture (RTT) using a CEGUI GUIContext ==
=== Rendering to a texture (RTT) using a CEGUI GUIContext ===
+
  
 
Since version 0.8 the new GUIContext class allows it to render your CEGUI interface into a texture instead of rendering it normally into your main buffer.
 
Since version 0.8 the new GUIContext class allows it to render your CEGUI interface into a texture instead of rendering it normally into your main buffer.
Line 52: Line 52:
 
=== Displaying a (RTT) texture inside your GUI using a CEGUI window ===
 
=== Displaying a (RTT) texture inside your GUI using a CEGUI window ===
  
The following section is only necessary if you want to use the rendered texture as a CEGUI Image again. Basically to render a CEGUI layout inside CEGUI. This won't be the case if you just want to render CEGUI into a texture and use it somewhere in your 3D application.
+
The following section can be used when you want to display a texture, which you create yourself and to which you might have rendered (RTT), inside a CEGUI window. For doing so, you need to create a CEGUI Image based on the rendered texture. The following code snippet shows you the full process of creating the image and setting the texture to it:
 
<source lang="cpp">
 
<source lang="cpp">
 +
// We create a CEGUI Texture using the renderer you use:
 +
CEGUI::Texture& texture = gui_renderer->createTexture("MyTextureName");
 +
 +
// Now we need to cast it to the CEGUI::Texture superclass which matches your Renderer. This can be CEGUI::OgreTexture or CEGUI::OpenGLTexture, depending on the renderer you use in your application
 +
// We will use Ogre here as an example
 +
CEGUI::OgreTexture& rendererTexture = static_cast<CEGUI::OgreTexture&>(texture);
 +
// Now we can set the appropriate Ogre::Texture for our CEGUI Texture
 +
rendererTexture.setOgreTexture(whateverTextureYouWannaUse, false);
 +
 
// We create a BasicImage and set the Texture
 
// We create a BasicImage and set the Texture
CEGUI::BasicImage* textureTargetImage = static_cast<CEGUI::BasicImage*>(&CEGUI::ImageManager::getSingleton().create("BasicImage", "MyTextureGroup/MyTextureName"));
+
CEGUI::BasicImage* image = static_cast<CEGUI::BasicImage*>(&CEGUI::ImageManager::getSingleton().create("BasicImage", "MyImageGroup/MyImageName"));
textureTargetImage->setTexture(&renderTextureTarget->getTexture());
+
image ->setTexture(rendererTexture);
  
 
//Flipping is necessary due to differences between renderers regarding top or bottom being the origin
 
//Flipping is necessary due to differences between renderers regarding top or bottom being the origin
 
bool isTextureTargetVerticallyFlipped = renderTextureTarget->isRenderingInverted();
 
bool isTextureTargetVerticallyFlipped = renderTextureTarget->isRenderingInverted();
CEGUI::Rectf renderArea;
+
CEGUI::Rectf imageArea;
  
 
if (isTextureTargetVerticallyFlipped)
 
if (isTextureTargetVerticallyFlipped)
     renderArea = CEGUI::Rectf(0.0f, height, width, 0.0f);
+
     imageArea= CEGUI::Rectf(0.0f, textureHeight, textureWidth, 0.0f);
 
else
 
else
     renderArea = CEGUI::Rectf(0.0f, 0.0f, width, height);
+
     imageArea= CEGUI::Rectf(0.0f, 0.0f, textureWidth, textureHeight);
  
textureTargetImage->setArea(renderArea);
+
textureTargetImage->setArea(imageArea);
 
</source>
 
</source>
  
  
 
[[Category:HowTo]]
 
[[Category:HowTo]]

Revision as of 14:10, 16 March 2014

Written for CEGUI 0.8


Works with versions 0.8.x (stable)

Works with latest CEGUI stable!

Rendering to a texture (RTT) using a CEGUI GUIContext

Since version 0.8 the new GUIContext class allows it to render your CEGUI interface into a texture instead of rendering it normally into your main buffer.


CEGUI::System& ceguiSystem = CEGUI::System::getSingleton();
 
//Taking some random values as example for a GUIContext size.
int width = 1024;
int height = 800;
CEGUI::Sizef size(static_cast<float>(width), static_cast<float>(height));
 
// We create a CEGUI texture target and create a GUIContext that will use it.
CEGUI::TextureTarget* renderTextureTarget = ceguiSystem.getRenderer()->createTextureTarget();
renderTextureTarget->declareRenderSize(size);
CEGUI::GUIContext& renderGuiContext = ceguiSystem.createGUIContext(static_cast<CEGUI::RenderTarget&>(*renderTextureTarget) );



For rendering you need to call the rendering function of your new GuiContext directly. Here is an example of how to call the renderer and the guicontext in the right order when rendering:

    CEGUI::Renderer* gui_renderer(gui_system.getRenderer());
    gui_renderer->beginRendering();
 
    renderTextureTarget->clear();
    renderGuiContext.draw();
 
    gui_renderer->endRendering();

Also do not forget to correctly update CEGUI and the GUIContext with all timepulses and inputs, just as you would do it normally but by using your new GUIContext instead of the default one!

If you want to retrieve the renderer-specific texture from a CEGUI texture you will need to cast it:

For example for OpenGL:

CEGUI::OpenGLTexture& glTexture = static_cast<CEGUI::OpenGLTexture&>(renderTextureTarget ->getTexture());

After that you can use the member function to receive whatever render-specific traits you need, such as the textureID in Opengl as an unsigned integer, etc.


Displaying a (RTT) texture inside your GUI using a CEGUI window

The following section can be used when you want to display a texture, which you create yourself and to which you might have rendered (RTT), inside a CEGUI window. For doing so, you need to create a CEGUI Image based on the rendered texture. The following code snippet shows you the full process of creating the image and setting the texture to it:

// We create a CEGUI Texture using the renderer you use:
CEGUI::Texture& texture = gui_renderer->createTexture("MyTextureName");
 
// Now we need to cast it to the CEGUI::Texture superclass which matches your Renderer. This can be CEGUI::OgreTexture or CEGUI::OpenGLTexture, depending on the renderer you use in your application
// We will use Ogre here as an example
CEGUI::OgreTexture& rendererTexture = static_cast<CEGUI::OgreTexture&>(texture);
// Now we can set the appropriate Ogre::Texture for our CEGUI Texture
rendererTexture.setOgreTexture(whateverTextureYouWannaUse, false);
 
// We create a BasicImage and set the Texture
CEGUI::BasicImage* image = static_cast<CEGUI::BasicImage*>(&CEGUI::ImageManager::getSingleton().create("BasicImage", "MyImageGroup/MyImageName"));
image ->setTexture(rendererTexture);
 
//Flipping is necessary due to differences between renderers regarding top or bottom being the origin
bool isTextureTargetVerticallyFlipped = renderTextureTarget->isRenderingInverted();
CEGUI::Rectf imageArea;
 
if (isTextureTargetVerticallyFlipped)
    imageArea= CEGUI::Rectf(0.0f, textureHeight, textureWidth, 0.0f);
else
    imageArea= CEGUI::Rectf(0.0f, 0.0f, textureWidth, textureHeight);
 
textureTargetImage->setArea(imageArea);