Problem drawing a 3d dimand shaped object


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.glClearColor(red, green, blue, 1f);
    // which part of the triangle should be visible?
    // prepare to use vertex arrays we want to draw
    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);
    vertexBuffer = vbb.asFloatBuffer();
    // short has 2 bytes
    ByteBuffer ibb = ByteBuffer.allocateDirect(indices.length * 2);
    indexBuffer = ibb.asShortBuffer();

    // float has 4 bytes, 4 colors (RGBA) * number of vertices * 4 bytes
    ByteBuffer cbb = ByteBuffer.allocateDirect(colors.length * 4);
    colorBuffer = cbb.asFloatBuffer();
    vertexBuffer.put(coords); //all the points
    indexBuffer.put(indices); //draw order
    // 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.

Have you tried with culling disabled? Do you see the same thing with culling enabled/disabled?

I don’t understand what I see in the screenshot, sorry perhaps it is too late for me.

Thanks for the response. I disabled culling however the triangle is still not drawn.

The last triangle identified in the indices array isn’t being drawn:

short[] indices = new short[] {
    0, 1, 4, 
    0, 4, 3,
    0, 3, 1,
    2, 1, 3,
    2, 4, 1,
    2, 3, 4,

The triangle made from the points 2, 3, 4 are not being drawn. I’ve changed the draw order in the array. Its always the last triangle specified in the indices array that isn’t drawn which makes me believe I’m doing something wrong with the buffer.

You have to give the indices count to glDrawElements not the vertices count. What does “coord.length” returns exactly? Ithink it is the number of float elements in the array of float and in this case the name “vertexCount” is not suitable.

Try with 18 (6x3) as the number of elements in DrawElements since you have 18 elements if I am correct.

Thanks for your help. Removing the vetrtexCount and replacing it with indices.length worked.

gl.glDrawElements(GL10.GL_TRIANGLES, indices.length, GL10.GL_UNSIGNED_SHORT, indexBuffer);