SFML community forums

Help => Graphics => Topic started by: on February 21, 2012, 05:08:30 pm

Title: sf::RenderTexture clearing part of it
Post by: on February 21, 2012, 05:08:30 pm
Hi.
I try to clear part of sf::RenderTexture.
How can I do it?

I tried RenderTexture.GetTexture().Update(img, x,y) but it doesn't work.
Drawing sf::RectangleShape with transparent color doesn't work too. Change blending mode should solve problem but i don't know how change it.
Title: sf::RenderTexture clearing part of it
Post by: texus on February 21, 2012, 07:29:02 pm
Quote
I try to clear part of sf::RenderTexture.
Drawing sf::RectangleShape with transparent color doesn't work too.
Can't you just draw a non-transparent RectangleShape on top of the texture?
I could be wrong, I never used sf::RenderTexture.

Quote
Change blending mode should solve problem but i don't know how change it.
The second parameter to the Draw function is a sf::RenderStates. It contains a member called BlendMode.
Title: Re: sf::RenderTexture clearing part of it
Post by: Nexus on February 21, 2012, 08:05:45 pm
Quote from: "psrebniak"
Drawing sf::RectangleShape with transparent color doesn't work too.
It just has no effect. Or what do you expect a transparent color to look like? ;)
Title: Re: sf::RenderTexture clearing part of it
Post by: Vovosunt on March 30, 2012, 05:30:17 pm
I'm having the same problem. I need to clear a part of a renderTexture, but using transparent sprites/shapes won't work. They have got to have at least 1/255 opacity even with BlendMode set to BlendNone.
As I understand it's a transparent color exception  :-\

I think I might be able to do it with openGL, but I'm still not good enough with it  :(

Common guys, I know you have a solution for this  ;)
Title: Re: sf::RenderTexture clearing part of it
Post by: Laurent on March 30, 2012, 06:01:43 pm
Quote
I need to clear a part of a renderTexture, but using transparent sprites/shapes won't work. They have got to have at least 1/255 opacity even with BlendMode set to BlendNone.
I changed this a few days ago, it should work with the latest revision of SFML 2.

But what do you want to do? Create "holes" in your render texture ?
Title: Re: sf::RenderTexture clearing part of it
Post by: Vovosunt on April 01, 2012, 06:59:38 pm
But what do you want to do? Create "holes" in your render texture ?

Yes.  I've made a tilemap, that renders to a renderTexture and only changed tiles are updated afterwards. For that I need to remove the old tiles aka clear the place for the new tiles or make "holes" like you said.

I've tried modifying SFML source code by adding BlendClear to the BlendModes enum and setting the case for it (in applyblendmode) to (GL_ZERO, GL_ONE_MINUS_SRC_ALPHA), which is supposed to use the src alpha  as a mask, but it doen't seem to work  :-\

BTW, kind of off topic, but is there any way to shift all pixels in a rendertexture? I was thinking of creating a Worms style destructible pixel world. All the pixels would get rendered to a renderTexture and only the new pixels would get updated as the player moves through the world. The only other way I see to do it is by using 4 renderTextures that get pixels rendered on them as they come into view.
Also, is there any way to render singular pixels to a render texture?
Title: Re: sf::RenderTexture clearing part of it
Post by: Laurent on April 01, 2012, 07:24:54 pm
Quote
Yes.  I've made a tilemap, that renders to a renderTexture and only changed tiles are updated afterwards. For that I need to remove the old tiles aka clear the place for the new tiles or make "holes" like you said.
I'm not sure it's the best solution. Have you tried to use a vertex array instead? So changing a tile would be as simple as changing the texture coordinates of 4 vertices in the array.

Quote
Also, is there any way to render singular pixels to a render texture?
Draw vertices as sf::Points primitives.
Title: Re: sf::RenderTexture clearing part of it
Post by: Vovosunt on April 01, 2012, 07:59:51 pm
Thanks a lot for the quick answer   :D

I'm not sure it's the best solution. Have you tried to use a vertex array instead? So changing a tile would be as simple as changing the texture coordinates of 4 vertices in the array.

