Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Rendering to sf::Texture  (Read 5059 times)

0 Members and 1 Guest are viewing this topic.

Siile

  • Newbie
  • *
  • Posts: 15
    • View Profile
    • Email
Rendering to sf::Texture
« 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)

G.

  • Hero Member
  • *****
  • Posts: 1593
    • View Profile
Re: Rendering to sf::Texture
« Reply #1 on: September 10, 2013, 06:07:48 pm »
You can draw to an sf::RenderTexture.

Siile

  • Newbie
  • *
  • Posts: 15
    • View Profile
    • Email
Re: Rendering to sf::Texture
« Reply #2 on: September 10, 2013, 06:50:32 pm »
You can draw to an sf::RenderTexture.

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);

G.

  • Hero Member
  • *****
  • Posts: 1593
    • View Profile
Re: Rendering to sf::Texture
« Reply #3 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, which would give you
sf::Texture::bind(&myRenderTexture.getTexture());
« Last Edit: September 10, 2013, 07:19:17 pm by G. »

Siile

  • Newbie
  • *
  • Posts: 15
    • View Profile
    • Email
Re: Rendering to sf::Texture
« Reply #4 on: September 10, 2013, 07:43:22 pm »
Thanks! This makes things super easy!  ;D

Siile

  • Newbie
  • *
  • Posts: 15
    • View Profile
    • Email
Re: Rendering to sf::Texture
« Reply #5 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

G.

  • Hero Member
  • *****
  • Posts: 1593
    • View Profile
Re: Rendering to sf::Texture
« Reply #6 on: September 11, 2013, 07:44:15 pm »
Hmmm,  I don't understand the question!  ???

Siile

  • Newbie
  • *
  • Posts: 15
    • View Profile
    • Email
Re: Rendering to sf::Texture
« Reply #7 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.

Ixrec

  • Hero Member
  • *****
  • Posts: 1241
    • View Profile
    • Email
Re: Rendering to sf::Texture
« Reply #8 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 ?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Rendering to sf::Texture
« Reply #9 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.
Laurent Gomila - SFML developer

Siile

  • Newbie
  • *
  • Posts: 15
    • View Profile
    • Email
Re: Rendering to sf::Texture
« Reply #10 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.