LWJGL Forum

Programming => OpenGL => Topic started by: geminus on April 20, 2016, 15:30:15

Title: Problem drawing the content of multiple VAOs
Post by: geminus on April 20, 2016, 15:30:15
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:

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