Problem with GLSL communication

I have run into plenty of problems related to uniform and attribute variables not being set in GLSL as they should and I'm wondering if this is a known issue, or if I am doing something wrong. The problems seem to be centered around that Gl.glGetAttribLocationARB(int programObj, string name) returns the wrong values, once the number of defined GLSL variables exceeds some random number (thee to eight). The erroneous values I get are either -1 or a value already associated with another variable, which leads to runtime errors later on. All variables can work separaterly, just not together and there are no warning reported by the shader compiler. I have also gotten these problems in multiple different projects, which leads me to think the error lies somewhere in Tao. The problems can vary depending on which computer the code is run on, but they are consistent between different runs on the same computer.

What I do, in short, is this:

1) Create and initialize a SimpleOpenGLControl.
2) Compile and link shaders.
3) Call Gl.glGetAttribLocationARB() for all the variables I want to pass values to later on.
4) Set the values with Gl.glVertexAttrib1f(), Gl.glUniform1f(), etc.

2 questions... Do you check

2 questions...

  • Do you check that the shaders are compiled correctly? (ie. if you add an error, you DO get a warning)
  • Are you using glGetAttribLocationARB() for uniforms? If so, try using glGetUniformLocationARB()...

Yes, I get errors and

Yes, I get errors and warnings when there are ones and yes, I use Attrib and Uniform in the correct way, both when getting the locations and when passing values (I just double checked this). Any other ideas?

If you haven't done so

If you haven't done so already, update to Tao.OpenGl 2.1.0.12+ (check the development forum).

------
OpenTK

I updated the files as

I updated the files as suggested and moved the glGetLocation() calls to my render method. The latter resulted in that the locations get valid numbers. In fact, all values now seem to be set and passed correctly (I checked this with glslDevil), but somehow they still have the wrong values in the shader.

This is the code that I am running. I am well aware of that there are far quicker ways of doing the same thing, but speed is not the issue here. Getting it to work at all is.

//======================================================
C# program
//======================================================

void Render()
{
// Irrelevant stuff
/* ... */

//Pick a shader program
Gl.glUseProgram(_myShader);

_timeLocation = Gl.glGetUniformLocationARB(_myShader, "time");
_numberOfVerticesLocation = Gl.glGetUniformLocationARB(_myShader, "noOfVertices");
_numberOfObjectsLocation = Gl.glGetUniformLocationARB(_myShader, "noOfObjects");
_vertexIDLocation = Gl.glGetAttribLocationARB(_myShader, "vertexID");
_objectIDLocation = Gl.glGetAttribLocationARB(_myShader, "objectID");

// Set shader variables
Gl.glUniform1fARB(_timeLocation, (float)(currentTime - _startTime) * 0.001f);
Gl.glUniform1fARB(_numberOfVerticesLocation, (float)_myTexture.Height);
Gl.glUniform1fARB(_numberOfObjectsLocation, (float)_myTexture.Width);
Gl.glBindTexture(Gl.GL_TEXTURE_2D, _myTextureID);

DrawSquares();

openGLControl.SwapBuffers();
}

private void DrawSquares()
{
Gl.glColor4f(0.5f, 0.5f, 0.5f, 1.0f);

Gl.glBegin(Gl.GL_QUADS);

for (float i = 0; i < _grassParameterTexture.Width; i++)
{
Gl.glVertexAttrib1fARB(_objectIDLocation, i);
DrawSquare();
}

Gl.glEnd();
}

private void DrawSquare()
{
Gl.glNormal3f(-1, 0, 0);
Gl.glVertexAttrib1fARB(_vertexIDLocation, 0f);
Gl.glVertex3f(0, -.5f, -.5f);
Gl.glVertexAttrib1fARB(_vertexIDLocation, 1f);
Gl.glVertex3f(0, .5f, -.5f);
Gl.glVertexAttrib1fARB(_vertexIDLocation, 2f);
Gl.glVertex3f(0, .5f, .5f);
Gl.glVertexAttrib1fARB(_vertexIDLocation, 3f);
Gl.glVertex3f(0, -.5f, .5f);
}

//======================================================

The vertex shader used is listed below. The first three lines that are commented away results in eight squares positioned in a rotating circle and colored according to their objectID. The last three lines are what I try to do which doesn't work. As soon as I use the variable vertexID in any way, the previously working code stops working and results in all the squares being rendered in the same position.

//======================================================
Vertex shader
//======================================================

uniform float time;
uniform sampler2D paramTexture;
attribute float vertexID;
attribute float objectID;
uniform float noOfVertices;
uniform float noOfObjects;
varying vec3 v_color;

void main( void )
{
//float angle = 6.283 * (objectID + time) / noOfObjects;
//gl_Position = gl_ModelViewProjectionMatrix * (gl_Vertex + vec4(0.0, sin(angle), cos(angle), 0.0));
//v_color = clamp(vec3(objectID*0.1, 0.0, 1.0 / noOfVertices), 0.0, 1.0);

float angle = 6.283 * (objectID + time) / noOfObjects;
gl_Position = gl_ModelViewProjectionMatrix * (gl_Vertex + vec4(0.0, sin(angle), cos(angle), 0.0));
v_color = clamp(vec3(objectID*0.1, 0.0, vertexID / noOfVertices), 0.0, 1.0);
}

//======================================================

Theme by La Boite a site | Powered by Drupal