Reputation: 5949
How can this be done? This code isn't working:
//////////INIT COLOR VBO FOR USE WITH A TRIANGLE
triColorBuffer = ByteBuffer.allocateDirect(3 * 4
* 4).order(ByteOrder.nativeOrder()).asFloatBuffer();
float[] colors = {
1f, 0f, 0f, 1f,
0f, 1f, 0f, 1f,
0f, 0f, 1f, 1f,};
triColorBuffer.put(colors);
triColorBuffer.flip();
int[] buffer = new int[1];
gl11.glGenBuffers(1, buffer, 0);
colorPointerTri = buffer[0];
gl11.glBindBuffer(GL11.GL_ARRAY_BUFFER, colorPointerTri);
gl11.glBufferData(GL11.GL_ARRAY_BUFFER, triColorBuffer.capacity()
* 4, triColorBuffer, GL11.GL_STATIC_DRAW);
////////////DRAW USING COLOR VBO
gl11.glEnableClientState(GL10.GL_COLOR_ARRAY);
gl11.glEnableClientState(GL10.GL_VERTEX_ARRAY);
gl11.glPushMatrix();
gl11.glBindBuffer(GL11.GL_ARRAY_BUFFER, vertexPointerTri);
gl11.glBindBuffer(GL11.GL_ELEMENT_ARRAY_BUFFER, indexPointerTri);
gl11.glBindBuffer(GL11.GL_ARRAY_BUFFER, colorPointerTri);
gl11.glColorPointer(4, GL10.GL_FLOAT, 0, 0);
gl11.glVertexPointer(3, GL10.GL_FLOAT, 0, 0);
gl11.glDrawElements(GL10.GL_TRIANGLES, 3, GL10.GL_UNSIGNED_SHORT, 0);
gl11.glDisableClientState(GL10.GL_COLOR_ARRAY);
gl11.glDisableClientState(GL10.GL_VERTEX_ARRAY);
gl11.glPopMatrix();
gl11.glDisableClientState(GL10.GL_COLOR_ARRAY);
gl11.glDisableClientState(GL10.GL_VERTEX_ARRAY);
gl11.glBindBuffer(GL11.GL_ARRAY_BUFFER, 0);
gl11.glBindBuffer(GL11.GL_ELEMENT_ARRAY_BUFFER, 0);
The triangle draws VBO'd or nor, and the color gradient (glcolorpointer) works non VBO'd, but I can't get the VBO'd triangle to color using a color pointer (VBO or without). Help Appreciated.
Upvotes: 0
Views: 2865
Reputation: 45948
You are binding the vertex buffer and right after that you bind the color buffer. When you now call glColor/VertexPointer, both colors and vertices are sourced from the color buffer. You need to write:
gl11.glBindBuffer(GL11.GL_ARRAY_BUFFER, vertexPointerTri);
gl11.glVertexPointer(3, GL10.GL_FLOAT, 0, 0);
gl11.glBindBuffer(GL11.GL_ARRAY_BUFFER, colorPointerTri);
gl11.glColorPointer(4, GL10.GL_FLOAT, 0, 0);
As the gl...Pointer command uses the buffer that is currently bound.
Upvotes: 3
Reputation: 6073
Please do correct me if I'm wrong, I started out with VBOs just 15mins ago.
Anyway, it seems so you have to use VBOs 'in two'. First you bind your data/array, and right after that tell GL context what you just gave it. Took me a good a while to read through this source file to figure out what was going on and truly hope I didn't misunderstood it totally. Excerpt from that file;
if (useTexture) {
gl11.glBindBuffer(GL11.GL_ARRAY_BUFFER, mTextureCoordBufferIndex);
gl11.glTexCoordPointer(2, mCoordinateType, 0, 0);
}
if (useColor) {
gl11.glBindBuffer(GL11.GL_ARRAY_BUFFER, mColorBufferIndex);
gl11.glColorPointer(4, mCoordinateType, 0, 0);
}
In your code, you're binding 3 times and right after start switching what was binded. I don't know what exactly happens there but I would give it a go to try e.g.
gl11.glBindBuffer(GL11.GL_ARRAY_BUFFER, vertexPointerTri);
gl11.glVertexPointer(3, GL10.GL_FLOAT, 0, 0);
gl11.glBindBuffer(GL11.GL_ARRAY_BUFFER, colorPointerTri);
gl11.glColorPointer(4, GL10.GL_FLOAT, 0, 0);
gl11.glBindBuffer(GL11.GL_ELEMENT_ARRAY_BUFFER, indexPointerTri);
gl11.glDrawElements(GL10.GL_TRIANGLES, 3, GL10.GL_UNSIGNED_SHORT, 0);
Also from what I've seen it shouldn't do any harm if you unbind your arrays after rendering. Maybe it's in your code not copy/pasted here, but just in case.
gl11.glBindBuffer(GL11.GL_ARRAY_BUFFER, 0);
gl11.glBindBuffer(GL11.GL_ELEMENT_ARRAY_BUFFER, 0);
Upvotes: 3