Hello Guest

Get uniform variable from shader

  • 2 Replies
  • 6674 Views
Get uniform variable from shader
« on: February 15, 2012, 21:41:16 »
Hi,

I can't figure out what is going wrong. Actually I don't know if I'm using glGetUniformLocation in the right way. Here's a piece of code:

Code: [Select]
                shaderIds[0] = glCreateProgram();
testError("ERROR: Could not create the shader program");

shaderIds[1] = ShaderUtil.loadShader("SimpleShader.fragment.glsl", GL_FRAGMENT_SHADER);
shaderIds[2] = ShaderUtil.loadShader("SimpleShader.vertex.glsl", GL_VERTEX_SHADER);
glAttachShader(shaderIds[0], shaderIds[1]);
glAttachShader(shaderIds[0], shaderIds[2]);

glLinkProgram(shaderIds[0]);
testError("ERROR: Could not link the shader program");

modelMatrixUniformLocation = glGetUniformLocation(shaderIds[0], "modelMatrix");
viewMatrixUniformLocation = glGetUniformLocation(shaderIds[0], "viewMatrix");
projectionMatrixUniformLocation = glGetUniformLocation(shaderIds[0], "projectionMatrix");
testError("ERROR: Could not get the shader uniform locations");

The problem is the last test. It fails with the following message: "Invalid Operation".
I've found some reasons for this problem in the internet, but none of them was the case.
What could be happening?

PS: loadShader is a method I created to load shaders. All it does is load the shader from a file to a string and then create the shader with the corresponding source loaded and compile it. The return value is the shaderId returned by glCreateShader function.

*

Kai

Re: Get uniform variable from shader
« Reply #1 on: February 18, 2012, 20:57:14 »
I guess "testError" only checks the general glerror which is not sufficient for testing whether the program has been linked successfully, because the program linking will not set the global glerror but indicate the success/failure as an attribute of the program object, as is described in the spec of "glLinkProgram":

Quote
The status of the link operation will be stored as part of the program object's state. This value will be set to GL_TRUE if the program object was linked without errors and is ready for use, and GL_FALSE otherwise. It can be queried by calling glGetProgram with arguments program and GL_LINK_STATUS.

I guess, the GL_INVALID_OPERATION is due to a failure in linking of the program.

Re: Get uniform variable from shader
« Reply #2 on: February 19, 2012, 01:52:02 »
You're right... The program wasn't linking correctly.
There were some errors in the shaders. Now it's working.
Thanks for the help.