# glUniformMatrix4fv - INVALID OPERATION?

I'm attempting to manually setup a matrix in my vertex shader purely because I like to learn as much as possible about anything I do usually.

So the problem is that I'm getting an GL_INVALID_OPERATION when too me my code seems clear as crystal complete.

const GLfloat modelviewmatrix[] = {
1.0f , 0.0f , 0.0f , 0.0f,
0.0f, 1.0f, 0.0f , 0.0f ,
0.0f, 0.0f, 1.0f, 0.0f,
0.0f, 0.0f, 0.0f, 1.0f
};

std::cout << "location->" <<locationID << std::endl;
glUniformMatrix4fv(locationID, 1, GL_FALSE, modelviewmatrix);

GLenum error;

error = glGetError();

if (error != GL_NO_ERROR)
{
std::cout << "not working" << std::endl;
}

if (error == GL_INVALID_OPERATION)
{
std::cout << "INVALID OPERATION" << std::endl;
}
if (error == GL_NO_ERROR)
{
std::cout << "No Error! -> CODE CHECKING <-" << std::endl;
}

if (error == GL_INVALID_VALUE)
{
std::cout << "NO VALUE" << std::endl;
}

if (error == GL_OUT_OF_MEMORY)
{
std::cout << "NO MEMORY" << std::endl;
}

if (error == GL_STACK_OVERFLOW)
{
std::cout << "NO OVERFLOW" << std::endl;
}

if (error == GL_INVALID_FRAMEBUFFER_OPERATION)
{
std::cout << "invalid framebuffer <-" << std::endl;
}

if (error == GL_OUT_OF_MEMORY)
{
std::cout << "Out of Memory" << std::endl;
}


#version 330 core

layout (location = 0) in vec3 position;

uniform mat4 modelviewMatrix;
uniform mat4 projection;

void main()
{

gl_Position = modelviewMatrix * vec4(position.x, position.y, position.z, 1.0);
}


What could be causing the problem , why is it causing it . how did you find out and what did you use too figure it out please?.

OpenGL 3.30+ , GLSL 3.30 core , Win10 64bit.

figured out what the problem was I was using glUseProgram after I was doing uniforms instead of doing that before.

full source here -> http://pastebin.com/zD7YEDjp

• I'm not sure of you know what stackoverflow means based on the debug message. – Bálint Apr 20 '16 at 17:53
• Also, you can write vec4(position, 1.0), and openGL fills out the gaps. – Bálint Apr 20 '16 at 17:54
• Bálint -> I was always wondering if I could do that or not thanks for clarifying for me. Logic is too be aware of as much as possible. – Dominic Hughes Apr 20 '16 at 17:55

figured out what the problem was I was using glUseProgram after I was doing uniforms instead of doing that before.

You get invalid operation, because you don't use the uniform "projection" in the shader, thus OpenGL deletes it to save memory.

This can be solved by simply referencing it with projection;, you don't need to use it.

Like this:

#version 330 core

layout (location = 0) in vec3 position;

uniform mat4 modelviewMatrix;
uniform mat4 projection;

void main()
{

gl_Position = modelviewMatrix * vec4(position.x, position.y, position.z, 1.0);
projection;
}

• Where does it say I must use every uniform in my shader it's their as my second problem to solve hopefully all by myself?. – Dominic Hughes Apr 20 '16 at 17:56
• I tried removing it and doesn't work so clearly that's not fixing the problem lol. – Dominic Hughes Apr 20 '16 at 18:00
• @Dominic Hughes if you don't use it, it pretty mich doesn't exist as a uniform, thus glGetUniformLocation returns -1, and you get an invalid operation from glUniformMatrix4fv. – Bálint Apr 20 '16 at 18:01
• The compiler's free to remove uniforms you don't use; it's pretty common. It's not the only reason you might get an invalid operation though. Have you read the docs for glUniform*, @DominicHughes? They enumerate the possible scenarios, and you're not currently checking for all of them. – user1430 Apr 20 '16 at 18:01
• @DominicHughes not remove it REFERENCE IT – Bálint Apr 20 '16 at 18:01