Render to texture?

Is there any way to use the graphics drawn by tilesheet.drawTiles as a texture in GL?

Currently I have to use bitmapdata.draw(graphics) to get the bitmapdata, and then upload it again to GPU, which is kind of a waste of performance…

This is an example for rendering (basically) full screen post-processing effects by catching the GL render call of a sprite (he used a library called tilelayer to efficiently use drawTiles API) and render it onto a texture.

How it works is basically add the Before and After class etc in such a way that for every frame it will:
-set the render target from to texture1 (the Before class),
-draws the sprite onto the texture1 (openfl will do this as it renders the sprite but since the render target is altered it draws onto the texture instead of the screen),
-renders the texture1 onto texture2 with a shader (the RenderToTextureLayer class),
-renders texture2 onto texture1 with another shader (the RenderToTextureLayer class),
-finally renders texture1 to screen (the After class).

This is another useful tool by ibilon that can apply a shader on a BitmapData. Take a look as well if it helps.

Thanks for the infomation, that’s really helpful!!

But I have a little question as I am quite new to GL programming. What I understand is that GL.bindFramebuffer(GL.FRAMEBUFFER, this.framebuffer) causes GL to draw on the specified framebuffer instead of the screen, but what is the exact code that cause texture1 to hold the framebuffer data?

Edit: did a bit reserach and seems it is this line of code:
GL.framebufferTexture2D(GL.FRAMEBUFFER, GL.COLOR_ATTACHMENT0, GL.TEXTURE_2D, texture1, 0);

Yes I believe it is, and also other lines of code around it. I’m not an expert, but that’s what I have figured out too.

Well, this seems more difficult than I thought. Are there any minimal code that do the following?

tilesheet.drawTiles on a GLTexture and then get the texture as uniform sampler2D in a fragment shader?

After 2 hours of testing I finally got it working.

Here is what I got for those who are interested:
Basically the “Before” class is an OpenGLView with its render function being overridden. Instead of rendering some visuals, the render function of the “Before” class now do nothing more than simply binding a specified framebuffer. So that GL won’t render to the screen after “rendering” the “Before” class. So all subsequent sprites in the display list will render to the specified framebuffer, which you can link a texture to and use later. At the end, an “After” class which is similar to “Before” class will use its overridden render() call to bind a null framebuffer, which means GL will draw to the screen afterwards.

Yes that’s how it works.Also the RenderToTexture class will upload the texture and the fragment shader can then access it via “uniform sampler 2D uImage0”.