Login  Register

[JOGL] VBO's not updating

Posted by Al on Jun 02, 2015; 9:05am
URL: https://forum.jogamp.org/JOGL-VBO-s-not-updating-tp4034566.html

Hi,

First, I apologize for eventually mistakes in english.

I am pretty new in OpenGL and I am creating an interface for creating voxelized object, using JOGL. Basically, we have the object in the middle of the interface, the list of materials to the right and few utilitary thing to the left. This object can be saved in the text format.

For now, I can load a file and display the object using vbo.
 

This is my code to load the object in my VBO's:

<code>
private void loadVoxels(GL2 gl)
        {
                float sizeVoxelX = VoxObject.getVoxelDimensionX()/2;
                float sizeVoxelY = VoxObject.getVoxelDimensionY()/2;
                float sizeVoxelZ = VoxObject.getVoxelDimensionZ()/2;
                int nbX = VoxObject.getNbVoxelX();
                int nbY = VoxObject.getNbVoxelY();
                int nbZ = VoxObject.getNbVoxelZ();
                nbVoxel = nbX*nbY*nbZ;

                /**  Load Vertex in a buffer **/
                vertexArray = Buffers.newDirectFloatBuffer(8*3*nbVoxel);
                for(int i=0;i<nbX;i++)
                {
                        for(int j=0;j<nbY;j++)
                        {
                                for(int k=0;k<nbZ;k++)
                                {
                                        float[] vertex = new float[]{
                                                        sizeVoxelX+(i*sizeVoxelX*2),sizeVoxelY+(j*sizeVoxelY*2),sizeVoxelZ-(k*sizeVoxelZ*2), //0
                                                        -sizeVoxelX+(i*sizeVoxelX*2),sizeVoxelY+(j*sizeVoxelY*2),sizeVoxelZ-(k*sizeVoxelZ*2), //1
                                                        sizeVoxelX+(i*sizeVoxelX*2),sizeVoxelY+(j*sizeVoxelY*2),-sizeVoxelZ-(k*sizeVoxelZ*2), //2
                                                        -sizeVoxelX+(i*sizeVoxelX*2),sizeVoxelY+(j*sizeVoxelY*2),-sizeVoxelZ-(k*sizeVoxelZ*2), //3

                                                        sizeVoxelX+(i*sizeVoxelX*2),-sizeVoxelY+(j*sizeVoxelY*2),sizeVoxelZ-(k*sizeVoxelZ*2), //4
                                                        -sizeVoxelX+(i*sizeVoxelX*2),-sizeVoxelY+(j*sizeVoxelY*2),sizeVoxelZ-(k*sizeVoxelZ*2), //5
                                                        sizeVoxelX+(i*sizeVoxelX*2),-sizeVoxelY+(j*sizeVoxelY*2),-sizeVoxelZ-(k*sizeVoxelZ*2), //6
                                                        -sizeVoxelX+(i*sizeVoxelX*2),-sizeVoxelY+(j*sizeVoxelY*2),-sizeVoxelZ-(k*sizeVoxelZ*2) //7
                                        };
                                        vertexArray.put(vertex);
                                }
                        }
                }
                vertexArray.flip();
               
               **  Load Indices in a buffer **/
                vertexInd = Buffers.newDirectIntBuffer(6*4*nbVoxel);
                for(int i=0;i<nbVoxel;i++)
                {
                        int[] indices = new int[]{
                                        0+(8*i),2+(8*i),3+(8*i), 1+(8*i),

                                        4+(8*i),6+(8*i),2+(8*i), 0+(8*i),

                                        5+(8*i),7+(8*i),6+(8*i), 4+(8*i),

                                        1+(8*i),3+(8*i),7+(8*i), 5+(8*i),

                                        2+(8*i),6+(8*i),7+(8*i), 3+(8*i),

                                        4+(8*i),0+(8*i),1+(8*i), 5+(8*i)
                        };
                        vertexInd.put(indices);
                }
                vertexInd.flip();
       
               **  Load color in a buffer **/
                colorArray = Buffers.newDirectFloatBuffer(8*3*nbVoxel);
                for(int i=0;i<nbVoxel;i++)
                {
                        float[] color= new float[]{
                                        1,0,0,
                                        1,0,0,
                                        1,0,0,
                                        1,0,0,
                                        1,0,0,
                                        1,0,0,
                                        1,0,0,
                                        1,0,0,
                        };
                        colorArray.put(color);
                }
                colorArray.flip();
               
                /******** VBO *********/
                /** The glGenBuffer is done in the init function **/

                gl.glBindBuffer(GL.GL_ARRAY_BUFFER, vbo[0]);
                gl.glBufferData(GL.GL_ARRAY_BUFFER, vertexArray.capacity()*Buffers.SIZEOF_FLOAT,vertexArray, GL.GL_DYNAMIC_DRAW);
                gl.glBindBuffer(GL.GL_ARRAY_BUFFER, 0);
               
                gl.glBindBuffer(GL.GL_ARRAY_BUFFER, vbo[1]);
                gl.glBufferData(GL.GL_ARRAY_BUFFER, colorArray.capacity()*Buffers.SIZEOF_FLOAT, colorArray, GL.GL_DYNAMIC_DRAW);
                gl.glBindBuffer(GL.GL_ARRAY_BUFFER, 0);
               
                gl.glBindBuffer(GL.GL_ELEMENT_ARRAY_BUFFER, vbo[2]);
                gl.glBufferData(GL.GL_ELEMENT_ARRAY_BUFFER, vertexInd.capacity()*Buffers.SIZEOF_INT, vertexInd, GL.GL_DYNAMIC_DRAW);
                gl.glBindBuffer(GL.GL_ELEMENT_ARRAY_BUFFER, 0);
               
                System.err.println(VoxObject.toString());
                vboIsUpToDate=true;
}

