PDA

View Full Version : VBO fails to render circles



form ostrich
05-01-2011, 03:40 PM
I've been trying for hours to put a circle into a VBO and then draw a bunch of them using OpenGL es 1.1. The circle draws fine when I use drawArray w/o using the VBO. The program compiles and runs but the screen is blank. This is pretty lengthy, so I don't expect a reply, but I would really appreciate some help. This is my second day of opengl programming, and I really enjoy it, but it can also be frustrating.

<pre><code>
////////////////////initialize vbos
void init(GL10 gl) {

/////SETUP BUFFERS
FloatBuffer testBuffer;
FloatBuffer indexBuffer;
ByteBuffer buffer = ByteBuffer.allocateDirect(numVerticesHR * 3 * 4);
buffer.order(ByteOrder.nativeOrder());
testBuffer = buffer.asFloatBuffer();
buffer = ByteBuffer.allocateDirect(numVerticesHR * 4);
buffer.order(ByteOrder.nativeOrder());
indexBuffer = buffer.asFloatBuffer();

//////CREATE CIRCLES
for (int j = 0; j < numVerticesHR; j++) {
testBuffer.put((float) (Math.cos(theta)));
testBuffer.put((float) (Math.sin(theta)));
testBuffer.put(1);
theta += 2 * Math.PI / (numVerticesHR);
indexBuffer.put(j);
}

//ADD VERTICES BUFFER TO VBO
GL11 gl11 = (GL11) gl;
testBuffer.flip();
int[] buffers = new int[1];
gl11.glGenBuffers(1, buffers, 0);
circleIndexHR = buffers[0];
gl11.glBindBuffer(GL11.GL_ARRAY_BUFFER, circleIndexHR);
final int siz = testBuffer.capacity() * Float.SIZE / 8;
gl11.glBufferData(GL11.GL_ARRAY_BUFFER, siz, testBuffer,
GL11.GL_STATIC_DRAW);


/////ADD INDEX BUFFER TO VBO
indexBuffer.flip();
gl11.glBindBuffer(GL11.GL_ARRAY_BUFFER, 0);
gl11.glGenBuffers(1, buffers, 0);
ind = buffers[0];
gl11.glBindBuffer(GL11.GL_ELEMENT_ARRAY_BUFFER, ind);
int size = indexBuffer.capacity() * Float.SIZE / 8;
gl11.glBufferData(GL11.GL_ELEMENT_ARRAY_BUFFER, size, indexBuffer,
GL11.GL_STATIC_DRAW);
gl11.glBindBuffer(GL11.GL_ELEMENT_ARRAY_BUFFER, 0);
}

////////////////////////////////////////////////////
/////////////////DRAW CIRCLES////////////////////////
public void drawCircles(GL10 gl) {
gl11.glEnableClientState(GL11.GL_VERTEX_ARRAY);

circleBuffer = new ArrayList<Circle>(data.circle);
for (int i = 0; i < circle.size(); i++) {
///////////GET OBJECT DATA
circle = circleBuffer.get(i);
RGB = circle.getColor();
red = RGB[0] / 255.0f;
green = RGB[1] / 255.0f;
blue = RGB[2] / 255.0f;

/////DRAW CIRCLES FROM VBO
gl11.glBindBuffer(GL11.GL_ARRAY_BUFFER, circleIndexHR);
gl11.glVertexPointer(3, GL10.GL_FLOAT, 0, 0);
gl11.glBindBuffer(GL11.GL_ELEMENT_ARRAY_BUFFER, ind);
gl11.glColor4f(.5f,1f,.7f,.5f);
gl11.glTranslatef(transX - innerBallOffset, transY
+ innerBallOffset, 0);
gl11.glScalef(scaleFactor * particleSize, scaleFactor
* particleSize, 0);
gl11.glDrawElements(GL11.GL_TRIANGLE_FAN, numVerticesHR,
GL11.GL_UNSIGNED_SHORT, 0);
gl11.glBindBuffer(GL11.GL_ARRAY_BUFFER, 0);
gl11.glBindBuffer(GL11.GL_ELEMENT_ARRAY_BUFFER, 0);
}
}

V-man
05-02-2011, 07:56 AM
When you call glBindBuffer, you allocate an amount in bytes. A float would be 4 bytes and ushort is 2 bytes.
I don't know the language you are using but probably this is wrong
int size = indexBuffer.capacity() * Float.SIZE / 8

Also, glDrawElements takes number of indices as a parameter, NOT number of vertices.
glDrawElements(GL11.GL_TRIANGLE_FAN, numVerticesHR, GL11.GL_UNSIGNED_SHORT, 0);