PDA

View Full Version : Problem using GL_COLOR_ARRAY to send information to shader



Mctittles
11-10-2012, 04:13 AM
I have an array of particles that I'd like to draw at once using glDrawArrays, however I would also like to dynamically set the alpha of some particles while using a shader. I came up with a method of using GL_COLOR_ARRAY to send the alpha information per point to the shader. It works fine, however after calling glDisableClientState(GL_COLOR_ARRAY); on certain graphics cards the color array stays active and modifies everything else drawn. For example:

Here is where I draw my particles:


glUseProgram(particleShader);
glEnableClientState(GL_COLOR_ARRAY);
glBindTexture(GL_TEXTURE_2D, particleTexture);
glColorPointer(4, GL_FLOAT, 0, betweenPartColorArray);
glTexCoordPointer(2, GL_FLOAT, 0,textureCoordsBetweenParticles);
glVertexPointer(3, GL_FLOAT, 0, verticesBetweenParticles);
glDrawArrays(GL_QUADS, 0, numberBetweenPartIndices);
glDisableClientState(GL_COLOR_ARRAY);
glUseProgram(0);

Here is the relevant part of my shader that takes the color array and uses that to change the alpha on individual particles:


Vertex:


varying vec4 vertex_color;
varying vec4 verpos; // had an error with verpos for whatever reason. If problems crop check into it
void main(void){
vertex_color = gl_Color;
gl_Position = ftransform();
gl_TexCoord[0] = gl_MultiTexCoord0;
verpos = gl_ModelViewMatrix*gl_Vertex;
}

Fragment:

varying vec4 vertex_color;
uniform sampler2D sceneTex;
void main (void) {
vec4 finalColor = texture2D(sceneTex, gl_TexCoord[0].xy) ;
gl_FragColor=finalColor;
finalColor.a*=vertex_color.a;
finalColor=clamp(finalColor, 0.0, 1.0);
gl_FragColor=finalColor;
}


The problem is (on certain computers) everything drawn after this gets tinted according to the color array even though glDisableClientState(GL_COLOR_ARRAY); is called. If I were to change the red component on the color array, everything drawn after the color array is disabled has a red tint.


Is there a way to make sure GL_COLOR_ARRAY is really disabled besides what I am doing here?
Also, is there a better method to send per vertex information to a shader. I mainly need to the shader to know which vertices I want alpha modified while leaving all of the particles in the array.

Thanks!

Dan Bartlett
11-10-2012, 05:19 AM
Do they take the colors in the array, or the value of the last color?
In earlier OpenGL versions, the spec states


the current normal coordinates, color, secondary color, color
index, edge flag, fog coordinate, texture coordinates, and generic attributes are each
indeterminate after the execution of DrawElements, if the corresponding array is
enabled. Current values corresponding to disabled arrays are not modified by the
execution of DrawElements.
The same text applies to DrawArrays.

In recent OpenGL specs they state:

If an array is enabled, the corresponding current vertex attribute value is unaffected
by the execution of DrawElementsOneInstance.

So older versions may or may not affect the current color after a draw call, but recent versions are guaranteed not to affect current values. This behavior changed in OpenGL version 4.1.

In older versions you would need to call glColor after a draw call that uses the color array and before a draw call that doesn't use it (eg. reset the current color to (1,1,1,1)) to guarantee that the color tint doesn't bleed over, but that's not required if your implementation follows a newer version of the OpenGL spec.

Mctittles
11-10-2012, 05:37 AM
Ahh yea, that might work. I haven't tested it yet (need my friends computer) but I believe it is taking the last color instead of the entire array as every object after is the same tint even though some have large arrays. Thanks, I'll try this.

Also still standing if anyone knows a better method to send per vertex information to a shader, let me know :).