I have the following vertex shader:

#version 120

attribute vec4 position;
attribute vec4 acolor;

varying vec4 theColor;

void main(){
    gl_Position = position;
    //theColor = acolor;  //[1]
    theColor = vec4(1.0,0.0,0.0,1.0); //[2]

When I use line [1], the color is transmitted rigth to the fragment shader, but when I change the color to the attribute acolor (using the line [2]) the positions of the triangles appear wrong. From the data of the main program, it seems to me like the positions get the color values.

I put two images, 1 with the rigth vertex positions and 2 with the wrong vertex positions.

enter image description here enter image description here

UPDATE: render code on Github


Check your glBindAttribLocation calls - you'll probably find that one of two things are happening; either (1) you're not doing any such calls and therefore GL is assigning it's own arbitrary attrib locations which are mismatching those used in your glVertexAttribPointer calls, or (2) you have specified a mismatch yourself.

Either way, the recommended approach would be to use "layout(location=" syntax in your vertex shader, which will make this kind of ambiguity go away.

  • \$\begingroup\$ Thanks, I'm using layout + location syntax in the 3.30 version of the shader, but with 1.20 it's not available. I assumed that attributes were listed the same order that source code declarations. WRONG, I addded glBindAttribLocation and now it works. \$\endgroup\$ – Zhen Feb 5 '13 at 17:36
  • 1
    \$\begingroup\$ You should also check for GL_ARB_explicit_attrib_location which would allow you to use layout(location= with older GL_VERSIONs, and which in practice will work on any GL2.1 hardware for which a reasonably up to date driver is available. On the other hand you may prefer to not have a third shader variation, but that should be manageable with some #defines. \$\endgroup\$ – Maximus Minimus Feb 6 '13 at 0:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.