vbo MapBuffer problem

Hi! I use JOGL 1.1.1.a. I can draw my cube using vbo but when I want to animate the cube I have a problem with change vertices position.

Init VBO


gl.glGenBuffers(bufferSize, IntBuffer.wrap(buffer));

verticesID = buffer[0];
gl.glBindBuffer(GL.GL_ARRAY_BUFFER, verticesID); 
gl.glBufferData(GL.GL_ARRAY_BUFFER, BufferUtil.SIZEOF_FLOAT * vertices.capacity(), vertices, GL.GL_DYNAMIC_DRAW);
gl.glBindBuffer(GL.GL_ARRAY_BUFFER, 0);

normalsID = buffer[1];
gl.glBindBuffer(GL.GL_ARRAY_BUFFER, normalsID); 
gl.glBufferData(GL.GL_ARRAY_BUFFER, BufferUtil.SIZEOF_FLOAT * normals.capacity(), normals, GL.GL_DYNAMIC_DRAW);
gl.glBindBuffer(GL.GL_ARRAY_BUFFER, 0);

gl.glEnableClientState(GL.GL_VERTEX_ARRAY);
gl.glEnableClientState(GL.GL_NORMAL_ARRAY);

Display and Update Vertices


gl.glPushMatrix();

gl.glMaterialfv(GL.GL_FRONT_AND_BACK, GL.GL_AMBIENT, material.getAmbient(), 0);
gl.glMaterialfv(GL.GL_FRONT_AND_BACK, GL.GL_DIFFUSE, material.getDiffuse(), 0);
gl.glMaterialfv(GL.GL_FRONT_AND_BACK, GL.GL_SPECULAR, material.getSpecular(), 0);
gl.glMaterialfv(GL.GL_FRONT_AND_BACK, GL.GL_EMISSION, material.getEmission(), 0);
gl.glMaterialfv(GL.GL_FRONT_AND_BACK, GL.GL_SHININESS, material.getShininess(), 0);

gl.glBindBuffer(GL.GL_ARRAY_BUFFER, verticesID);
gl.glVertexPointer(3, GL.GL_FLOAT, 0, 0);
gl.glBindBuffer(GL.GL_ARRAY_BUFFER, 0);

gl.glBindBuffer(GL.GL_ARRAY_BUFFER, normalsID);
gl.glNormalPointer(GL.GL_FLOAT, 0, 0);
gl.glBindBuffer(GL.GL_ARRAY_BUFFER, 0);

gl.glDrawArrays(GL.GL_QUADS, 0, 24);

gl.glBindBuffer(GL.GL_ARRAY_BUFFER, verticesID);

byteBuffer = gl.glMapBuffer(GL.GL_ARRAY_BUFFER, GL.GL_READ_WRITE);
if(bufferRW != null)
{
     srcVertices = byteBuffer.asFloatBuffer();
     
     vertices.rewind();
            
     for(int i = 0; i < 24; i++)
     {
          vX = srcVertices.get();
          vY = srcVertices.get();
          vZ = srcVertices.get();

          vertices.put(vX);
          vertices.put(vY + 0.2f);
          vertices.put(vZ);
     }
}
gl.glUnmapBuffer(GL.GL_ARRAY_BUFFER);
gl.glBindBuffer(GL.GL_ARRAY_BUFFER, 0);
gl.glPopMatrix();


The cube should move up but nothing happens. What is wrong with my code ?

You are writing to the vertices buffer but srcVertices and bytBuffer are the two mapped buffers. You need to write to either byteBuffer or srcVertices to make the graphics card see the data. In your for loop, just do:


vX = vertices.get();
vY= vertices.get();
vZ = vertices.get();

srcVertices.put(vX);
srcVertices.put(vY + .2f);
srcVertices.put(vZ);

I changed my code but still nothing happens.

Can you post all of your code, so I can copy it and try giving it a run, this is something where it’s easier for me to experiment with directly than brainstorm.

Netbeans 6.7.1 + JOGL 1.1.1.a (plugin). Complete Netbeans project: http://www.speedyshare.com/files/28257488/SimpleGLCanvas.rar

I should have seen this sooner, but the problem is that you want to read and write from srcVertices only. In your method, you read from srcVertices correctly but didn’t pass the modified values back to the mapped buffer. Since I just swapped your code around a bit, you would keep reading un-updated values from vertices and storing them into the mapped buffer correctly.

Here is a code block that works:


srcVertices = byteBuffer.asFloatBuffer(); 
srcVertices.rewind();                     
                                          
for(int i = 0; i < 24; i++)               
{                                                                             
    vX = srcVertices.get(i * 3);          
    vY = srcVertices.get(i * 3 + 1);      
    vZ = srcVertices.get(i * 3 + 2);      
                                          
    srcVertices.put(i * 3, vX);           
    srcVertices.put(i * 3 + 1, vY + 0.5f);
    srcVertices.put(i * 3 + 2, vZ);                                        
}                                         

This should get mapped buffers working. I do want to say that this isn’t the best way to animate a cube. If you’re just experimenting with mapped buffers, that’s fine, but you should know that there are transform matrices that you can modify to achieve the same translation effects without needing to map memory from the graphics card into a buffer.

Thank you. I examine how VBO works. It is good thing when you have many objects, for example in some physics simulations. However a transfer of data between a gpu memory and client memory might be a bottleneck. I want to use JCUDA to interact with JOGL. Then VBO cooperate with JCUDA and a data transfer between the client and server is needless. What do you mean by transform matrices ? Could you show some example? JOGL 1.1.1.a supports opengl 3.0. How big are difference between using VBO in JOGL 2 and JOGL 1.1.1.a ?

Using VBOs is not really different from JOGL2 and JOGL1.1.1a. They both wrap OpenGL, the main difference is that JOGL2 is the latest version and is still being maintained and updated so I would recommend switching to it. It shouldn’t be difficult to change over, in most cases you’ll switch GL to GL2 and it’s as simple as that.

As far as transforms go, every vertex that is rendered is multiplied with a 4x4 transform matrix. This lets the vertex be rotated and translated. You control these with commands such glTranslate, glRotate, glMultMatrix, and glLoadMatrix. If you haven’t learned about these concepts in OpenGL yet, I would recommend reading some OpenGL tutorials (even if they’re in C/C++ since the concepts are the same) before worrying about JCUDA.

I know these concepts (that are fundamentals of OpenGL), but I wrong understood you. My English isn’t perfect. I assumed that using glTranslate to animate is not a good idea. Of course when I only want to animate the cube and nothing else happens I can use glTranslate instead of glMapBuffer to achieve the same effect:


gl.glPushMatrix();
gl.glTranslatef(0.0f, y, 0.0f);
gl.glDrawArrays(GL.GL_QUADS, 0, 24);
gl.glPopMatrix();
y += 0.5f;

In more complex situation, for example in same physics simulation where many object interact with each other using glTranslate to animate objects seems to be unsuitable. I’m going to work in JOGL 2, because I want to learn opengl 3.3 (and subsequent) features. I know a few very good opengl tutorials but first I need to make good JOGL2 framework in Netbeans. Which JOGL2 tutorial can you recommend me ?

The transform matrices are a great way to animate complete objects, like the position of a player in the world. In physics, if it’s a rigid body simulation, you only need to use the transform matrices. When you do cloth simulation, then you’ll want to use mapped buffers to animate the actual geometry.

I didn’t use a tutorial for JOGL2 ever, I just looked at the Javadocs and found it pretty straightforward to change from JOGL1 to JOGL2. But here is the wiki for their mainsite, it will probably be helpful enough: http://jogamp.org/wiki/index.php/Main_Page