If I do addChild(sprite, -10); then I get texture rendered properly in rt, otherwise it renders nothing. Why I need to add child? How to avoid polluting the screen when I need to render in a texture (RenderTexture) and only then add the rendered final thing to the scene?
EDIT: I have tried to add child, render and then remove like this:
and when the above mentioned add child is done I see the rendered texture, otherwise the sprite to whom I set texture and its rect gets and empty texture.
Well setSpriteFrame simply calls ‘setTexture()’ and applies the spritesheet offset.
So only other thing I can think of is the order you call setTexture() and setTextureRect(), because setTexture() calls setTextureRect() internally and will probably override your setTextureRect() call.
Try calling setTexture() first and setTextureRect() second.
Oh and the Y-Flip is necessary because you are rendering to GL space using cocos coordinates. They use different origins I believe.
(Edit) After a bit of reading I think that while GL and cocos2d-x use bottom left origins, the OS window will use a top left origin. And since the flip is abstracted for all render paths by cocos to account for this, we end up flipping when rendering to texture too.
I dont think you really use render texture feature to show the sprite, but you’re using addchild to add the sprite to the screen itself.
You should think Render Texture as a buffer or a hidden board, where you draw some/may sprites on it to make it into another sprite with effect/combination of many sprites…
In your case, you dont need to this line addChild(sprite, -10); but you’ll need some thing like this after rt->end();
auto anotherSpriteWithRenderTextureEffect = Sprite::createWithTexture(rt->getSprite()->getTexture()); //now you must create new sprite contains the texture from Render Texture
it DOES change the this sprite texture (i.e. view). So, I use it, without using another sprite anotherSpriteWithRenderTextureEffect as you mention in your code. The problem is that it is not being rendered. Hope now it is clear how I use it, cause I have not described it in my previous comments.