<p>Since you only want full brightness and no brightness, sure you can.</p>
<p>Red channel on: additive blend with red<br />
Red channel off: multiplicative blend with cyan<br />
Blue channel on: additive blend with blue<br />
Blue channel off: multiplicative blend with yellow</p>
<p>I also don't understand why you can't just run your rendering code twice: once with a red color mask and once with a blue color mask. It doesn't seem like you need to control it from GLSL. You're allowed to render the same vertices twice with different states - commercial video games do that sort of thing routinely.</p>
<p>Switching the blend mode from additive to multiplicative is a state change, just like setting the colour mask.</p>

CC BY-SA 4.0
2022-04-11T10:25:27.047Z
2022-04-11T10:25:27.047Z
Criticize SE actions means ban
<p>Suppose I have previously rendered some set of pixels on the screen that each contain one of the following RGB values:</p>
<pre><code>0.0 0.0 0.0    in other words, black
1.0 0.0 0.0    in other words, red
0.0 0.0 1.0    in other words, blue
1.0 0.0 1.0    in other words, purple
</code></pre>
<p>Now I want to be able to selectively toggle the red or blue components of what's on the screen to 1.0 or 0.0, without affecting the other color components of what's on the screen.</p>
<p>For instance, say some pixel contains 1.0, 0.0, 1.0, purple, and I want to turn off the red in that pixel without changing the value of blue.</p>
<p>Assumption here is at this point I don't know if the blue in that pixel is 0.0 or 1.0. I just want to turn the red part of it off.</p>
<p>Also, I don't want to have to segregate the red changes from the blue changes into separate sets of vertex attributes, i.e. I'd like to be able to set just the red component of one set of pixels and set the blue component of another set of pixels in one call to glDrawArrays(). Also, within each set of pixels some of the reds I want to be able to set to 1.0 while others I want to set to 0.0 and likewise for the blue.</p>
<p>I have a hunch this would involve some creative usage of glBlendFunc(), but dang if I can figure out how to do it.</p>
<p>Note that what I am trying to accomplish here is a poor man's 3D rendering on a typical PC screen using a homemade set of goggles with a red lens for the right eye and a blue lens for the left eye. Think how they did 3D for black and white movies back in the 1950s.</p>
<p>Anybody got some suggestions?</p>
<p>Thanks</p>
<p>============================================================</p>
<p>To answer gilgamec's questions:</p>
<p>&quot;where are the pixels coming from? A texture map? An image? Rendered triangles?&quot;</p>
<p>The GLSL part of the application consists of a vertex shader and a fragment shader in one GLSL program and a vertex shader that does transform feedback in a second GLSL program. Pairs of vertices are encoded by the host CPU into a vertex attribute array and passed to the first GLSL program for rendering to the screen via a call to glDrawArrays(GL_LINES,...). The same set of vertices is then passed to the second GLSL program whose vertex shader does the same transformations as the first's but then feeds them back to the host CPU.</p>
<p>&quot;Why can't you alter the input directly?&quot;</p>
<p>Well, I can. The problem is algorithm running in the host CPU code that generates the vertex pairs is very CPU intensive and thus gets delivered to the GPUs rather slowly. Slow enough that there is a visible delay and there's really no way to speed that up much. It's modelling reflections of images off of sets of mirrors as viewed by some observer that moves around virtually among the set of mirrors. Imagine a big polyhedron made of mirrors with their reflective surfaces facing inward and the observer is standing inside of it along with a light source consisting of a collection of line segments. I've copied a non-steroscopic rendering of it as an example below. Here the mirrored polyhedron is a cube that the observer is standing in. The big red triangle is the single light source and everything else is reflections of reflections of the triangle plus reflections of the edges of the cube in blue. The observer himself is transparent so his head doesn't get in the way of the reflections. 8)</p>
<p><a href="https://i.sstatic.net/Z5ity.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/Z5ity.png" alt="cube" /></a></p>
<p>I can run this program in the usual manner in which on each pass through the main loop the screen is first cleared via glClear(), transform matrices are computed and vertices are rendered via glDrawArrays(GL_LINES...). The problem there is the vertices representing the reflections get generated slowly with large models, so it takes a visible while for rendering to complete. So I've implemented a second mode of operation in which I only clear the screen once at program start up. On each pass through the main loop after glDrawArrays() and the screen buffer swap I copy the front frame buffer to the back buffer. On the next pass through the loop as new reflections come out of the algorithm I use the feed back data from the second GLSL program to &quot;erase&quot; the old reflections from the screen by painting them black in the fragment shader before each new reflection is painted in red or blue. The effect isn't perfect, there's still lag, but the old reflections get sort of dragged along as the observer moves around until they finally get erased and replaced by new reflections once the observer sits still long enough. It's pretty, which is the whole point of this exercise.</p>
<p>So I've got this second &quot;incremental screen update&quot; mode implemented for non-stereoscopic (1 eyed observer) mode and it works. And I have non-incremental screen mode implemented for red-blue stereoscopic mode, see below:</p>
<p><a href="https://i.sstatic.net/tUKa9.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/tUKa9.png" alt="cube3d" /></a></p>
<p>If you happen to have some red and blue transparent sheets lying around, get in a dark room and look at the above with the red in front of your right eye and the blue in front of your left eye. Relax your eyes a bit and you should be able to perceive the reflections in 3D (at least I can see it and my eyes are 65 years old, 8).</p>
<p>But to get incremental screen mode to work in 3D, I need to be able to write 0.0 (to erase to black) or 1.0 to the blue component of pixels on the screen without affecting the red and vis-versa. I think I can do that with glColorMask() but that means segregating the vertex attributes corresponding to left eye (blue) perspective from those corresponding to right eye (red) perspective.</p>
<p>&quot;You mention OpenGL functions, so have you just tried modifying the fragment shader?&quot;</p>
<p>That's what the question is about. Is there a way to write to only the red or blue components of existing pixels in the frame buffer from a fragment shader?</p>

