# Passing Colour from program to vertex shader

New to opengl! I am having issues with passing colours from the program to vertex shader.

I am creating a grid where I want to control the color of each cell.Thus I am generating each cell and having two vbo's, one for the vertices, and another for the colour. I want my grid to look like this:

But I end up with:

I have confirmed if I generate the colour data in the vertex shader and then pass it to the fragment, I get the first case. It is only when I grab the colour data from the program that I get the second case.

Code for creating each vbo(written in go):

var vbo [2]uint32
gl.GenBuffers(2, &vbo[0])
gl.BindBuffer(gl.ARRAY_BUFFER, vbo[0])
gl.BufferData(gl.ARRAY_BUFFER, 4*len(points), gl.Ptr(points), gl.STATIC_DRAW)
gl.BindBuffer(gl.ARRAY_BUFFER, vbo[1])
gl.BufferData(gl.ARRAY_BUFFER, 4*3, gl.Ptr([]float32{0.5,0.5,0.0}), gl.STATIC_DRAW)
var vao uint32
gl.GenVertexArrays(1, &vao)
gl.BindVertexArray(vao)
gl.EnableVertexAttribArray(0)
gl.BindBuffer(gl.ARRAY_BUFFER, vbo[0])
gl.VertexAttribPointer(0, 3, gl.FLOAT, false, 0, nil)

gl.EnableVertexAttribArray(1)
gl.BindBuffer(gl.ARRAY_BUFFER, vbo[1])
gl.VertexAttribPointer(1, 3, gl.FLOAT, false, 0, nil)


#version 410
/*layout (location=0) */in vec3 vp;
/*layout (location=1)*/ in vec3 colour;
/*layout (location=2)*/  out vec3 outcolour;
void main(){
gl_Position = vec4(vp,1.0);
//outcolour = vec3(0.5,0.5,0.0);
outcolour = vec3(colour);
}


#version 410
in vec3 outcolour;
out vec4 frag_colour;
void main(){
frag_colour = vec4(outcolour,1.0);
}

• /*layout (location=1)*/ Why did you comment out important declarations like that? Jun 23 '17 at 1:35
• I had a bug where I was unable to obtain attribute indices that I resolved. The indices that I declared (and commented out) just so happened to be the same ones that the compiler chose. Already changed it back to query the attribute index based on the name! Jun 23 '17 at 5:22
• Why query the attribute index when you can just specify it in the shader? Jun 23 '17 at 13:35

I don't know what language that is, but I'm going to hazard a guess that the arrays points and gl.Ptr([]float32{0.5,0.5,0.0}) are not the same size. If you intend to provide per-vertex colors, then you must provide one color for every vertex. If you want to provide a single color that all vertices share, then that would be a uniform, not a vertex shader input.
Also, commenting out your layout declarations isn't helping. Though if you are going to declare a location for the VS output variable, then the matching FS input needs to have the same location declared.
• not the same size solved my problem. Oct 27 '18 at 19:37