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

Author Topic: Clearing only Selected parts of a render texture.  (Read 4757 times)

0 Members and 1 Guest are viewing this topic.

glider521al

  • Newbie
  • *
  • Posts: 4
    • View Profile
Clearing only Selected parts of a render texture.
« on: August 23, 2013, 03:38:22 pm »
The problem
I'm using the .net implementation of SFML, and I can't see any way of clearing only a selected region of the RenderTexture

I was hoping for renderTexture.Clear(intRect);


Some background on why I need this.
I plan to have a way of having multiple layers of (often) semi-transparent tiles, that frequently change,
render to the screen with much better performance on older computers:

  • The world could is divided into chunks each with a render texture comprising 256 tiles (16 w, 16 h)
  • As the player moves through the world, each chunk that is seen, is rendered once to a render texture
  • When the window renders the world, it only needs to render the render texture of the chunks that are visible
  • Each time tiles are terraformed (which in my case is frequent), only a small section of the render texture should be cleared and rerendered


The problem is that I can't clear selected parts of the texture to become transparent. I need to clear and redraw to the entire render texture :(


Maybe there's a better way around this. Any ideas?
Thanks in advance.

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6286
  • Thor Developer
    • View Profile
    • Bromeon
Re: Clearing only Selected parts of a render texture.
« Reply #1 on: August 23, 2013, 03:50:39 pm »
Please read the red box in this tutorial... 256 tiles are nothing. Have you measured the performance impact? How do you draw the tiles?

It would be possible to draw a rectangle shape with no blend mode, but before complicating your code, please make sure you need it and the disadvantages of this cache won't affect your application.
« Last Edit: August 23, 2013, 03:52:58 pm by Nexus »
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

glider521al

  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: Clearing only Selected parts of a render texture.
« Reply #2 on: August 23, 2013, 08:15:03 pm »
*Sigh*, yes I have done performance tests several times over and of course I've read all the tutorials before posting (except directly interfacing with OpenGL).

I'm already a few months into the project and it runs at a solid 60 FPS on my laptop on battery saving mode (not running well on the EePC though).

I'm currently rendering  3000+ tiles at a time, by precaching possible combinations of layers at a given tileX,tileY position to new 32*32 pixel textures and allowing them to be accessed multiple times. It works really well after a long loading time. The initial loading time is what's giving me grief.

The tiles themselves aren't always always opaque or square, some have boundaries that deliberately overlap slightly
and in certain levels are prone to frequent change.
This is why I may need to cache several layers together.  In my honest opinion anything that can potentially give a decent boost to performance is worth a try (especially if it can be used with a possible 2D lighting system later)

Edit 2: Sorry, I didn't mean to come across as pretentious, it was 4:30 AM in the morning when I posted and I was  a bit irritable. 

Please read the red box in this tutorial... 256 tiles are nothing. Have you measured the performance impact? How do you draw the tiles?

It would be possible to draw a rectangle shape with no blend mode, but before complicating your code, please make sure you need it and the disadvantages of this cache won't affect your application.

« Last Edit: August 24, 2013, 01:25:47 am by glider521al »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10919
    • View Profile
    • development blog
    • Email
AW: Clearing only Selected parts of a render texture.
« Reply #3 on: August 23, 2013, 09:33:27 pm »
You can achieve this by rendering a rectangle shape with a fully transparent color and the blend mode none.

win.draw(rect, sf::BlendMode::None)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

glider521al

  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: AW: Clearing only Selected parts of a render texture.
« Reply #4 on: August 24, 2013, 01:32:49 am »
Thanks I hadn't noticed the render states class in the .net implementation had an option for blend mode.

Looks exactly what I was looking for, I will give this a try :)


You can achieve this by rendering a rectangle shape with a fully transparent color and the blend mode none.

win.draw(rect, sf::BlendMode::None)

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6286
  • Thor Developer
    • View Profile
    • Bromeon
Re: Clearing only Selected parts of a render texture.
« Reply #5 on: August 24, 2013, 09:25:42 pm »
By the way, I already mentioned that.
It would be possible to draw a rectangle shape with no blend mode

You should read more precisely, even if one part of the answer doesn't please you... Although I might have been clearer, too ;)

Are you using sprites to render the tiles? If so, a promising optimization would be vertex arrays, at least if you are able to draw many tiles with the same texture at once.
« Last Edit: August 24, 2013, 09:30:29 pm by Nexus »
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

glider521al

  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: Clearing only Selected parts of a render texture.
« Reply #6 on: August 25, 2013, 08:31:34 am »
My apologies, when you said no blend mode, I thought you meant without transformations/shaders applied. Sorry for my ineptitude, I wasn't reading carefully, and it was 4:30 in the morning when I posted
 (not that that's an excuse).

I am using sprites to render the tiles.
I've seen vertex arrays before, but in my case I still like the render texture solution better as there can be potentially upto 4 layers at any given time (fixed background/background/midground (solid)/midground (decorative like vines on top of the tile)).
In addition the tiles change images according to their slope and neighbor.

Hence I like having it all render to a few renderTextures at one time, as needed.

Edit: Thanks for your help guys, I've got a huge improvement in performance from render textures.


By the way, I already mentioned that.
It would be possible to draw a rectangle shape with no blend mode

You should read more precisely, even if one part of the answer doesn't please you... Although I might have been clearer, too ;)

Are you using sprites to render the tiles? If so, a promising optimization would be vertex arrays, at least if you are able to draw many tiles with the same texture at once.
« Last Edit: August 25, 2013, 08:36:10 am by glider521al »

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6286
  • Thor Developer
    • View Profile
    • Bromeon
Re: Clearing only Selected parts of a render texture.
« Reply #7 on: August 25, 2013, 03:37:58 pm »
I've seen vertex arrays before, but in my case I still like the render texture solution better
They are not mutually exclusive. You can render sf::VertexArray (like any other sf::Drawable) to sf::RenderTexture.

You just replace sf::Sprite with sf::VertexArray. Of course only if you need the performance or flexibility, as vertex arrays will complicate the code.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Ancurio

  • Jr. Member
  • **
  • Posts: 66
    • View Profile
Re: Clearing only Selected parts of a render texture.
« Reply #8 on: August 31, 2013, 10:40:40 am »
If you want to skip the sf::Drawable/Transformable overhead, you could alternatively simply draw a transparent quad.