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?