<p>I don't know what language that is, but I'm going to hazard a guess that the arrays <code>points</code> and <code>gl.Ptr([]float32{0.5,0.5,0.0})</code> are <em>not</em> the same size. If you intend to provide per-vertex colors, then you must provide one color for <em>every vertex</em>. If you want to provide a single color that all vertices share, then that would be a <code>uniform</code>, not a vertex shader <code>in</code>put.</p>

<p>Also, commenting out your <code>layout</code> declarations isn't helping. Though if you are going to declare a <code>location</code> for the VS output variable, then the matching FS input needs to have the same <code>location</code> declared.</p>

CC BY-SA 4.0
2018-10-27T19:37:00.463Z
`not the same size` solved my problem.
CiaranWelsh
CC BY-SA 3.0
2017-06-23T05:18:09.657Z
Thank you! Updating it to have one color per vertex worked!
user3855005
CC BY-SA 3.0
2017-06-23T05:21:01.093Z
If I may ask for furthur optimization and advice, Currently in order to generate those quadrilateral cells, I am using 8 vertices for each cell. This then means each time I change the colour of a cell, I send 3*4*8 bytes each time. I changed it to send a third of the data by creating the colour in the shader itself, and using an int to determine which colour to use. Is there a more efficient method than the one I just described?
user3855005
CC BY-SA 3.0
2017-06-23T08:06:29.767Z
Since the original question was about getting the code to work correctly, you could post a separate question about efficiency.
trichoplax is on Codidact now
CC BY-SA 3.0
2017-06-23T01:37:30.183Z
2017-06-23T01:37:30.183Z
Nicol Bolas
<p>New to opengl! I am having issues with passing colours from the program to vertex shader.</p>

<p>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: </p>

<p><a href="https://i.sstatic.net/LIN0W.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/LIN0W.png" alt="Ideal"></a> </p>

<p>But I end up with:</p>

<p><a href="https://i.sstatic.net/Pxew3.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/Pxew3.png" alt="Reality"></a></p>

<p>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.</p>

<p>Code for creating each vbo(written in go):</p>

<pre><code>var vbo [2]uint32
gl.GenBuffers(2, &amp;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, &amp;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)
</code></pre>

<p>Vertex Shader:</p>

<pre><code>#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);
}
</code></pre>

<p>Fragment Shader</p>

<pre><code>#version 410
in vec3 outcolour;
out vec4 frag_colour;
void main(){
    frag_colour = vec4(outcolour,1.0);
}
</code></pre>

CC BY-SA 3.0
2017-06-23T01:35:19.970Z
`/*layout (location=1)*/` Why did you comment out important declarations like that?
Nicol Bolas
CC BY-SA 3.0
2017-06-23T05:22:18.383Z
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!
user3855005
CC BY-SA 3.0
2017-06-23T13:35:15.483Z
Why query the attribute index when you can just *specify it* in the shader?
Nicol Bolas
<opengl><vertex-buffer-object>
Passing Colour from program to vertex shader
user3855005
# Passing Colour from program to vertex shader

(Asked by: username_0 on 2017-06-23)

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:

[![Ideal](https://i.sstatic.net/LIN0W.png)](https://i.sstatic.net/LIN0W.png)

But I end up with:

[![Reality](https://i.sstatic.net/Pxew3.png)](https://i.sstatic.net/Pxew3.png)

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)

```

Vertex Shader:

```
#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);
}

```

Fragment Shader

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

```

**Tags:** opengl, vertex-buffer-object

> **Comments:**
> **username_1**: `/*layout (location=1)*/` Why did you comment out important declarations like that?  
> **username_0**: 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!  
> **username_1**: Why query the attribute index when you can just *specify it* in the shader?  

---

## 1 answers

### Answer by username_1 on 2017-06-23. Score: 2 | Accepted answer

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 `in`put.

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.

> **Comments:**
> **username_0**: Thank you! Updating it to have one color per vertex worked!  
> **username_0**: If I may ask for furthur optimization and advice, Currently in order to generate those quadrilateral cells, I am using 8 vertices for each cell. This then means each time I change the colour of a cell, I send 3*4*8 bytes each time. I changed it to send a third of the data by creating the colour in the shader itself, and using an int to determine which colour to use. Is there a more efficient method than the one I just described?  
> **username_3**: Since the original question was about getting the code to work correctly, you could post a separate question about efficiency.  
> **username_2**: `not the same size` solved my problem.  

---

