jodev's picture

GL.Uniform4 call results in InvalidOperation error

Hi all,

I've been pulling my hair out on an issue that has me stumped. I'm very much a OpenGL beginner and I'm trying to figure out why this code fails. I've tried to isolate all relevant parts, but if you need to know more please let me know.

case GLTStockShader.Identity:
                    iColor = GL.GetUniformLocation(uiStockShaders[(int)nShaderID], "vColor");
                    vColor = uniformList[0] as float[];
                    GL.Uniform4(iColor, 1, vColor);
 
// debug info
                    string vColorSummary = string.Join(", ", vColor);
                    System.Diagnostics.Debug.WriteLine("iColor        = {0}", iColor);
                    System.Diagnostics.Debug.WriteLine("vColor        = " + vColorSummary);
                    System.Diagnostics.Debug.WriteLine("GL.GetError() = {0}", GL.GetError());
                    break;

This outputs the following debug info:

iColor = 0
vColor = 1, 0, 0, 1
GL.GetError() = InvalidOperation

The vertex and fragment shaders are defined as follows:

const string szIdentityShaderVP = "attribute vec4 vVertex;\n" +
                                    "void main(void)\n" +
                                    "{ gl_Position = vVertex;\n" +
                                    "}";
 
const string szIdentityShaderFP = "precision mediump float;\n" +
                                    "uniform vec4 vColor;\n" +
                                    "void main(void)\n" +
                                    "{ gl_FragColor = vColor;\n" +
                                    "}";

The triangle that it should draw does indeed draw, albeit white. When I debug the active program (= iColor) in gDEBugger it shows the active uniform vColor as an four zero float array. Any thoughts would be appreciated. Thanks!


Comments

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
muralha's picture

The problem you have is that you haven't defined a vColor in your vertex shader. The way it works is that you pass values in the vertex shader, like colors at the vertices. Then, you pass them from the vertex shader to the fragment shader by setting varying variables, which linearly interpolate them. The shaders should look like this:

Vertex Shader:

attribute vec3 aVertex;
attribute vec4 aColor;
 
varying vec4 vColor;
void main(void)
{
    gl_Position = vVertex;
    vColor = aColor;
}

Fragment Shader:

varying vec4 vColor;
void main(void)
{
    gl_FragColor = vColor;
}

A common problem is that you pass the wrong number of values to the shader variable, e.g. 4 values to a vec3.

jodev's picture

Hi muralha,

First of all, thanks for taking the time to reply. Much appreciated. I managed to find the bug a few minutes ago elsewhere in the code.

It turned out that the shaders are actually working fine (because I'm using a uniform for the color). I introduced a bug elsewhere where I erroneously used the program 0, contrary to what I initally said about the active program in use. Let's just say I learned a bit more about gDEBugger in the process too... ;)

Thanks again!