NStal
NStal

Reputation: 999

how to get gl_BackColor using webGL glsl?

In some other version of GLSL,gl_BackColor seems to provide the access to the color behind the current rendering fragment.This is useful for some custom alpha blending.But glsl for webgl seems not to support it.On the other hand,read from gl_FragColor before assign any value to it seems get the correct backColor, but only works in my Ubuntu.On my Mac Book Pro, it fails and seems to get only some useless random color.

So my question is,is there any direct way to gain access to the backColor behind the current rendering fragment?If not,how can I do it?

Upvotes: 1

Views: 957

Answers (2)

Mortennobel
Mortennobel

Reputation: 3481

There is no direct way, as Nicol Bolas write.

However you can use an indirect way, by using a render to texture approach:

  1. First render the opaque objects (if any) to a offscreen texture instead of the screen.
  2. Render the offscreen texture to the screen
  3. Render the transparent "custom blending" object to the screen using a shader that does the custom blending. (Since you are doing the blending manually the GL's Blend flag should not be enabled). You should add the offscreen texture as a uniform to the fragment-shader which let you sample the background color and calculate your custom blending.

If you need to render multiple transparent objects you can use two offscreen textures and ping-pong between them and finally render the result to the screen when all objects has been rendered.

Upvotes: 1

Nicol Bolas
Nicol Bolas

Reputation: 474446

In some other version of GLSL,gl_BackColor seems to provide the access to the color behind the current rendering fragment.

No, this has never been the case. gl_BackColor was the backface color, for doing two-sided lighting. And it was never accessible from the fragment shader; it was a vertex shader variable.

For two-sided lighting, you wrote to both gl_FrontColor and gl_BackColor in the vertex shader. The fragment shader's gl_Color variable is filled in with which ever side's color is facing the camera. So if the back-face of the triangle is forward, then it gets the interpolated gl_BackColor.

What you are asking for has never been available in GLSL.

Upvotes: 2

Related Questions