Problem drawing the content of multiple VAOs

Started by geminus, April 20, 2016, 15:30:15

Previous topic - Next topic

geminus

I am pretty inexperienced in graphics programming. Two days ago I encountered a bug I still could not fix. Although I would bet it is a very simple mistake I just canÃ,´t find it. This is why I will only bore you with a minimum of my unorganized code for now.

Currently I am writing a simple voxel engine in Java with LWJGL in order to learn more about OpenGL. Right now I generate meshes for 9 chunks and try to render them in a 3x3 square. Each chunk is saved in itÃ,´s own VAO containing multiple VBOs for indices, positions, normals, etc.

But when rendering all the VAOs it only renders the chunk that is inside the last VAO that was bound that frame. So if I render VAO1 and then VAO2 it will only display the chunk in VAO2.

What I (believe to) know:

  • Each VAO has itÃ,´s own ID and a different amount of vertices (250 000 to 300 000) contained in itÃ,´s own set of VBOs.
  • Each VAO/chunk rendered by itself is displaying correctly. The VAOs seem to have the correct data inside.
  • If I bind 0 as vertex buffer in order to unbind everything after the last draw call I get a blank screen.
  • Using glFinish() or glFlush() after each draw call does not change anything.

This code is used to init the VAOs:
public static Model loadModel(float[] positions, float[] textureCoords, float[] normals, float[] colors, int[] indices) {
	int vaoID = createVAO();
	bindIndicesBuffer(indices);
	storeDataInAttributeList(Shader.POS_ATTRIB, 3, positions);
	storeDataInAttributeList(Shader.TCOORD_ATTRIB, 2, textureCoords);
	storeDataInAttributeList(Shader.NORMAL_ATTRIB, 3, normals);
	storeDataInAttributeList(Shader.COLOR_ATTRIB, 4, colors);
	glBindVertexArray(0);
	return new Model(vaoID, indices.length);
}

private static void bindIndicesBuffer(int[] indices) {
	int vboID = glGenBuffers();
	glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vboID);
	IntBuffer buffer = BufferUtils.createIntBuffer(indices);
	glBufferData(GL_ELEMENT_ARRAY_BUFFER, buffer, GL_STATIC_DRAW);
}

private static void storeDataInAttributeList(int attributeNumber, int coordinateSize, float[] data) {
	int vboID = glGenBuffers();
	glBindBuffer(GL_ARRAY_BUFFER, vboID);
	FloatBuffer buffer = BufferUtils.createFloatBuffer(data);
	glBufferData(GL_ARRAY_BUFFER, buffer, GL_STATIC_DRAW);
	glVertexAttribPointer(attributeNumber, coordinateSize, GL_FLOAT, false, 0, 0);
	glBindBuffer(GL_ARRAY_BUFFER, 0);
}


This code is executed every frame:
Shader.WORLD.start();
Shader.WORLD.setUniformMat4f("projection", cam.getProjection());
Shader.WORLD.setUniformMat4f("view", cam.getView());
Shader.WORLD.setUniform3f("lightDirInv", new Vector3f(1, 2, 1).normalize());

glEnable(GL_DEPTH_TEST);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_CULL_FACE);
glCullFace(GL_BACK);

glEnableVertexAttribArray(Shader.POS_ATTRIB);
glEnableVertexAttribArray(Shader.TCOORD_ATTRIB);
glEnableVertexAttribArray(Shader.NORMAL_ATTRIB);
glEnableVertexAttribArray(Shader.COLOR_ATTRIB);

glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, world.getTextureAtlas().getTextureID());

for (ModelInstance instance : renderChunks.values()) {
        glBindVertexArray(instance.getModel().getVAO());
	Shader.WORLD.setUniformMat4f("model", instance.getModelMatrix());
	glDrawElements(GL_TRIANGLES, instance.getModel().getVertexCount(), GL_UNSIGNED_INT, 0);
}

glBindTexture(GL_TEXTURE_2D, 0);
// glBindVertexArray(0);
glDisableVertexAttribArray(Shader.POS_ATTRIB);
glDisableVertexAttribArray(Shader.TCOORD_ATTRIB);
glDisableVertexAttribArray(Shader.NORMAL_ATTRIB);
glDisableVertexAttribArray(Shader.COLOR_ATTRIB);

Shader.WORLD.end();


I have run out of ideas as to why this happens. Please give me any suggestion you might have!
Thank you,
Geminus