Hi,
I’m very new to Open GL. I’m attempting to draw a diamond shape that is missing a side. I have verifed that the points are drawn clockwise.
I believe it is an issue with the way I’m using the buffer but I can’t figure out what I’m doing wrong. I have a feeling my eyes are glazing over a typo.
public void onDrawFrame(GL10 gl) {
// Back color
gl.glClearColor(0f, 0.8f, 0f, 1.0f);
// reset the matrix
gl.glLoadIdentity();
//gl.glClearColor(red, green, blue, 1f);
gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
// which part of the triangle should be visible?
gl.glEnable(GL10.GL_CULL_FACE);
gl.glFrontFace(GL10.GL_CCW);
gl.glCullFace(GL10.GL_FRONT);
//TRIANGLE CODE START
// prepare to use vertex arrays we want to draw
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
gl.glEnableClientState(GL10.GL_COLOR_ARRAY);
float[] coords = {
0f, 0f, 1f, //0
0f, 0.25f, 0f, //1
0f, 0f, -0.25f, //2
0.25f, -0.25f, 0f, //3
-0.25f, -0.25f, 0f, //4
};
int vertexCount = coords.length;
float[] colors = {
1f, 0f, 0f, 1f, // red
1f, 1f, 0f, 1f, // yellow
1f, 1f, 1f, 1f, // white
1f, 1f, 0f, 1f, // yellow
1f, 1f, 0f, 1f, // yellow
};
short[] indices = new short[] {
0, 1, 4,
0, 4, 3,
0, 3, 1,
2, 1, 3,
2, 4, 1,
2, 3, 4,
};
// float has 4 bytes
ByteBuffer vbb = ByteBuffer.allocateDirect(coords.length * 4);
vbb.order(ByteOrder.nativeOrder());
vertexBuffer = vbb.asFloatBuffer();
// short has 2 bytes
ByteBuffer ibb = ByteBuffer.allocateDirect(indices.length * 2);
ibb.order(ByteOrder.nativeOrder());
indexBuffer = ibb.asShortBuffer();
// float has 4 bytes, 4 colors (RGBA) * number of vertices * 4 bytes
ByteBuffer cbb = ByteBuffer.allocateDirect(colors.length * 4);
cbb.order(ByteOrder.nativeOrder());
colorBuffer = cbb.asFloatBuffer();
vertexBuffer.put(coords); //all the points
indexBuffer.put(indices); //draw order
colorBuffer.put(colors);
vertexBuffer.position(0);
indexBuffer.position(0);
colorBuffer.position(0);
// define the vertices we want to draw. 3 points in a triangle
gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexBuffer);
//4 points in the color def
gl.glColorPointer(4, GL10.GL_FLOAT, 0, colorBuffer);
// finally draw the vertices
gl.glDrawElements(GL10.GL_TRIANGLES, vertexCount, GL10.GL_UNSIGNED_SHORT, indexBuffer);
}
The code above produces the following:
Any help is greatly appreciated. Thanks in advance.