Posted by
DearDaniel on
May 21, 2016; 5:35am
URL: https://forum.jogamp.org/glVertexAttribPointer-and-BindBuffer-tp4036732.html
I am doing some practise on OpenGL 3. I read the hello triangle and wrote a program to generate 1000 spinning cubes. It works well. I am now revising my codes, and comparing to the codes of others. I am reading the sample code from
https://github.com/elect86/helloTriangle/tree/master/HelloTriangle/src/gl3/helloTriangle. I found some difference in the codes but I have no idea why both of them works the same.
// This is my code, originally from the hello triangle code, I changed it a bit
static void bindBuffer(GL3 gl, int bufferId, float[] dataArray, int dataLoc){
Buffer tmp = Buffers.newDirectFloatBuffer(dataArray);
gl.glBindBuffer(GL.GL_ARRAY_BUFFER, bufferId);
gl.glBufferData(GL.GL_ARRAY_BUFFER, dataArray.length * Float.BYTES, tmp, GL.GL_STATIC_DRAW);
gl.glEnableVertexAttribArray(dataLoc);
gl.glVertexAttribPointer(dataLoc, 4, GL.GL_FLOAT, false, 0, 0);
BufferUtils.destroyDirectBuffer(tmp);
}
//This is from the small wiki page
private void initVertexArray(GL3 gl3) {
gl3.glGenVertexArrays(1, vertexArrayName);
gl3.glBindVertexArray(vertexArrayName.get(0));
{
gl3.glBindBuffer(GL_ARRAY_BUFFER, bufferName.get(Buffer.VERTEX));
{
int stride = (2 + 3) * Float.BYTES;
int offset = 0 * Float.BYTES;
gl3.glEnableVertexAttribArray(Semantic.Attr.POSITION);
gl3.glVertexAttribPointer(Semantic.Attr.POSITION, 2, GL_FLOAT, false, stride, offset);
offset = 2 * Float.BYTES;
gl3.glEnableVertexAttribArray(Semantic.Attr.COLOR);
gl3.glVertexAttribPointer(Semantic.Attr.COLOR, 3, GL_FLOAT, false, stride, offset);
}
gl3.glBindBuffer(GL_ARRAY_BUFFER, 0);
gl3.glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, bufferName.get(Buffer.ELEMENT));
}
gl3.glBindVertexArray(0);
checkError(gl3, "initVao");
}
Can anyone explain to me the following differences?
1. For the glVertexAttribPointer, the stride of my code is 0, and the sample one is none-zero. I changed my code and it's worked fine also. So, What's the stride for? Why 0 is also acceptable?
2. The sample code has a line for unbinding the Array. Which is, "gl3.glBindBuffer(GL_ARRAY_BUFFER, 0);" and "gl3.glBindVertexArray(0);" When I add it to my code, error occurs but it didn't mentioned anything about the error. Could any tell me why I can't do this in my code? Also, as a follow up question, what is the difference between unbinding and without unbinding?
Daniel
Never Stop ... Learn more ... Be humble ...