2
$\begingroup$

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

But I end up with:

Reality

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);
}
$\endgroup$
  • $\begingroup$ /*layout (location=1)*/ Why did you comment out important declarations like that? $\endgroup$ – Nicol Bolas Jun 23 '17 at 1:35
  • $\begingroup$ 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! $\endgroup$ – user3855005 Jun 23 '17 at 5:22
  • $\begingroup$ Why query the attribute index when you can just specify it in the shader? $\endgroup$ – Nicol Bolas Jun 23 '17 at 13:35
2
$\begingroup$

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.

$\endgroup$
  • $\begingroup$ Thank you! Updating it to have one color per vertex worked! $\endgroup$ – user3855005 Jun 23 '17 at 5:18
  • $\begingroup$ 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? $\endgroup$ – user3855005 Jun 23 '17 at 5:21
  • 2
    $\begingroup$ Since the original question was about getting the code to work correctly, you could post a separate question about efficiency. $\endgroup$ – trichoplax Jun 23 '17 at 8:06
  • $\begingroup$ not the same size solved my problem. $\endgroup$ – CiaranWelsh Oct 27 '18 at 19:37

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.