Reply – Multiple VAO and VBO with JOGL
Your Name
Subject
Message
or Cancel
In Reply To
Multiple VAO and VBO with JOGL
— by Chen norris Chen norris
Hello everyone,

This is my very first post in here about JOGL so sorry in advance if I missed a step before posting in here.
The problem I'm facing deals with VAO/VBO comprehension. As an example, I'd like to render a square in very specific (and not optimized) way : I would like to render two triangles with two different VAOs. The structure of my code looks like this :

INIT : RENDERING : My code for the init part is then the following :
// creation of two VAOs
int[] tempVertexArrayObject = new int[2];
gl3.glGenVertexArrays(2, tempVertexArrayObject, 0);
vaoId1 = tempVertexArrayObject[0];
vaoId2 = tempVertexArrayObject[1];

// creation of three VBOs
IntBuffer idArray = GLBuffers.newDirectIntBuffer(3);
gl3.glGenBuffers(3, idArray);
positionBufferId = idArray.get(0);
colorBufferId = idArray.get(1);
indexBufferId = idArray.get(2);

// binding and buffering the first VAO
gl3.glBindVertexArray(vaoId1);

// buffering positions
gl3.glBindBuffer(GL.GL_ARRAY_BUFFER, positionBufferId);
gl3.glBufferData(GL.GL_ARRAY_BUFFER, positionBuffer.capacity() * Buffers.SIZEOF_FLOAT, positionBuffer, GL.GL_STATIC_DRAW);
gl3.glEnableVertexAttribArray(positionHandler);
gl3.glVertexAttribPointer(positionHandler, 3, GL.GL_FLOAT, false, 0, 0);
gl3.glBindBuffer(GL.GL_ARRAY_BUFFER, 0);

// buffering colors
gl3.glBindBuffer(GL.GL_ARRAY_BUFFER, colorBufferId);
gl3.glBufferData(GL.GL_ARRAY_BUFFER, colorBuffer.capacity() * Buffers.SIZEOF_FLOAT, colorBuffer, GL.GL_STATIC_DRAW);
gl3.glEnableVertexAttribArray(colorHandler);
gl3.glVertexAttribPointer(colorHandler, 4, GL.GL_FLOAT, false, 0, 0);
gl3.glBindBuffer(GL.GL_ARRAY_BUFFER, 0);

// buffering indices
gl3.glBindBuffer(GL.GL_ELEMENT_ARRAY_BUFFER, indexBufferId);
gl3.glBufferData(GL.GL_ELEMENT_ARRAY_BUFFER, indexBuffer.capacity() * Buffers.SIZEOF_INT, indexBuffer, GL.GL_STATIC_DRAW);

// binding and buffering the second VAO + buffering positions + ...
// same as before, but gl.glBindVertexArray(vaoId2) instead of gl.glBindVertexArray(vaoId1), and with other buffers
// that contain data for the other triangle
My code for the rendering part is the following :
// Drawing the first triangle
gl3.glBindVertexArray(vaoId1);
gl3.glDrawElements(GL3.GL_TRIANGLES, 3, GL3.GL_UNSIGNED_INT, 0);
gl3.glBindVertexArray(0);

// Drawing the second triangle
gl3.glBindVertexArray(vaoId2);
gl3.glDrawElements(GL3.GL_TRIANGLES, 3, GL3.GL_UNSIGNED_INT, 0);
gl3.glBindVertexArray(0);
Of course, I didn't put the part where I clear my background, I enable depth test, ...
My problem is that only the second triangle is drawn, as if buffering data when VAO #2 is binded buffers data for VAO #1. Another strange behavior I can see is that when I only call the Drawing the first triangle instructions, I can see my second triangle.

Any advice on something I misunderstood or on something I may do wrong ?

Thanks in advance for your help.