VBOs

Ok, I already searched past topics in here and took a look at the jogl demos source for vertex buffer objects but I have a few questions because I couldn’t make it work yet.

I’m not sure if the following code is correct:

  • Is this correct?

The first time it reaches the geometry it sets the buffer:


			int numVertices = poly.getVertices().size();
			int numNormals = poly.getNormals().size();

			FloatBuffer vertices = BufferUtil.newFloatBuffer(numVertices * 3);
			FloatBuffer normals = BufferUtil.newFloatBuffer(numNormals * 3);

			for (int i = 0; i < numVertices; i++) {
				Vector3f vertex = poly.getVertices().get(i);
				vertices.put(vertex.x);
				vertices.put(vertex.y);
				vertices.put(vertex.z);
			}

			for (int i = 0; i < numNormals; i++) {
				Vector3f normal = poly.getNormals().get(i);
				normals.put(normal.x);
				normals.put(normal.y);
				normals.put(normal.z);
			}

			vertices.flip();
			normals.flip();

			int[] ids = new int[2];
			gl.glGenBuffersARB(2, ids, 0);
			gl.glBindBufferARB(GL_ARRAY_BUFFER_ARB, ids[0]);
			gl.glBufferDataARB(GL_ARRAY_BUFFER_ARB, vertices.capacity() * 4,
					vertices, GL_STATIC_DRAW_ARB);
			poly.setVertexBufferId(ids[0]);

			gl.glBindBufferARB(GL_ARRAY_BUFFER_ARB, ids[1]);
			gl.glBufferDataARB(GL_ARRAY_BUFFER_ARB, normals.capacity() * 4,
					normals, GL_STATIC_DRAW_ARB);
			poly.setNormalBufferId(ids[1]);

The next times it just binds the buffer and draws the arrays:


			gl.glBindBufferARB(GL_ARRAY_BUFFER_ARB, poly.getVertexBufferId());
			gl.glVertexPointer(3, GL_FLOAT, 0, 0);

			gl.glBindBufferARB(GL_ARRAY_BUFFER_ARB, poly.getNormalBufferId());
			gl.glNormalPointer(GL_FLOAT, 0, 0);

			gl.glDrawArrays(GL_POLYGON, 0, poly.getVertices().size());

  • Why in the jogl demos glMapBuffer is used? Is this really needed?

Are you 100% sure you want GL_POLYGON and not GL_TRIANGLES ?

to answer you 2nd question: glMapBuffer is not needed

This is just sample data that draws a cube. I won’t really use polygons when I start loading my own models.

GL_POLYGON is really not what you want to draw a cube

all vertices of a polygon must lay on the same plane, and the shape must be convex.

So really, switch to either triangles or quads for your cube.

1 cube = 6 sides = 6 polygons

K :-*

Anyway, did you set:
glEnable(GL_VERTEX_ARRAY);
glEnable(GL_NORMAL_ARRAY);

I found where the problem is, it’s related to the view and camera code, if I put a value in lookAt manually it shows the cube. I’m going to debug it tomorrow.

Yes, it seems VBO is working fine. Thanks.