CC BY-SA 4.0
2021-08-09T08:49:58.950Z
You haven't really provided enough information to let us help you: where are the pixels coming from? A texture map? An image? Rendered triangles? Why can't you alter the input directly? You mention OpenGL functions, so have you just tried modifying the fragment shader?
gilgamec
CC BY-SA 4.0
2021-08-11T19:43:25.197Z
So are you looking to use blend equation of add?
joojaa
CC BY-SA 4.0
2021-08-13T02:08:27.397Z
joojaa, I don't understand your question. What I'm looking for is a way to specify which color components will be written to the screen buffer from a fragment shader. Something analogous to glColorMask() that can be set in GLSL rather than from the host CPU.
user256552
CC BY-SA 4.0
2022-04-11T10:23:13.353Z
I really do not understand why you can't use glColorMask. Whatever you are drawing, why can't you draw the red part with a red colour mask and the blue part with a blue colour mask?
Criticize SE actions means ban
CC BY-SA 4.0
2022-04-12T17:29:41.947Z
Honestly, this incremental update thing where you keep the old framebuffer data and try to erase bits of it, sounds like way more trouble than it's worth. You can keep the vertex buffer data generated by the CPU and re-render it from scratch on each new frame. GPUs are plenty fast enough for it - there's really no reason to mess around with trying to selectively erase and redraw bits of the previous frame. When the CPU has generated a new vertex buffer you can then swap that in at the  beginning of the next frame.
Nathan Reed
CC BY-SA 4.0
2022-04-14T23:11:08.273Z
Ya, I gave up on this approach months ago and now do as Nathan Reed suggests above. That latter approach works much better.
user256552
<blending>
how to blend RGB components separately
user256552
# how to blend RGB components separately

(Asked by: username_0 on 2021-08-08)

Suppose I have previously rendered some set of pixels on the screen that each contain one of the following RGB values:

```
0.0 0.0 0.0    in other words, black
1.0 0.0 0.0    in other words, red
0.0 0.0 1.0    in other words, blue
1.0 0.0 1.0    in other words, purple

```

Now I want to be able to selectively toggle the red or blue components of what's on the screen to 1.0 or 0.0, without affecting the other color components of what's on the screen.

For instance, say some pixel contains 1.0, 0.0, 1.0, purple, and I want to turn off the red in that pixel without changing the value of blue.

Assumption here is at this point I don't know if the blue in that pixel is 0.0 or 1.0. I just want to turn the red part of it off.

Also, I don't want to have to segregate the red changes from the blue changes into separate sets of vertex attributes, i.e. I'd like to be able to set just the red component of one set of pixels and set the blue component of another set of pixels in one call to glDrawArrays(). Also, within each set of pixels some of the reds I want to be able to set to 1.0 while others I want to set to 0.0 and likewise for the blue.

I have a hunch this would involve some creative usage of glBlendFunc(), but dang if I can figure out how to do it.

Note that what I am trying to accomplish here is a poor man's 3D rendering on a typical PC screen using a homemade set of goggles with a red lens for the right eye and a blue lens for the left eye. Think how they did 3D for black and white movies back in the 1950s.

Anybody got some suggestions?

Thanks

============================================================

To answer username_1's questions:

"where are the pixels coming from? A texture map? An image? Rendered triangles?"

The GLSL part of the application consists of a vertex shader and a fragment shader in one GLSL program and a vertex shader that does transform feedback in a second GLSL program. Pairs of vertices are encoded by the host CPU into a vertex attribute array and passed to the first GLSL program for rendering to the screen via a call to glDrawArrays(GL\_LINES,...). The same set of vertices is then passed to the second GLSL program whose vertex shader does the same transformations as the first's but then feeds them back to the host CPU.

