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 18: Line 18:
 
renderTextureTarget->declareRenderSize(size);
 
renderTextureTarget->declareRenderSize(size);
 
</source>
 
</source>
 +
 +
  
 
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.
 
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.
Line 28: Line 30:
 
setTextureTargetImageArea(textureTargetImage, static_cast<float>(width). static_cast<float>(height));
 
setTextureTargetImageArea(textureTargetImage, static_cast<float>(width). static_cast<float>(height));
 
</source>
 
</source>
 
 
Here is the function for sizing the BasicImage:
 
Here is the function for sizing the BasicImage:
 
 
 
<source lang="cpp">
 
<source lang="cpp">
 
void setTextureTargetImageArea(CEGUI::BasicImage* textureTargetImage, float width, float height)
 
void setTextureTargetImageArea(CEGUI::BasicImage* textureTargetImage, float width, float height)
Line 47: Line 46:
 
}
 
}
 
</source>
 
</source>
 +
 +
  
 
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:
 
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:

Revision as of 21:53, 4 March 2014

Written for CEGUI 0.8


Works with versions 0.8.x (stable)

Works with latest CEGUI stable!

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();
CEGUI::GUIContext& renderGuiContext = ceguiSystem.createGUIContext(static_cast<CEGUI::RenderTarget&>(*renderTextureTarget) );
renderTextureTarget->declareRenderSize(size);


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.

// We create a BasicImage and set the Texture
CEGUI::BasicImage* textureTargetImage = static_cast<CEGUI::BasicImage*>(&CEGUI::ImageManager::getSingleton().create("BasicImage", "MyTextureGroup/MyTextureName"));
textureTargetImage->setTexture(&renderTextureTarget->getTexture());
 
// We size the image using a function
setTextureTargetImageArea(textureTargetImage, static_cast<float>(width). static_cast<float>(height));

Here is the function for sizing the BasicImage:

void setTextureTargetImageArea(CEGUI::BasicImage* textureTargetImage, float width, float height)
{
    //Flipping is necessary due to differences between renderers regarding top or bottom being the origin
    bool isTextureTargetVerticallyFlipped = renderTextureTarget->isRenderingInverted();
    CEGUI::Rectf renderArea;
 
    if (isTextureTargetVerticallyFlipped)
        renderArea = CEGUI::Rectf(0.0f, height, width, 0.0f);
    else
        renderArea = CEGUI::Rectf(0.0f, 0.0f, width, height);
 
    textureTargetImage->setImageArea(renderArea);
}


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&>(d_textureTarget->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.