Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot pass in uniform color in fragment shader opengl

I am using openGL 3.1 and programming with QGLfunctions. I cannot pass in the uniform vec4 mycolor to the fragment shader I have initialized my program using:

m_program = new QOpenGLShaderProgram(this);
m_program->addShaderFromSourceCode(QOpenGLShader::Vertex, vertexShaderSource);
m_program->addShaderFromSourceCode(QOpenGLShader::Fragment, fragmentShaderSource);
m_program->link();
m_posAttr = m_program->attributeLocation("posAttr");
m_mycolor = m_program->uniformLocation("mycolor");
m_matrixUniform = m_program->uniformLocation("matrix");

and then I try to send color

 GLfloat color[] = {
        0.0f, 1.0f, 0.0f, 1.0f    };
glUniform4fv(m_mycolor, 4, color);

to my fragment shader which is:

 static const char *fragmentShaderSource =
    "uniform vec4 mycolor;\n"
    "void main() {\n"
    "   gl_FragColor = mycolor;\n"
    "}\n";

and I get nothing displayed

like image 895
M_global Avatar asked Nov 20 '25 10:11

M_global


1 Answers

glUniform4fv(m_mycolor, 4, color);

From the glUniform documentation about the count parameter:

Specifies the number of elements that are to be modified. This should be 1 if the targeted uniform variable is not an array, and 1 or more if it is an array.

The count parameter in your case should be 1 since your color[] array still only represents one vec4 entry.

you are trying to fill a uniform vec4 color[4] array.

The correct version :

glUniform4fv(m_mycolor, 1, color);

As @Bahbar also pointed out : "GL_INVALID_OPERATION is generated if count is greater than 1 and the indicated uniform variable is not an array variable." So.. check your GL errors!

like image 54
Grimmy Avatar answered Nov 23 '25 09:11

Grimmy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!