SFML community forums

Help => Graphics => Topic started by: Siile on September 10, 2013, 05:41:01 pm

Title: Rendering to sf::Texture
Post by: Siile on September 10, 2013, 05:41:01 pm
Hi ppl!

I'm creating this most awesome 2d multiplayer ninja game using bone animations and all kinds of cool stuff. I was wondering is there's good and fast way to render sfml-draw-using-stuff directly into a texture. I'd do this with opengl but Spine (awesome bone animation tool btw) is using sfml's draw-commands (which I dont want to use that much. For anything else sfml is cool).

I need to render the bone animation to texture for cool shader effects (e.g. motion blur and chakra flow visuals in ninja game anyone?). I'm sending the texture to different shaders for processing before rendering to screen.

Any fast way to do this?


And to all opengl & sfml experts: Will this be too slow if done 20 times in a frame? (Shaders will be super light)
Title: Re: Rendering to sf::Texture
Post by: G. on September 10, 2013, 06:07:48 pm
You can draw to an sf::RenderTexture (http://www.sfml-dev.org/documentation/2.1/classsf_1_1RenderTexture.php).
Title: Re: Rendering to sf::Texture
Post by: Siile on September 10, 2013, 06:50:32 pm
You can draw to an sf::RenderTexture (http://www.sfml-dev.org/documentation/2.1/classsf_1_1RenderTexture.php).

Thanks!

Can I do this and draw opengl to RenderTexture? That'd give some great possibilities with line of sight and dark blurred areas.
Code: [Select]
sf::Texture::bind(myRenderTexture);
Title: Re: Rendering to sf::Texture
Post by: G. on September 10, 2013, 07:17:05 pm
Yes, you can draw with OpenGL on an sf::RenderTexture. I'll quote Laurent on how to do this
Since you now have two render targets (the window and the render-texture), every time you make OpenGL calls you first need to set the active target. Every time you use the render-texture, you must call window.setActive() so that subsequent OpenGL calls will apply to the window, not to the render-texture.
sf::Texture::bind uses an sf::Texture*, so if you'll have to retrieve the Texture from your RenderTexture first with sf::RenderTexture::getTexture (http://www.sfml-dev.org/documentation/2.1/classsf_1_1RenderTexture.php#a95bc5152c497066d31fdc57da8e17678), which would give you
sf::Texture::bind(&myRenderTexture.getTexture());
Title: Re: Rendering to sf::Texture
Post by: Siile on September 10, 2013, 07:43:22 pm
Thanks! This makes things super easy!  ;D
Title: Re: Rendering to sf::Texture
Post by: Siile on September 11, 2013, 06:54:00 pm
1 more question (before the next one)!  ;D

If I use sfml:s draw-commands to render to texture I cant use openGL to draw it? At least looks so, my game is going wild each time I try it. :P
Title: Re: Rendering to sf::Texture
Post by: G. on September 11, 2013, 07:44:15 pm
Hmmm,  I don't understand the question!  ???
Title: Re: Rendering to sf::Texture
Post by: Siile on September 11, 2013, 09:02:51 pm
Example (right from the source):

I initialize my RenderTextures:
Code: [Select]
sf::RenderTexture ninjaRender[maxPlayers];
...
for (int i = 0; i < maxPlayers; i++)
{
ninjaRender[i].create(256, 256);
ninjaRender[i].setView(*ninjaRenderView);
ninjaRender[i].clear(sf::Color(0, 0, 0, 0));
ninjaRender[i].display();
}

Then I cap and draw them:
Code: [Select]
ninjaRender[ninjaNum].clear(sf::Color(0, 0, 0, 0));
ninjaRender[ninjaNum].draw(*anim->drawable);
ninjaRender[ninjaNum].display();

mainWindow->draw(sf::Sprite(ninjaRender[ninjaNum].getTexture()));

Works (nvm crappy solutions, just testing).

However, when I try this:

Code: [Select]
glEnable(GL_TEXTURE_2D);
sf::Texture::bind(&ninjaRender[ninjaNum].getTexture());

glColor4f(1.0f, 1.0f, 1.0f, 1.0f);

glBegin(GL_QUADS);
glTexCoord2f(0.0f, 1.0f); glVertex2f(0, 0);
glTexCoord2f(1.0f, 1.0f); glVertex2f(256, 0);
glTexCoord2f(1.0f, 0.0f); glVertex2f(256, 256);
glTexCoord2f(0.0f, 0.0f); glVertex2f(0, 256);
glEnd();

...it breaks everything.
Title: Re: Rendering to sf::Texture
Post by: Ixrec on September 11, 2013, 09:06:59 pm
Have you tried using window.pushGLStates() and window.popGLStates() as described in this tutorial http://sfml-dev.org/tutorials/2.1/window-opengl.php ?
Title: Re: Rendering to sf::Texture
Post by: Laurent on September 11, 2013, 09:57:48 pm
I don't think your problem is related to sf::RenderTexture, i.e. the same code using a regular texture should not work better.
Title: Re: Rendering to sf::Texture
Post by: Siile on September 12, 2013, 06:01:05 pm
I got it working by removing ninjaRender[ninjaNum]->display(); from my source. Dunno why..

The same code using regular texture actually works. As it does when using openGL-rendered RendererTexture. My RenderTexture is drawn like this: myRenderTexture->draw(*stuff))

When I have my RenderTexture using sfml's draw()-method I can use display()-command. However when I use openGL for rendering my RenderTexture, if I used display() when rendering to RenderTexture, it will show but mess up other openGL-drawings.

Question: Is there disadvantages when not using display() when rendering to RenderTexture? Memory leaks or something nasty hidden behind the structures?

Works fine now anyway.  :)


Have you tried using window.pushGLStates() and window.popGLStates() as described in this tutorial http://sfml-dev.org/tutorials/2.1/window-opengl.php ?

 Oh plz! ;D

ps. The same code works fine with regular textures.