<blockquote>
<pre><code>glUniformMatrix4fv(transformLocation, sizeof(transform), GL_FALSE, &amp;transform.m[0][0]);
</code></pre>
</blockquote>

<p>The <a href="http://docs.gl/gl4/glUniform" rel="nofollow noreferrer">second parameter to <code>glUniformMatrix</code></a> is not the number of bytes in the data. OpenGL can figure that out from the <code>4fv</code> part of the command (a 4x4 matrix of <code>GLfloat</code>s). The second parameter is the number of <em>matrices</em> you're sending to that uniform, in case it is an array of <code>mat4</code>s.</p>

<p>So that value ought to be 1. You almost certainly got a <code>GL_INVALID_OPERATION</code> error from calling it with <code>sizeof(transform)</code>.</p>

CC BY-SA 3.0
2017-01-21T00:39:31.387Z
Thank you for your reply Nicol. I`ve fixed the argument mistake and found out the root cause. I have specified 'transformation' as my uniform value and was trying to use 'transform'.... It works llike a charm now.
Jigo
CC BY-SA 3.0
2017-01-21T00:27:46.957Z
2017-01-21T00:27:46.957Z
Nicol Bolas
<p>I am fairly new to graphics programming and I am trying to pass a custom Matrix4 (non-glm) to my vertex shader by using a uniform.</p>

<pre><code>    Matrix44 transform;  //creates a 4x4 identity matrix
    transform.SetTranslation(Vector3(0.5f, 0.3f, 0.0f));
    GLint transformLocation = glGetUniformLocation(ourShader.Program, "transformation");
    glUniformMatrix4fv(transformLocation, sizeof(transform), GL_FALSE, &amp;transform.m[0][0]);
</code></pre>

<p>Above is the code which I am using to send the matrix to the shaders.</p>

<p>(the .m allows me to access the matrix as a 2D array of 4x4 floats);</p>

<p>This is how my vertex shader looks like:</p>

<pre><code>#version 430 core

layout (location = 0) in vec3 position;

uniform mat4 transform;

void main()
{                   
   gl_Position = transform*vec4(position,1.0f);     
}
</code></pre>

<p>When I run it, I don't get anything drawn on my screen, except the background color.</p>

<p>Below is the matrix which I send to the shader. I am using column major, so as far as I've read this should be the translation part of the 4x4. I've also tried transposing the matrix with no luck whatsoever.</p>

<p><a href="https://i.sstatic.net/ay01I.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/ay01I.png" alt="enter image description here"></a></p>

<p>The last clue I have is that the values which I am sending are 0s. I came to this conclusion because I tried playing with the fragment shader and I set the color to be equal to the last column from the uniform mat4:</p>

<pre><code>#version 430 core
out vec4 color;

uniform mat4 transform;

void main()
{
 color = vec4(transform[0][3],transform[1][3], transform[2][3], transform[3][3]);   
} 
</code></pre>

<p>Which gives me a black triangle i.e. the color vec4 is filled with 0s.</p>

<p><a href="https://i.sstatic.net/Fi9c5.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/Fi9c5.png" alt="enter image description here"></a></p>

<p>I apologize in advance if the question is too noob-ish but I've been struggling with this for several days and I could not find a way to run it :(</p>

<p>Any advise would be appreciated!</p>

<opengl><shader><glsl><matrix>
Unable to pass custom Matrix4 to GLSL as a uniform
Jigo
# Unable to pass custom Matrix4 to GLSL as a uniform

(Asked by: username_0 on 2017-01-20)

I am fairly new to graphics programming and I am trying to pass a custom Matrix4 (non-glm) to my vertex shader by using a uniform.
    Matrix44 transform;  //creates a 4x4 identity matrix
    transform.SetTranslation(Vector3(0.5f, 0.3f, 0.0f));
    GLint transformLocation = glGetUniformLocation(ourShader.Program, "transformation");
    glUniformMatrix4fv(transformLocation, sizeof(transform), GL_FALSE, &transform.m[0][0]);

Above is the code which I am using to send the matrix to the shaders.
(the .m allows me to access the matrix as a 2D array of 4x4 floats);
This is how my vertex shader looks like:
#version 430 core

layout (location = 0) in vec3 position;

uniform mat4 transform;

void main()
{                   
   gl_Position = transform*vec4(position,1.0f);     
}

When I run it, I don't get anything drawn on my screen, except the background color.
Below is the matrix which I send to the shader. I am using column major, so as far as I've read this should be the translation part of the 4x4. I've also tried transposing the matrix with no luck whatsoever.

The last clue I have is that the values which I am sending are 0s. I came to this conclusion because I tried playing with the fragment shader and I set the color to be equal to the last column from the uniform mat4:
#version 430 core
out vec4 color;

uniform mat4 transform;

void main()
{
 color = vec4(transform[0][3],transform[1][3], transform[2][3], transform[3][3]);   
} 

Which gives me a black triangle i.e. the color vec4 is filled with 0s.

I apologize in advance if the question is too noob-ish but I've been struggling with this for several days and I could not find a way to run it :(
Any advise would be appreciated!

**Tags:** opengl, shader, glsl, matrix

---

## 1 answers

### Answer by username_1 on 2017-01-21. Score: 3 | Accepted answer

glUniformMatrix4fv(transformLocation, sizeof(transform), GL_FALSE, &transform.m[0][0]);


The second parameter to glUniformMatrix is not the number of bytes in the data. OpenGL can figure that out from the 4fv part of the command (a 4x4 matrix of GLfloats). The second parameter is the number of matrices you're sending to that uniform, in case it is an array of mat4s.
So that value ought to be 1. You almost certainly got a GL_INVALID_OPERATION error from calling it with sizeof(transform).

> **Comments:**
> **username_0**: Thank you for your reply Nicol. I`ve fixed the argument mistake and found out the root cause. I have specified 'transformation' as my uniform value and was trying to use 'transform'.... It works llike a charm now.  

---