"Why can't you alter the input directly?"

Well, I can. The problem is algorithm running in the host CPU code that generates the vertex pairs is very CPU intensive and thus gets delivered to the GPUs rather slowly. Slow enough that there is a visible delay and there's really no way to speed that up much. It's modelling reflections of images off of sets of mirrors as viewed by some observer that moves around virtually among the set of mirrors. Imagine a big polyhedron made of mirrors with their reflective surfaces facing inward and the observer is standing inside of it along with a light source consisting of a collection of line segments. I've copied a non-steroscopic rendering of it as an example below. Here the mirrored polyhedron is a cube that the observer is standing in. The big red triangle is the single light source and everything else is reflections of reflections of the triangle plus reflections of the edges of the cube in blue. The observer himself is transparent so his head doesn't get in the way of the reflections. 8)

[![cube](https://i.sstatic.net/Z5ity.png)](https://i.sstatic.net/Z5ity.png)

I can run this program in the usual manner in which on each pass through the main loop the screen is first cleared via glClear(), transform matrices are computed and vertices are rendered via glDrawArrays(GL\_LINES...). The problem there is the vertices representing the reflections get generated slowly with large models, so it takes a visible while for rendering to complete. So I've implemented a second mode of operation in which I only clear the screen once at program start up. On each pass through the main loop after glDrawArrays() and the screen buffer swap I copy the front frame buffer to the back buffer. On the next pass through the loop as new reflections come out of the algorithm I use the feed back data from the second GLSL program to "erase" the old reflections from the screen by painting them black in the fragment shader before each new reflection is painted in red or blue. The effect isn't perfect, there's still lag, but the old reflections get sort of dragged along as the observer moves around until they finally get erased and replaced by new reflections once the observer sits still long enough. It's pretty, which is the whole point of this exercise.

So I've got this second "incremental screen update" mode implemented for non-stereoscopic (1 eyed observer) mode and it works. And I have non-incremental screen mode implemented for red-blue stereoscopic mode, see below:

[![cube3d](https://i.sstatic.net/tUKa9.png)](https://i.sstatic.net/tUKa9.png)

If you happen to have some red and blue transparent sheets lying around, get in a dark room and look at the above with the red in front of your right eye and the blue in front of your left eye. Relax your eyes a bit and you should be able to perceive the reflections in 3D (at least I can see it and my eyes are 65 years old, 8).

But to get incremental screen mode to work in 3D, I need to be able to write 0.0 (to erase to black) or 1.0 to the blue component of pixels on the screen without affecting the red and vis-versa. I think I can do that with glColorMask() but that means segregating the vertex attributes corresponding to left eye (blue) perspective from those corresponding to right eye (red) perspective.

"You mention OpenGL functions, so have you just tried modifying the fragment shader?"

That's what the question is about. Is there a way to write to only the red or blue components of existing pixels in the frame buffer from a fragment shader?

**Tags:** blending

> **Comments:**
> **username_1**: You haven't really provided enough information to let us help you: where are the pixels coming from? A texture map? An image? Rendered triangles? Why can't you alter the input directly? You mention OpenGL functions, so have you just tried modifying the fragment shader?  
> **username_2**: So are you looking to use blend equation of add?  
> **username_0**: username_2, I don't understand your question. What I'm looking for is a way to specify which color components will be written to the screen buffer from a fragment shader. Something analogous to glColorMask() that can be set in GLSL rather than from the host CPU.  
> **username_3**: I really do not understand why you can't use glColorMask. Whatever you are drawing, why can't you draw the red part with a red colour mask and the blue part with a blue colour mask?  
> **username_4**: Honestly, this incremental update thing where you keep the old framebuffer data and try to erase bits of it, sounds like way more trouble than it's worth. You can keep the vertex buffer data generated by the CPU and re-render it from scratch on each new frame. GPUs are plenty fast enough for it - there's really no reason to mess around with trying to selectively erase and redraw bits of the previous frame. When the CPU has generated a new vertex buffer you can then swap that in at the  beginning of the next frame.  
> **username_0**: Ya, I gave up on this approach months ago and now do as username_4 username_4 suggests above. That latter approach works much better.  

---

## 1 answers

### Answer by username_3 on 2022-04-11. Score: 1

Since you only want full brightness and no brightness, sure you can.

Red channel on: additive blend with red  
Red channel off: multiplicative blend with cyan  
Blue channel on: additive blend with blue  
Blue channel off: multiplicative blend with yellow

I also don't understand why you can't just run your rendering code twice: once with a red color mask and once with a blue color mask. It doesn't seem like you need to control it from GLSL. You're allowed to render the same vertices twice with different states - commercial video games do that sort of thing routinely.

Switching the blend mode from additive to multiplicative is a state change, just like setting the colour mask.


---

