Search code examples
javaopengllwjglvbovao

Render different VAOs


I am learning and programming with LWJGL/OpenGL. I want to render my map in chunks. For this I create a VAO and VBO for each chunk and upload and link them.

The problem is, that if I render the first chunk, all of the following 3 chunks will be rendered on top of that. If I render the second chunk the following 2 chunks will be rendered on top.

This is the log of my application, describing how I setup my VAOs:

Chunk: 0 0
Generate VBO: 1
Generate VAO: 1
Bind VAO: 1
Bind VBO: 1
Enable and set VertexAttribPointer
Bufferdata (Buffersize: 36864)
Bind VAO: 0

Chunk: 0 1
Generate VBO: 2
Generate VAO: 2
Bind VAO: 2
Bind VBO: 2
Enable and set VertexAttribPointer
Bufferdata (Buffersize: 1536)
Bind VAO: 0

Chunk: 1 0
Generate VBO: 3
Generate VAO: 3
Bind VAO: 3
Bind VBO: 3
Enable and set VertexAttribPointer
Bufferdata (Buffersize: 1536)
Bind VAO: 0

Chunk: 1 1
Generate VBO: 4
Generate VAO: 4
Bind VAO: 4
Bind VBO: 4
Enable and set VertexAttribPointer
Bufferdata (Buffersize: 64)
Bind VAO: 0

Draw
Bind VAO: 1
Set Unifoms
Draw Arrays

The buffersizes are as expected.

For binding I use glBindVertexArray() and glBindBuffer(GL_ARRAY_BUFFER, )

For drawing I use glDrawArrays()

Is the way I setup my VAOs wrong, or do you need my code to solve my problem?


Solution

  • When using glDrawArrays, the count parameter specifies the number of vertices, not, unlike some other functions, the number of values (floats) or bytes. OpenGL does not check whether the count exceeds the capacity of the VBO. Therefore, when you specify a larger count value than there is storage in the VBO, OpenGL will silently take values from adjacent memory, which in this case contains your other VBOs.

    So you have to be sure you pass the number of vertices, because the number of floats is three times as large, drawing all your data instead of just the data for one chunk.


    Original Post

    It seems that you have depth testing disabled, which is a technique that discards all fragments that are farther away than existing ones to prevent farther objects from covering closer ones.

    You can enable it with

    glEnable(GL_DEPTH_TEST);
    

    during initialization.