# Alpha blending for mixtures with Screen(&#39;FillRect&#39;)

**URL:** <https://psychtoolbox.discourse.group/t/alpha-blending-for-mixtures-with-screen-39-fillrect-39/137>\
**Category:** Uncategorized\
**Created:** [December 6, 2016, 11:04am UTC](https://psychtoolbox.discourse.group/t/alpha-blending-for-mixtures-with-screen-39-fillrect-39/137 "2016-12-06T11:04:48Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Allen\_Ingling](https://avatars.discourse-cdn.com/v4/letter/a/a183cd/32.png) [@Allen\_Ingling](https://psychtoolbox.discourse.group/u/Allen_Ingling)\
**Post date:** [December 6, 2016, 11:04am UTC](https://psychtoolbox.discourse.group/t/alpha-blending-for-mixtures-with-screen-39-fillrect-39/137/1 "2016-12-06T11:04:48Z")

</div>

I'd like to create a visual stimulus that is a mixture of two components, each of which is created using the Screen('FillRect', ...) command. The alpha-blending strategies suggested in BubbleDemo.m for example, seems like a reasonable approach, but I can't get it to work. In particular, I can't get the mixing weights to apply to an image drawn with Screen('FillRect', ...) rather than Screen('DrawTexture', ...). For example, exchanging the relevant Screen('DrawTexture', ...) commands in the SimpleImageMixingDemo.m (lines 127 and 131 in my version of PTB) with Screen('FillRect', ...) commands does not work.  
  
I've also tried the strategies used in both BubbleDemo.m and ImageMixingTutorial.m, but neither seems to work. Is it possible to use alpha-blending to mix two rectangles drawn via Screen('FillRect', ...) rather than Screen('DrawTexture', ...)?  
  
To be clear, I know that I can mix two rectangles by specifying their alpha-values directly in the Screen('FillRect', ...) command. This would work if the two objects are in the same spatial location on the screen and if the alpha value was constant across the entire rectangle. But I would like to use the alpha blending to specify a spatial arrangement of the components, i.e., one will only be at one location and the other will be at another location. Furthermore, the location of each component will change over time. So I'd really like to be able to specify the alpha-mask texture as an arbitrary matrix, whose elements select which rectangles are in which location on the screen.

---

<div class="post-metadata">

**Author:** ![mannow1](https://avatars.discourse-cdn.com/v4/letter/m/919ad9/32.png) [@mannow1](https://psychtoolbox.discourse.group/u/mannow1)\
**Post date:** [March 27, 2017, 7:06pm UTC](https://psychtoolbox.discourse.group/t/alpha-blending-for-mixtures-with-screen-39-fillrect-39/137/2 "2017-03-27T19:06:12Z")

</div>

Hi,&nbsp;
  

I think I am facing the same issue. Although there are many ways to display our stimuli, I would like to simply switch from drawtexture to fillrect in my code as (the following code is based on ImageMixingTutorial.m)

  

for ll = 1 : MaskMotion

% Draw the mask

&nbsp; &nbsp; &nbsp;&nbsp;

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Screen('BlendFunction', number, GL\_ONE, GL\_ZERO, [0 0 0 1]);&nbsp;

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Screen('FillOval', number, [0 0 0 0], Tab(ll).Rect);&nbsp;

  

% &nbsp;Draw peripheral image

  

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Screen('BlendFunction', number, GL\_DST\_ALPHA, GL\_ZERO, [1 1 1 0]);

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Screen('DrawTexture', number, ImageTexture1,&nbsp;&nbsp;[] ,&nbsp; []);

  

% Draw foveated imag,

  

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Screen('BlendFunction', number, GL\_ONE\_MINUS\_DST\_ALPHA, GL\_ONE, [1 1 1 0]);

  

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if Condition1 %image to be displayed

&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Screen('DrawTexture', number, ImageTexture2, &nbsp;&nbsp;[] ,&nbsp; []);

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; elseif&nbsp;Condition2&nbsp;% simple color to be displayed instead

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Screen('FillRect', number, [R G B]);

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end&nbsp;

  

If condition 1 is called here, the blending function works fine (see image 2 through an apperture/mask, but if condition 2 is called, the fillrect fills the all screen, so I guess that fillrect dosn't obey 'blendfunction'.

  

I worked around this issue writing condition 2 as:

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; elseif&nbsp;Condtion2&nbsp;% simple color to be displayed instead

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;MyRectBack = cat(3, R, G, B);

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;rectText = Screen('MakeTexture', number, MyRectBack); &nbsp; &nbsp;&nbsp;

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Screen('DrawTexture', number,rectText , [] ,&nbsp;[]);

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end&nbsp;

  

It works fine, but it is still slower than fillrect (x10 slower) and I wonder why fillrect does not obey blendfunction? I checked the C code and isn't it supposed to do so?

  

Best regards&nbsp;

&nbsp;

  

Romain

---

<div class="post-metadata">

**Author:** ![mannow1](https://avatars.discourse-cdn.com/v4/letter/m/919ad9/32.png) [@mannow1](https://psychtoolbox.discourse.group/u/mannow1)\
**Post date:** [March 27, 2017, 8:23pm UTC](https://psychtoolbox.discourse.group/t/alpha-blending-for-mixtures-with-screen-39-fillrect-39/137/3 "2017-03-27T20:23:58Z")

</div>

Quick update,
it might be a bug since the previous code works fine if I call 'FillOval' or FillPoly instead od 'FillRect'

  

  

Romain
