Render to texture?

Started by Jeanne-Kamikaze, 23 May 2014, 13:12:14

Jeanne-Kamikaze

Hi,

Is it possible to render a tgui::Gui into a sf::RenderTexture? I have been reading this post https://forum.tgui.eu/index.php/topic,119.0.html, but that code seems to target version 0.5 of tgui.

Reading up on the docs, it seems tgui:Gui has a method

void setWindow (sf::RenderWindow &window)

If that were generalised to

void setWindow (sf::RenderTarget &target)

then one could easily render into a texture, but there doesn't seem to be such an option.

Could anyone shed some light on this? Thanks.

texus

I'm afraid that it isn't possible.
That topic was about the fact that his render texure didn't display correctly anymore after using tgui, which was caused by badly linking the library. So you won't find much interesting in that topic.

Your generalisation will also not be possible due to the way the window is internally used (access to its view and the mapPixelToCoords function).

But depending on what you are trying to do, there are alternatives. If you only want to draw to a render texture in order to only use the gui on part of the screen, then you would be better of with setting a view for the gui.

So what is it that you are trying to accomplish exactly by rendering the gui to a render texture?

Jeanne-Kamikaze

#2
My goal is to apply some transformation to the resulting image, such as embedding the gui onto some 3D object. From what I'm seeing, I can always use a sf::Texture and update(the RenderWindow) to retrieve the result, but I suppose that's going to be slower.

texus

I looked into it in more detail and it seems that I made a mistake in my previous post.
Your generalisation will work. I though the view was something from sf::Window, but apparently not.

I've made the change on github, so if you download and compile the latest code in the v0.6 branch then you will be able to render to a RenderTarget.

Jeanne-Kamikaze

Fantastic, thanks. Will check it out now.

Jeanne-Kamikaze

I forgot to mention that the current state of the library now is a bit weird because you have a method called setWindow that takes a RenderWindow or RenderTarget through overloading. Perhaps you might consider getting rid of setWindow / getWindow and have just two methods, setRenderTarget and getRenderTarget, that take and return a RenderTarget, respectively.

texus

Right after I made the change, I noticed a problem with the render texture. In order to use the system clipboard on windows, I need the window handle. Currently the system clipboard simply doesn't get used when you pass a RenderTarget instead of a RenderWindow. So I can't get rid of the setWindow function, unless I would add a new setWindowHandle function and force people to call it just to get the system clipboard working on windows.

I hope that sfml will at some point add a clipboard, so that I can remove that code and make the change you suggest.

Jeanne-Kamikaze

Ah, your solution seems reasonable enough in that case.