<h2>And this is the one to render:
public void display(GLAutoDrawable drawable) {
                // TODO Auto-generated method stub
               
                GL2 gl = drawable.getGL().getGL2();

                camera.look(gl,glu);
                if(!vboIsUpToDate)
                        loadVoxels(gl);
               
                gl.glHint(GL2.GL_PERSPECTIVE_CORRECTION_HINT, GL2.GL_NICEST);
                gl.glEnable(GL.GL_DEPTH_TEST);
                gl.glUseProgram(program.getProgramId());
               
                gl.glEnableClientState(GL2.GL_VERTEX_ARRAY);
                gl.glEnableClientState(GL2.GL_COLOR_ARRAY);
                gl.glEnableClientState(GL2.GL_INDEX_ARRAY);

                gl.glBindBuffer(GL.GL_ARRAY_BUFFER, vbo[0]);
                gl.glVertexPointer(3, GL.GL_FLOAT, 0, 0);
                gl.glBindBuffer(GL.GL_ARRAY_BUFFER, vbo[1]);
                gl.glColorPointer(3, GL.GL_FLOAT, 0, 0);
                gl.glBindBuffer(GL.GL_ELEMENT_ARRAY_BUFFER, vbo[2]);
               
                gl.glDrawElements(GL2.GL_POINTS, vertexInd.capacity(), GL.GL_UNSIGNED_INT, 0);

                gl.glDisableClientState(GL2.GL_VERTEX_ARRAY);
                gl.glDisableClientState(GL2.GL_COLOR_ARRAY);
                gl.glDisableClientState(GL2.GL_INDEX_ARRAY);
               
                gl.glBindBuffer(GL.GL_ARRAY_BUFFER, 0);
                gl.glBindBuffer(GL.GL_ELEMENT_ARRAY_BUFFER, 0);
               
                gl.glUseProgram(0);
        }


The shader is a simple one, my problem don't resolve itself by not activating it. And I make, for those wo will notice, my glGenBuffers in the init method.

This is my problem:


You see that ? ==> if(!vboIsUpToDate)
                                  loadVoxels(gl);

It's to update vertices when something changes, like the loading of a new file. Here is the thing: When I loading a new file, my new voxObject is well loaded (I made some Sysout to check) but the animation is not update, I always see the previous object on my screen.

gl.glBindBuffer(GL.GL_ARRAY_BUFFER, vbo[0]);
                gl.glBufferData(GL.GL_ARRAY_BUFFER, vertexArray.capacity()*Buffers.SIZEOF_FLOAT,vertexArray, GL.GL_DYNAMIC_DRAW);
                gl.glBindBuffer(GL.GL_ARRAY_BUFFER, 0);
               
                gl.glBindBuffer(GL.GL_ARRAY_BUFFER, vbo[1]);
                gl.glBufferData(GL.GL_ARRAY_BUFFER, colorArray.capacity()*Buffers.SIZEOF_FLOAT, colorArray, GL.GL_DYNAMIC_DRAW);
                gl.glBindBuffer(GL.GL_ARRAY_BUFFER, 0);
               
                gl.glBindBuffer(GL.GL_ELEMENT_ARRAY_BUFFER, vbo[2]);
                gl.glBufferData(GL.GL_ELEMENT_ARRAY_BUFFER, vertexInd.capacity()*Buffers.SIZEOF_INT, vertexInd, GL.GL_DYNAMIC_DRAW);
                gl.glBindBuffer(GL.GL_ELEMENT_ARRAY_BUFFER, 0);


I want this last code to update vbo's, but the vbo's seems to be still the same. But why ? I fill them with a new vertex Array with a new size, why I always see the same object ?

For resume, I can't modify VBO's after having filled it a first time.

Someone can help me, please ? I am break my head on this for two days...


EDIT:To precise, my Scene is in a FPSAnimator and the change with a new loading is done like that:


/*** Ask to the user the new file **/
File file = FileChooser.getSelectedFile();

/*** Load the file in the field member VoxelizedSample ***/
loadFile(file.getAbsolutePath());

/*** Give it to the Scene, setting vboIsUpToDate to false ***/
Scene.setNewObject(VoxSample);


Thank you for reading, hope someone will help !