I've been working through some opengl in golang learning and have the following fragments:
import(
"github.com/go-gl/gl/v3.3-core/gl"
)
vertices := []float32{
// Position // Colors // Texture Coords
1.0, 1.0, 0.0, 1.0, 0.0, 0.0, 1.0, 1.0, // Top Right
1.0, -1.0, 0.0, 0.0, 1.0, 0.0, 1.0, 0.0, // Bottom Right
-1.0, -1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, // Bottom Left
-1.0, 1.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0, // Top Left
}
vlen := int32(len(vertices) * 4 / 4)
var offset uint32 = 0
//position
gl.VertexAttribPointer(0, 3, gl.FLOAT, false, vlen, nil)
gl.EnableVertexAttribArray(0)
//color
offset = offset + 12
gl.VertexAttribPointer(1, 3, gl.FLOAT, false, vlen, unsafe.Pointer(&offset))
gl.EnableVertexAttribArray(1)
offset = 0
So I get my basic square I've been working on -- nil in the 5th position parameter for VertexAttribPointer for position seems to work, but I get nothing with color in the larger program. And if I try to specify an offset unsafe pointer of 0 for poistion I get nothing but empty space.
Specifying an unsafe pointer there is something I'm not understanding at the moment -- I think I need to specify where in vertices the offset for color is, but I'm not following. I am not sure vlen is correct either. Any input appreciated.
EDIT:
Self found solution -- what I needed was gl.PtrOffset, which I wasnt aware of when initially creating the quetion.
gl.VertexAttribPointer(1, 3, gl.FLOAT, false, vlen, gl.PtrOffset(offset))
Your problem comes from VertexAttribPointer(att, size, type, normalized, stride, pointer)
size is the number of components per generic vertex attribute. Position and color have 3 components, texture coords only 2.
stride is number of bytes offset between two consecutive values for the attribute you want to read in the shader.
Your buffer is PPPCCCTT, PPPCCCTT, etc (i.e. position, color, texture coords, position, color, texture coords, etc). Let's put it in bytes (a "float" is 4-bytes sized):
444 444 44, 444 444 44, etc
I detail size and stride:
Position: 3 floats, (3x4 +2x4)=20 bytes, 3 floats, 20 bytes, etc
Color: 3 floats, (2x4 + 3x4)=20 bytes, 3 floats, 20 bytes, etc
TextCoo: 2 floats, (3x4 + 3x4)=24 bytes, 2 floats, 24 bytes, etc
Position attribute: VertexAttribPointer(att1, 3, gl.FLOAT, false, 20, nil)
Color attribute: VertexAttribPointer(att2, 3, gl.FLOAT, false, 20, nil)
Texture coords attribute: VertexAttribPointer(att3, 2, gl.FLOAT, false, 24, nil)
att
is the location of the attribute in your shader.