# Glew.h and other shaders

**URL:** <https://mworks.discourse.group/t/glew-h-and-other-shaders/308>\
**Category:** Support\
**Created:** [January 8, 2018, 3:26pm UTC](https://mworks.discourse.group/t/glew-h-and-other-shaders/308 "2018-01-08T15:26:13Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![cstawarz](https://yyz1.discourse-cdn.com/flex031/user_avatar/mworks.discourse.group/cstawarz/32/3_2.png) [@cstawarz](https://mworks.discourse.group/u/cstawarz)\
**Post date:** [January 8, 2018, 3:26pm UTC](https://mworks.discourse.group/t/glew-h-and-other-shaders/308/1 "2018-01-08T15:26:13Z")

</div>

Hi Chris,

Is there a reason why you removed those?  
We have expanded the functionality of the sinusoidal grating plugin relying on those libraries, and now that they are gone, we cannot compile the plugin with new versions of mworks.

Best  
Najib

---

<div class="post-metadata">

**Author:** ![cstawarz](https://yyz1.discourse-cdn.com/flex031/user_avatar/mworks.discourse.group/cstawarz/32/3_2.png) [@cstawarz](https://mworks.discourse.group/u/cstawarz)\
**Post date:** [January 9, 2018, 4:50pm UTC](https://mworks.discourse.group/t/glew-h-and-other-shaders/308/2 "2018-01-09T16:50:00Z")

</div>

Hi Najib,

Until recently, MWorks’ stimulus drawing code used the (rather ancient) OpenGL 2.1 API. As part of porting MWorks to iOS, I updated it to use the (somewhat less ancient) OpenGL 3.3 API, which is largely compatible with the OpenGL ES 3.0 API available on iOS.

The source files in question were written against OpenGL 2.1 and wouldn’t work with the updated stimulus code. Since they were used exactly nowhere in MWorks’ core or standard plugins, I simply discarded them.

Updating your stimulus plugin to function with the current MWorks nightly build (and upcoming 0.8 release) is going to take some work. Apple has an [overview document](https://developer.apple.com/library/content/documentation/GraphicsImaging/Conceptual/OpenGL-MacProgGuide/UpdatinganApplicationtoSupportOpenGL3/UpdatinganApplicationtoSupportOpenGL3.html) describing the changes required to move from OpenGL 2.1 to 3.3. However, if your grating plugin derives from MWorks’ stock grating, then the best starting point in probably the latest [drifting grating source code](https://github.com/mworks/mworks/blob/master/plugins/core/DriftingGratingStimulus/DriftingGratingStimulus.cpp).

Of course, I’m happy to provide whatever help you need to make this transition. Alternatively, if you’re interested in sharing your expanded functionality with the broader MWorks community, then we can discuss integrating your changes in to the standard grating.

Cheers,  
Chris

---

<div class="post-metadata">

**Author:** ![cstawarz](https://yyz1.discourse-cdn.com/flex031/user_avatar/mworks.discourse.group/cstawarz/32/3_2.png) [@cstawarz](https://mworks.discourse.group/u/cstawarz)\
**Post date:** [February 27, 2018, 4:55pm UTC](https://mworks.discourse.group/t/glew-h-and-other-shaders/308/3 "2018-02-27T16:55:26Z")

</div>

Dear Chris,

I had hoped that it would be easy to port some of our expanded  
functionality to the latest version of mworks, but I am worried that we  
might have hit a wall and would appreciate any help you can provide.

The two main things we would like to achieve:

1-Give mworks the ability to set/change gamma.  
2-Expand the drifting grating plugin to allow for multiple gratings that  
can be added and multiplied.

I am more than happy to share our old code, but I worry that it is probably  
not as documented as you would like. Is there a chance we can chat via  
skype to make a plan.

I know that you have been in the process of releasing a new version of  
mworks, but maybe we can at least make a plan.

Best  
Najib

---

<div class="post-metadata">

**Author:** ![cstawarz](https://yyz1.discourse-cdn.com/flex031/user_avatar/mworks.discourse.group/cstawarz/32/3_2.png) [@cstawarz](https://mworks.discourse.group/u/cstawarz)\
**Post date:** [March 1, 2018, 3:12pm UTC](https://mworks.discourse.group/t/glew-h-and-other-shaders/308/4 "2018-03-01T15:12:06Z")

</div>

Hi Najib,

Probably the best approach would be for me to take a look at your old code first, as that will give me a clear idea of what needs to be done. Can you point me to it?

Thanks,  
Chris

---

<div class="post-metadata">

**Author:** ![cstawarz](https://yyz1.discourse-cdn.com/flex031/user_avatar/mworks.discourse.group/cstawarz/32/3_2.png) [@cstawarz](https://mworks.discourse.group/u/cstawarz)\
**Post date:** [March 7, 2018, 4:48pm UTC](https://mworks.discourse.group/t/glew-h-and-other-shaders/308/5 "2018-03-07T16:48:16Z")

</div>

Dear Chris,

Enclosed please fine an archive that includes the changes we care about:

1-The changes that allowed us to do gamma correction are mostly in the  
server and the stimulus display, I believe Romesh did his best to comment  
on the code.

2-Our drifting grating plugin builds from the old plugin, it allow for  
multiple gratings that can be added or multiplied and different windowing  
options.

Take a look and let me know how you would like to proceed. The sooner we  
can implement the easier it is for me to push the lab to adopt the new  
mworks.

Best  
Najib

---

<div class="post-metadata">

**Author:** ![cstawarz](https://yyz1.discourse-cdn.com/flex031/user_avatar/mworks.discourse.group/cstawarz/32/3_2.png) [@cstawarz](https://mworks.discourse.group/u/cstawarz)\
**Post date:** [March 9, 2018, 3:13pm UTC](https://mworks.discourse.group/t/glew-h-and-other-shaders/308/6 "2018-03-09T15:13:33Z")

</div>

Dear Najib and Chris,

Chris, I think I got Romesh’s modifications to your code to include the monitor gamma correction to work on version 7 of MWorks. I’ve attached the code for that, hopefully you can see where the changes were made, and incorporate them as appropriate.

Thank you so much for working on the other aspects of MWorks (drifting gratings and words out on the firmata protocol).

Best,  
mariana

---

<div class="post-metadata">

**Author:** ![cstawarz](https://yyz1.discourse-cdn.com/flex031/user_avatar/mworks.discourse.group/cstawarz/32/3_2.png) [@cstawarz](https://mworks.discourse.group/u/cstawarz)\
**Post date:** [March 9, 2018, 9:31pm UTC](https://mworks.discourse.group/t/glew-h-and-other-shaders/308/7 "2018-03-09T21:31:08Z")

</div>

Hi Najib & Mariana,

Thanks for sending the code. I’ll try to take a look at it and get back to you early next week.

Cheers,  
Chris

---

<div class="post-metadata">

**Author:** ![cstawarz](https://yyz1.discourse-cdn.com/flex031/user_avatar/mworks.discourse.group/cstawarz/32/3_2.png) [@cstawarz](https://mworks.discourse.group/u/cstawarz)\
**Post date:** [March 9, 2018, 9:38pm UTC](https://mworks.discourse.group/t/glew-h-and-other-shaders/308/8 "2018-03-09T21:38:12Z")

</div>

Thank you Chris, much appreciated.

Best  
Najib

---

<div class="post-metadata">

**Author:** ![cstawarz](https://yyz1.discourse-cdn.com/flex031/user_avatar/mworks.discourse.group/cstawarz/32/3_2.png) [@cstawarz](https://mworks.discourse.group/u/cstawarz)\
**Post date:** [March 15, 2018, 2:36pm UTC](https://mworks.discourse.group/t/glew-h-and-other-shaders/308/9 "2018-03-15T14:36:58Z")

</div>

Hi Najib & Mariana,

I had a look at the code you sent.

The changes to support gamma control appear very straightforward, and I think we should make them (or equivalent) a standard MWorks feature. However, this won’t happen until after the impending 0.8 release. Is that OK with you?

The drifting grating changes are a bit more involved, although it still doesn’t seem like a huge job. I think it will take a couple days of my time to update the plugin, but, again, this probably won’t happen until after the 0.8 release. Are you interested in contributing these changes to MWorks, so that others in the community can use them, too? To be clear, this does _not_ affect my willingness to work on the plugin in any way. Also, I’m not 100% sure at this point if it would even make sense to try to integrate your changes into the standard grating stimulus. I’m just wondering if you’re open to that possibility.

Cheers,  
Chris

---

<div class="post-metadata">

**Author:** ![cstawarz](https://yyz1.discourse-cdn.com/flex031/user_avatar/mworks.discourse.group/cstawarz/32/3_2.png) [@cstawarz](https://mworks.discourse.group/u/cstawarz)\
**Post date:** [March 15, 2018, 2:59pm UTC](https://mworks.discourse.group/t/glew-h-and-other-shaders/308/10 "2018-03-15T14:59:23Z")

</div>

Dear Chris,

Absolutely 🙂 Completely okay with us, the reason why we didn’t want to  
share them is because I wasn’t sure that we could support them…  
When do you anticipate the 0.8 release?

Best  
Najib

---

<div class="post-metadata">

**Author:** ![cstawarz](https://yyz1.discourse-cdn.com/flex031/user_avatar/mworks.discourse.group/cstawarz/32/3_2.png) [@cstawarz](https://mworks.discourse.group/u/cstawarz)\
**Post date:** [March 16, 2018, 2:12pm UTC](https://mworks.discourse.group/t/glew-h-and-other-shaders/308/11 "2018-03-16T14:12:27Z")

</div>

Hi Najib,

> Absolutely 🙂 Completely okay with us, the reason why we didn’t want to  
> share them is because I wasn’t sure that we could support them…

Great, thanks! One benefit of sharing your code is that maintaining it becomes my job 🙂

> When do you anticipate the 0.8 release?

I hope to have it out sometime in the next few weeks.

Chris

---

<div class="post-metadata">

**Author:** ![cstawarz](https://yyz1.discourse-cdn.com/flex031/user_avatar/mworks.discourse.group/cstawarz/32/3_2.png) [@cstawarz](https://mworks.discourse.group/u/cstawarz)\
**Post date:** [March 16, 2018, 3:43pm UTC](https://mworks.discourse.group/t/glew-h-and-other-shaders/308/12 "2018-03-16T15:43:43Z")

</div>

Awesome.  
Next few weeks is completely workable for us.  
Once I have chat with Romesh, there are other plugins that are not so high  
priority that I might ask your advice on.

Best  
Najib

---

<div class="post-metadata">

**Author:** ![cstawarz](https://yyz1.discourse-cdn.com/flex031/user_avatar/mworks.discourse.group/cstawarz/32/3_2.png) [@cstawarz](https://mworks.discourse.group/u/cstawarz)\
**Post date:** [May 8, 2018, 2:27pm UTC](https://mworks.discourse.group/t/glew-h-and-other-shaders/308/13 "2018-05-08T14:27:53Z")

</div>

Hi Najib & Mariana,

The MWorks [nightly build](https://mworks.github.io/downloads/) now includes support for setting the display gamma.

Note that I used different keys than you did in `#mainScreenInfo`: `set_display_gamma` controls whether the gamma is set or not; and `red_gamma`, `green_gamma`, and `blue_gamma` specify the (inverse) gamma value for each channel. You can change these settings in MWServer’s preferences window (Display → Advanced…).

Also, the gamma settings apply only when color management is disabled (which can also be done in Display → Advanced…). If your goal is linear color response (e.g. for gratings), then profiling your display and enabling color management in MWorks is a better solution than changing the display gamma. But in any case, doing both at the same time makes no sense, so MWorks doesn’t allow it.

When you have a chance, please test these changes and let me know if they meet your needs.

Thanks,  
Chris

---

<div class="post-metadata">

**Author:** ![cstawarz](https://yyz1.discourse-cdn.com/flex031/user_avatar/mworks.discourse.group/cstawarz/32/3_2.png) [@cstawarz](https://mworks.discourse.group/u/cstawarz)\
**Post date:** [June 27, 2018, 3:03pm UTC](https://mworks.discourse.group/t/glew-h-and-other-shaders/308/14 "2018-06-27T15:03:07Z")

</div>

Hi Najib,

I’m trying to understand the definition of the “flattop” mask types in your drifting grating code (and which you also mentioned in [another discussion](https://mworks.discourse.group/discussions/questions/790#comment_44341708)). Searching the web for “flattop distribution” or “flat-top Gaussian” doesn’t yield much info. [This](https://math.stackexchange.com/a/1514765) and [this](https://stats.stackexchange.com/a/237660) are about all I could find, and neither really matches the equation in the code.

In said code, the value of the non-flat portion of the mask is the same as that of an un-normalized Gaussian mask with mean `1-2/N` and standard deviation `2/(3*N)`, where `N` is 8 or 4 (for flattop/flattop8 and flattop4, respectively). The flat portion begins where the distance from the center equals the Gaussian mean.

My question: Why do you use those particular values for the mean and standard deviation? Is there some reference you can point me to that explains the reasoning behind them?

Thanks,  
Chris

---

<div class="post-metadata">

**Author:** ![cstawarz](https://yyz1.discourse-cdn.com/flex031/user_avatar/mworks.discourse.group/cstawarz/32/3_2.png) [@cstawarz](https://mworks.discourse.group/u/cstawarz)\
**Post date:** [June 27, 2018, 4:18pm UTC](https://mworks.discourse.group/t/glew-h-and-other-shaders/308/15 "2018-06-27T16:18:38Z")

</div>

Hi Christopher,

Great to hear from you.  
Flatop masks or raised cosine masks are lab jargon that you probably won’t  
find references for online.  
Similar to a guassian mask, the idea is to taper the age of the window.  
Hard edges typically introduces very high frequency components that are  
quite visible and the taper is meant to get rid of that.  
If you us an actual gaussian to, the top of the guassian is rounded and not  
flat, in other words the luminance starts dropping as soon as you leave the  
center of the images.  
The flattop mask is meant as work around from that, the middle of the image  
will always have the same luminance and taper only starts at some point.  
The steepness of the taper is then determined by the standard deviation of  
a gaussian and one half of the gaussian is used to taper the edge.

Hope this is clear, if you can think of a cleaner way of achieving that, I  
would love to discuss it.

On a different note, I have been meaning to contact you regarding data  
analysis.  
The matlab getEvents funcition is rather slow when trying to import large  
files and signals like eye position that are updated often.  
Is there a faster way to get around that problem?

Best  
Najib

---

<div class="post-metadata">

**Author:** ![cstawarz](https://yyz1.discourse-cdn.com/flex031/user_avatar/mworks.discourse.group/cstawarz/32/3_2.png) [@cstawarz](https://mworks.discourse.group/u/cstawarz)\
**Post date:** [June 27, 2018, 5:12pm UTC](https://mworks.discourse.group/t/glew-h-and-other-shaders/308/16 "2018-06-27T17:12:06Z")

</div>

Hi Najib,

> The flattop mask is meant as work around from that, the middle of the image  
> will always have the same luminance and taper only starts at some point.  
> The steepness of the taper is then determined by the standard deviation of  
> a gaussian and one half of the gaussian is used to taper the edge.

Yes, that’s all fine. The question is, why are the mean (i.e. the radius of the untapered central region) and the standard deviation defined by the particular expressions I mentioned? Do you just like the look of the taper they produce?

> if you can think of a cleaner way of achieving that, I would love to discuss it.

I can think of a simpler and more general way of achieving it: Instead of having separate “flattop” mask types, just add another parameter for the Gaussian mask that controls what happens in the center when the mean is non-zero. That is, does it decay back toward zero, producing a ring-like mask, or does it stay at 1, producing a flat center with Gaussian outer edge (left vs. right in the attached image)?

This is what I’ll do, unless there’s a particular reason to constrain the mean and standard deviation the way your plugin does.

Thanks,  
Chris

---

<div class="post-metadata">

**Author:** ![cstawarz](https://yyz1.discourse-cdn.com/flex031/user_avatar/mworks.discourse.group/cstawarz/32/3_2.png) [@cstawarz](https://mworks.discourse.group/u/cstawarz)\
**Post date:** [June 27, 2018, 5:12pm UTC](https://mworks.discourse.group/t/glew-h-and-other-shaders/308/17 "2018-06-27T17:12:06Z")

</div>

Attachment: [gaussian\_vs\_flattop.jpeg](https://mworks.discourse.group/uploads/short-url/taSyqUUhjzsHvkQP63IE06IyNj7.jpeg) (19.8 KB)

---

<div class="post-metadata">

**Author:** ![cstawarz](https://yyz1.discourse-cdn.com/flex031/user_avatar/mworks.discourse.group/cstawarz/32/3_2.png) [@cstawarz](https://mworks.discourse.group/u/cstawarz)\
**Post date:** [June 28, 2018, 4:30pm UTC](https://mworks.discourse.group/t/glew-h-and-other-shaders/308/18 "2018-06-28T16:30:21Z")

</div>

Hi Chris,

I am a bit confused about the image that you sent, in particular, I am not  
sure why we would need the donut (left image).  
I am attaching a figure that shows a cross section of the tapers that we  
are trying to achieve.  
The idea is that you specify the size of the mask in this case 256 pixels.  
If the mask is a sharp circle, then this cross section profile would like a  
rectangle. Full transparency from -128:128 and opaque elsewhere.  
In the flattop8 condition the taper is on each side is 32 pixels wide 1/8  
of 256 hence the flattop8 designation 🙂  
In the flattop4 condition the taper is 64 pixels on each side 1/4 of 256  
producing a flattop4 mask.  
There are multiple ways to achieve this with minor differences. The key is  
that the taper can’t be a straight line because you produce artifacts so it  
has to look like a gaussian or a sinewave.

Best  
Najib

---

<div class="post-metadata">

**Author:** ![cstawarz](https://yyz1.discourse-cdn.com/flex031/user_avatar/mworks.discourse.group/cstawarz/32/3_2.png) [@cstawarz](https://mworks.discourse.group/u/cstawarz)\
**Post date:** [June 28, 2018, 4:30pm UTC](https://mworks.discourse.group/t/glew-h-and-other-shaders/308/19 "2018-06-28T16:30:21Z")

</div>

Attachment: [flatop4\_flattop8.pdf](https://mworks.discourse.group/uploads/short-url/wePdqivDDAb5wHSWC1kzGqjZvaj.pdf) (4.69 KB)

---

<div class="post-metadata">

**Author:** ![cstawarz](https://yyz1.discourse-cdn.com/flex031/user_avatar/mworks.discourse.group/cstawarz/32/3_2.png) [@cstawarz](https://mworks.discourse.group/u/cstawarz)\
**Post date:** [June 29, 2018, 2:47pm UTC](https://mworks.discourse.group/t/glew-h-and-other-shaders/308/20 "2018-06-29T14:47:27Z")

</div>

Hi Najib,

> I am a bit confused about the image that you sent, in particular, I am not sure why we would need the donut (left image).

Sorry, I should have provided more context. I don’t imagine you (or anyone) would need the donut. I was just illustrating what the drifting grating’s [mean](https://mworks.github.io/documentation/latest/components/drifting_grating_stimulus.html#mean) parameter does, namely, turn the Gaussian peak into a circle, instead of a point. (Why it works this way is [not at all clear](https://mworks.discourse.group/discussions/questions/736).) If you modify the Gaussian mask so that the alpha is uniformly one inside the circle (thereby filling the donut hole), you get a mask that’s uniform in the center and Gaussian at the edge, which is exactly what your “flattop” masks are.

> In the flattop8 condition the taper is on each side is 32 pixels wide 1/8  
> of 256 hence the flattop8 designation 🙂  
> In the flattop4 condition the taper is 64 pixels on each side 1/4 of 256 producing a flattop4 mask.

And to get these edge widths using MWorks’ grating, you would set `mean` to 0.75 and 0.5, respectively:

```
mean = 1 - 2/N
1 - 2/8 = 0.75
1 - 2/4 = 0.5

```

> There are multiple ways to achieve this with minor differences. The key is that the taper can’t be a straight line because you produce artifacts so it has to look like a gaussian or a sinewave.

OK, got it. I’ll figure out the best way to integrate support for this in to MWorks.

Thanks,  
Chris

[Next page](https://mworks.discourse.group/t/glew-h-and-other-shaders/308.md?page=2)
