Java-Gaming.org Hi !
Featured games (91)
games approved by the League of Dukes
Games in Showcase (762)
Games in Android Showcase (229)
games submitted by our members
Games in WIP (846)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Problem drawing the content of multiple VAOs  (Read 2317 times)
0 Members and 1 Guest are viewing this topic.
Offline Geminus22

Junior Newbie





« Posted 2016-04-20 16:18:58 »

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:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
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:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
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
Offline elect

JGO Knight


Medals: 59



« Reply #1 - Posted 2016-04-20 17:25:06 »

This is the workflow for setting up a generic vao:

- generate the vao
- bind the vao

for each buffer containing data:
   - bind the vbo

   for each attribute:
      - enable it (glEnableVertexAttribArray)
      - set the layout (glVertexAttribPointer)

if you use index rendering:
   bind the element buffer

unbind the vao


Some observations:

- vbo is not part of the vao, it must only be bound when you call glVertexAttribPointer, because then OpenGL will use whatever vbo is bound to GL_ARRAY_BUFFER at that moment to fetch the data from. Indeed, you can unbind the vbo after.

- the number of the enable vertex attributes is part of the vao, so you don't need to call glEnableVertexAttribArray again in the rendering process

- ebo/ibo is part of the vao, so you have to bind it and leave it bound.

- it is important to unbind the vao at the end (at least at the begin when you are inexperienced), because if you leave it bound, any successive new binding call to the GL_ELEMENT_ARRAY_BUFFER will be saved in the vao that is bound at that moment, and you don't want that

Offline Geminus22

Junior Newbie





« Reply #2 - Posted 2016-04-20 17:43:53 »

Thanks for the feedback! I will improve my code and come back once I have some results.
Pages: [1]
  ignore  |  Print  
 
 

 
EgonOlsen (353 views)
2018-06-10 19:43:48

EgonOlsen (354 views)
2018-06-10 19:43:44

EgonOlsen (294 views)
2018-06-10 19:43:20

DesertCoockie (528 views)
2018-05-13 18:23:11

nelsongames (844 views)
2018-04-24 18:15:36

nelsongames (832 views)
2018-04-24 18:14:32

ivj94 (1264 views)
2018-03-24 14:47:39

ivj94 (396 views)
2018-03-24 14:46:31

ivj94 (1067 views)
2018-03-24 14:43:53

Solater (414 views)
2018-03-17 05:04:08
Java Gaming Resources
by philfrei
2017-12-05 19:38:37

Java Gaming Resources
by philfrei
2017-12-05 19:37:39

Java Gaming Resources
by philfrei
2017-12-05 19:36:10

Java Gaming Resources
by philfrei
2017-12-05 19:33:10

List of Learning Resources
by elect
2017-03-13 14:05:44

List of Learning Resources
by elect
2017-03-13 14:04:45

SF/X Libraries
by philfrei
2017-03-02 08:45:19

SF/X Libraries
by philfrei
2017-03-02 08:44:05
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!