glMapBuffer returns null

I have a simple JOGL application that uses VBOs to switch between two simple models (a 2D triangle and a 2D square) once every 5 seconds. I wrote it to just get a feel for how vertex buffer objects work. Anyway, everything is working fine except for calls to glMapBuffer(). glMapBuffer always returns null, and I have no idea why. Any help figuring this out would be appreciated. I’m using JSR-231 Beta 5.

Here’s the relevant code:


        IntBuffer squareVerts, indices, triVerts;

	private void switchModels(GL gl)
	{
		int numIndices;
		IntBuffer currModel;
		
		if(useSquare)
		{
		    useSquare = false;
		    numIndices = 3;
		    currModel = triVerts;
		}
		else
		{
		    useSquare = true;
		    numIndices = 4;
		    currModel = squareVerts;
		}
		
		//copy new model into the buffer
		currModel.rewind();
		gl.glBindBuffer(GL.GL_ARRAY_BUFFER, bufferID);
		gl.glBufferSubData(GL.GL_ARRAY_BUFFER, 
		                                   0, 
		                                   numIndices*3*BufferUtil.SIZEOF_INT, 
                                                   currModel);
		gl.glBindBuffer(GL.GL_ARRAY_BUFFER, 0);
		
		//set number of indices for new model
		indices.flip();
		indices.limit(numIndices);
		
		//print out changed buffer
		ByteBuffer vboContents = gl.glMapBuffer(GL.GL_ARRAY_BUFFER, bufferID);
		if(vboContents != null)
		{
		    IntBuffer tmp = vboContents.asIntBuffer();
		    int[] currVerts = tmp.array();
			
		     for(int i = 0; i < currVerts.length; i+=3)
		    {
			    System.out.println("vert " + i + ": ( " + currVerts[i] +
							     ", " + currVerts[i+1] + ", " + currVerts[i+2]);
		    }
		    System.out.println();
		}
		else
		{
		    System.out.println("glMapBuffer returned null");
		}
		gl.glUnmapBuffer(bufferID);
	}

	public void display(GLAutoDrawable drawable) {
	    GL gl = drawable.getGL();
	    GLU glu = new GLU();
		
	    gl.glMatrixMode(GL.GL_MODELVIEW);
    	    gl.glLoadIdentity();
    	
            gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
        
            cam.position(glu);
        
            gl.glColor3f(1.0f, 0.0f, 0.0f);
        
            gl.glBindBuffer(GL.GL_ARRAY_BUFFER, bufferID);
            gl.glVertexPointer(3, GL.GL_INT, 0, 0);
        
        
            gl.glDrawElements(GL.GL_TRIANGLE_STRIP, indices.limit(), GL.GL_UNSIGNED_INT, indices);

            indices.rewind();
            gl.glBindBuffer(GL.GL_ARRAY_BUFFER, 0);
        
            long currTime = System.currentTimeMillis();
            if(currTime - lastTick > 5000){
        	    switchModels(gl);
        	    lastTick = currTime;
            }
	}

	public void initVBO(GL gl)
	{
		int[] ids = new int[1];
		gl.glGenBuffers(1, ids, 0);
		bufferID = ids[0];
		
		int[] sv= {-1, 1, 0,
				 -1, -1, 0,
				 1, 1, 0,
				 1, -1, 0};
		
		int[] tv = {0, 1, 0,
				-1, -1, 0,
				 1, -1, 0};
		
		squareVerts = BufferUtil.newIntBuffer(sv.length);
		squareVerts.put(sv);
		squareVerts.flip();
		
		triVerts = BufferUtil.newIntBuffer(tv.length);
		triVerts.put(tv);
		triVerts.flip();
		
		gl.glBindBuffer(GL.GL_ARRAY_BUFFER, bufferID);
		gl.glBufferData(GL.GL_ARRAY_BUFFER, sv.length*4, squareVerts, GL.GL_DYNAMIC_DRAW);
		gl.glBindBuffer(GL.GL_ARRAY_BUFFER, 0);
		
		int[] temp = {0, 1, 2, 3};
		indices = BufferUtil.newIntBuffer(temp.length);
		indices.put(temp);
		indices.flip();
		
	}

And of course, about 10 minutes after posting this, I finally figure it out. Go figure. :stuck_out_tongue:

I misunderstood what the arguments to glMapBuffer should be. The correct sequence of calls should be:


gl.glBindBuffer(GL.GL_ARRAY_BUFFER, bufferID);
ByteBuffer theBuffer = gl.glMapBuffer(GL.GL_ARRAY_BUFFER, GL.GL_READ_ONLY);  //this app is only reading from the buffer during the mapping
<read from buffer.  Do Stuff.>
gl.glUnmapBuffer(GL.GL_ARRAY_BUFFER);
gl.glBindBuffer(GL.GL_ARRAY_BUFFER, 0);

I made the above changes, and now everything works the way I intended. Hopefully, if anyone else had the same misunderstanding I did, this post will help them.