Render to Texture: 2D Filter system with the OpenGL Wrapper (bgl)

Disclaimer: The following script/demo exploits the default method of rendering to texture of BGE 2.7x, it doesn’t work when using an offscreen buffer and, since UPBGE renders exclusively to offscreen buffers, it does not work on UPBGE.

Usually when rendering to texture the refresh(bool) method is used to update the texture, but there’s also the render() method that can be used on the source for the Video Texture:

Render the scene but do not extract the pixels yet. The function returns as soon as the render commands have been send to the GPU. The render will proceed asynchronously in the GPU while the host can perform other tasks. To complete the render, you can either call refresh() directly of refresh the texture of which this object is the source. This method is useful to implement asynchronous render for optimal performance: call render() on frame n and refresh() on frame n+1 to give as much as time as possible to the GPU to render the frame while the game engine can perform other tasks.

When not using an offscreen buffer, the image is actually rendered to the viewport, then copied to the texture. The script I’m sharing, after calling the render() method, copies the rendered image to a custom texture, then renders it back in the same place with the filter applied, replicating the system used to apply 2D filters. All is done with the OpenGL Wrapper (bgl) module: creating the new textures to copy the render, compiling the 2D filter shaders and rendering the image.

It can be something as simple as a blur, but filters that require the depth texture are also supported, such as ambient occlusion:

The source of the filter is provided by a 2D Filter actuator connected to the python controller containing the script. Multiple filters are supported:

The pass numbers will be respected. Game properties as uniforms for the filters are also supported, some filters may need a timer property. It works with the ImageMirror source too, but not for filters that use depth. At the beginning of the script you will find the typical options for the techinque: name of the camera, size of the texture, etc.
Before you get the file, I must warn you that this is mostly a hack of the ‘render to texture’ technique and it may not work on every scene or with every filter, use it at your own risk. Consider it rather a demo.

Render2Texture-2Dfilters.blend (124.5 KB)

3 Likes

I guess that the use of this, in a game, would be some night-vision monitoring camera.

in upbge 3x we can target a offscreen in a 2d filter and then later composite this with main

we can also save the screen buffer to a offscreen to do things like screen space motion blue etc.