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

Author Topic: Storing data in RenderTexture alpha channel  (Read 2292 times)

0 Members and 1 Guest are viewing this topic.

Fewes

  • Newbie
  • *
  • Posts: 21
    • View Profile
    • Email
Storing data in RenderTexture alpha channel
« on: June 05, 2015, 12:49:24 pm »
Hi,

I've got some code set up for drawing to a RenderTexture instead of directly to the window to be able to run screen based shaders over the "world". This RenderTexture could be thought of as a scene buffer and in my case I do not need it to be transparent since all I do is draw it to the window in the end. This frees up the alpha channel of the RenderTexture which is great because I can use that to store a depth buffer in the same draw call, or so I thought.

Turns out that it doesn't behave the way you would expect, and somehow when I supply the RenderTexture to a shader the alpha channel is completely white and the alpha I set when drawing to it seems to be taken into account when sampling the RGB channels.

Here's the shader I use to write sprites to the buffer:
uniform sampler2D texture;     // Sprite texture
uniform sampler2D rt_scene;    // Scene render texture, which is what the shader is drawing to. I pass this in here so I can mix without setting the alpha
uniform float r_depth;         // Fixed depth value for every sprite
void main()
{
    vec4 color = texture2D( texture, gl_TexCoord[0].xy );
    vec4 sceneBuffer = texture2D( rt_scene, gl_TexCoord[0].zw ); // gl_TexCoord[0].zw are screen space texture coords
   
    gl_FragColor.rgb = mix( sceneBuffer.rgb, color.rgb, color.a );
    gl_FragColor.a = mix( sceneBuffer.a, r_depth, color.a );
}

Then when I draw the Render Texture to the window I also have a custom shader and do:
vec3 color = texture2D( texture, gl_TexCoord[0].xy ).rgb;
gl_FragColor.rgb = color;
gl_FragColor.a = 1;
 

And here's an image that shows the problem (the contents of the RenderTexture):


I've done some testing and it'd be my guess that the problem lies outside of the GLSL code but I'm not very familiar with things like OpenGL flags and the like, so I come here hoping someone might have an idea of what to do. Fixing this would bring about a pretty significant performance boost (and order-independent draw sorting(!)) so I would certainly appreciate it!

Thanks
« Last Edit: June 05, 2015, 03:47:12 pm by Fewes »

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Storing data in RenderTexture alpha channel
« Reply #1 on: June 05, 2015, 03:54:43 pm »
The default blend mode is alpha blending, which takes into account the alpha channel of the source. Choose a different blending mode that fits your needs.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Fewes

  • Newbie
  • *
  • Posts: 21
    • View Profile
    • Email
Re: Storing data in RenderTexture alpha channel
« Reply #2 on: June 05, 2015, 04:41:05 pm »
That was indeed it! Setting the blend mode to None does exactly what I was looking for. Many thanks! :)