I was trying to setup a 2d screen with two layers of drawing
1) a grid made of white solid lines 2) tiles made of colored square
here are my opengl codes:
setup grid
//glGenBuffers(1, &gridVAO);
glGenVertexArrays(1, &gridVAO); // **updated**
glBindVertexArray(gridVAO);
glGenBuffers(2, gridVBO);
glBindBuffer(GL_ARRAY_BUFFER, gridVBO[0]);
glBufferData(GL_ARRAY_BUFFER, sizeof(gridPos), gridPos, GL_STATIC_DRAW);
glVertexAttribPointer(vPosition, 2, GL_FLOAT, GL_FALSE, 0, 0);
glBindBuffer(GL_ARRAY_BUFFER, gridVBO[1]);
glBufferData(GL_ARRAY_BUFFER, sizeof(gridColors), gridColors, GL_STATIC_DRAW);
glVertexAttribPointer(vColor, 3, GL_FLOAT, GL_FALSE, 0, 0);
glBindVertexArray(0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
setup tile
// glGenBuffers(1, &tileVAO);
glGenVertexArrays(1, &tileVAO); //**updated**
glBindVertexArray(tileVAO);
glGenBuffers(2, tileVBO);
glBindBuffer(GL_ARRAY_BUFFER, tileVBO[0]);
glBufferData(GL_ARRAY_BUFFER, sizeof(tilePos), tilePos, GL_STATIC_DRAW);
glVertexAttribPointer(vPosition, 2, GL_FLOAT, GL_FALSE, 0, 0);
glBindBuffer(GL_ARRAY_BUFFER, tileVBO[1]);
glBufferData(GL_ARRAY_BUFFER, sizeof(tileColors), tileColors, GL_STATIC_DRAW);
glVertexAttribPointer(vColor, 3, GL_FLOAT, GL_FALSE, 0, 0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);
draw tiles
glBindVertexArray(tileVAO);
glDrawArrays(GL_TRIANGLES, 0, TILE_COUNT_X * TILE_COUNT_Y * 6);
glBindVertexArray(0);
draw grid
glBindVertexArray(gridVAO);
glDrawArrays(GL_LINES, 0, girdVertexCount);
glBindVertexArray(0);
by keeping only setup+draw grid produces this:
by keeping only setup+draw tiles produces this:
The VAOs are working properly by themselves.
However, magic begins here:
If I do the combination of
setup grid
setup tiles
// draw tiles
draw grid
It produces this:
Looks like the draw grid codes pick up buffers from tile's VAO. Help Please :D
update:
after corrected to glGenVertexArrays
the same problem still happens
but i found both VAOs are 0
cout<<"gridVAO "<<gridVAO<<endl;
cout<<"tileVAO "<<tileVAO<<endl;
output:
gridVAO 0
tileVAO 0