That's quite a good idea, especially since it kills the need for multiple renderTextures. But I'm not so sure of performance (I've got around 7500 tiles rendered each frame = 30000 vertexes, but then again I suppose vertexArray uses 3 GL buffers so it might be just what I need :) )

I'm also wondering, is possible to enable other glBlendModes?
Title: Re: sf::RenderTexture clearing part of it
Post by: Laurent on April 01, 2012, 08:49:13 pm
Quote
I've got around 7500 tiles rendered each frame = 30000 vertexes
Today's graphics cards (and even older ones) can render millions of triangles per second.

Quote
I'm also wondering, is possible to enable other glBlendModes?
Nop. What other blending mode do you need?
Title: Re: sf::RenderTexture clearing part of it
Post by: Vovosunt on April 02, 2012, 05:41:01 pm
Today's graphics cards (and even older ones) can render millions of triangles per second.
Well, unless its an Intel graphics card like I have on my laptop. I used a vertexarray, but the framerate is horrible(rendering 7500 sprites was actually faster) and I think I screwed up the texCoords. Are the texture coordinates in pixels or relative to the texture (like in standard opengl, from 0 to 1)? (I tried both ways, still the texture was screwed up  :-\ )
(http://img94.imageshack.us/img94/9282/vertexi.jpg)
Quote
Nop. What other blending mode do you need?
The one I mentioned above, so that you could use the src alpha as a mask. (http://)
Title: Re: sf::RenderTexture clearing part of it
Post by: Laurent on April 02, 2012, 06:01:05 pm
Quote
I used a vertexarray, but the framerate is horrible(rendering 7500 sprites was actually faster)
I think you did something wrong, it's not expected, especially if it's slower than rendering sprites.

Quote
Are the texture coordinates in pixels or relative to the texture
They are in pixels.

Quote
The one I mentioned above, so that you could use the src alpha as a mask.
Source alpha is used as a mask in BlendAlpha.
Title: Re: sf::RenderTexture clearing part of it
Post by: Vovosunt on April 02, 2012, 06:28:25 pm
I think you did something wrong, it's not expected, especially if it's slower than rendering sprites.

Oh, please excuse my bloody idiocity, it seems I forgot to put the breaks in my cases and ended up rendering 96 times more geometry *facepalm*
I'm still getting this weird triangle cut off from a side of every tile.
Maybe you could check what I'm doing wrong  :-[

//using a generated terrain field[][] with values from 0 to 24
//creating/initing the vertexArray:
sf::VertexArray vertarr(sf::PrimitiveType::Quads);
        for (int x = 0; x< terrx; ++x)
                for (int y=0; y< terry; ++y)
                {
                        if (field[x][y] != 0)
                        {
                                switch (field[x][y])                   
                                {
                                case 1:
                                        vertarr.append(sf::Vertex(sf::Vector2f(8*x, 8*y), sf::Vector2f(0,0)));
                                        vertarr.append(sf::Vertex(sf::Vector2f(8*x+8, 8*y), sf::Vector2f(8,0)));
                                        vertarr.append(sf::Vertex(sf::Vector2f(8*x, 8*y+8), sf::Vector2f(0,8)));
                                        vertarr.append(sf::Vertex(sf::Vector2f(8*x+8, 8*y+8), sf::Vector2f(8,8)));
                                        break;
                                case 2:
                                        vertarr.append(sf::Vertex(sf::Vector2f(8*x, 8*y), sf::Vector2f(0,8)));
                                        vertarr.append(sf::Vertex(sf::Vector2f(8*x+8, 8*y), sf::Vector2f(8,8)));
                                        vertarr.append(sf::Vertex(sf::Vector2f(8*x, 8*y+8), sf::Vector2f(0,16)));
                                        vertarr.append(sf::Vertex(sf::Vector2f(8*x+8, 8*y+8), sf::Vector2f(8,16)));
                                        break;
                                case 3:
                                        vertarr.append(sf::Vertex(sf::Vector2f(8*x, 8*y), sf::Vector2f(0,16)));
                                        vertarr.append(sf::Vertex(sf::Vector2f(8*x+8, 8*y), sf::Vector2f(8,16)));
                                        vertarr.append(sf::Vertex(sf::Vector2f(8*x, 8*y+8), sf::Vector2f(0,24)));
                                        vertarr.append(sf::Vertex(sf::Vector2f(8*x+8, 8*y+8), sf::Vector2f(8,24)));
                                        break;
                                case 4:
                                        vertarr.append(sf::Vertex(sf::Vector2f(8*x, 8*y), sf::Vector2f(8,0)));
                                        vertarr.append(sf::Vertex(sf::Vector2f(8*x+8, 8*y), sf::Vector2f(16,0)));
                                        vertarr.append(sf::Vertex(sf::Vector2f(8*x, 8*y+8), sf::Vector2f(8,8)));
                                        vertarr.append(sf::Vertex(sf::Vector2f(8*x+8, 8*y+8), sf::Vector2f(16,8)));
                                        break;
                               
                                [...]

                                case 24:
                                        vertarr.append(sf::Vertex(sf::Vector2f(8*x, 8*y), sf::Vector2f(56,16)));
                                        vertarr.append(sf::Vertex(sf::Vector2f(8*x+8, 8*y), sf::Vector2f(64,16)));
                                        vertarr.append(sf::Vertex(sf::Vector2f(8*x, 8*y+8), sf::Vector2f(56,24)));
                                        vertarr.append(sf::Vertex(sf::Vector2f(8*x+8, 8*y+8), sf::Vector2f(64,24)));
                                        break;
                                }
                        }
                }
//Then in the while rendering with
Window.draw(vertarr,sf::RenderStates(&mud));

 

Quote
The one I mentioned above, so that you could use the src alpha as a mask.
Source alpha is used as a mask in BlendAlpha.
No I mean like (GL_ZERO, GL_ONE_MINUS_SCR_ALPHA), basically the texture would be used as a hole-cutter
Like the 3rd example here http://www.machwerx.com/2009/02/11/glblendfunc/ (http://www.machwerx.com/2009/02/11/glblendfunc/)
Title: Re: sf::RenderTexture clearing part of it
Post by: Vovosunt on April 02, 2012, 06:30:49 pm
Nevermind, wrong vertex order *facepalmultimate*
Title: Re: sf::RenderTexture clearing part of it
Post by: Laurent on April 02, 2012, 07:36:13 pm
;D

Quote
No I mean like (GL_ZERO, GL_ONE_MINUS_SCR_ALPHA), basically the texture would be used as a hole-cutter
Since we're using only the alpha channel here, the color doesn't really matter and thus the BlendAlpha mode would work too.