Get uniform variable from shader

Started by gutomarzagao, February 15, 2012, 21:41:16

Previous topic - Next topic

gutomarzagao

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:

                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

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":

QuoteThe 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.

gutomarzagao

You're right... The program wasn't linking correctly.
There were some errors in the shaders. Now it's working.
Thanks for the help.