# OpenGLShaderProgram::Uniform::set confusion

**URL:** <https://forum.juce.com/t/openglshaderprogram-set-confusion/28112>\
**Category:** General JUCE discussion\
**Created:** [June 13, 2018, 12:06pm UTC](https://forum.juce.com/t/openglshaderprogram-set-confusion/28112 "2018-06-13T12:06:17Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![PluginPenguin](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/pluginpenguin/32/10203_2.png) [@PluginPenguin](https://forum.juce.com/u/PluginPenguin)\
**Post date:** [June 13, 2018, 12:06pm UTC](https://forum.juce.com/t/openglshaderprogram-set-confusion/28112/1 "2018-06-13T12:06:18Z")

</div>

Hi,

while learning some OpenGL, I stumbled upon one thing I don’t understand.

`OpenGLShaderProgram::Uniform::set` has multiple overloaded implementations. I now want to set a `uniform vec4 uMyVec` in my fragment shader. `std::unique_ptr<OpenGLShaderProgram::Uniform> myVec` represents this uniform. For debugging purposes I assign it’s value inside the shader to the colour of the shape I’m drawing.

The following works, e.g. the shape becomes completely white:  
`myVec->set (1.0f, 1.0f, 1.0f, 1.0f);`

I’d expect this call to do the same:

```auto
float white[4] = {1.0f, 1.0f, 1.0f, 1.0f};
myVec->set (white, 4);

```

However the second version results in an invisible shape. Did I get something about the pointer + numValues overload completely wrong or is that a bug?

By the way, to be a bit more specific about what I’m actually trying to do: I’m using glm as it has a few more sophisticated linear algebra operations compared to the onboard JUCE classes. So my final approach is to call `myVec->set (glm::value_ptr (someGlmVector4), 4);` but this doesn’t work - however `glm::value_ptr` returns some valid results, so this can not be the problem.

---

<div class="post-metadata">

**Author:** ![PluginPenguin](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/pluginpenguin/32/10203_2.png) [@PluginPenguin](https://forum.juce.com/u/PluginPenguin)\
**Post date:** [June 13, 2018, 7:15pm UTC](https://forum.juce.com/t/openglshaderprogram-set-confusion/28112/2 "2018-06-13T19:15:44Z")

</div>

Okay, I finally got it.

A look at the OpenGL docs show that there are a lot of different possible calls to set uniforms: [https://www.khronos.org/registry/OpenGL-Refpages/gl4/html/glUniform.xhtml](https://www.khronos.org/registry/OpenGL-Refpages/gl4/html/glUniform.xhtml)  
(By the way, being used to the nice looking JUCE online documentation I am shocked of how ugly the khronos documentation website looks 😬)

To set a `uniform vec4` there are those two possibilities:

```auto
void glUniform4f (GLint location, GLfloat v0, GLfloat v1, GLfloat v2,GLfloat v3);

void glUniform4fv (GLint location, GLsizei count, const GLfloat *value);

```

The first option is more or less obvious (side note to the JUCE team - why is the fourth parameter a normal float instead of a GLfloat in the JUCE-Implementation? --\> [https://github.com/WeAreROLI/JUCE/blob/06c3674e510f991d88f881203aedd85a60fbced9/modules/juce\_opengl/opengl/juce\_OpenGLShaderProgram.cpp#L156](https://github.com/WeAreROLI/JUCE/blob/06c3674e510f991d88f881203aedd85a60fbced9/modules/juce_opengl/opengl/juce_OpenGLShaderProgram.cpp#L156))

However the second option has the count argument. According to the khronos docs, this allows the user to assign an array of `vec4` uniforms. Coming back to my initial post, this proved to work:

```auto
float white[4] = {1.0f, 1.0f, 1.0f, 1.0f};
glUniform4fv (myVec->uniformID, 1, white); 

```

As well as what I really want to do with glm:

```auto
glUniform4fv (uniforms.lightPosition->uniformID, 1, glm::value_ptr (lightPosition));

```

Looking further at the JUCE codebase, I found out that `OpenGLShaderProgram::Uniform::set (const GLfloat *values, int numValues)` calls `glUniform1fv (uniformID, numValues, values)` which might be used to assign an array of uniform float scalars, but not to assign a uniform vector.

So basically this was my fault as a beginner as I didn’t read the documentation of the underlying OpenGL API. This explains why assigning a `vec4` this way didn’t work.

To avoid this pitfall for other OpenGL newbies my proposal to the JUCE team would be at least to change the documentation a bit so that it clearly says `Sets an array of scalar float uniforms` instead of the current `Sets a vector float uniform` as vector is some kind of “reserved” OpenGL keyword that was misleading in this case for me.

Furthermore, what about adding `glUniform2fv`, `glUniform3fv` and `glUniform4fv` setter functions to `OpenGLShaderProgram::Uniform` and maybe while in that section of code even overload the `OpenGLShaderProgram::Uniform::setMatrix4` to directly accept a `Matrix3D<float>` which would make this API a bit more complete and a lot nicer to use? @fabian, @jules (I think you are the ones mainly in charge of all this UI stuff?)

---

<div class="post-metadata">

**Author:** ![matkatmusic](https://avatars.discourse-cdn.com/v4/letter/m/dfb087/32.png) [@matkatmusic](https://forum.juce.com/u/matkatmusic)\
**Post date:** [June 14, 2018, 7:38am UTC](https://forum.juce.com/t/openglshaderprogram-set-confusion/28112/3 "2018-06-14T07:38:00Z")

</div>

> [@PluginPenguin](#):
>
> So basically this was my fault as a beginner as I didn’t read the documentation

Man, this quote should be a sticky post for all the newbs flooding the forum lately!!!

---

<div class="post-metadata">

**Author:** ![PluginPenguin](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/pluginpenguin/32/10203_2.png) [@PluginPenguin](https://forum.juce.com/u/PluginPenguin)\
**Post date:** [June 14, 2018, 7:55am UTC](https://forum.juce.com/t/openglshaderprogram-set-confusion/28112/4 "2018-06-14T07:55:09Z")

</div>

🤣

Glad to make you happy with this little bit of self-criticism (which in my opinion is crucial for getting better in anything one learns…)

---

<div class="post-metadata">

**Author:** ![matkatmusic](https://avatars.discourse-cdn.com/v4/letter/m/dfb087/32.png) [@matkatmusic](https://forum.juce.com/u/matkatmusic)\
**Post date:** [June 14, 2018, 7:56am UTC](https://forum.juce.com/t/openglshaderprogram-set-confusion/28112/5 "2018-06-14T07:56:47Z")

</div>

Print that out and tape it to the bottom of your monitor!
